From 32ee0a3947fe35e06eba64e213830b03e6f53520 Mon Sep 17 00:00:00 2001 From: omar Date: Tue, 10 Apr 2018 22:32:08 +0200 Subject: [PATCH] Viewport, Platform: Added a way to register monitor bounds to imgui so they can be used to clamp individual-viewport tooltips/popups so they don't straddle monitors. (#1542) --- examples/imgui_impl_glfw.cpp | 19 +++++++++++++++++++ examples/imgui_impl_sdl2.cpp | 17 +++++++++++++++++ examples/imgui_impl_win32.cpp | 18 ++++++++++++++++++ imgui.cpp | 10 ++++++++++ imgui.h | 21 +++++++++++++++------ 5 files changed, 79 insertions(+), 6 deletions(-) diff --git a/examples/imgui_impl_glfw.cpp b/examples/imgui_impl_glfw.cpp index 0241542b..4750341f 100644 --- a/examples/imgui_impl_glfw.cpp +++ b/examples/imgui_impl_glfw.cpp @@ -519,6 +519,23 @@ static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst } #endif // GLFW_HAS_VULKAN +// FIXME-PLATFORM: Update when changed (using glfwSetMonitorCallback?) +static void ImGui_ImplGlfw_UpdateMonitors() +{ + ImGuiPlatformData* platform_data = ImGui::GetPlatformData(); + int monitors_count = 0; + GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count); + platform_data->Monitors.resize(monitors_count); + for (int n = 0; n < monitors_count; n++) + { + int x, y; + glfwGetMonitorPos(glfw_monitors[n], &x, &y); + const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]); + platform_data->Monitors[n].Pos = ImVec2((float)x, (float)y); + platform_data->Monitors[n].Size = ImVec2((float)vid_mode->width, (float)vid_mode->height); + } +} + static void ImGui_ImplGlfw_InitPlatformInterface() { // Register platform interface (will be coupled with a renderer interface) @@ -540,6 +557,8 @@ static void ImGui_ImplGlfw_InitPlatformInterface() platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface; #endif + ImGui_ImplGlfw_UpdateMonitors(); + // Register main window handle (which is owned by the main application, not by us) ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGuiViewportDataGlfw* data = IM_NEW(ImGuiViewportDataGlfw)(); diff --git a/examples/imgui_impl_sdl2.cpp b/examples/imgui_impl_sdl2.cpp index c1e0c8ea..ae49a39a 100644 --- a/examples/imgui_impl_sdl2.cpp +++ b/examples/imgui_impl_sdl2.cpp @@ -437,6 +437,21 @@ static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst } #endif // SDL_HAS_VULKAN +// FIXME-PLATFORM: Update when changed? +static void ImGui_ImplSDL2_UpdateMonitors() +{ + ImGuiPlatformData* platform_data = ImGui::GetPlatformData(); + int display_count = SDL_GetNumVideoDisplays(); + platform_data->Monitors.resize(display_count); + for (int n = 0; n < display_count; n++) + { + SDL_Rect r; + SDL_GetDisplayBounds(n, &r); + platform_data->Monitors[n].Pos = ImVec2((float)r.x, (float)r.y); + platform_data->Monitors[n].Size = ImVec2((float)r.w, (float)r.h); + } +} + static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_gl_context) { // Register platform interface (will be coupled with a renderer interface) @@ -455,6 +470,8 @@ static void ImGui_ImplSDL2_InitPlatformInterface(SDL_Window* window, void* sdl_g platform_io.Platform_CreateVkSurface = ImGui_ImplSDL2_CreateVkSurface; #endif + ImGui_ImplSDL2_UpdateMonitors(); + // Register main window handle (which is owned by the main application, not by us) ImGuiViewport* main_viewport = ImGui::GetMainViewport(); ImGuiViewportDataSDL2* data = IM_NEW(ImGuiViewportDataSDL2)(); diff --git a/examples/imgui_impl_win32.cpp b/examples/imgui_impl_win32.cpp index a6b6a1bf..6be55ced 100644 --- a/examples/imgui_impl_win32.cpp +++ b/examples/imgui_impl_win32.cpp @@ -535,6 +535,22 @@ static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, return DefWindowProc(hWnd, msg, wParam, lParam); } +static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR, HDC, LPRECT rect, LPARAM) +{ + ImGuiPlatformMonitor imgui_monitor; + imgui_monitor.Pos = ImVec2((float)rect->left, (float)rect->top); + imgui_monitor.Size = ImVec2((float)(rect->right - rect->left), (float)(rect->bottom - rect->top)); + ImGui::GetPlatformData()->Monitors.push_back(imgui_monitor); + return TRUE; +} + +// FIXME-PLATFORM: Update list when changed (WM_DISPLAYCHANGE?) +static void ImGui_ImplWin32_UpdateMonitors() +{ + ImGui::GetPlatformData()->Monitors.resize(0); + ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, NULL); +} + static void ImGui_ImplWin32_InitPlatformInterface() { WNDCLASSEX wcex; @@ -552,6 +568,8 @@ static void ImGui_ImplWin32_InitPlatformInterface() wcex.hIconSm = NULL; ::RegisterClassEx(&wcex); + ImGui_ImplWin32_UpdateMonitors(); + // Register platform interface (will be coupled with a renderer interface) ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow; diff --git a/imgui.cpp b/imgui.cpp index 73f5b003..99167347 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -14182,6 +14182,16 @@ void ImGui::ShowMetricsWindow(bool* p_open) ImGui::Indent(ImGui::GetTreeNodeToLabelSpacing()); ImGui::ShowViewportThumbnails(); ImGui::Unindent(ImGui::GetTreeNodeToLabelSpacing()); + if (g.PlatformData.Monitors.Size > 0 && ImGui::TreeNode("Monitors", "Monitors (%d)", g.PlatformData.Monitors.Size)) + { + ImGui::TextWrapped("(When viewports are enabled, imgui optionally uses monitor data to position popup/tooltips so they don't straddle monitors.)"); + for (int i = 0; i < g.PlatformData.Monitors.Size; i++) + { + const ImGuiPlatformMonitor& mon = g.PlatformData.Monitors[i]; + ImGui::BulletText("Monitor #%d: Min (%.0f,%.0f) Max (%.0f,%.0f) Size (%.0f,%.0f)", i, mon.Pos.x, mon.Pos.y, mon.Pos.x + mon.Size.x, mon.Pos.y + mon.Size.y, mon.Size.x, mon.Size.y); + } + ImGui::TreePop(); + } for (int i = 0; i < g.Viewports.Size; i++) { ImGuiViewportP* viewport = g.Viewports[i]; diff --git a/imgui.h b/imgui.h index 437f6d8d..ce13fcbb 100644 --- a/imgui.h +++ b/imgui.h @@ -74,7 +74,7 @@ struct ImGuiListClipper; // Helper to manually clip large list of ite struct ImGuiPayload; // User data payload for drag and drop operations struct ImGuiViewport; // Viewport (generally ~1 per window to output to at the OS level. Need per-platform support to use multiple viewports) struct ImGuiPlatformIO; // Multi-viewport support: interface for Platform/Renderer back-ends -struct ImGuiPlatformData; // Multi-viewport support: list of viewports to render +struct ImGuiPlatformData; // Multi-viewport support: list of viewports to render + list of monitors provided by back-end. struct ImGuiContext; // ImGui context (opaque) #ifndef ImTextureID @@ -1907,13 +1907,22 @@ struct ImGuiPlatformIO void (*Renderer_SwapBuffers)(ImGuiViewport* vp, void* render_arg); // (Optional) Call Present/SwapBuffers (renderer side) }; -// List of viewports to render as platform window (updated by ImGui::UpdatePlatformWindows) +struct ImGuiPlatformMonitor +{ + ImVec2 Pos; + ImVec2 Size; +}; + +// List of viewports to render as platform window, updated by ImGui::UpdatePlatformWindows() +// FIXME: Merge into ImGuiPlatformIO struct ImGuiPlatformData { - // Viewports[0] is guaranteed to be _always_ the same as MainViewport. Following it are the secondary viewports. - // The main viewport is included in the list because it is more convenient for looping code. - ImGuiViewport* MainViewport; - ImVector Viewports; + // Viewports (written by: imgui, used by: app/back-end to turn into displayable platform windows) + ImGuiViewport* MainViewport; // Guaranteed to be == Viewports[0] + ImVector Viewports; // Main viewports, followed by all secondary viewports. + + // Monitors (written by: app/back-end, used by: imgui to clamp popups/tooltips within same monitor and not have them straddle monitors) + ImVector Monitors; ImGuiPlatformData() { MainViewport = NULL; } };