@@ -4480,6 +4480,156 @@ static void ShowDemoWindowLayout()
4480
4480
4481
4481
ImGui::TreePop();
4482
4482
}
4483
+
4484
+ #if IMGUI_HAS_STACK_LAYOUT
4485
+ IMGUI_DEMO_MARKER("Layout/Stack Layout");
4486
+ if (ImGui::TreeNode("Stack Layout"))
4487
+ {
4488
+ static bool widget_a = true, widget_b = true, widget_c = true;
4489
+ static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
4490
+ static bool minimize_width = false, minimize_height = true;
4491
+ static bool horizontal = true, draw_springs = true;
4492
+ static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
4493
+ static float a_c_spring_weight = 0.0f;
4494
+ static float ab_spring_weight = 0.5f;
4495
+ static float alignment = 0.5f;
4496
+
4497
+ struct funcs
4498
+ {
4499
+ static void VisibleSpring(float spring_weight)
4500
+ {
4501
+ ImGui::Spring(spring_weight);
4502
+ if (!draw_springs)
4503
+ return;
4504
+
4505
+ ImVec2 rect_min = ImGui::GetItemRectMin();
4506
+ ImVec2 rect_max = ImGui::GetItemRectMax();
4507
+
4508
+ ImVec2 rect_size = ImGui::GetItemRectSize();
4509
+ if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
4510
+ return;
4511
+
4512
+ // Draw zig-zag
4513
+ float width = 0.0f, spacing = 0.0f;
4514
+ ImVec2 direction, origin;
4515
+ ImVec2 spacing_min, spring_max;
4516
+
4517
+ if (horizontal)
4518
+ {
4519
+ spacing = floorf(item_spacing.x);
4520
+ width = rect_size.x - spacing;
4521
+ origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
4522
+ direction = ImVec2(1.0f, 0.0f);
4523
+ spring_max = ImVec2(rect_min.x + width, rect_max.y);
4524
+ spacing_min = ImVec2(rect_min.x + width, rect_min.y);
4525
+ }
4526
+ else
4527
+ {
4528
+ spacing = floorf(item_spacing.y);
4529
+ width = rect_size.y - spacing;
4530
+ origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
4531
+ direction = ImVec2(0.0f, 1.0f);
4532
+ spring_max = ImVec2(rect_max.x, rect_min.y + width);
4533
+ spacing_min = ImVec2(rect_min.x, rect_min.y + width);
4534
+ }
4535
+
4536
+ if (spring_weight <= 0.0f && spacing <= 0.0f)
4537
+ return;
4538
+
4539
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
4540
+
4541
+ draw_list->PushClipRect(rect_min, rect_max, true);
4542
+
4543
+ draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
4544
+ draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
4545
+
4546
+ const float zig_zag_size = 3;
4547
+ ImVec2 normal = ImVec2(-direction.y, direction.x);
4548
+
4549
+ draw_list->PathClear();
4550
+ origin.x += 0.5f;
4551
+ origin.y += 0.5f;
4552
+ draw_list->PathLineTo(origin);
4553
+ for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
4554
+ {
4555
+ ImVec2 p;
4556
+ p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
4557
+ p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
4558
+ draw_list->PathLineTo(p);
4559
+ normal = ImVec2(-normal.x, -normal.y);
4560
+ }
4561
+ draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
4562
+
4563
+ draw_list->PopClipRect();
4564
+ }
4565
+ };
4566
+
4567
+ ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
4568
+ ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
4569
+ ImGui::Checkbox("Widget C", &widget_c);
4570
+ ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
4571
+ ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
4572
+ ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
4573
+ ImGui::Checkbox("Spring C", &spring_c);
4574
+ ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
4575
+ ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
4576
+ ImGui::Checkbox("Minimize Height", &minimize_height);
4577
+ ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
4578
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
4579
+ ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4580
+ ImGui::TextUnformatted("Spring"); ImGui::SameLine();
4581
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
4582
+ ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
4583
+ ImGui::TextUnformatted("Item Spacing");
4584
+ ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
4585
+ ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
4586
+ ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
4587
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
4588
+ ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
4589
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
4590
+ ImGui::Text("Layout widgets:");
4591
+ ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
4592
+
4593
+ ImGui::Spacing();
4594
+
4595
+ ImVec2 widget_size;
4596
+ widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
4597
+ widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
4598
+
4599
+ ImVec2 small_widget_size = widget_size;
4600
+ if (horizontal)
4601
+ small_widget_size.y = floorf(small_widget_size.y / 2);
4602
+ else
4603
+ small_widget_size.x = floorf(small_widget_size.x / 2);
4604
+
4605
+ ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
4606
+ if (minimize_width) layout_size.x = 0.0f;
4607
+ if (minimize_height) layout_size.y = 0.0f;
4608
+
4609
+ // Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
4610
+ // Example:
4611
+ // ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
4612
+
4613
+ ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
4614
+
4615
+ if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
4616
+ if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
4617
+ if (widget_a) { ImGui::Button("Widget A", widget_size); }
4618
+ if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
4619
+ if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
4620
+ if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
4621
+ if (widget_c) { ImGui::Button("Widget C", widget_size); }
4622
+ if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
4623
+ if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
4624
+
4625
+ ImGui::PopStyleVar();
4626
+
4627
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
4628
+ draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
4629
+
4630
+ ImGui::TreePop();
4631
+ }
4632
+ #endif // IMGUI_HAS_STACK_LAYOUT
4483
4633
}
4484
4634
4485
4635
//-----------------------------------------------------------------------------
0 commit comments