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