Ver Fonte

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)

omar há 7 anos atrás
pai
commit
32ee0a3947
5 ficheiros alterados com 79 adições e 6 exclusões
  1. 19 0
      examples/imgui_impl_glfw.cpp
  2. 17 0
      examples/imgui_impl_sdl2.cpp
  3. 18 0
      examples/imgui_impl_win32.cpp
  4. 10 0
      imgui.cpp
  5. 15 6
      imgui.h

+ 19 - 0
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)();

+ 17 - 0
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)();

+ 18 - 0
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;

+ 10 - 0
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];

+ 15 - 6
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<ImGuiViewport*>    Viewports;
+    // Viewports (written by: imgui, used by: app/back-end to turn into displayable platform windows)
+    ImGuiViewport*                  MainViewport;           // Guaranteed to be == Viewports[0]
+    ImVector<ImGuiViewport*>        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<ImGuiPlatformMonitor>  Monitors;
 
     ImGuiPlatformData() { MainViewport = NULL; }
 };