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