Skip to content

Commit 1d9e99d

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 ee977d4 commit 1d9e99d

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
@@ -1394,6 +1394,7 @@ ImGuiStyle::ImGuiStyle()
13941394
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
13951395
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
13961396
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
1397+
LayoutAlign = 0.5f; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
13971398
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
13981399
ImageBorderSize = 0.0f; // Thickness of border around tabs.
13991400
TabRounding = 5.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -3532,7 +3533,8 @@ static const ImGuiStyleVarInfo GStyleVarsInfo[] =
35323533
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
35333534
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
35343535
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
3535-
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3536+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, ImageBorderSize) }, // ImGuiStyleVar_ImageBorderSize
3537+
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, LayoutAlign) }, // ImGuiStyleVar_LayoutAlign
35363538
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
35373539
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize
35383540
{ 1, ImGuiDataType_Float, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize
@@ -11603,6 +11605,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
1160311605
g.NextItemData.HasFlags = ImGuiNextItemDataFlags_None;
1160411606
g.NextItemData.ItemFlags = ImGuiItemFlags_None;
1160511607

11608+
#if IMGUI_HAS_STACK_LAYOUT
11609+
ImGuiInternal::UpdateItemRect(window->ID, bb.Min, bb.Max);
11610+
#endif
11611+
1160611612
#ifdef IMGUI_ENABLE_TEST_ENGINE
1160711613
if (id != 0)
1160811614
IMGUI_TEST_ENGINE_ITEM_ADD(id, g.LastItemData.NavRect, &g.LastItemData);
@@ -11687,6 +11693,38 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1168711693
if (window->SkipItems)
1168811694
return;
1168911695

11696+
#if IMGUI_HAS_STACK_LAYOUT
11697+
ImGuiLayoutType layout_type = ImGuiInternal::GetCurrentLayoutType(window->ID);
11698+
#else
11699+
ImGuiLayoutType layout_type = window->DC.LayoutType;
11700+
#endif
11701+
11702+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorPos, 3.0f, IM_COL32(255,255,0,255), 4); // [DEBUG] Widget position
11703+
11704+
// Stack Layouts: Handle horizontal case first to simplify merge in case code handling vertical changes.
11705+
if (layout_type == ImGuiLayoutType_Horizontal)
11706+
{
11707+
const float line_width = ImMax(window->DC.CurrLineSize.x, size.x);
11708+
11709+
// Always align ourselves on pixel boundaries
11710+
//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]
11711+
window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x;
11712+
window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y + size.y;
11713+
window->DC.CursorPos.x = IM_TRUNC(window->DC.CursorPos.x + line_width + g.Style.ItemSpacing.x);
11714+
window->DC.CursorPos.y = IM_TRUNC(window->DC.CursorPosPrevLine.y - size.y);
11715+
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x - g.Style.ItemSpacing.x);
11716+
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPosPrevLine.y);
11717+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
11718+
11719+
window->DC.PrevLineSize.x = line_width;
11720+
window->DC.PrevLineSize.y = 0.0f;
11721+
window->DC.CurrLineSize.x = 0.0f;
11722+
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
11723+
window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
11724+
window->DC.IsSameLine = window->DC.IsSetPos = false;
11725+
return;
11726+
}
11727+
1169011728
// We increase the height in this function to accommodate for baseline offset.
1169111729
// In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor,
1169211730
// but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect.
@@ -11705,15 +11743,12 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1170511743
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y);
1170611744
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
1170711745

11746+
window->DC.PrevLineSize.x = 0.0f;
1170811747
window->DC.PrevLineSize.y = line_height;
1170911748
window->DC.CurrLineSize.y = 0.0f;
1171011749
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
1171111750
window->DC.CurrLineTextBaseOffset = 0.0f;
1171211751
window->DC.IsSameLine = window->DC.IsSetPos = false;
11713-
11714-
// Horizontal layout mode
11715-
if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
11716-
SameLine();
1171711752
}
1171811753
IM_MSVC_RUNTIME_CHECKS_RESTORE
1171911754

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]
@@ -1924,6 +1926,7 @@ enum ImGuiStyleVar_
19241926
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
19251927
ImGuiStyleVar_GrabRounding, // float GrabRounding
19261928
ImGuiStyleVar_ImageBorderSize, // float ImageBorderSize
1929+
ImGuiStyleVar_LayoutAlign, // float LayoutAlign
19271930
ImGuiStyleVar_TabRounding, // float TabRounding
19281931
ImGuiStyleVar_TabBorderSize, // float TabBorderSize
19291932
ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize
@@ -2422,6 +2425,7 @@ struct ImGuiStyle
24222425
float ScrollbarRounding; // Radius of grab corners for scrollbar.
24232426
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
24242427
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
2428+
float LayoutAlign; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
24252429
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
24262430
float ImageBorderSize; // Thickness of border around Image() calls.
24272431
float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
@@ -4505,6 +4509,10 @@ typedef ImFontAtlasRect ImFontAtlasCustomRect;
45054509
#pragma warning (pop)
45064510
#endif
45074511

4512+
#if IMGUI_HAS_STACK_LAYOUT
4513+
#include "imgui_stacklayout.h"
4514+
#endif
4515+
45084516
// Include imgui_user.h at the end of imgui.h
45094517
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
45104518
#ifdef IMGUI_INCLUDE_IMGUI_USER_H

imgui_demo.cpp

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

52235223
ImGui::TreePop();
52245224
}
5225+
5226+
#if IMGUI_HAS_STACK_LAYOUT
5227+
IMGUI_DEMO_MARKER("Layout/Stack Layout");
5228+
if (ImGui::TreeNode("Stack Layout"))
5229+
{
5230+
static bool widget_a = true, widget_b = true, widget_c = true;
5231+
static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
5232+
static bool minimize_width = false, minimize_height = true;
5233+
static bool horizontal = true, draw_springs = true;
5234+
static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
5235+
static float a_c_spring_weight = 0.0f;
5236+
static float ab_spring_weight = 0.5f;
5237+
static float alignment = 0.5f;
5238+
5239+
struct funcs
5240+
{
5241+
static void VisibleSpring(float spring_weight)
5242+
{
5243+
ImGui::Spring(spring_weight);
5244+
if (!draw_springs)
5245+
return;
5246+
5247+
ImVec2 rect_min = ImGui::GetItemRectMin();
5248+
ImVec2 rect_max = ImGui::GetItemRectMax();
5249+
5250+
ImVec2 rect_size = ImGui::GetItemRectSize();
5251+
if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
5252+
return;
5253+
5254+
// Draw zig-zag
5255+
float width = 0.0f, spacing = 0.0f;
5256+
ImVec2 direction, origin;
5257+
ImVec2 spacing_min, spring_max;
5258+
5259+
if (horizontal)
5260+
{
5261+
spacing = floorf(item_spacing.x);
5262+
width = rect_size.x - spacing;
5263+
origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
5264+
direction = ImVec2(1.0f, 0.0f);
5265+
spring_max = ImVec2(rect_min.x + width, rect_max.y);
5266+
spacing_min = ImVec2(rect_min.x + width, rect_min.y);
5267+
}
5268+
else
5269+
{
5270+
spacing = floorf(item_spacing.y);
5271+
width = rect_size.y - spacing;
5272+
origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
5273+
direction = ImVec2(0.0f, 1.0f);
5274+
spring_max = ImVec2(rect_max.x, rect_min.y + width);
5275+
spacing_min = ImVec2(rect_min.x, rect_min.y + width);
5276+
}
5277+
5278+
if (spring_weight <= 0.0f && spacing <= 0.0f)
5279+
return;
5280+
5281+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5282+
5283+
draw_list->PushClipRect(rect_min, rect_max, true);
5284+
5285+
draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
5286+
draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
5287+
5288+
const float zig_zag_size = 3;
5289+
ImVec2 normal = ImVec2(-direction.y, direction.x);
5290+
5291+
draw_list->PathClear();
5292+
origin.x += 0.5f;
5293+
origin.y += 0.5f;
5294+
draw_list->PathLineTo(origin);
5295+
for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
5296+
{
5297+
ImVec2 p;
5298+
p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
5299+
p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
5300+
draw_list->PathLineTo(p);
5301+
normal = ImVec2(-normal.x, -normal.y);
5302+
}
5303+
draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
5304+
5305+
draw_list->PopClipRect();
5306+
}
5307+
};
5308+
5309+
ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
5310+
ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
5311+
ImGui::Checkbox("Widget C", &widget_c);
5312+
ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
5313+
ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
5314+
ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
5315+
ImGui::Checkbox("Spring C", &spring_c);
5316+
ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
5317+
ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
5318+
ImGui::Checkbox("Minimize Height", &minimize_height);
5319+
ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
5320+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5321+
ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5322+
ImGui::TextUnformatted("Spring"); ImGui::SameLine();
5323+
ImGui::TextUnformatted(" "); ImGui::SameLine();
5324+
ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5325+
ImGui::TextUnformatted("Item Spacing");
5326+
ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
5327+
ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
5328+
ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
5329+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
5330+
ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
5331+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
5332+
ImGui::Text("Layout widgets:");
5333+
ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
5334+
5335+
ImGui::Spacing();
5336+
5337+
ImVec2 widget_size;
5338+
widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
5339+
widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
5340+
5341+
ImVec2 small_widget_size = widget_size;
5342+
if (horizontal)
5343+
small_widget_size.y = floorf(small_widget_size.y / 2);
5344+
else
5345+
small_widget_size.x = floorf(small_widget_size.x / 2);
5346+
5347+
ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
5348+
if (minimize_width) layout_size.x = 0.0f;
5349+
if (minimize_height) layout_size.y = 0.0f;
5350+
5351+
// Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
5352+
// Example:
5353+
// ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
5354+
5355+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
5356+
5357+
if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
5358+
if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
5359+
if (widget_a) { ImGui::Button("Widget A", widget_size); }
5360+
if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
5361+
if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
5362+
if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
5363+
if (widget_c) { ImGui::Button("Widget C", widget_size); }
5364+
if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
5365+
if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
5366+
5367+
ImGui::PopStyleVar();
5368+
5369+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
5370+
draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
5371+
5372+
ImGui::TreePop();
5373+
}
5374+
#endif // IMGUI_HAS_STACK_LAYOUT
52255375
}
52265376

52275377
//-----------------------------------------------------------------------------

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
@@ -247,6 +248,14 @@ typedef ImU16 ImGuiTableDrawChannelIdx;
247248
extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
248249
#endif
249250

251+
//-------------------------------------------------------------------------
252+
// [SECTION] Stack Layout includes
253+
//-------------------------------------------------------------------------
254+
255+
#if IMGUI_HAS_STACK_LAYOUT
256+
# include "imgui_stacklayout_internal.h"
257+
#endif
258+
250259
//-----------------------------------------------------------------------------
251260
// [SECTION] Macros
252261
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)