Skip to content

Commit a152b0d

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 c3e6acd commit a152b0d

File tree

7 files changed

+1403
-5
lines changed

7 files changed

+1403
-5
lines changed

imgui.cpp

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,7 @@ ImGuiStyle::ImGuiStyle()
13771377
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
13781378
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
13791379
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
1380+
LayoutAlign = 0.5f; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
13801381
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
13811382
ImageBorderSize = 0.0f; // Thickness of border around tabs.
13821383
TabRounding = 5.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -3511,7 +3512,8 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
35113512
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
35123513
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
35133514
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
3514-
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3515+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3516+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, LayoutAlign) }, // ImGuiStyleVar_LayoutAlign
35153517
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
35163518
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize
35173519
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize
@@ -11518,6 +11520,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
1151811520
g.NextItemData.HasFlags = ImGuiNextItemDataFlags_None;
1151911521
g.NextItemData.ItemFlags = ImGuiItemFlags_None;
1152011522

11523+
#if IMGUI_HAS_STACK_LAYOUT
11524+
ImGuiInternal::UpdateItemRect(window->ID, bb.Min, bb.Max);
11525+
#endif
11526+
1152111527
#ifdef IMGUI_ENABLE_TEST_ENGINE
1152211528
if (id != 0)
1152311529
IMGUI_TEST_ENGINE_ITEM_ADD(id, g.LastItemData.NavRect, &g.LastItemData);
@@ -11602,6 +11608,38 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1160211608
if (window->SkipItems)
1160311609
return;
1160411610

11611+
#if IMGUI_HAS_STACK_LAYOUT
11612+
ImGuiLayoutType layout_type = ImGuiInternal::GetCurrentLayoutType(window->ID);
11613+
#else
11614+
ImGuiLayoutType layout_type = window->DC.LayoutType;
11615+
#endif
11616+
11617+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorPos, 3.0f, IM_COL32(255,255,0,255), 4); // [DEBUG] Widget position
11618+
11619+
// Stack Layouts: Handle horizontal case first to simplify merge in case code handling vertical changes.
11620+
if (layout_type == ImGuiLayoutType_Horizontal)
11621+
{
11622+
const float line_width = ImMax(window->DC.CurrLineSize.x, size.x);
11623+
11624+
// Always align ourselves on pixel boundaries
11625+
//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]
11626+
window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x;
11627+
window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y + size.y;
11628+
window->DC.CursorPos.x = IM_TRUNC(window->DC.CursorPos.x + line_width + g.Style.ItemSpacing.x);
11629+
window->DC.CursorPos.y = IM_TRUNC(window->DC.CursorPosPrevLine.y - size.y);
11630+
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x - g.Style.ItemSpacing.x);
11631+
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPosPrevLine.y);
11632+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
11633+
11634+
window->DC.PrevLineSize.x = line_width;
11635+
window->DC.PrevLineSize.y = 0.0f;
11636+
window->DC.CurrLineSize.x = 0.0f;
11637+
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
11638+
window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
11639+
window->DC.IsSameLine = window->DC.IsSetPos = false;
11640+
return;
11641+
}
11642+
1160511643
// We increase the height in this function to accommodate for baseline offset.
1160611644
// In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor,
1160711645
// but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect.
@@ -11620,15 +11658,12 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1162011658
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y);
1162111659
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
1162211660

11661+
window->DC.PrevLineSize.x = 0.0f;
1162311662
window->DC.PrevLineSize.y = line_height;
1162411663
window->DC.CurrLineSize.y = 0.0f;
1162511664
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
1162611665
window->DC.CurrLineTextBaseOffset = 0.0f;
1162711666
window->DC.IsSameLine = window->DC.IsSetPos = false;
11628-
11629-
// Horizontal layout mode
11630-
if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
11631-
SameLine();
1163211667
}
1163311668
IM_MSVC_RUNTIME_CHECKS_RESTORE
1163411669

imgui.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define IMGUI_HAS_VIEWPORT // Viewport WIP branch
3535
#define IMGUI_HAS_DOCK // Docking WIP branch
3636
#define IMGUI_HAS_TEXTURES // 1.92+ WIP branch with ImGuiBackendFlags_RendererHasTextures
37+
#define IMGUI_HAS_STACK_LAYOUT 1 // Stack-Layout PR #846
38+
3739

3840
//
3941
// Adaptations for ImGui Bundle are noted with [ADAPT_IMGUI_BUNDLE]
@@ -1916,6 +1918,7 @@ enum ImGuiStyleVar_
19161918
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
19171919
ImGuiStyleVar_GrabRounding, // float GrabRounding
19181920
ImGuiStyleVar_ImageBorderSize, // float ImageBorderSize
1921+
ImGuiStyleVar_LayoutAlign, // float LayoutAlign
19191922
ImGuiStyleVar_TabRounding, // float TabRounding
19201923
ImGuiStyleVar_TabBorderSize, // float TabBorderSize
19211924
ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize
@@ -2412,6 +2415,7 @@ struct ImGuiStyle
24122415
float ScrollbarRounding; // Radius of grab corners for scrollbar.
24132416
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
24142417
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
2418+
float LayoutAlign; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
24152419
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
24162420
float ImageBorderSize; // Thickness of border around Image() calls.
24172421
float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -4450,6 +4454,10 @@ typedef ImTextureRect ImFontAtlasCustomRect;
44504454
#pragma warning (pop)
44514455
#endif
44524456

4457+
#if IMGUI_HAS_STACK_LAYOUT
4458+
#include "imgui_stacklayout.h"
4459+
#endif
4460+
44534461
// Include imgui_user.h at the end of imgui.h
44544462
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
44554463
#ifdef IMGUI_INCLUDE_IMGUI_USER_H

imgui_demo.cpp

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

51875187
ImGui::TreePop();
51885188
}
5189+
5190+
#if IMGUI_HAS_STACK_LAYOUT
5191+
IMGUI_DEMO_MARKER("Layout/Stack Layout");
5192+
if (ImGui::TreeNode("Stack Layout"))
5193+
{
5194+
static bool widget_a = true, widget_b = true, widget_c = true;
5195+
static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
5196+
static bool minimize_width = false, minimize_height = true;
5197+
static bool horizontal = true, draw_springs = true;
5198+
static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
5199+
static float a_c_spring_weight = 0.0f;
5200+
static float ab_spring_weight = 0.5f;
5201+
static float alignment = 0.5f;
5202+
5203+
struct funcs
5204+
{
5205+
static void VisibleSpring(float spring_weight)
5206+
{
5207+
ImGui::Spring(spring_weight);
5208+
if (!draw_springs)
5209+
return;
5210+
5211+
ImVec2 rect_min = ImGui::GetItemRectMin();
5212+
ImVec2 rect_max = ImGui::GetItemRectMax();
5213+
5214+
ImVec2 rect_size = ImGui::GetItemRectSize();
5215+
if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
5216+
return;
5217+
5218+
// Draw zig-zag
5219+
float width = 0.0f, spacing = 0.0f;
5220+
ImVec2 direction, origin;
5221+
ImVec2 spacing_min, spring_max;
5222+
5223+
if (horizontal)
5224+
{
5225+
spacing = floorf(item_spacing.x);
5226+
width = rect_size.x - spacing;
5227+
origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
5228+
direction = ImVec2(1.0f, 0.0f);
5229+
spring_max = ImVec2(rect_min.x + width, rect_max.y);
5230+
spacing_min = ImVec2(rect_min.x + width, rect_min.y);
5231+
}
5232+
else
5233+
{
5234+
spacing = floorf(item_spacing.y);
5235+
width = rect_size.y - spacing;
5236+
origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
5237+
direction = ImVec2(0.0f, 1.0f);
5238+
spring_max = ImVec2(rect_max.x, rect_min.y + width);
5239+
spacing_min = ImVec2(rect_min.x, rect_min.y + width);
5240+
}
5241+
5242+
if (spring_weight <= 0.0f && spacing <= 0.0f)
5243+
return;
5244+
5245+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5246+
5247+
draw_list->PushClipRect(rect_min, rect_max, true);
5248+
5249+
draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
5250+
draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
5251+
5252+
const float zig_zag_size = 3;
5253+
ImVec2 normal = ImVec2(-direction.y, direction.x);
5254+
5255+
draw_list->PathClear();
5256+
origin.x += 0.5f;
5257+
origin.y += 0.5f;
5258+
draw_list->PathLineTo(origin);
5259+
for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
5260+
{
5261+
ImVec2 p;
5262+
p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
5263+
p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
5264+
draw_list->PathLineTo(p);
5265+
normal = ImVec2(-normal.x, -normal.y);
5266+
}
5267+
draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
5268+
5269+
draw_list->PopClipRect();
5270+
}
5271+
};
5272+
5273+
ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
5274+
ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
5275+
ImGui::Checkbox("Widget C", &widget_c);
5276+
ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
5277+
ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
5278+
ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
5279+
ImGui::Checkbox("Spring C", &spring_c);
5280+
ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
5281+
ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
5282+
ImGui::Checkbox("Minimize Height", &minimize_height);
5283+
ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
5284+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5285+
ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5286+
ImGui::TextUnformatted("Spring"); ImGui::SameLine();
5287+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5288+
ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5289+
ImGui::TextUnformatted("Item Spacing");
5290+
ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
5291+
ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
5292+
ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
5293+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
5294+
ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
5295+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
5296+
ImGui::Text("Layout widgets:");
5297+
ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
5298+
5299+
ImGui::Spacing();
5300+
5301+
ImVec2 widget_size;
5302+
widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
5303+
widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
5304+
5305+
ImVec2 small_widget_size = widget_size;
5306+
if (horizontal)
5307+
small_widget_size.y = floorf(small_widget_size.y / 2);
5308+
else
5309+
small_widget_size.x = floorf(small_widget_size.x / 2);
5310+
5311+
ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
5312+
if (minimize_width) layout_size.x = 0.0f;
5313+
if (minimize_height) layout_size.y = 0.0f;
5314+
5315+
// Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
5316+
// Example:
5317+
// ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
5318+
5319+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
5320+
5321+
if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
5322+
if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
5323+
if (widget_a) { ImGui::Button("Widget A", widget_size); }
5324+
if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
5325+
if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
5326+
if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
5327+
if (widget_c) { ImGui::Button("Widget C", widget_size); }
5328+
if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
5329+
if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
5330+
5331+
ImGui::PopStyleVar();
5332+
5333+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5334+
draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
5335+
5336+
ImGui::TreePop();
5337+
}
5338+
#endif // IMGUI_HAS_STACK_LAYOUT
51895339
}
51905340

51915341
//-----------------------------------------------------------------------------

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
@@ -243,6 +244,14 @@ using ImGuiErrorStringCallback = std::function<void(const std::string&)>;
243244
extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
244245
#endif
245246

247+
//-------------------------------------------------------------------------
248+
// [SECTION] Stack Layout includes
249+
//-------------------------------------------------------------------------
250+
251+
#if IMGUI_HAS_STACK_LAYOUT
252+
# include "imgui_stacklayout_internal.h"
253+
#endif
254+
246255
//-----------------------------------------------------------------------------
247256
// [SECTION] Macros
248257
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)