Skip to content

Commit da07028

Browse files
inflexpiernov
authored andcommitted
Added ability to center per axis by passing -FLT_MAX to which ever axis
you want to have centered, ie; ImGui::SetNextWindowPos(ImVec2(-FLT_MAX, 100);
1 parent 5e1caaa commit da07028

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

imgui.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,8 @@ ImGuiWindow::ImGuiWindow(const char* name)
17941794
AutoPosLastDirection = -1;
17951795
HiddenFrames = 0;
17961796
SetWindowPosAllowFlags = SetWindowSizeAllowFlags = SetWindowCollapsedAllowFlags = ImGuiSetCond_Always | ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing;
1797-
SetWindowPosCenterWanted = false;
1797+
SetWindowPosXAxisCenterWanted = false;
1798+
SetWindowPosYAxisCenterWanted = false;
17981799

17991800
LastFrameActive = -1;
18001801
ItemWidthDefault = 0.0f;
@@ -3931,10 +3932,11 @@ bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_on_first_us
39313932
const ImVec2 backup_cursor_pos = window->DC.CursorPos; // FIXME: not sure of the exact reason of this saving/restore anymore :( need to look into that.
39323933
if (!window_was_active || window_appearing_after_being_hidden) window->SetWindowPosAllowFlags |= ImGuiSetCond_Appearing;
39333934
window_pos_set_by_api = (window->SetWindowPosAllowFlags & g.SetNextWindowPosCond) != 0;
3934-
if (window_pos_set_by_api && ImLengthSqr(g.SetNextWindowPosVal - ImVec2(-FLT_MAX,-FLT_MAX)) < 0.001f)
3935+
if (window_pos_set_by_api && ((g.SetNextWindowPosVal.x == -FLT_MAX) || (g.SetNextWindowPosVal.y == -FLT_MAX)))
39353936
{
3936-
window->SetWindowPosCenterWanted = true; // May be processed on the next frame if this is our first frame and we are measuring size
39373937
window->SetWindowPosAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
3938+
if (g.SetNextWindowPosVal.x == -FLT_MAX) window->SetWindowPosXAxisCenterWanted = true;
3939+
if (g.SetNextWindowPosVal.y == -FLT_MAX) window->SetWindowPosYAxisCenterWanted = true;
39383940
}
39393941
else
39403942
{
@@ -4117,12 +4119,15 @@ bool ImGui::Begin(const char* name, bool* p_open, const ImVec2& size_on_first_us
41174119
}
41184120

41194121
bool window_pos_center = false;
4120-
window_pos_center |= (window->SetWindowPosCenterWanted && window->HiddenFrames == 0);
4122+
window_pos_center |= ((window->SetWindowPosXAxisCenterWanted | window->SetWindowPosYAxisCenterWanted) && window->HiddenFrames == 0);
41214123
window_pos_center |= ((flags & ImGuiWindowFlags_Modal) && !window_pos_set_by_api && window_appearing_after_being_hidden);
41224124
if (window_pos_center)
41234125
{
41244126
// Center (any sort of window)
4125-
SetWindowPos(window, ImMax(style.DisplaySafeAreaPadding, fullscreen_rect.GetCenter() - window->SizeFull * 0.5f), 0);
4127+
ImVec2 centerv = fullscreen_rect.GetCenter() - window->SizeFull * 0.5f;
4128+
if (!window->SetWindowPosXAxisCenterWanted) centerv.x = g.SetNextWindowPosVal.x;
4129+
if (!window->SetWindowPosYAxisCenterWanted) centerv.y = g.SetNextWindowPosVal.y;
4130+
SetWindowPos(window, ImMax(style.DisplaySafeAreaPadding, centerv), 0);
41264131
}
41274132
else if (flags & ImGuiWindowFlags_ChildMenu)
41284133
{
@@ -4919,7 +4924,8 @@ static void SetWindowPos(ImGuiWindow* window, const ImVec2& pos, ImGuiSetCond co
49194924
if (cond && (window->SetWindowPosAllowFlags & cond) == 0)
49204925
return;
49214926
window->SetWindowPosAllowFlags &= ~(ImGuiSetCond_Once | ImGuiSetCond_FirstUseEver | ImGuiSetCond_Appearing);
4922-
window->SetWindowPosCenterWanted = false;
4927+
window->SetWindowPosXAxisCenterWanted = false;
4928+
window->SetWindowPosYAxisCenterWanted = false;
49234929

49244930
// Set
49254931
const ImVec2 old_pos = window->Pos;

imgui_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ struct IMGUI_API ImGuiWindow
650650
int SetWindowPosAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowPos() call will succeed with this particular flag.
651651
int SetWindowSizeAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowSize() call will succeed with this particular flag.
652652
int SetWindowCollapsedAllowFlags; // bit ImGuiSetCond_*** specify if SetWindowCollapsed() call will succeed with this particular flag.
653-
bool SetWindowPosCenterWanted;
653+
bool SetWindowPosXAxisCenterWanted;
654+
bool SetWindowPosYAxisCenterWanted;
654655

655656
ImGuiDrawContext DC; // Temporary per-window data, reset at the beginning of the frame
656657
ImVector<ImGuiID> IDStack; // ID stack. ID are hashes seeded with the value at the top of the stack

0 commit comments

Comments
 (0)