Skip to content

Commit a3d3d1b

Browse files
thedmdpthom
authored andcommitted
thedmd: Stack Layout implementation v2.1 / pthom: revert clipping
See ocornut#846 (comment) : clipping has an issue, when mixed with your node editor and springs.
1 parent 77fe4d0 commit a3d3d1b

File tree

7 files changed

+1402
-5
lines changed

7 files changed

+1402
-5
lines changed

imgui.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ ImGuiStyle::ImGuiStyle()
13751375
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
13761376
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
13771377
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
1378+
LayoutAlign = 0.5f; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
13781379
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
13791380
ImageBorderSize = 0.0f; // Thickness of border around tabs.
13801381
TabRounding = 5.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -3503,7 +3504,8 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
35033504
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
35043505
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
35053506
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
3506-
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3507+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3508+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, LayoutAlign) }, // ImGuiStyleVar_LayoutAlign
35073509
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
35083510
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize
35093511
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize
@@ -11407,6 +11409,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
1140711409
g.NextItemData.HasFlags = ImGuiNextItemDataFlags_None;
1140811410
g.NextItemData.ItemFlags = ImGuiItemFlags_None;
1140911411

11412+
#if IMGUI_HAS_STACK_LAYOUT
11413+
ImGuiInternal::UpdateItemRect(window->ID, bb.Min, bb.Max);
11414+
#endif
11415+
1141011416
#ifdef IMGUI_ENABLE_TEST_ENGINE
1141111417
if (id != 0)
1141211418
IMGUI_TEST_ENGINE_ITEM_ADD(id, g.LastItemData.NavRect, &g.LastItemData);
@@ -11491,6 +11497,38 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1149111497
if (window->SkipItems)
1149211498
return;
1149311499

11500+
#if IMGUI_HAS_STACK_LAYOUT
11501+
ImGuiLayoutType layout_type = ImGuiInternal::GetCurrentLayoutType(window->ID);
11502+
#else
11503+
ImGuiLayoutType layout_type = window->DC.LayoutType;
11504+
#endif
11505+
11506+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorPos, 3.0f, IM_COL32(255,255,0,255), 4); // [DEBUG] Widget position
11507+
11508+
// Stack Layouts: Handle horizontal case first to simplify merge in case code handling vertical changes.
11509+
if (layout_type == ImGuiLayoutType_Horizontal)
11510+
{
11511+
const float line_width = ImMax(window->DC.CurrLineSize.x, size.x);
11512+
11513+
// Always align ourselves on pixel boundaries
11514+
//if (g.IO.KeyAlt) window->DrawList->AddRect(window->DC.CursorPos, window->DC.CursorPos + ImVec2(size.x, line_height), IM_COL32(255,0,0,200)); // [DEBUG]
11515+
window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x;
11516+
window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y + size.y;
11517+
window->DC.CursorPos.x = IM_TRUNC(window->DC.CursorPos.x + line_width + g.Style.ItemSpacing.x);
11518+
window->DC.CursorPos.y = IM_TRUNC(window->DC.CursorPosPrevLine.y - size.y);
11519+
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x - g.Style.ItemSpacing.x);
11520+
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPosPrevLine.y);
11521+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
11522+
11523+
window->DC.PrevLineSize.x = line_width;
11524+
window->DC.PrevLineSize.y = 0.0f;
11525+
window->DC.CurrLineSize.x = 0.0f;
11526+
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
11527+
window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
11528+
window->DC.IsSameLine = window->DC.IsSetPos = false;
11529+
return;
11530+
}
11531+
1149411532
// We increase the height in this function to accommodate for baseline offset.
1149511533
// In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor,
1149611534
// but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect.
@@ -11509,15 +11547,12 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1150911547
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y);
1151011548
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
1151111549

11550+
window->DC.PrevLineSize.x = 0.0f;
1151211551
window->DC.PrevLineSize.y = line_height;
1151311552
window->DC.CurrLineSize.y = 0.0f;
1151411553
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
1151511554
window->DC.CurrLineTextBaseOffset = 0.0f;
1151611555
window->DC.IsSameLine = window->DC.IsSetPos = false;
11517-
11518-
// Horizontal layout mode
11519-
if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
11520-
SameLine();
1152111556
}
1152211557
IM_MSVC_RUNTIME_CHECKS_RESTORE
1152311558

imgui.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define IMGUI_HAS_TABLE
3434
#define IMGUI_HAS_VIEWPORT // Viewport WIP branch
3535
#define IMGUI_HAS_DOCK // Docking WIP branch
36+
#define IMGUI_HAS_STACK_LAYOUT 1 // Stack-Layout PR #846
3637

3738
//
3839
// Adaptations for ImGui Bundle are noted with [ADAPT_IMGUI_BUNDLE]
@@ -1854,6 +1855,7 @@ enum ImGuiStyleVar_
18541855
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
18551856
ImGuiStyleVar_GrabRounding, // float GrabRounding
18561857
ImGuiStyleVar_ImageBorderSize, // float ImageBorderSize
1858+
ImGuiStyleVar_LayoutAlign, // float LayoutAlign
18571859
ImGuiStyleVar_TabRounding, // float TabRounding
18581860
ImGuiStyleVar_TabBorderSize, // float TabBorderSize
18591861
ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize
@@ -2350,6 +2352,7 @@ struct ImGuiStyle
23502352
float ScrollbarRounding; // Radius of grab corners for scrollbar.
23512353
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
23522354
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
2355+
float LayoutAlign; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
23532356
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
23542357
float ImageBorderSize; // Thickness of border around Image() calls.
23552358
float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -4195,6 +4198,10 @@ namespace ImGui
41954198
#pragma warning (pop)
41964199
#endif
41974200

4201+
#if IMGUI_HAS_STACK_LAYOUT
4202+
#include "imgui_stacklayout.h"
4203+
#endif
4204+
41984205
// Include imgui_user.h at the end of imgui.h
41994206
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
42004207
#ifdef IMGUI_INCLUDE_IMGUI_USER_H

imgui_demo.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5147,6 +5147,156 @@ static void DemoWindowLayout()
51475147

51485148
ImGui::TreePop();
51495149
}
5150+
5151+
#if IMGUI_HAS_STACK_LAYOUT
5152+
IMGUI_DEMO_MARKER("Layout/Stack Layout");
5153+
if (ImGui::TreeNode("Stack Layout"))
5154+
{
5155+
static bool widget_a = true, widget_b = true, widget_c = true;
5156+
static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
5157+
static bool minimize_width = false, minimize_height = true;
5158+
static bool horizontal = true, draw_springs = true;
5159+
static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
5160+
static float a_c_spring_weight = 0.0f;
5161+
static float ab_spring_weight = 0.5f;
5162+
static float alignment = 0.5f;
5163+
5164+
struct funcs
5165+
{
5166+
static void VisibleSpring(float spring_weight)
5167+
{
5168+
ImGui::Spring(spring_weight);
5169+
if (!draw_springs)
5170+
return;
5171+
5172+
ImVec2 rect_min = ImGui::GetItemRectMin();
5173+
ImVec2 rect_max = ImGui::GetItemRectMax();
5174+
5175+
ImVec2 rect_size = ImGui::GetItemRectSize();
5176+
if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
5177+
return;
5178+
5179+
// Draw zig-zag
5180+
float width = 0.0f, spacing = 0.0f;
5181+
ImVec2 direction, origin;
5182+
ImVec2 spacing_min, spring_max;
5183+
5184+
if (horizontal)
5185+
{
5186+
spacing = floorf(item_spacing.x);
5187+
width = rect_size.x - spacing;
5188+
origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
5189+
direction = ImVec2(1.0f, 0.0f);
5190+
spring_max = ImVec2(rect_min.x + width, rect_max.y);
5191+
spacing_min = ImVec2(rect_min.x + width, rect_min.y);
5192+
}
5193+
else
5194+
{
5195+
spacing = floorf(item_spacing.y);
5196+
width = rect_size.y - spacing;
5197+
origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
5198+
direction = ImVec2(0.0f, 1.0f);
5199+
spring_max = ImVec2(rect_max.x, rect_min.y + width);
5200+
spacing_min = ImVec2(rect_min.x, rect_min.y + width);
5201+
}
5202+
5203+
if (spring_weight <= 0.0f && spacing <= 0.0f)
5204+
return;
5205+
5206+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5207+
5208+
draw_list->PushClipRect(rect_min, rect_max, true);
5209+
5210+
draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
5211+
draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
5212+
5213+
const float zig_zag_size = 3;
5214+
ImVec2 normal = ImVec2(-direction.y, direction.x);
5215+
5216+
draw_list->PathClear();
5217+
origin.x += 0.5f;
5218+
origin.y += 0.5f;
5219+
draw_list->PathLineTo(origin);
5220+
for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
5221+
{
5222+
ImVec2 p;
5223+
p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
5224+
p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
5225+
draw_list->PathLineTo(p);
5226+
normal = ImVec2(-normal.x, -normal.y);
5227+
}
5228+
draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
5229+
5230+
draw_list->PopClipRect();
5231+
}
5232+
};
5233+
5234+
ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
5235+
ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
5236+
ImGui::Checkbox("Widget C", &widget_c);
5237+
ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
5238+
ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
5239+
ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
5240+
ImGui::Checkbox("Spring C", &spring_c);
5241+
ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
5242+
ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
5243+
ImGui::Checkbox("Minimize Height", &minimize_height);
5244+
ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
5245+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5246+
ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5247+
ImGui::TextUnformatted("Spring"); ImGui::SameLine();
5248+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5249+
ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5250+
ImGui::TextUnformatted("Item Spacing");
5251+
ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
5252+
ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
5253+
ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
5254+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
5255+
ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
5256+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
5257+
ImGui::Text("Layout widgets:");
5258+
ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
5259+
5260+
ImGui::Spacing();
5261+
5262+
ImVec2 widget_size;
5263+
widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
5264+
widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
5265+
5266+
ImVec2 small_widget_size = widget_size;
5267+
if (horizontal)
5268+
small_widget_size.y = floorf(small_widget_size.y / 2);
5269+
else
5270+
small_widget_size.x = floorf(small_widget_size.x / 2);
5271+
5272+
ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
5273+
if (minimize_width) layout_size.x = 0.0f;
5274+
if (minimize_height) layout_size.y = 0.0f;
5275+
5276+
// Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
5277+
// Example:
5278+
// ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
5279+
5280+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
5281+
5282+
if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
5283+
if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
5284+
if (widget_a) { ImGui::Button("Widget A", widget_size); }
5285+
if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
5286+
if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
5287+
if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
5288+
if (widget_c) { ImGui::Button("Widget C", widget_size); }
5289+
if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
5290+
if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
5291+
5292+
ImGui::PopStyleVar();
5293+
5294+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5295+
draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
5296+
5297+
ImGui::TreePop();
5298+
}
5299+
#endif // IMGUI_HAS_STACK_LAYOUT
51505300
}
51515301

51525302
//-----------------------------------------------------------------------------

imgui_internal.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Index of this file:
1111
// [SECTION] Forward declarations
1212
// [SECTION] Context pointer
1313
// [SECTION] STB libraries includes
14+
// [SECTION] Stack Layout includes
1415
// [SECTION] Macros
1516
// [SECTION] Generic helpers
1617
// [SECTION] ImDrawList support
@@ -239,6 +240,14 @@ using ImGuiErrorStringCallback = std::function<void(const std::string&)>;
239240
extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
240241
#endif
241242

243+
//-------------------------------------------------------------------------
244+
// [SECTION] Stack Layout includes
245+
//-------------------------------------------------------------------------
246+
247+
#if IMGUI_HAS_STACK_LAYOUT
248+
# include "imgui_stacklayout_internal.h"
249+
#endif
250+
242251
//-----------------------------------------------------------------------------
243252
// [SECTION] Macros
244253
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)