Skip to content

Commit f2db8c9

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 d1ce9c2 commit f2db8c9

File tree

7 files changed

+1401
-4
lines changed

7 files changed

+1401
-4
lines changed

imgui.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ ImGuiStyle::ImGuiStyle()
13221322
ScrollbarRounding = 9.0f; // Radius of grab corners rounding for scrollbar
13231323
GrabMinSize = 12.0f; // Minimum width/height of a grab box for slider/scrollbar
13241324
GrabRounding = 0.0f; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
1325+
LayoutAlign = 0.5f; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
13251326
LogSliderDeadzone = 4.0f; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
13261327
TabRounding = 4.0f; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
13271328
TabBorderSize = 0.0f; // Thickness of border around tabs.
@@ -3409,6 +3410,7 @@ static const ImGuiDataVarInfo GStyleVarInfo[] =
34093410
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, ScrollbarRounding) }, // ImGuiStyleVar_ScrollbarRounding
34103411
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabMinSize) }, // ImGuiStyleVar_GrabMinSize
34113412
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, GrabRounding) }, // ImGuiStyleVar_GrabRounding
3413+
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, LayoutAlign) }, // ImGuiStyleVar_LayoutAlign
34123414
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabRounding) }, // ImGuiStyleVar_TabRounding
34133415
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBorderSize) }, // ImGuiStyleVar_TabBorderSize
34143416
{ ImGuiDataType_Float, 1, (ImU32)offsetof(ImGuiStyle, TabBarBorderSize) }, // ImGuiStyleVar_TabBarBorderSize
@@ -11270,6 +11272,10 @@ bool ImGui::ItemAdd(const ImRect& bb, ImGuiID id, const ImRect* nav_bb_arg, ImGu
1127011272
g.NextItemData.Flags = ImGuiNextItemDataFlags_None;
1127111273
g.NextItemData.ItemFlags = ImGuiItemFlags_None;
1127211274

11275+
#if IMGUI_HAS_STACK_LAYOUT
11276+
ImGuiInternal::UpdateItemRect(window->ID, bb.Min, bb.Max);
11277+
#endif
11278+
1127311279
#ifdef IMGUI_ENABLE_TEST_ENGINE
1127411280
if (id != 0)
1127511281
IMGUI_TEST_ENGINE_ITEM_ADD(id, g.LastItemData.NavRect, &g.LastItemData);
@@ -11351,6 +11357,38 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1135111357
if (window->SkipItems)
1135211358
return;
1135311359

11360+
#if IMGUI_HAS_STACK_LAYOUT
11361+
ImGuiLayoutType layout_type = ImGuiInternal::GetCurrentLayoutType(window->ID);
11362+
#else
11363+
ImGuiLayoutType layout_type = window->DC.LayoutType;
11364+
#endif
11365+
11366+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorPos, 3.0f, IM_COL32(255,255,0,255), 4); // [DEBUG] Widget position
11367+
11368+
// Stack Layouts: Handle horizontal case first to simplify merge in case code handling vertical changes.
11369+
if (layout_type == ImGuiLayoutType_Horizontal)
11370+
{
11371+
const float line_width = ImMax(window->DC.CurrLineSize.x, size.x);
11372+
11373+
// Always align ourselves on pixel boundaries
11374+
//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]
11375+
window->DC.CursorPosPrevLine.x = window->DC.CursorPos.x;
11376+
window->DC.CursorPosPrevLine.y = window->DC.CursorPos.y + size.y;
11377+
window->DC.CursorPos.x = IM_TRUNC(window->DC.CursorPos.x + line_width + g.Style.ItemSpacing.x);
11378+
window->DC.CursorPos.y = IM_TRUNC(window->DC.CursorPosPrevLine.y - size.y);
11379+
window->DC.CursorMaxPos.x = ImMax(window->DC.CursorMaxPos.x, window->DC.CursorPos.x - g.Style.ItemSpacing.x);
11380+
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPosPrevLine.y);
11381+
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
11382+
11383+
window->DC.PrevLineSize.x = line_width;
11384+
window->DC.PrevLineSize.y = 0.0f;
11385+
window->DC.CurrLineSize.x = 0.0f;
11386+
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
11387+
window->DC.CurrLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
11388+
window->DC.IsSameLine = window->DC.IsSetPos = false;
11389+
return;
11390+
}
11391+
1135411392
// We increase the height in this function to accommodate for baseline offset.
1135511393
// In theory we should be offsetting the starting position (window->DC.CursorPos), that will be the topic of a larger refactor,
1135611394
// but since ItemSize() is not yet an API that moves the cursor (to handle e.g. wrapping) enlarging the height has the same effect.
@@ -11369,15 +11407,12 @@ void ImGui::ItemSize(const ImVec2& size, float text_baseline_y)
1136911407
window->DC.CursorMaxPos.y = ImMax(window->DC.CursorMaxPos.y, window->DC.CursorPos.y - g.Style.ItemSpacing.y);
1137011408
//if (g.IO.KeyAlt) window->DrawList->AddCircle(window->DC.CursorMaxPos, 3.0f, IM_COL32(255,0,0,255), 4); // [DEBUG]
1137111409

11410+
window->DC.PrevLineSize.x = 0.0f;
1137211411
window->DC.PrevLineSize.y = line_height;
1137311412
window->DC.CurrLineSize.y = 0.0f;
1137411413
window->DC.PrevLineTextBaseOffset = ImMax(window->DC.CurrLineTextBaseOffset, text_baseline_y);
1137511414
window->DC.CurrLineTextBaseOffset = 0.0f;
1137611415
window->DC.IsSameLine = window->DC.IsSetPos = false;
11377-
11378-
// Horizontal layout mode
11379-
if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
11380-
SameLine();
1138111416
}
1138211417
IM_MSVC_RUNTIME_CHECKS_RESTORE
1138311418

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]
@@ -1855,6 +1856,7 @@ enum ImGuiStyleVar_
18551856
ImGuiStyleVar_ScrollbarRounding, // float ScrollbarRounding
18561857
ImGuiStyleVar_GrabMinSize, // float GrabMinSize
18571858
ImGuiStyleVar_GrabRounding, // float GrabRounding
1859+
ImGuiStyleVar_LayoutAlign, // float LayoutAlign
18581860
ImGuiStyleVar_TabRounding, // float TabRounding
18591861
ImGuiStyleVar_TabBorderSize, // float TabBorderSize
18601862
ImGuiStyleVar_TabBarBorderSize, // float TabBarBorderSize
@@ -2338,6 +2340,7 @@ struct ImGuiStyle
23382340
float ScrollbarRounding; // Radius of grab corners for scrollbar.
23392341
float GrabMinSize; // Minimum width/height of a grab box for slider/scrollbar.
23402342
float GrabRounding; // Radius of grabs corners rounding. Set to 0.0f to have rectangular slider grabs.
2343+
float LayoutAlign; // Element alignment inside horizontal and vertical layouts (0.0f - left/top, 1.0f - right/bottom, 0.5f - center).
23412344
float LogSliderDeadzone; // The size in pixels of the dead-zone around zero on logarithmic sliders that cross zero.
23422345
float TabRounding; // Radius of upper corners of a tab. Set to 0.0f to have rectangular tabs.
23432346
float TabBorderSize; // Thickness of border around tabs.
@@ -4101,6 +4104,10 @@ namespace ImGui
41014104
#pragma warning (pop)
41024105
#endif
41034106

4107+
#if IMGUI_HAS_STACK_LAYOUT
4108+
#include "imgui_stacklayout.h"
4109+
#endif
4110+
41044111
// Include imgui_user.h at the end of imgui.h
41054112
// May be convenient for some users to only explicitly include vanilla imgui.h and have extra stuff included.
41064113
#ifdef IMGUI_INCLUDE_IMGUI_USER_H

imgui_demo.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4824,6 +4824,156 @@ static void ShowDemoWindowLayout()
48244824

48254825
ImGui::TreePop();
48264826
}
4827+
4828+
#if IMGUI_HAS_STACK_LAYOUT
4829+
IMGUI_DEMO_MARKER("Layout/Stack Layout");
4830+
if (ImGui::TreeNode("Stack Layout"))
4831+
{
4832+
static bool widget_a = true, widget_b = true, widget_c = true;
4833+
static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
4834+
static bool minimize_width = false, minimize_height = true;
4835+
static bool horizontal = true, draw_springs = true;
4836+
static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
4837+
static float a_c_spring_weight = 0.0f;
4838+
static float ab_spring_weight = 0.5f;
4839+
static float alignment = 0.5f;
4840+
4841+
struct funcs
4842+
{
4843+
static void VisibleSpring(float spring_weight)
4844+
{
4845+
ImGui::Spring(spring_weight);
4846+
if (!draw_springs)
4847+
return;
4848+
4849+
ImVec2 rect_min = ImGui::GetItemRectMin();
4850+
ImVec2 rect_max = ImGui::GetItemRectMax();
4851+
4852+
ImVec2 rect_size = ImGui::GetItemRectSize();
4853+
if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
4854+
return;
4855+
4856+
// Draw zig-zag
4857+
float width = 0.0f, spacing = 0.0f;
4858+
ImVec2 direction, origin;
4859+
ImVec2 spacing_min, spring_max;
4860+
4861+
if (horizontal)
4862+
{
4863+
spacing = floorf(item_spacing.x);
4864+
width = rect_size.x - spacing;
4865+
origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
4866+
direction = ImVec2(1.0f, 0.0f);
4867+
spring_max = ImVec2(rect_min.x + width, rect_max.y);
4868+
spacing_min = ImVec2(rect_min.x + width, rect_min.y);
4869+
}
4870+
else
4871+
{
4872+
spacing = floorf(item_spacing.y);
4873+
width = rect_size.y - spacing;
4874+
origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
4875+
direction = ImVec2(0.0f, 1.0f);
4876+
spring_max = ImVec2(rect_max.x, rect_min.y + width);
4877+
spacing_min = ImVec2(rect_min.x, rect_min.y + width);
4878+
}
4879+
4880+
if (spring_weight <= 0.0f && spacing <= 0.0f)
4881+
return;
4882+
4883+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
4884+
4885+
draw_list->PushClipRect(rect_min, rect_max, true);
4886+
4887+
draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
4888+
draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
4889+
4890+
const float zig_zag_size = 3;
4891+
ImVec2 normal = ImVec2(-direction.y, direction.x);
4892+
4893+
draw_list->PathClear();
4894+
origin.x += 0.5f;
4895+
origin.y += 0.5f;
4896+
draw_list->PathLineTo(origin);
4897+
for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
4898+
{
4899+
ImVec2 p;
4900+
p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
4901+
p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
4902+
draw_list->PathLineTo(p);
4903+
normal = ImVec2(-normal.x, -normal.y);
4904+
}
4905+
draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
4906+
4907+
draw_list->PopClipRect();
4908+
}
4909+
};
4910+
4911+
ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
4912+
ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
4913+
ImGui::Checkbox("Widget C", &widget_c);
4914+
ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
4915+
ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
4916+
ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
4917+
ImGui::Checkbox("Spring C", &spring_c);
4918+
ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
4919+
ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
4920+
ImGui::Checkbox("Minimize Height", &minimize_height);
4921+
ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
4922+
ImGui::TextUnformatted(" "); ImGui::SameLine();
4923+
ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4924+
ImGui::TextUnformatted("Spring"); ImGui::SameLine();
4925+
ImGui::TextUnformatted(" "); ImGui::SameLine();
4926+
ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4927+
ImGui::TextUnformatted("Item Spacing");
4928+
ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
4929+
ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
4930+
ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
4931+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
4932+
ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
4933+
if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
4934+
ImGui::Text("Layout widgets:");
4935+
ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
4936+
4937+
ImGui::Spacing();
4938+
4939+
ImVec2 widget_size;
4940+
widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
4941+
widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
4942+
4943+
ImVec2 small_widget_size = widget_size;
4944+
if (horizontal)
4945+
small_widget_size.y = floorf(small_widget_size.y / 2);
4946+
else
4947+
small_widget_size.x = floorf(small_widget_size.x / 2);
4948+
4949+
ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
4950+
if (minimize_width) layout_size.x = 0.0f;
4951+
if (minimize_height) layout_size.y = 0.0f;
4952+
4953+
// Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
4954+
// Example:
4955+
// ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
4956+
4957+
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
4958+
4959+
if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
4960+
if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
4961+
if (widget_a) { ImGui::Button("Widget A", widget_size); }
4962+
if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
4963+
if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
4964+
if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
4965+
if (widget_c) { ImGui::Button("Widget C", widget_size); }
4966+
if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
4967+
if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
4968+
4969+
ImGui::PopStyleVar();
4970+
4971+
ImDrawList* draw_list = ImGui::GetWindowDrawList();
4972+
draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
4973+
4974+
ImGui::TreePop();
4975+
}
4976+
#endif // IMGUI_HAS_STACK_LAYOUT
48274977
}
48284978

48294979
//-----------------------------------------------------------------------------

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
@@ -240,6 +241,14 @@ namespace ImStb
240241

241242
} // namespace ImStb
242243

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

0 commit comments

Comments
 (0)