How to center a window? (was: How do I set a window position that relies on knowing window attributes without weird initial jittering?) #5618

Closed
opened 2022-08-28 15:47:23 +00:00 by tmsrise · 5 comments
tmsrise commented 2022-08-28 15:47:23 +00:00 (Migrated from github.com)
Dear ImGui 1.88 (18800)
--------------------------------

sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1931
define: _MSVC_LANG=202002
--------------------------------

io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx11
io.ConfigFlags: 0x00000022
 NavEnableGamepad
 NoMouseCursorChange
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000000E
 HasMouseCursors
 HasSetMousePos
 RendererHasVtxOffset
--------------------------------

io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 4096,8192
io.DisplaySize: 1920.00,1080.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------

style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 5.00,2.00
style.FrameRounding: 3.00
style.FrameBorderSize: 1.00
style.ItemSpacing: 6.00,6.00
style.ItemInnerSpacing: 6.00,6.00

My Issue/Question:

With SetWindowPos, centering horizontally is easy:

ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0));

But that introduces initial frames where the window is a different shape or position and it snaps into place.

SetNextWindow position solves this, but then you can't center the window or really move it into any position that relies on knowing its width or height, because you don't know those attributes yet. How do I solve this?

Screenshots/Video

https://streamable.com/4oride

The initial popup doesn't have the artifacting issue, because I hardcoded SetNextWindowPos to look as centered as possible, which you can see below.

Example:

if (ImGui::Button("Weapon Wheel Options [select]") || ImGui::IsKeyPressed(ImGuiKey_GamepadBack)) {
        ImGui::OpenPopup("wheel options");
        ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth()/1.3, 0)); 
       //hardcoded to seem centered
}

flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove;
if (ImGui::BeginPopup("wheel options", flags))
{
        //ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0));
        
        
        if (ImGui::Button("Advanced Settings")) 
	        ImGui::OpenPopup("Advanced Settings");

        if (ImGui::BeginPopup("Advanced Settings", flags))
        {
	        ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0));
	        ImGui::Text("Advanced Settings");
	        ImGui::EndPopup();
        }
        
        ImGui::EndPopup();
}
``` Dear ImGui 1.88 (18800) -------------------------------- sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20 define: __cplusplus=199711 define: _WIN32 define: _WIN64 define: _MSC_VER=1931 define: _MSVC_LANG=202002 -------------------------------- io.BackendPlatformName: imgui_impl_win32 io.BackendRendererName: imgui_impl_dx11 io.ConfigFlags: 0x00000022 NavEnableGamepad NoMouseCursorChange io.ConfigInputTextCursorBlink io.ConfigWindowsResizeFromEdges io.ConfigMemoryCompactTimer = 60.0 io.BackendFlags: 0x0000000E HasMouseCursors HasSetMousePos RendererHasVtxOffset -------------------------------- io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 4096,8192 io.DisplaySize: 1920.00,1080.00 io.DisplayFramebufferScale: 1.00,1.00 -------------------------------- style.WindowPadding: 8.00,8.00 style.WindowBorderSize: 1.00 style.FramePadding: 5.00,2.00 style.FrameRounding: 3.00 style.FrameBorderSize: 1.00 style.ItemSpacing: 6.00,6.00 style.ItemInnerSpacing: 6.00,6.00 ``` **My Issue/Question:** With SetWindowPos, centering horizontally is easy: `ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0));` But that introduces initial frames where the window is a different shape or position and it snaps into place. SetNextWindow position solves this, but then you can't center the window or really move it into any position that relies on knowing its width or height, because you don't know those attributes yet. How do I solve this? **Screenshots/Video** https://streamable.com/4oride The initial popup doesn't have the artifacting issue, because I hardcoded SetNextWindowPos to look as centered as possible, which you can see below. **Example:** ``` if (ImGui::Button("Weapon Wheel Options [select]") || ImGui::IsKeyPressed(ImGuiKey_GamepadBack)) { ImGui::OpenPopup("wheel options"); ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth()/1.3, 0)); //hardcoded to seem centered } flags = ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove; if (ImGui::BeginPopup("wheel options", flags)) { //ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0)); if (ImGui::Button("Advanced Settings")) ImGui::OpenPopup("Advanced Settings"); if (ImGui::BeginPopup("Advanced Settings", flags)) { ImGui::SetWindowPos(ImVec2(io.DisplaySize.x / 2 - ImGui::GetWindowWidth() / 2, 0)); ImGui::Text("Advanced Settings"); ImGui::EndPopup(); } ImGui::EndPopup(); } ```
ocornut commented 2022-08-29 06:14:20 +00:00 (Migrated from github.com)

In this situation you can use the pivot argment of SetNextWindowPos(). Set it to (0.5f, 0.5f) and so you can pass the central point. When the position is resolved on the appearing frame it’ll have access to size and can center the window without a hitch.

In this situation you can use the pivot argment of SetNextWindowPos(). Set it to (0.5f, 0.5f) and so you can pass the central point. When the position is resolved on the appearing frame it’ll have access to size and can center the window without a hitch.
ocornut commented 2022-08-31 16:51:40 +00:00 (Migrated from github.com)

Closing this as answered.

See Demo e.g.

// Always center this window when appearing
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));

if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize))

I guess this is not explicitly documented as a demo scenario.

Closing this as answered. See Demo e.g. ```cpp // Always center this window when appearing ImVec2 center = ImGui::GetMainViewport()->GetCenter(); ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f)); if (ImGui::BeginPopupModal("Delete?", NULL, ImGuiWindowFlags_AlwaysAutoResize)) ``` I guess this is not explicitly documented as a demo scenario.
JustaSimpleUser commented 2022-09-01 00:51:07 +00:00 (Migrated from github.com)

What's the importance of the ImGuiCond_Appearing condition? I've looked at the ImGuiCond_ enum definition and it doesn't help. When I use this example to centre a window, it does behave as I expect unless the condition is set to None or Always (I realize they're the same).

I suspect I don't understand what the comments mean when they refer to "the variable".

What's the importance of the _ImGuiCond_Appearing_ condition? I've looked at the _ImGuiCond__ enum definition and it doesn't help. When I use this example to centre a window, it does behave as I expect unless the condition is set to None or Always (I realize they're the same). I suspect I don't understand what the comments mean when they refer to "the variable".
ocornut commented 2022-09-01 09:13:22 +00:00 (Migrated from github.com)

Normally it should be sufficient to position during the frames the window is Appearing, as on the second frame the content size should be known. If Appearing doesn't work for you because the window keeps resizing for more than one frame then using no condition is fine.

Normally it should be sufficient to position during the frames the window is Appearing, as on the second frame the content size should be known. If Appearing doesn't work for you because the window keeps resizing for more than one frame then using no condition is fine.
JustaSimpleUser commented 2022-09-01 20:16:42 +00:00 (Migrated from github.com)

I think I understand that. Just to be clear, mine wasn't a resizing problem but a position problem. I'm trying to understand the various coordinate systems (the screen, the window, the child window, and the viewport) so that I can reliably place child windows programmatically, independent of the main window and screen location. In my case, the child window position did not change when I tried to centre it. Only changing the condition to Always permitted the move.

My current investigative goal is to prevent child windows from appearing outside the main window, which can occur on app start, since Windows is determining the initial position and size of the main window.

I cannot find a description of SetNextWindow's 3rd parameter, pivot. I note that it is simply passed to NextWindowData.PosPivotVal. Looking through that struct's definition doesn't provide any clarity and I can't find any other reference to PosPivotVal that might give me a clue.

Not understanding the coordinate systems, makes trial and error testing a waste of time. I am trying to use the demo's tools to display the window and viewport coordinates.

I think I understand that. Just to be clear, mine wasn't a resizing problem but a position problem. I'm trying to understand the various coordinate systems (the screen, the window, the child window, and the viewport) so that I can reliably place child windows programmatically, independent of the main window and screen location. In my case, the child window position did not change when I tried to centre it. Only changing the condition to Always permitted the move. My current investigative goal is to prevent child windows from appearing outside the main window, which can occur on app start, since Windows is determining the initial position and size of the main window. I cannot find a description of SetNextWindow's 3rd parameter, pivot. I note that it is simply passed to NextWindowData.PosPivotVal. Looking through that struct's definition doesn't provide any clarity and I can't find any other reference to PosPivotVal that might give me a clue. Not understanding the coordinate systems, makes trial and error testing a waste of time. I am trying to use the demo's tools to display the window and viewport coordinates.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#5618
No description provided.