Skip to content

Commit

Permalink
Backends: Win32, SDL, GLFW: only honor io.WantSetMousePos when focuse…
Browse files Browse the repository at this point in the history
…d + fix GLFW uninstalling handler + tweaks to reduce branch drift with docking. (#787, #2445, #2696, #3751, #4377)

# Conflicts:
#	backends/imgui_impl_glfw.cpp
#	backends/imgui_impl_sdl.cpp
#	backends/imgui_impl_win32.cpp
  • Loading branch information
ocornut committed Aug 2, 2021
1 parent f9b5168 commit 80b5fb5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
9 changes: 5 additions & 4 deletions backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
io.MouseHoveredViewport = 0;

// Update mouse button
// Update mouse buttons
// (if a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame)
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
{
Expand All @@ -384,10 +384,11 @@ static void ImGui_ImplGlfw_UpdateMousePosAndButtons()

#ifdef __EMSCRIPTEN__
const bool focused = true;
IM_ASSERT(platform_io.Viewports.Size == 1);
#else
const bool focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
#endif
GLFWwindow* mouse_window = (bd->MouseWindow == window || focused) ? window : NULL;

// Update mouse buttons
if (focused)
for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
Expand All @@ -399,10 +400,10 @@ static void ImGui_ImplGlfw_UpdateMousePosAndButtons()
glfwSetCursorPos(window, (double)(mouse_pos_prev.x - viewport->Pos.x), (double)(mouse_pos_prev.y - viewport->Pos.y));

// Set Dear ImGui mouse position from OS position
if (bd->MouseWindow == window || focused)
if (mouse_window != NULL)
{
double mouse_x, mouse_y;
glfwGetCursorPos(window, &mouse_x, &mouse_y);
glfwGetCursorPos(mouse_window, &mouse_x, &mouse_y);
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
// Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
Expand Down
11 changes: 5 additions & 6 deletions backends/imgui_impl_sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
#endif

// SDL Data
// SDL Data
struct ImGui_ImplSDL2_Data
{
SDL_Window* Window;
Expand Down Expand Up @@ -254,7 +254,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, void* sdl_gl_context)
// Our mouse update function expect PlatformHandle to be filled for the main viewport
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = (void*)window;
#if defined(_WIN32)
#ifdef _WIN32
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
if (SDL_GetWindowWMInfo(window, &info))
Expand Down Expand Up @@ -318,7 +318,6 @@ void ImGui_ImplSDL2_Shutdown()

ImGui_ImplSDL2_ShutdownPlatformInterface();

// Destroy last known clipboard data
if (bd->ClipboardTextData)
SDL_free(bd->ClipboardTextData);
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
Expand All @@ -332,12 +331,12 @@ void ImGui_ImplSDL2_Shutdown()
// This code is incredibly messy because some of the functions we need for full viewport support are not available in SDL < 2.0.4.
static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
{
ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDL2_Data* bd = ImGui_ImplSDL2_GetBackendData();
ImGuiIO& io = ImGui::GetIO();

ImVec2 mouse_pos_prev = io.MousePos;
io.MouseHoveredViewport = 0;
io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
io.MouseHoveredViewport = 0;

// Update mouse buttons
int mouse_x_local, mouse_y_local;
Expand Down Expand Up @@ -391,7 +390,7 @@ static void ImGui_ImplSDL2_UpdateMousePosAndButtons()
else
{
// Single-viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
// Unlike local position obtained earlier this will be valid when straying out of bounds too.
// Unlike local position obtained earlier this will be valid when straying out of bounds.
int window_x, window_y;
SDL_GetWindowPosition(mouse_window, &window_x, &window_y);
io.MousePos = ImVec2((float)(mouse_x_global - window_x), (float)(mouse_y_global - window_y));
Expand Down
3 changes: 1 addition & 2 deletions backends/imgui_impl_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ static void ImGui_ImplWin32_InitPlatformInterface();
static void ImGui_ImplWin32_ShutdownPlatformInterface();
static void ImGui_ImplWin32_UpdateMonitors();

// Win32
struct ImGui_ImplWin32_Data
{
HWND hWnd;
Expand Down Expand Up @@ -259,7 +258,7 @@ static void ImGui_ImplWin32_UpdateMousePos()

// Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
// (When multi-viewports are enabled, all Dear ImGui positions are same as OS positions)
if (io.WantSetMousePos && mouse_window != NULL)
if (io.WantSetMousePos)
{
POINT pos = { (int)mouse_pos_prev.x, (int)mouse_pos_prev.y };
if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0)
Expand Down

0 comments on commit 80b5fb5

Please sign in to comment.