summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-06-03 10:15:45 +0800
committerchai <215380520@qq.com>2024-06-03 10:15:45 +0800
commitacea7b2e728787a0d83bbf83c8c1f042d2c32e7e (patch)
tree0bfec05c1ca2d71be2c337bcd110a0421f19318b /Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs
parent88febcb02bf127d961c6471d9e846c0e1315f5c3 (diff)
+ plugins project
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs69
1 files changed, 69 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs
new file mode 100644
index 0000000..5d8926d
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs
@@ -0,0 +1,69 @@
+using System;
+using Microsoft.Xna.Framework;
+
+namespace MonoGame.Extended.Gui.Controls
+{
+ public class StackPanel : LayoutControl
+ {
+ public StackPanel()
+ {
+ }
+
+ public Orientation Orientation { get; set; } = Orientation.Vertical;
+ public int Spacing { get; set; }
+
+ public override Size GetContentSize(IGuiContext context)
+ {
+ var width = 0;
+ var height = 0;
+
+ foreach (var control in Items)
+ {
+ var actualSize = control.CalculateActualSize(context);
+
+ switch (Orientation)
+ {
+ case Orientation.Horizontal:
+ width += actualSize.Width;
+ height = actualSize.Height > height ? actualSize.Height : height;
+ break;
+ case Orientation.Vertical:
+ width = actualSize.Width > width ? actualSize.Width : width;
+ height += actualSize.Height;
+ break;
+ default:
+ throw new InvalidOperationException($"Unexpected orientation {Orientation}");
+ }
+ }
+
+ width += Orientation == Orientation.Horizontal ? (Items.Count - 1) * Spacing : 0;
+ height += Orientation == Orientation.Vertical ? (Items.Count - 1) * Spacing : 0;
+
+ return new Size(width, height);
+ }
+
+ protected override void Layout(IGuiContext context, Rectangle rectangle)
+ {
+ foreach (var control in Items)
+ {
+ var actualSize = control.CalculateActualSize(context);
+
+ switch (Orientation)
+ {
+ case Orientation.Vertical:
+ PlaceControl(context, control, rectangle.X, rectangle.Y, rectangle.Width, actualSize.Height);
+ rectangle.Y += actualSize.Height + Spacing;
+ rectangle.Height -= actualSize.Height;
+ break;
+ case Orientation.Horizontal:
+ PlaceControl(context, control, rectangle.X, rectangle.Y, actualSize.Width, rectangle.Height);
+ rectangle.X += actualSize.Width + Spacing;
+ rectangle.Width -= actualSize.Width;
+ break;
+ default:
+ throw new InvalidOperationException($"Unexpected orientation {Orientation}");
+ }
+ }
+ }
+ }
+}