Просмотр исходного кода

Viewport, Platform, DPI: Back-end store per-monitor DPI information in ImGuiPlatformMonitor. (#1542, #1676)

omar 7 лет назад
Родитель
Сommit
950539b768
5 измененных файлов с 29 добавлено и 17 удалено
  1. 12 10
      examples/imgui_impl_glfw.cpp
  2. 12 5
      examples/imgui_impl_sdl2.cpp
  3. 2 1
      examples/imgui_impl_win32.cpp
  4. 1 1
      imgui.cpp
  5. 2 0
      imgui.h

+ 12 - 10
examples/imgui_impl_glfw.cpp

@@ -35,15 +35,11 @@
 #define GLFW_EXPOSE_NATIVE_WIN32
 #include <GLFW/glfw3native.h>   // for glfwGetWin32Window
 #endif
-#ifdef GLFW_HOVERED
-#define GLFW_HAS_GLFW_HOVERED   1
-#else
-#define GLFW_HAS_GLFW_HOVERED   0
-#endif
-#define GLFW_HAS_WINDOW_ALPHA   (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+
-#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+
-#define GLFW_HAS_VULKAN         (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+
-
+#define GLFW_HAS_WINDOW_TOPMOST     (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
+#define GLFW_HAS_WINDOW_HOVERED     (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
+#define GLFW_HAS_WINDOW_ALPHA       (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
+#define GLFW_HAS_PER_MONITOR_DPI    (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
+#define GLFW_HAS_VULKAN             (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
 
 // Data
 enum GlfwClientApi
@@ -530,7 +526,7 @@ static void ImGui_ImplGlfw_UpdateMonitors()
     ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
     int monitors_count = 0;
     GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
-    platform_io.Monitors.resize(monitors_count);
+    platform_io.Monitors.resize(monitors_count, ImGuiPlatformMonitor());
     for (int n = 0; n < monitors_count; n++)
     {
         int x, y;
@@ -538,6 +534,12 @@ static void ImGui_ImplGlfw_UpdateMonitors()
         const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]);
         platform_io.Monitors[n].Pos = ImVec2((float)x, (float)y);
         platform_io.Monitors[n].Size = ImVec2((float)vid_mode->width, (float)vid_mode->height);
+#if GLFW_HAS_PER_MONITOR_DPI
+        // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
+        float x_scale, y_scale;
+        glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale);
+        platform_io.Monitors[n].DpiScale = x_scale;
+#endif
     }
 }
 

+ 12 - 5
examples/imgui_impl_sdl2.cpp

@@ -32,10 +32,11 @@
 // SDL
 #include <SDL.h>
 #include <SDL_syswm.h>
-#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_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_PER_MONITOR_DPI     SDL_VERSION_ATLEAST(2,0,4)
+#define SDL_HAS_VULKAN              SDL_VERSION_ATLEAST(2,0,6)
 #if !SDL_HAS_VULKAN
 static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
 #endif
@@ -446,13 +447,19 @@ static void ImGui_ImplSDL2_UpdateMonitors()
 {
     ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
     int display_count = SDL_GetNumVideoDisplays();
-    platform_io.Monitors.resize(display_count);
+    platform_io.Monitors.resize(display_count, ImGuiPlatformMonitor());
     for (int n = 0; n < display_count; n++)
     {
+        // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
         SDL_Rect r;
         SDL_GetDisplayBounds(n, &r);
         platform_io.Monitors[n].Pos = ImVec2((float)r.x, (float)r.y);
         platform_io.Monitors[n].Size = ImVec2((float)r.w, (float)r.h);
+#if SDL_HAS_PER_MONITOR_DPI
+        float dpi = 0.0f;
+        SDL_GetDisplayDPI(n, &dpi, NULL, NULL);
+        platform_io.Monitors[n].DpiScale = dpi / 96.0f;
+#endif
     }
 }
 

+ 2 - 1
examples/imgui_impl_win32.cpp

@@ -550,11 +550,12 @@ 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)
+static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, 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_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
     ImGui::GetPlatformIO().Monitors.push_back(imgui_monitor);
     return TRUE;
 }

+ 1 - 1
imgui.cpp

@@ -14258,7 +14258,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
                 for (int i = 0; i < g.PlatformIO.Monitors.Size; i++)
                 {
                     const ImGuiPlatformMonitor& mon = g.PlatformIO.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::BulletText("Monitor #%d: DPI %.0f%%, Min (%.0f,%.0f), Max (%.0f,%.0f), Size (%.0f,%.0f)", i, mon.DpiScale * 100.0f, 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();
             }

+ 2 - 0
imgui.h

@@ -1879,6 +1879,8 @@ struct ImGuiPlatformMonitor
 {
     ImVec2  Pos;
     ImVec2  Size;
+    float   DpiScale;
+    ImGuiPlatformMonitor() { Pos = ImVec2(0,0); Size = ImVec2(0,0); DpiScale = 1.0f; }
 };
 
 // (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled