From 3d56fcd8488472911f690eaec8f127feb949e313 Mon Sep 17 00:00:00 2001 From: Chris Savoie Date: Thu, 4 Apr 2019 21:41:26 -0700 Subject: [PATCH] [Win32]: Always update mouse position regardless of focus. --- examples/imgui_impl_win32.cpp | 56 +++++++++++++++-------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/examples/imgui_impl_win32.cpp b/examples/imgui_impl_win32.cpp index 8a2b6771e625..014ae5c2aced 100644 --- a/examples/imgui_impl_win32.cpp +++ b/examples/imgui_impl_win32.cpp @@ -165,40 +165,32 @@ static void ImGui_ImplWin32_UpdateMousePos() POINT mouse_screen_pos; if (!::GetCursorPos(&mouse_screen_pos)) return; - if (HWND focused_hwnd = ::GetForegroundWindow()) + + if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) { - if (::IsChild(focused_hwnd, g_hWnd)) - focused_hwnd = g_hWnd; - 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) - // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient(). - if (ImGui::FindViewportByPlatformHandle((void*)focused_hwnd) != NULL) - io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y); - } - 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.) - // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE. - if (focused_hwnd == g_hWnd) - { - POINT mouse_client_pos = mouse_screen_pos; - ::ScreenToClient(focused_hwnd, &mouse_client_pos); - io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y); - } - } + // 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) + // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient(). + io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y); + + // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering. + // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because + // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows). + // - This is _regardless_ of whether another viewport is focused or being dragged from. + // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the back-end, imgui will ignore this field and infer the information by relying on the + // rectangles and last focused time of every viewports it knows about. It will be unaware of foreign windows that may be sitting between or over your windows. + if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos)) + if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd)) + if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated? + io.MouseHoveredViewport = viewport->ID; + } + 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.) + // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE. + POINT mouse_client_pos = mouse_screen_pos; + ::ScreenToClient(g_hWnd, &mouse_client_pos); + io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y); } - - // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering. - // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because - // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows). - // - This is _regardless_ of whether another viewport is focused or being dragged from. - // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the back-end, imgui will ignore this field and infer the information by relying on the - // rectangles and last focused time of every viewports it knows about. It will be unaware of foreign windows that may be sitting between or over your windows. - if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos)) - if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd)) - if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated? - io.MouseHoveredViewport = viewport->ID; } #ifdef _MSC_VER