From acea7b2e728787a0d83bbf83c8c1f042d2c32e7e Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 3 Jun 2024 10:15:45 +0800 Subject: + plugins project --- .../MonoGame.Extended.Gui/Controls/StackPanel.cs | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/StackPanel.cs') 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}"); + } + } + } + } +} -- cgit v1.1-26-g67d0