From 090eb437ed973603fb99beb25e4500c04499bbfb Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 11 May 2018 16:41:32 +0200 Subject: [PATCH] Viewport, Platform: Cleaned up xxx_UpdateMouseCursor() functions to make them more consistent. (#1542) --- examples/imgui_impl_glfw.cpp | 41 ++++++++++++++++++++--------------- examples/imgui_impl_sdl2.cpp | 34 +++++++++++++++++------------ examples/imgui_impl_win32.cpp | 6 ++--- 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/examples/imgui_impl_glfw.cpp b/examples/imgui_impl_glfw.cpp index 88f61d1e9..36d500ff6 100644 --- a/examples/imgui_impl_glfw.cpp +++ b/examples/imgui_impl_glfw.cpp @@ -196,7 +196,7 @@ void ImGui_ImplGlfw_Shutdown() g_ClientApi = GlfwClientApi_Unknown; } -static void ImGui_ImplGlfw_UpdateMouse() +static void ImGui_ImplGlfw_UpdateMousePosAndButtons() { ImGuiIO& io = ImGui::GetIO(); const ImVec2 mouse_pos_backup = io.MousePos; @@ -241,24 +241,30 @@ static void ImGui_ImplGlfw_UpdateMouse() io.MouseHoveredViewport = viewport->ID; #endif } +} - // 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) +static void ImGui_ImplGlfw_UpdateMouseCursor() +{ + ImGuiIO& io = ImGui::GetIO(); + if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(g_Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED) + return; + + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); + for (int n = 0; n < platform_io.Viewports.Size; n++) { - ImGuiMouseCursor cursor = ImGui::GetMouseCursor(); - for (int n = 0; n < platform_io.Viewports.Size; n++) + GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle; + if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) { - 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); - } + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); + } + else + { + // Show OS mouse cursor + // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here. + glfwSetCursor(window, g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } } } @@ -283,7 +289,8 @@ void ImGui_ImplGlfw_NewFrame() io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); g_Time = current_time; - ImGui_ImplGlfw_UpdateMouse(); + ImGui_ImplGlfw_UpdateMousePosAndButtons(); + ImGui_ImplGlfw_UpdateMouseCursor(); // Gamepad navigation mapping [BETA] memset(io.NavInputs, 0, sizeof(io.NavInputs)); diff --git a/examples/imgui_impl_sdl2.cpp b/examples/imgui_impl_sdl2.cpp index 5d1532494..abf034e73 100644 --- a/examples/imgui_impl_sdl2.cpp +++ b/examples/imgui_impl_sdl2.cpp @@ -197,7 +197,7 @@ void ImGui_ImplSDL2_Shutdown() memset(g_MouseCursors, 0, sizeof(g_MouseCursors)); } -static void ImGui_ImplSDL2_UpdateMouse() +static void ImGui_ImplSDL2_UpdateMousePosAndButtons() { ImGuiIO& io = ImGui::GetIO(); const ImVec2 mouse_pos_backup = io.MousePos; @@ -250,20 +250,25 @@ static void ImGui_ImplSDL2_UpdateMouse() if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS) io.MousePos = ImVec2((float)mx, (float)my); #endif +} - // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor - if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0) +static void ImGui_ImplSDL2_UpdateMouseCursor() +{ + ImGuiIO& io = ImGui::GetIO(); + if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) + return; + + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + if (io.MouseDrawCursor || imgui_cursor == ImGuiMouseCursor_None) { - ImGuiMouseCursor cursor = ImGui::GetMouseCursor(); - if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None) - { - SDL_ShowCursor(SDL_FALSE); - } - else - { - SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); - SDL_ShowCursor(SDL_TRUE); - } + // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor + SDL_ShowCursor(SDL_FALSE); + } + else + { + // Show OS mouse cursor + SDL_SetCursor(g_MouseCursors[imgui_cursor] ? g_MouseCursors[imgui_cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); + SDL_ShowCursor(SDL_TRUE); } } @@ -286,7 +291,8 @@ void ImGui_ImplSDL2_NewFrame(SDL_Window* window) io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f); g_Time = current_time; - ImGui_ImplSDL2_UpdateMouse(); + ImGui_ImplSDL2_UpdateMousePosAndButtons(); + ImGui_ImplSDL2_UpdateMouseCursor(); // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application. ImGui::NewFrame(); diff --git a/examples/imgui_impl_win32.cpp b/examples/imgui_impl_win32.cpp index 95c92e8e0..f43ebd0c2 100644 --- a/examples/imgui_impl_win32.cpp +++ b/examples/imgui_impl_win32.cpp @@ -94,15 +94,15 @@ static bool ImGui_ImplWin32_UpdateMouseCursor() if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) return false; - ImGuiMouseCursor imgui_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor(); - if (imgui_cursor == ImGuiMouseCursor_None) + ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); + if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) { // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor ::SetCursor(NULL); } else { - // Hardware cursor type + // Show OS mouse cursor LPTSTR win32_cursor = IDC_ARROW; switch (imgui_cursor) {