diff options
author | chai <215380520@qq.com> | 2024-06-03 10:15:45 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2024-06-03 10:15:45 +0800 |
commit | acea7b2e728787a0d83bbf83c8c1f042d2c32e7e (patch) | |
tree | 0bfec05c1ca2d71be2c337bcd110a0421f19318b /Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs | |
parent | 88febcb02bf127d961c6471d9e846c0e1315f5c3 (diff) |
+ plugins project
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs')
-rw-r--r-- | Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs new file mode 100644 index 0000000..7dfe1e6 --- /dev/null +++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/Controls/DockPanel.cs @@ -0,0 +1,104 @@ +using System; +using Microsoft.Xna.Framework; + +namespace MonoGame.Extended.Gui.Controls +{ + public enum Dock + { + Left, Right, Top, Bottom + } + + public class DockPanel : LayoutControl + { + public override Size GetContentSize(IGuiContext context) + { + var size = new Size(); + + for (var i = 0; i < Items.Count; i++) + { + var control = Items[i]; + var actualSize = control.CalculateActualSize(context); + + if (LastChildFill && i == Items.Count - 1) + { + size.Width += actualSize.Width; + size.Height += actualSize.Height; + } + else + { + var dock = control.GetAttachedProperty(DockProperty) as Dock? ?? Dock.Left; + + switch (dock) + { + case Dock.Left: + case Dock.Right: + size.Width += actualSize.Width; + break; + case Dock.Top: + case Dock.Bottom: + size.Height += actualSize.Height; + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + } + + return size; + } + + protected override void Layout(IGuiContext context, Rectangle rectangle) + { + for (var i = 0; i < Items.Count; i++) + { + var control = Items[i]; + + if (LastChildFill && i == Items.Count - 1) + { + PlaceControl(context, control, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height); + } + else + { + var actualSize = control.CalculateActualSize(context); + var dock = control.GetAttachedProperty(DockProperty) as Dock? ?? Dock.Left; + + switch (dock) + { + case Dock.Left: + PlaceControl(context, control, rectangle.Left, rectangle.Top, actualSize.Width, rectangle.Height); + rectangle.X += actualSize.Width; + rectangle.Width -= actualSize.Width; + break; + case Dock.Right: + PlaceControl(context, control, rectangle.Right - actualSize.Width, rectangle.Top, actualSize.Width, rectangle.Height); + rectangle.Width -= actualSize.Width; + break; + case Dock.Top: + PlaceControl(context, control, rectangle.Left, rectangle.Top, rectangle.Width, actualSize.Height); + rectangle.Y += actualSize.Height; + rectangle.Height -= actualSize.Height; + break; + case Dock.Bottom: + PlaceControl(context, control, rectangle.Left, rectangle.Bottom - actualSize.Height, rectangle.Width, actualSize.Height); + rectangle.Height -= actualSize.Height; + break; + default: + throw new ArgumentOutOfRangeException(); + } + } + } + } + + public const string DockProperty = "Dock"; + + public override Type GetAttachedPropertyType(string propertyName) + { + if (string.Equals(DockProperty, propertyName, StringComparison.OrdinalIgnoreCase)) + return typeof(Dock); + + return base.GetAttachedPropertyType(propertyName); + } + + public bool LastChildFill { get; set; } = true; + } +}
\ No newline at end of file |