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