Prechádzať zdrojové kódy

Viewport: Merged ImGuiPlatformData into ImGuiPlatformIO. Comments. (#1542)

omar 7 rokov pred
rodič
commit
83bd3595a4

+ 7 - 7
examples/imgui_impl_glfw.cpp

@@ -220,10 +220,10 @@ static void ImGui_ImplGlfw_UpdateMouse()
         g_MouseJustPressed[i] = false;
     }
 
-    ImGuiPlatformData* platform_data = ImGui::GetPlatformData();
-    for (int n = 0; n < platform_data->Viewports.Size; n++)
+    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+    for (int n = 0; n < platform_io.Viewports.Size; n++)
     {
-        ImGuiViewport* viewport = platform_data->Viewports[n];
+        ImGuiViewport* viewport = platform_io.Viewports[n];
         GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
         IM_ASSERT(window != NULL);
         if (glfwGetWindowAttrib(window, GLFW_FOCUSED))
@@ -522,17 +522,17 @@ static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst
 // FIXME-PLATFORM: Update when changed (using glfwSetMonitorCallback?)
 static void ImGui_ImplGlfw_UpdateMonitors()
 {
-    ImGuiPlatformData* platform_data = ImGui::GetPlatformData();
+    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
     int monitors_count = 0;
     GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
-    platform_data->Monitors.resize(monitors_count);
+    platform_io.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);
+        platform_io.Monitors[n].Pos = ImVec2((float)x, (float)y);
+        platform_io.Monitors[n].Size = ImVec2((float)vid_mode->width, (float)vid_mode->height);
     }
 }
 

+ 4 - 4
examples/imgui_impl_sdl2.cpp

@@ -440,15 +440,15 @@ static int ImGui_ImplSDL2_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_inst
 // FIXME-PLATFORM: Update when changed?
 static void ImGui_ImplSDL2_UpdateMonitors()
 {
-    ImGuiPlatformData* platform_data = ImGui::GetPlatformData();
+    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
     int display_count = SDL_GetNumVideoDisplays();
-    platform_data->Monitors.resize(display_count);
+    platform_io.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);
+        platform_io.Monitors[n].Pos = ImVec2((float)r.x, (float)r.y);
+        platform_io.Monitors[n].Size = ImVec2((float)r.w, (float)r.h);
     }
 }
 

+ 3 - 3
examples/imgui_impl_win32.cpp

@@ -408,7 +408,7 @@ static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
     {
         if (::GetCapture() == data->Hwnd)
         {
-            // Transfer capture so if we started dragging from a window that later disappears, we'll still release the MOUSEUP event.
+            // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event.
             ::ReleaseCapture();
             ::SetCapture(g_hWnd);
         }
@@ -540,14 +540,14 @@ static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR, HDC, LPRE
     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);
+    ImGui::GetPlatformIO().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);
+    ImGui::GetPlatformIO().Monitors.resize(0);
     ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, NULL);
 }
 

+ 17 - 23
imgui.cpp

@@ -3386,7 +3386,7 @@ static ImGuiViewportP* FindViewportHoveredFromPlatformWindowStack(const ImVec2 m
 static void ImGui::UpdateViewports()
 {
     ImGuiContext& g = *GImGui;
-    IM_ASSERT(g.PlatformData.Viewports.Size <= g.Viewports.Size);
+    IM_ASSERT(g.PlatformIO.Viewports.Size <= g.Viewports.Size);
 
     // Mouse handling: latch the expected mouse OS position (if any) before processing viewport erasure
     ImGuiViewportP* viewport_ref = g.IO.MousePosViewport ? FindViewportByID(g.IO.MousePosViewport) : g.Viewports[0];
@@ -3410,7 +3410,7 @@ static void ImGui::UpdateViewports()
             if (viewport == g.MousePosViewport)         g.MousePosViewport = NULL;
             if (viewport == g.MouseHoveredPrevViewport) g.MouseHoveredPrevViewport = NULL;
             IM_ASSERT(viewport->RendererUserData == NULL && viewport->PlatformUserData == NULL && viewport->PlatformHandle == NULL);
-            IM_ASSERT(g.PlatformData.Viewports.contains(viewport) == false);
+            IM_ASSERT(g.PlatformIO.Viewports.contains(viewport) == false);
             IM_DELETE(viewport);
             n--;
             continue;
@@ -3516,11 +3516,6 @@ static void ImGui::UpdateViewports()
     IM_ASSERT(g.MousePosViewport != NULL);
 }
 
-ImGuiPlatformData* ImGui::GetPlatformData()
-{
-    return &GImGui->PlatformData;
-}
-
 void ImGui::UpdatePlatformWindows()
 {
     ImGuiContext& g = *GImGui;
@@ -3611,11 +3606,11 @@ void ImGui::UpdatePlatformWindows()
 // Custom renderers may prefer to not call this function at all, and instead iterate the platform data and handle rendering/sync themselves.
 // The Render/Swap functions stored in ImGuiPlatformIO are merely here to allow for this helper to exist, but you can do it yourself:
 //
-//    ImGuiPlatformData* data = ImGui::GetPlatformData();
-//    for (int i = 1; i < data->Viewports.Size; i++)
-//        MyRenderFunction(data->Viewports[i], my_args);
-//    for (int i = 1; i < data->Viewports.Size; i++)
-//        MySwapBufferFunction(data->Viewports[i], my_args);
+//    ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
+//    for (int i = 1; i < platform_io.Viewports.Size; i++)
+//        MyRenderFunction(platform_io.Viewports[i], my_args);
+//    for (int i = 1; i < platform_io.Viewports.Size; i++)
+//        MySwapBufferFunction(platform_io.Viewports[i], my_args);
 //
 void ImGui::RenderPlatformWindowsDefault(void* platform_render_arg, void* renderer_render_arg)
 {
@@ -3623,17 +3618,16 @@ void ImGui::RenderPlatformWindowsDefault(void* platform_render_arg, void* render
         return;
 
     // Skip the main viewport (index 0), which is always fully handled by the application!
-    ImGuiPlatformData* data = ImGui::GetPlatformData();
     ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
-    for (int i = 1; i < data->Viewports.Size; i++)
+    for (int i = 1; i < platform_io.Viewports.Size; i++)
     {
-        ImGuiViewport* viewport = data->Viewports[i];
+        ImGuiViewport* viewport = platform_io.Viewports[i];
         if (platform_io.Platform_RenderWindow) platform_io.Platform_RenderWindow(viewport, platform_render_arg);
         if (platform_io.Renderer_RenderWindow) platform_io.Renderer_RenderWindow(viewport, renderer_render_arg);
     }
-    for (int i = 1; i < data->Viewports.Size; i++)
+    for (int i = 1; i < platform_io.Viewports.Size; i++)
     {
-        ImGuiViewport* viewport = data->Viewports[i];
+        ImGuiViewport* viewport = platform_io.Viewports[i];
         if (platform_io.Platform_SwapBuffers) platform_io.Platform_SwapBuffers(viewport, platform_render_arg);
         if (platform_io.Renderer_SwapBuffers) platform_io.Renderer_SwapBuffers(viewport, renderer_render_arg);
     }
@@ -4497,8 +4491,8 @@ void ImGui::EndFrame()
     }
 
     // Update user-side viewport list
-    g.PlatformData.MainViewport = g.Viewports[0];
-    g.PlatformData.Viewports.resize(0);
+    g.PlatformIO.MainViewport = g.Viewports[0];
+    g.PlatformIO.Viewports.resize(0);
     for (int i = 0; i < g.Viewports.Size; i++)
     {
         ImGuiViewportP* viewport = g.Viewports[i];
@@ -4506,7 +4500,7 @@ void ImGui::EndFrame()
             continue;
         if (i > 0)
             IM_ASSERT(viewport->Window != NULL);
-        g.PlatformData.Viewports.push_back(viewport);
+        g.PlatformIO.Viewports.push_back(viewport);
     }
 
     // Sort the window list so that all child windows are after their parent
@@ -14182,12 +14176,12 @@ 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))
+            if (g.PlatformIO.Monitors.Size > 0 && ImGui::TreeNode("Monitors", "Monitors (%d)", g.PlatformIO.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++)
+                for (int i = 0; i < g.PlatformIO.Monitors.Size; i++)
                 {
-                    const ImGuiPlatformMonitor& mon = g.PlatformData.Monitors[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::TreePop();

+ 32 - 29
imgui.h

@@ -73,8 +73,7 @@ struct ImGuiSizeCallbackData;       // Structure used to constraint window size
 struct ImGuiListClipper;            // Helper to manually clip large list of items
 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 + list of monitors provided by back-end.
+struct ImGuiPlatformIO;             // Multi-viewport support: interface for Platform/Renderer back-ends + viewports to render
 struct ImGuiContext;                // ImGui context (opaque)
 
 #ifndef ImTextureID
@@ -546,14 +545,13 @@ namespace ImGui
     IMGUI_API const char*   GetClipboardText();
     IMGUI_API void          SetClipboardText(const char* text);
 
-    // (Optional) Platform interface for multi-viewport support
-    IMGUI_API ImGuiPlatformIO&   GetPlatformIO();                   // Platform/Renderer function, for back-end to setup.
-    IMGUI_API ImGuiPlatformData* GetPlatformData();                 // List of viewports. Viewport 0 is always the main viewport, followed by the secondary viewports.
-    IMGUI_API ImGuiViewport*     GetMainViewport();                 // == GetPlatformData()->MainViewport == GetPlatformData()->Viewports[0]
-    IMGUI_API void               UpdatePlatformWindows();           // Call in main loop. Will call CreateWindow/ResizeWindow/etc. platform functions for each secondary viewport, and DestroyWindow for each inactive viewport.
-    IMGUI_API void               RenderPlatformWindowsDefault(void* platform_arg = NULL, void* renderer_arg = NULL); // Call in main loop. Will call RenderWindow/SwapBuffers platform functions for each secondary viewport. May be reimplemented by user for custom rendering needs.
-    IMGUI_API void               DestroyPlatformWindows();          // (Optional) Call DestroyWindow platform functions for all viewports. Call from back-end Shutdown() if you need to close platform windows before imgui shutdown. Otherwise will be called by DestroyContext().
-    IMGUI_API ImGuiViewport*     FindViewportByPlatformHandle(void* platform_handle);
+    // (Optional) Platform/OS interface for multi-viewport support
+    IMGUI_API ImGuiPlatformIO&  GetPlatformIO();                                                // platform/renderer functions, for back-end to setup + viewports list.
+    IMGUI_API ImGuiViewport*    GetMainViewport();                                              // shortcut to == GetPlatformIO().MainViewport == GetPlatformIO().Viewports[0]
+    IMGUI_API void              UpdatePlatformWindows();                                        // call in main loop. will call CreateWindow/ResizeWindow/etc. platform functions for each secondary viewport, and DestroyWindow for each inactive viewport.
+    IMGUI_API void              RenderPlatformWindowsDefault(void* platform_arg = NULL, void* renderer_arg = NULL); // call in main loop. will call RenderWindow/SwapBuffers platform functions for each secondary viewport. may be reimplemented by user for custom rendering needs.
+    IMGUI_API void              DestroyPlatformWindows();                                       // call DestroyWindow platform functions for all viewports. call from back-end Shutdown() if you need to close platform windows before imgui shutdown. otherwise will be called by DestroyContext().
+    IMGUI_API ImGuiViewport*    FindViewportByPlatformHandle(void* platform_handle);            // this is a helper for back-ends. the type platform_handle is decided by the back-end (e.g. HWND, MyWindow*, GLFWwindow* etc.)
 
     // Memory Utilities
     // All those functions are not reliant on the current context.
@@ -1875,6 +1873,14 @@ struct ImFont
 // - if you are new to dear imgui and trying to integrate it into your engine, you should probably ignore this for now.
 //-----------------------------------------------------------------------------
 
+// (Optional) Represent the bounds of each connected monitor/display
+// Dear ImGui only uses this to clamp the position of popups and tooltips so they don't straddle multiple monitors
+struct ImGuiPlatformMonitor
+{
+    ImVec2  Pos;
+    ImVec2  Size;
+};
+
 // (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled
 // This is designed so we can mix and match two imgui_impl_xxxx files, one for the Platform (~window handling), one for Renderer.
 // Custom engine back-ends will often provide both Platform and Renderer interfaces and thus may not need to use all functions.
@@ -1883,10 +1889,14 @@ struct ImFont
 // You may skip using RenderPlatformWindowsDefault() and call your draw/swap functions yourself if you need specific behavior for your multi-window rendering.
 struct ImGuiPlatformIO
 {
-    // Platform (e.g. Win32, GLFW, SDL2)
+    //------------------------------------------------------------------
+    // Input - Back-end interface/functions + Monitor List
+    //------------------------------------------------------------------
+
+    // Platform functions (e.g. Win32, GLFW, SDL2)
     void    (*Platform_CreateWindow)(ImGuiViewport* vp);                    // Create a new platform window for the given viewport
     void    (*Platform_DestroyWindow)(ImGuiViewport* vp);
-    void    (*Platform_ShowWindow)(ImGuiViewport* vp);                      // Newly created windows are initially hidden so we have a chance to call SetWindowPos/Size/Title on them.
+    void    (*Platform_ShowWindow)(ImGuiViewport* vp);                      // Newly created windows are initially hidden so SetWindowPos/Size/Title can be called on them first
     void    (*Platform_SetWindowPos)(ImGuiViewport* vp, ImVec2 pos);
     ImVec2  (*Platform_GetWindowPos)(ImGuiViewport* vp);
     void    (*Platform_SetWindowSize)(ImGuiViewport* vp, ImVec2 size);
@@ -1899,32 +1909,25 @@ struct ImGuiPlatformIO
     void    (*Platform_OnChangedViewport)(ImGuiViewport* vp);               // (Optional) DPI handling: Called during Begin() every time the viewport we are outputting into changes, so back-end has a chance to swap fonts to adjust style.
     int     (*Platform_CreateVkSurface)(ImGuiViewport* vp, ImU64 vk_inst, const void* vk_allocators, ImU64* out_vk_surface); // (Optional) For Renderer to call into Platform code
 
-    // Renderer (e.g. DirectX, OpenGL3, Vulkan)
+    // Renderer functions (e.g. DirectX, OpenGL3, Vulkan)
     void    (*Renderer_CreateWindow)(ImGuiViewport* vp);                    // Create swap chains, frame buffers etc.
     void    (*Renderer_DestroyWindow)(ImGuiViewport* vp);
     void    (*Renderer_SetWindowSize)(ImGuiViewport* vp, ImVec2 size);      // Resize swap chain, frame buffers etc.
     void    (*Renderer_RenderWindow)(ImGuiViewport* vp, void* render_arg);  // (Optional) Clear targets, Render viewport->DrawData
     void    (*Renderer_SwapBuffers)(ImGuiViewport* vp, void* render_arg);   // (Optional) Call Present/SwapBuffers (renderer side)
-};
 
-struct ImGuiPlatformMonitor
-{
-    ImVec2  Pos;
-    ImVec2  Size;
-};
+    // List of monitors (updated by: app/back-end, used by: imgui to clamp popups/tooltips within same monitor and not have them straddle monitors)
+    ImVector<ImGuiPlatformMonitor>  Monitors;
 
-// List of viewports to render as platform window, updated by ImGui::UpdatePlatformWindows()
-// FIXME: Merge into ImGuiPlatformIO
-struct ImGuiPlatformData
-{
-    // 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. 
+    //------------------------------------------------------------------
+    // Output - List of viewports to render into platform windows
+    //------------------------------------------------------------------
 
-    // 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;
+    // List of viewports (updated by ImGui::UpdatePlatformWindows() along when calling the Platform/Renderer functions)
+    ImGuiViewport*                  MainViewport;                           // Guaranteed to be == Viewports[0]
+    ImVector<ImGuiViewport*>        Viewports;                              // Main viewports, followed by all secondary viewports. 
 
-    ImGuiPlatformData() { MainViewport = NULL; }
+    ImGuiPlatformIO()               { memset(this, 0, sizeof(*this)); }     // Zero clear
 };
 
 // Flags stored in ImGuiViewport::Flags, giving indications to the platform back-ends

+ 0 - 2
imgui_internal.h

@@ -638,7 +638,6 @@ struct ImGuiContext
 
     // Viewports
     ImVector<ImGuiViewportP*> Viewports;
-    ImGuiPlatformData       PlatformData;                       // This is essentially the public facing version of the Viewports vector (it is updated in UpdatePlatformWindows and exclude the viewports about to be destroyed)
     ImGuiViewportP*         CurrentViewport;                    // We track changes of viewport (happening in Begin) so we can call Platform_OnChangedViewport()
     ImGuiViewportP*         MousePosViewport;
     ImGuiViewportP*         MousePosPrevViewport;
@@ -742,7 +741,6 @@ struct ImGuiContext
         FontSize = FontBaseSize = 0.0f;
         FontAtlasOwnedByContext = shared_font_atlas ? false : true;
         IO.Fonts = shared_font_atlas ? shared_font_atlas : IM_NEW(ImFontAtlas)();
-        memset(&PlatformIO, 0, sizeof(PlatformIO));
 
         Time = 0.0f;
         FrameCount = 0;