@@ -5234,6 +5234,156 @@ static void DemoWindowLayout()
5234
5234
5235
5235
ImGui::TreePop();
5236
5236
}
5237
+
5238
+ #if IMGUI_HAS_STACK_LAYOUT
5239
+ IMGUI_DEMO_MARKER("Layout/Stack Layout");
5240
+ if (ImGui::TreeNode("Stack Layout"))
5241
+ {
5242
+ static bool widget_a = true, widget_b = true, widget_c = true;
5243
+ static bool spring_a = true, spring_ab = true, spring_bc = true, spring_c = true;
5244
+ static bool minimize_width = false, minimize_height = true;
5245
+ static bool horizontal = true, draw_springs = true;
5246
+ static ImVec2 item_spacing = ImGui::GetStyle().ItemSpacing;
5247
+ static float a_c_spring_weight = 0.0f;
5248
+ static float ab_spring_weight = 0.5f;
5249
+ static float alignment = 0.5f;
5250
+
5251
+ struct funcs
5252
+ {
5253
+ static void VisibleSpring(float spring_weight)
5254
+ {
5255
+ ImGui::Spring(spring_weight);
5256
+ if (!draw_springs)
5257
+ return;
5258
+
5259
+ ImVec2 rect_min = ImGui::GetItemRectMin();
5260
+ ImVec2 rect_max = ImGui::GetItemRectMax();
5261
+
5262
+ ImVec2 rect_size = ImGui::GetItemRectSize();
5263
+ if (rect_size.x <= 0.0f && rect_size.y <= 0.0f)
5264
+ return;
5265
+
5266
+ // Draw zig-zag
5267
+ float width = 0.0f, spacing = 0.0f;
5268
+ ImVec2 direction, origin;
5269
+ ImVec2 spacing_min, spring_max;
5270
+
5271
+ if (horizontal)
5272
+ {
5273
+ spacing = floorf(item_spacing.x);
5274
+ width = rect_size.x - spacing;
5275
+ origin = ImVec2(floorf(rect_min.x), floorf(rect_min.y + (rect_max.y - rect_min.y) / 2));
5276
+ direction = ImVec2(1.0f, 0.0f);
5277
+ spring_max = ImVec2(rect_min.x + width, rect_max.y);
5278
+ spacing_min = ImVec2(rect_min.x + width, rect_min.y);
5279
+ }
5280
+ else
5281
+ {
5282
+ spacing = floorf(item_spacing.y);
5283
+ width = rect_size.y - spacing;
5284
+ origin = ImVec2(floorf(rect_min.x + (rect_max.x - rect_min.x) / 2), floorf(rect_min.y));
5285
+ direction = ImVec2(0.0f, 1.0f);
5286
+ spring_max = ImVec2(rect_max.x, rect_min.y + width);
5287
+ spacing_min = ImVec2(rect_min.x, rect_min.y + width);
5288
+ }
5289
+
5290
+ if (spring_weight <= 0.0f && spacing <= 0.0f)
5291
+ return;
5292
+
5293
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
5294
+
5295
+ draw_list->PushClipRect(rect_min, rect_max, true);
5296
+
5297
+ draw_list->AddRectFilled(rect_min, spring_max, ImColor(80, 20, 80));
5298
+ draw_list->AddRectFilled(spacing_min, rect_max, ImColor(80, 20, 20));
5299
+
5300
+ const float zig_zag_size = 3;
5301
+ ImVec2 normal = ImVec2(-direction.y, direction.x);
5302
+
5303
+ draw_list->PathClear();
5304
+ origin.x += 0.5f;
5305
+ origin.y += 0.5f;
5306
+ draw_list->PathLineTo(origin);
5307
+ for (float x = zig_zag_size * 0.5f; x <= width; x += zig_zag_size)
5308
+ {
5309
+ ImVec2 p;
5310
+ p.x = origin.x + direction.x * x + normal.x * zig_zag_size;
5311
+ p.y = origin.y + direction.y * x + normal.y * zig_zag_size;
5312
+ draw_list->PathLineTo(p);
5313
+ normal = ImVec2(-normal.x, -normal.y);
5314
+ }
5315
+ draw_list->PathStroke(ImColor(255, 255, 255, 190), false, 1.0f);
5316
+
5317
+ draw_list->PopClipRect();
5318
+ }
5319
+ };
5320
+
5321
+ ImGui::Checkbox("Widget A", &widget_a); ImGui::SameLine();
5322
+ ImGui::Checkbox("Widget B", &widget_b); ImGui::SameLine();
5323
+ ImGui::Checkbox("Widget C", &widget_c);
5324
+ ImGui::Checkbox("Spring A", &spring_a); ImGui::SameLine();
5325
+ ImGui::Checkbox("Spring AB", &spring_ab); ImGui::SameLine();
5326
+ ImGui::Checkbox("Spring BC", &spring_bc); ImGui::SameLine();
5327
+ ImGui::Checkbox("Spring C", &spring_c);
5328
+ ImGui::Checkbox("Horizontal", &horizontal); ImGui::SameLine();
5329
+ ImGui::Checkbox("Minimize Width", &minimize_width); ImGui::SameLine();
5330
+ ImGui::Checkbox("Minimize Height", &minimize_height);
5331
+ ImGui::Checkbox("Draw Springs", &draw_springs); ImGui::SameLine();
5332
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
5333
+ ImGui::ColorButton("- Spring", ImColor(80, 20, 80), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5334
+ ImGui::TextUnformatted("Spring"); ImGui::SameLine();
5335
+ ImGui::TextUnformatted(" "); ImGui::SameLine();
5336
+ ImGui::ColorButton("- Spacing", ImColor(80, 20, 20), ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoPicker); ImGui::SameLine();
5337
+ ImGui::TextUnformatted("Item Spacing");
5338
+ ImGui::DragFloat("Item Spacing", horizontal ? &item_spacing.x : &item_spacing.y, 0.1f, 0.0f, 50.0f);
5339
+ ImGui::DragFloat("A & C Spring Weight", &a_c_spring_weight, 0.002f, 0.0f, 1.0f);
5340
+ ImGui::DragFloat("AB Spring Weight", &ab_spring_weight, 0.002f, 0.0f, 1.0f);
5341
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("BC Spring Weight = 1 - AB Spring Weight");
5342
+ ImGui::DragFloat("Minor Axis Alignment", &alignment, 0.002f, 0.0f, 1.0f);
5343
+ if (ImGui::IsItemHovered()) ImGui::SetTooltip("This is vertical alignment for horizontal layouts and horizontal alignment for vertical layouts.");
5344
+ ImGui::Text("Layout widgets:");
5345
+ ImGui::Text("| Spring A | Widget A | Spring AB | Widget B | Spring BC | Widget C | Spring C |");
5346
+
5347
+ ImGui::Spacing();
5348
+
5349
+ ImVec2 widget_size;
5350
+ widget_size.x = floorf(ImGui::GetContentRegionAvail().x / 4);
5351
+ widget_size.y = horizontal ? floorf(widget_size.x / 3) : widget_size.x;
5352
+
5353
+ ImVec2 small_widget_size = widget_size;
5354
+ if (horizontal)
5355
+ small_widget_size.y = floorf(small_widget_size.y / 2);
5356
+ else
5357
+ small_widget_size.x = floorf(small_widget_size.x / 2);
5358
+
5359
+ ImVec2 layout_size = ImVec2(widget_size.x * 4, widget_size.y * 4);
5360
+ if (minimize_width) layout_size.x = 0.0f;
5361
+ if (minimize_height) layout_size.y = 0.0f;
5362
+
5363
+ // Minor axis alignment can be set by style or directly in BeginHorizontal/BeginVertical
5364
+ // Example:
5365
+ // ImGui::PushStyleVar(ImGuiStyleVar_LayoutAlign, alignment);
5366
+
5367
+ ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(floorf(item_spacing.x), floorf(item_spacing.y)));
5368
+
5369
+ if (horizontal) { ImGui::BeginHorizontal("h1", layout_size, alignment); } else { ImGui::BeginVertical("v1", layout_size, alignment); }
5370
+ if (spring_a) { funcs::VisibleSpring(a_c_spring_weight); }
5371
+ if (widget_a) { ImGui::Button("Widget A", widget_size); }
5372
+ if (spring_ab) { funcs::VisibleSpring(ab_spring_weight); }
5373
+ if (widget_b) { ImGui::Button("Widget B", small_widget_size); }
5374
+ if (spring_bc) { funcs::VisibleSpring(1.0f - ab_spring_weight); }
5375
+ if (widget_c) { ImGui::Button("Widget C", widget_size); }
5376
+ if (spring_c) { funcs::VisibleSpring(a_c_spring_weight); }
5377
+ if (horizontal) { ImGui::EndHorizontal(); } else { ImGui::EndVertical(); }
5378
+
5379
+ ImGui::PopStyleVar();
5380
+
5381
+ ImDrawList* draw_list = ImGui::GetWindowDrawList();
5382
+ draw_list->AddRect(ImGui::GetItemRectMin(), ImGui::GetItemRectMax(), ImGui::GetColorU32(ImGuiCol_Border));
5383
+
5384
+ ImGui::TreePop();
5385
+ }
5386
+ #endif // IMGUI_HAS_STACK_LAYOUT
5237
5387
}
5238
5388
5239
5389
//-----------------------------------------------------------------------------
0 commit comments