From 80b5fb51edba2fd3dea76ec3e88153e2492243d1 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 2 Aug 2021 15:48:20 +0200 Subject: [PATCH] Backends: Win32, SDL, GLFW: only honor io.WantSetMousePos when focused + 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 --- backends/imgui_impl_glfw.cpp | 9 +++++---- backends/imgui_impl_sdl.cpp | 11 +++++------ backends/imgui_impl_win32.cpp | 3 +-- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp index 6489bd0238c9..61e1e377ef64 100644 --- a/backends/imgui_impl_glfw.cpp +++ b/backends/imgui_impl_glfw.cpp @@ -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++) { @@ -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++) @@ -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) diff --git a/backends/imgui_impl_sdl.cpp b/backends/imgui_impl_sdl.cpp index 03a6499e4cee..aa2acbd118aa 100644 --- a/backends/imgui_impl_sdl.cpp +++ b/backends/imgui_impl_sdl.cpp @@ -74,7 +74,7 @@ static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; #endif - // SDL Data +// SDL Data struct ImGui_ImplSDL2_Data { SDL_Window* Window; @@ -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)) @@ -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++) @@ -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; @@ -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)); diff --git a/backends/imgui_impl_win32.cpp b/backends/imgui_impl_win32.cpp index 71c3a56c9eec..051a1b7dacbb 100644 --- a/backends/imgui_impl_win32.cpp +++ b/backends/imgui_impl_win32.cpp @@ -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; @@ -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)