From 423577e14edd93dd45b36028811c587d44a67b2e Mon Sep 17 00:00:00 2001 From: omar Date: Mon, 23 Apr 2018 15:54:38 +0200 Subject: [PATCH] Viewport, Platforms: SDL: Fixed initial focus click being ignored as a mouse button. GLFW: Fixed mouse cursor support for multi-viewport with GLFW 3.3 (current master) - somehow doesn't appear to work with GLFW 3.2 but I'm not too fussed about it. (#1542) --- examples/imgui_impl_dx10.cpp | 1 - examples/imgui_impl_dx12.cpp | 2 +- examples/imgui_impl_glfw.cpp | 23 ++++++++++++++--------- examples/imgui_impl_sdl2.cpp | 27 ++++++++++++++++----------- examples/imgui_impl_win32.cpp | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/examples/imgui_impl_dx10.cpp b/examples/imgui_impl_dx10.cpp index 9d5972461..db6f99d65 100644 --- a/examples/imgui_impl_dx10.cpp +++ b/examples/imgui_impl_dx10.cpp @@ -521,7 +521,6 @@ static void ImGui_ImplDX10_CreateWindow(ImGuiViewport* viewport) ImGuiViewportDataDx10* data = IM_NEW(ImGuiViewportDataDx10)(); viewport->RendererUserData = data; - // FIXME-PLATFORM HWND hwnd = (HWND)viewport->PlatformHandle; IM_ASSERT(hwnd != 0); diff --git a/examples/imgui_impl_dx12.cpp b/examples/imgui_impl_dx12.cpp index 1aa0845b2..9ae3a0d3f 100644 --- a/examples/imgui_impl_dx12.cpp +++ b/examples/imgui_impl_dx12.cpp @@ -605,7 +605,7 @@ bool ImGui_ImplDX12_Init(ID3D12Device* device, int num_frames_in_flight, DXGI_FO } // Setup back-end capabilities flags - // FIXME-VIEWPORT: Actually unfinshed.. + // FIXME-VIEWPORT: Actually unfinished.. ImGuiIO& io = ImGui::GetIO(); io.BackendFlags |= ImGuiBackendFlags_RendererHasViewports; // We can create multi-viewports on the Renderer side (optional) if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) diff --git a/examples/imgui_impl_glfw.cpp b/examples/imgui_impl_glfw.cpp index b826188b8..19fa93d32 100644 --- a/examples/imgui_impl_glfw.cpp +++ b/examples/imgui_impl_glfw.cpp @@ -242,17 +242,22 @@ static void ImGui_ImplGlfw_UpdateMouse() } // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor + // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0 && glfwGetInputMode(g_Window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED) { ImGuiMouseCursor cursor = ImGui::GetMouseCursor(); - if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None) + for (int n = 0; n < platform_io.Viewports.Size; n++) { - glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); - } - else - { - glfwSetCursor(g_Window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); - glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle; + if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None) + { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + } + else + { + glfwSetCursor(window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } } } } @@ -520,8 +525,8 @@ static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst } #endif // GLFW_HAS_VULKAN -// FIXME-PLATFORM: Update when changed (using glfwSetMonitorCallback?) -// FIXME-PLATFORM: GLFW doesn't export work area (see https://github.com/glfw/glfw/pull/989) +// FIXME-PLATFORM: Update monitor list when changed (using glfwSetMonitorCallback?) +// FIXME-PLATFORM: GLFW doesn't export monitor work area (see https://github.com/glfw/glfw/pull/989) static void ImGui_ImplGlfw_UpdateMonitors() { ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); diff --git a/examples/imgui_impl_sdl2.cpp b/examples/imgui_impl_sdl2.cpp index 9a67f0e1d..4342afbb7 100644 --- a/examples/imgui_impl_sdl2.cpp +++ b/examples/imgui_impl_sdl2.cpp @@ -30,15 +30,16 @@ #include "imgui_impl_sdl2.h" // SDL -// (the multi-viewports feature requires SDL features suppoted from SDL 2.0.5+) +// (the multi-viewports feature requires SDL features supported from SDL 2.0.5+) #include #include -#define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4) -#define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4) -#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) +#define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4) +#define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5) +#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) +#define SDL_HAS_USABLE_DISPLAY_BOUNDS SDL_VERSION_ATLEAST(2,0,5) +#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4) +#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) +#define SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH SDL_VERSION_ATLEAST(2,0,5) #if !SDL_HAS_VULKAN static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; #endif @@ -231,7 +232,7 @@ static void ImGui_ImplSDL2_UpdateMouse() io.MousePosViewport = viewport->ID; } - // We already retrieve global mouse position, SDL_CaptureMouse() also let the OS know our drag outside boundaries shouldn't trigger, e.g.: OS window resize cursor + // We already retrieve global mouse position, SDL_CaptureMouse() also let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't trigger the OS window resize cursor // The function is only supported from SDL 2.0.4 (released Jan 2016) bool any_mouse_button_down = ImGui::IsAnyMouseDown(); SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE); @@ -301,11 +302,10 @@ static void ImGui_ImplSDL2_CreateWindow(ImGuiViewport* viewport) ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)(); viewport->PlatformUserData = data; - // Share GL resources with main context - // FIXME-PLATFORM ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGuiViewportDataSDL2* main_viewport_data = (ImGuiViewportDataSDL2*)main_viewport->PlatformUserData; + // Share GL resources with main context bool use_opengl = (main_viewport_data->GLContext != NULL); SDL_GLContext backup_context = NULL; if (use_opengl) @@ -444,7 +444,7 @@ static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst } #endif // SDL_HAS_VULKAN -// FIXME-PLATFORM: Update when changed? +// FIXME-PLATFORM: Update monitor list when changed? static void ImGui_ImplSDL2_UpdateMonitors() { ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); @@ -490,6 +490,11 @@ static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_g platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface; #endif + // SDL2 by default doesn't pass mouse clicks to the application when the click focused a window. This is getting in the way of our interactions and we disable that behavior. +#if SDL_HAS_MOUSE_FOCUS_CLICKTHROUGH + SDL_SetHint(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, "1"); +#endif + ImGui_ImplSDL2_UpdateMonitors(); // Register main window handle (which is owned by the main application, not by us) diff --git a/examples/imgui_impl_win32.cpp b/examples/imgui_impl_win32.cpp index c68dcc8b0..7cc6e5aeb 100644 --- a/examples/imgui_impl_win32.cpp +++ b/examples/imgui_impl_win32.cpp @@ -566,7 +566,7 @@ static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, H return TRUE; } -// FIXME-PLATFORM: Update list when changed (WM_DISPLAYCHANGE?) +// FIXME-PLATFORM: Update monitor list when changed (WM_DISPLAYCHANGE?) static void ImGui_ImplWin32_UpdateMonitors() { ImGui::GetPlatformIO().Monitors.resize(0);