@@ -4881,6 +4881,156 @@ static void ShowDemoWindowLayout()
4881
4881
4882
4882
ImGui::TreePop();
4883
4883
}
4884
+
4885
+ #if IMGUI_HAS_STACK_LAYOUT
4886
+ IMGUI_DEMO_MARKER("Layout/Stack Layout");
4887
+ if (ImGui::TreeNode("Stack Layout"))
4888
+ {
4889
+ static bool widget_a = true, widget_b = true, widget_c = true;
4890
+ static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
4891
+ static bool minimize_width = false, minimize_height = true;
4892
+ static bool horizontal = true, draw_springs = true;
4893
+ static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
4894
+ static float a_c_spring_weight = 0.0f;
4895
+ static float ab_spring_weight = 0.5f;
4896
+ static float alignment = 0.5f;
4897
+
4898
+ struct funcs
4899
+ {
4900
+ static void VisibleSpring(float spring_weight)
4901
+ {
4902
+ ImGui::Spring(spring_weight);
4903
+ if (!draw_springs)
4904
+ return;
4905
+
4906
+ ImVec2 rect_min = ImGui::GetItemRectMin();
4907
+ ImVec2 rect_max = ImGui::GetItemRectMax();
4908
+
4909
+ ImVec2 rect_size = ImGui::GetItemRectSize();
4910
+ if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
4911
+ return;
4912
+
4913
+ // Draw zig-zag
4914
+ float width = 0.0f, spacing = 0.0f;
4915
+ ImVec2 direction, origin;
4916
+ ImVec2 spacing_min, spring_max;
4917
+
4918
+ if (horizontal)
4919
+ {
4920
+ spacing = floorf(item_spacing.x);
4921
+ width = rect_size.x - spacing;
4922
+ origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
4923
+ direction = ImVec2(1.0f, 0.0f);
4924
+ spring_max = ImVec2(rect_min.x + width, rect_max.y);
4925
+ spacing_min = ImVec2(rect_min.x + width, rect_min.y);
4926
+ }
4927
+ else
4928
+ {
4929
+ spacing = floorf(item_spacing.y);
4930
+ width = rect_size.y - spacing;
4931
+ origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
4932
+ direction = ImVec2(0.0f, 1.0f);
4933
+ spring_max = ImVec2(rect_max.x, rect_min.y + width);
4934
+ spacing_min = ImVec2(rect_min.x, rect_min.y + width);
4935
+ }
4936
+
4937
+ if (spring_weight <= 0.0f && spacing <= 0.0f)
4938
+ return;
4939
+
4940
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
4941
+
4942
+ draw_list->PushClipRect(rect_min, rect_max, true);
4943
+
4944
+ draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
4945
+ draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
4946
+
4947
+ const float zig_zag_size = 3;
4948
+ ImVec2 normal = ImVec2(-direction.y, direction.x);
4949
+
4950
+ draw_list->PathClear();
4951
+ origin.x += 0.5f;
4952
+ origin.y += 0.5f;
4953
+ draw_list->PathLineTo(origin);
4954
+ for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
4955
+ {
4956
+ ImVec2 p;
4957
+ p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
4958
+ p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
4959
+ draw_list->PathLineTo(p);
4960
+ normal = ImVec2(-normal.x, -normal.y);
4961
+ }
4962
+ draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
4963
+
4964
+ draw_list->PopClipRect();
4965
+ }
4966
+ };
4967
+
4968
+ ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
4969
+ ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
4970
+ ImGui::Checkbox("Widget C", &widget_c);
4971
+ ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
4972
+ ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
4973
+ ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
4974
+ ImGui::Checkbox("Spring C", &spring_c);
4975
+ ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
4976
+ ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
4977
+ ImGui::Checkbox("Minimize Height", &minimize_height);
4978
+ ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
4979
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
4980
+ ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4981
+ ImGui::TextUnformatted("Spring"); ImGui::SameLine();
4982
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
4983
+ ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4984
+ ImGui::TextUnformatted("Item Spacing");
4985
+ ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
4986
+ ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
4987
+ ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
4988
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
4989
+ ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
4990
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
4991
+ ImGui::Text("Layout widgets:");
4992
+ ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
4993
+
4994
+ ImGui::Spacing();
4995
+
4996
+ ImVec2 widget_size;
4997
+ widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
4998
+ widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
4999
+
5000
+ ImVec2 small_widget_size = widget_size;
5001
+ if (horizontal)
5002
+ small_widget_size.y = floorf(small_widget_size.y / 2);
5003
+ else
5004
+ small_widget_size.x = floorf(small_widget_size.x / 2);
5005
+
5006
+ ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
5007
+ if (minimize_width) layout_size.x = 0.0f;
5008
+ if (minimize_height) layout_size.y = 0.0f;
5009
+
5010
+ // Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
5011
+ // Example:
5012
+ // ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
5013
+
5014
+ ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
5015
+
5016
+ if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
5017
+ if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
5018
+ if (widget_a) { ImGui::Button("Widget A", widget_size); }
5019
+ if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
5020
+ if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
5021
+ if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
5022
+ if (widget_c) { ImGui::Button("Widget C", widget_size); }
5023
+ if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
5024
+ if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
5025
+
5026
+ ImGui::PopStyleVar();
5027
+
5028
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
5029
+ draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
5030
+
5031
+ ImGui::TreePop();
5032
+ }
5033
+ #endif // IMGUI_HAS_STACK_LAYOUT
4884
5034
}
4885
5035
4886
5036
//-----------------------------------------------------------------------------
0 commit comments