Skip to content

Commit ba7495a

Browse files
committed
Move Layouts into ImGuiWindow DC.
1 parent cbaff75 commit ba7495a

File tree

2 files changed

+57
-65
lines changed

2 files changed

+57
-65
lines changed

imgui.cpp

Lines changed: 53 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ static void BeginLayout(ImGuiID id, ImGuiLayoutType type, ImVec2 siz
685685
static void EndLayout(ImGuiLayoutType type);
686686
static void PushLayout(ImGuiLayout* layout);
687687
static void PopLayout(ImGuiLayout* layout);
688-
static void ReflowLayouts();
688+
static void ProcessLayouts(ImGuiWindow* window);
689689
static void CalculateLayoutAvailableSpace(ImGuiLayout* layout);
690690
static void PropagateLayoutSpace(ImGuiLayout* layout, const ImVec2& extents);
691691
static void DistributeAvailableLayoutSpace(ImGuiLayout* layout);
@@ -1792,6 +1792,13 @@ ImGuiWindow::ImGuiWindow(const char* name)
17921792

17931793
ImGuiWindow::~ImGuiWindow()
17941794
{
1795+
for (int i = 0; i < DC.Layouts.Data.Size; ++i)
1796+
{
1797+
ImGuiLayout* layout = (ImGuiLayout*)DC.Layouts.Data[i].val_p;
1798+
layout->~ImGuiLayout();
1799+
ImGui::MemFree(layout);
1800+
}
1801+
17951802
DrawList->~ImDrawList();
17961803
ImGui::MemFree(DrawList);
17971804
DrawList = NULL;
@@ -1878,7 +1885,7 @@ void ImGui::ItemSize(const ImVec2& size, float text_offset_y)
18781885
const float text_base_offset = ImMax(window->DC.CurrentLineTextBaseOffset, text_offset_y);
18791886
window->DC.CursorPosPrevLine = ImVec2(window->DC.CursorPos.x + size.x, window->DC.CursorPos.y);
18801887

1881-
if (!g.CurrentLayout || g.CurrentLayout->Type == ImGuiLayoutType_Vertical)
1888+
if (!window->DC.CurrentLayout || window->DC.CurrentLayout->Type == ImGuiLayoutType_Vertical)
18821889
{
18831890
// Move cursor to next line, x to left, y down.
18841891
window->DC.CursorPos = ImVec2(
@@ -2370,12 +2377,6 @@ void ImGui::Shutdown()
23702377

23712378
SaveSettings();
23722379

2373-
for (int i = 0; i < g.Layouts.Size; ++i)
2374-
{
2375-
g.Layouts[i]->~ImGuiLayout();
2376-
ImGui::MemFree(g.Layouts[i]);
2377-
}
2378-
23792380
for (int i = 0; i < g.Windows.Size; i++)
23802381
{
23812382
g.Windows[i]->~ImGuiWindow();
@@ -2644,8 +2645,6 @@ void ImGui::EndFrame()
26442645
IM_ASSERT(g.Initialized); // Forgot to call ImGui::NewFrame()
26452646
IM_ASSERT(g.FrameCountEnded != g.FrameCount); // ImGui::EndFrame() called multiple times, or forgot to call ImGui::NewFrame() again
26462647

2647-
ReflowLayouts();
2648-
26492648
// Render tooltip
26502649
if (g.Tooltip[0])
26512650
{
@@ -3724,14 +3723,14 @@ static void CheckStacksSize(ImGuiWindow* window, bool write)
37243723
{ int current = g.StyleModifiers.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current && "PushStyleVar/PopStyleVar Mismatch!"); p_backup++; } // User forgot PopStyleVar()
37253724
{ int current = g.FontStack.Size; if (write) *p_backup = current; else IM_ASSERT(*p_backup == current && "PushFont/PopFont Mismatch!"); p_backup++; } // User forgot PopFont()
37263725
{ // User forgot EndVertical() or EndHorizontal()
3727-
int current = g.LayoutStack.Size;
3726+
int current = window->DC.LayoutStack.Size;
37283727
if (write)
37293728
{
37303729
*p_backup = current;
37313730
}
3732-
else if (!g.LayoutStack.empty() && g.LayoutStack.back())
3731+
else if (!window->DC.LayoutStack.empty() && window->DC.LayoutStack.back())
37333732
{
3734-
if (current == 0 || g.LayoutStack.back()->Type == ImGuiLayoutType_Horizontal)
3733+
if (current == 0 || window->DC.LayoutStack.back()->Type == ImGuiLayoutType_Horizontal)
37353734
IM_ASSERT(*p_backup == current && "BeginHorizontal/EndHorizontal Mismatch!");
37363735
else
37373736
IM_ASSERT(*p_backup == current && "BeginVertical/EndVertical Mismatch!");
@@ -4448,6 +4447,8 @@ void ImGui::End()
44484447
Columns(1, "#CloseColumns");
44494448
PopClipRect(); // inner window clip rectangle
44504449

4450+
ProcessLayouts(window);
4451+
44514452
// Stop logging
44524453
if (!(window->Flags & ImGuiWindowFlags_ChildWindow)) // FIXME: add more options for scope of logging
44534454
LogFinish();
@@ -9310,39 +9311,33 @@ static ImGuiLayout* FindLayout(ImGuiID id, ImGuiLayoutType type)
93109311
{
93119312
IM_ASSERT(type == ImGuiLayoutType_Horizontal || type == ImGuiLayoutType_Vertical);
93129313

9313-
ImGuiContext& g = *GImGui;
9314+
ImGuiWindow* window = ImGui::GetCurrentWindow();
9315+
ImGuiLayout* layout = (ImGuiLayout*)window->DC.Layouts.GetVoidPtr(id);
9316+
if (!layout)
9317+
return NULL;
93149318

9315-
for (int i = 0; i != g.Layouts.Size; i++)
9319+
if (layout->Type != type)
93169320
{
9317-
ImGuiLayout* layout = g.Layouts[i];
9318-
if (layout->Id == id)
9319-
{
9320-
if (layout->Type != type)
9321-
{
9322-
layout->Type = type;
9323-
layout->Dirty = ImGuiLayoutDirtyFlags_Uninitialized;
9324-
layout->Bounds = ImVec2(0.0f, 0.0f);
9325-
layout->Items.clear();
9326-
}
9327-
9328-
return layout;
9329-
}
9321+
layout->Type = type;
9322+
layout->Dirty = ImGuiLayoutDirtyFlags_Uninitialized;
9323+
layout->Bounds = ImVec2(0.0f, 0.0f);
9324+
layout->Items.clear();
93309325
}
93319326

9332-
return NULL;
9327+
return layout;
93339328
}
93349329

93359330
static ImGuiLayout* CreateNewLayout(ImGuiID id, ImGuiLayoutType type, ImVec2 size)
93369331
{
93379332
IM_ASSERT(type == ImGuiLayoutType_Horizontal || type == ImGuiLayoutType_Vertical);
93389333

9339-
ImGuiContext& g = *GImGui;
9334+
ImGuiWindow* window = ImGui::GetCurrentWindow();
93409335

93419336
ImGuiLayout* layout = (ImGuiLayout*)ImGui::MemAlloc(sizeof(ImGuiLayout));
93429337
IM_PLACEMENT_NEW(layout) ImGuiLayout(id, type);
93439338
layout->Size = size;
93449339

9345-
g.Layouts.push_back(layout);
9340+
window->DC.Layouts.SetVoidPtr(id, layout);
93469341

93479342
return layout;
93489343
}
@@ -9383,11 +9378,11 @@ static void BeginLayout(ImGuiID id, ImGuiLayoutType type, ImVec2 size, float ali
93839378

93849379
static void EndLayout(ImGuiLayoutType type)
93859380
{
9386-
ImGuiContext& g = *GImGui;
9387-
IM_ASSERT(g.CurrentLayout);
9388-
IM_ASSERT(g.CurrentLayout->Type == type);
9381+
ImGuiWindow* window = ImGui::GetCurrentWindow();
9382+
IM_ASSERT(window->DC.CurrentLayout);
9383+
IM_ASSERT(window->DC.CurrentLayout->Type == type);
93899384

9390-
ImGuiLayout* layout = g.CurrentLayout;
9385+
ImGuiLayout* layout = window->DC.CurrentLayout;
93919386

93929387
EndLayoutItem(layout, true);
93939388

@@ -9494,16 +9489,16 @@ static ImVec2 CalculateMinimumLayoutSize(ImGuiLayout* layout)
94949489

94959490
static void PushLayout(ImGuiLayout* layout)
94969491
{
9497-
ImGuiContext& g = *GImGui;
9492+
ImGuiWindow* window = ImGui::GetCurrentWindow();
94989493

94999494
if (layout)
95009495
{
9501-
layout->Parent = g.CurrentLayout;
9502-
if (g.CurrentLayout)
9496+
layout->Parent = window->DC.CurrentLayout;
9497+
if (window->DC.CurrentLayout)
95039498
{
9504-
layout->NextSibling = g.CurrentLayout->FirstChild;
9499+
layout->NextSibling = window->DC.CurrentLayout->FirstChild;
95059500
layout->FirstChild = NULL;
9506-
g.CurrentLayout->FirstChild = layout;
9501+
window->DC.CurrentLayout->FirstChild = layout;
95079502
}
95089503
else
95099504
{
@@ -9512,23 +9507,23 @@ static void PushLayout(ImGuiLayout* layout)
95129507
}
95139508
}
95149509

9515-
g.LayoutStack.push_back(layout);
9516-
g.CurrentLayout = layout;
9510+
window->DC.LayoutStack.push_back(layout);
9511+
window->DC.CurrentLayout = layout;
95179512
}
95189513

95199514
static void PopLayout(ImGuiLayout* layout)
95209515
{
9521-
ImGuiContext& g = *GImGui;
9516+
ImGuiWindow* window = ImGui::GetCurrentWindow();
95229517

9523-
IM_ASSERT(!g.LayoutStack.empty());
9524-
IM_ASSERT(g.LayoutStack.back() == layout);
9518+
IM_ASSERT(!window->DC.LayoutStack.empty());
9519+
IM_ASSERT(window->DC.LayoutStack.back() == layout);
95259520

9526-
g.LayoutStack.pop_back();
9521+
window->DC.LayoutStack.pop_back();
95279522

9528-
if (!g.LayoutStack.empty())
9529-
g.CurrentLayout = g.LayoutStack.back();
9523+
if (!window->DC.LayoutStack.empty())
9524+
window->DC.CurrentLayout = window->DC.LayoutStack.back();
95309525
else
9531-
g.CurrentLayout = NULL;
9526+
window->DC.CurrentLayout = NULL;
95329527
}
95339528

95349529
static void PropagateLayoutSpace(ImGuiLayout* layout, const ImVec2& extents)
@@ -9642,13 +9637,11 @@ static void DistributeAvailableLayoutSpace(ImGuiLayout* layout)
96429637
layout->Dirty = ImGuiLayoutDirtyFlags_None;
96439638
}
96449639

9645-
static void ReflowLayouts()
9640+
static void ProcessLayouts(ImGuiWindow* window)
96469641
{
9647-
ImGuiContext& g = *GImGui;
9648-
9649-
for (int i = 0; i < g.Layouts.size(); ++i)
9642+
for (int i = 0; i < window->DC.Layouts.Data.size(); ++i)
96509643
{
9651-
ImGuiLayout* layout = g.Layouts[i];
9644+
ImGuiLayout* layout = (ImGuiLayout*)window->DC.Layouts.Data[i].val_p;
96529645
if (!layout->Parent && layout->Dirty)
96539646
{
96549647
CalculateLayoutAvailableSpace(layout);
@@ -9891,23 +9884,22 @@ void ImGui::EndVertical()
98919884
// spacing >= 0 : enforce spacing amount
98929885
void ImGui::Spring(float weight/* = 1.0f*/, float spacing/* = -1.0f*/)
98939886
{
9894-
ImGuiContext& g = *GImGui;
9895-
IM_ASSERT(g.CurrentLayout);
9887+
ImGuiWindow* window = GetCurrentWindow();
9888+
IM_ASSERT(window->DC.CurrentLayout);
98969889

9897-
AddLayoutSpring(g.CurrentLayout, weight, spacing);
9890+
AddLayoutSpring(window->DC.CurrentLayout, weight, spacing);
98989891
}
98999892

99009893
void ImGui::SuspendLayout()
99019894
{
9902-
ImGuiContext& g = *GImGui;
99039895
PushLayout(NULL);
99049896
}
99059897

99069898
void ImGui::ResumeLayout()
99079899
{
9908-
ImGuiContext& g = *GImGui;
9909-
IM_ASSERT(!g.CurrentLayout);
9910-
IM_ASSERT(!g.LayoutStack.empty());
9900+
ImGuiWindow* window = GetCurrentWindow();
9901+
IM_ASSERT(!window->DC.CurrentLayout);
9902+
IM_ASSERT(!window->DC.LayoutStack.empty());
99119903
PopLayout(NULL);
99129904
}
99139905

imgui_internal.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,6 @@ struct ImGuiContext
442442
ImVector<ImFont*> FontStack; // Stack for PushFont()/PopFont()
443443
ImVector<ImGuiPopupRef> OpenPopupStack; // Which popups are open (persistent)
444444
ImVector<ImGuiPopupRef> CurrentPopupStack; // Which level of BeginPopup() we are in (reset every frame)
445-
ImGuiLayout* CurrentLayout;
446-
ImVector<ImGuiLayout*> LayoutStack;
447-
ImVector<ImGuiLayout*> Layouts;
448445

449446
// Storage for SetNexWindow** and SetNextTreeNode*** functions
450447
ImVec2 SetNextWindowPosVal;
@@ -528,7 +525,6 @@ struct ImGuiContext
528525
MovedWindow = NULL;
529526
MovedWindowMoveId = 0;
530527
SettingsDirtyTimer = 0.0f;
531-
CurrentLayout = NULL;
532528

533529
SetNextWindowPosVal = ImVec2(0.0f, 0.0f);
534530
SetNextWindowSizeVal = ImVec2(0.0f, 0.0f);
@@ -596,6 +592,9 @@ struct IMGUI_API ImGuiDrawContext
596592
ImVector<ImGuiWindow*> ChildWindows;
597593
ImGuiStorage* StateStorage;
598594
ImGuiLayoutType LayoutType;
595+
ImGuiLayout* CurrentLayout;
596+
ImVector<ImGuiLayout*> LayoutStack;
597+
ImGuiStorage Layouts;
599598

600599
// We store the current settings outside of the vectors to increase memory locality (reduce cache misses). The vectors are rarely modified. Also it allows us to not heap allocate for short-lived windows which are not using those settings.
601600
float ItemWidth; // == ItemWidthStack.back(). 0.0: default, >0.0: width in pixels, <0.0: align xx pixels to the right of window
@@ -638,6 +637,7 @@ struct IMGUI_API ImGuiDrawContext
638637
MenuBarOffsetX = 0.0f;
639638
StateStorage = NULL;
640639
LayoutType = ImGuiLayoutType_Vertical;
640+
CurrentLayout = NULL;
641641
ItemWidth = 0.0f;
642642
ButtonRepeat = false;
643643
AllowKeyboardFocus = true;

0 commit comments

Comments
 (0)