summaryrefslogtreecommitdiff
path: root/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs')
-rw-r--r--Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs65
1 files changed, 65 insertions, 0 deletions
diff --git a/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs
new file mode 100644
index 0000000..1268bff
--- /dev/null
+++ b/Plugins/MonoGame.Extended/source/MonoGame.Extended.Gui/LayoutHelper.cs
@@ -0,0 +1,65 @@
+using System;
+using Microsoft.Xna.Framework;
+using MonoGame.Extended.Gui.Controls;
+
+namespace MonoGame.Extended.Gui
+{
+ public enum HorizontalAlignment { Left, Right, Centre, Stretch }
+ public enum VerticalAlignment { Top, Bottom, Centre, Stretch }
+
+ public static class LayoutHelper
+ {
+ public static void PlaceControl(IGuiContext context, Control control, float x, float y, float width, float height)
+ {
+ var rectangle = new Rectangle((int)x, (int)y, (int)width, (int)height);
+ var desiredSize = control.CalculateActualSize(context);
+ var alignedRectangle = AlignRectangle(control.HorizontalAlignment, control.VerticalAlignment, desiredSize, rectangle);
+
+ control.Position = new Point(control.Margin.Left + alignedRectangle.X, control.Margin.Top + alignedRectangle.Y);
+ control.ActualSize = (Size)alignedRectangle.Size - control.Margin.Size;
+ control.InvalidateMeasure();
+ }
+
+ public static Rectangle AlignRectangle(HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, Size size, Rectangle targetRectangle)
+ {
+ var x = GetHorizontalPosition(horizontalAlignment, size, targetRectangle);
+ var y = GetVerticalPosition(verticalAlignment, size, targetRectangle);
+ var width = horizontalAlignment == HorizontalAlignment.Stretch ? targetRectangle.Width : size.Width;
+ var height = verticalAlignment == VerticalAlignment.Stretch ? targetRectangle.Height : size.Height;
+
+ return new Rectangle(x, y, width, height);
+ }
+
+ public static int GetHorizontalPosition(HorizontalAlignment horizontalAlignment, Size size, Rectangle targetRectangle)
+ {
+ switch (horizontalAlignment)
+ {
+ case HorizontalAlignment.Stretch:
+ case HorizontalAlignment.Left:
+ return targetRectangle.X;
+ case HorizontalAlignment.Right:
+ return targetRectangle.Right - size.Width;
+ case HorizontalAlignment.Centre:
+ return targetRectangle.X + targetRectangle.Width / 2 - size.Width / 2;
+ default:
+ throw new ArgumentOutOfRangeException(nameof(horizontalAlignment), horizontalAlignment, $"{horizontalAlignment} is not supported");
+ }
+ }
+
+ public static int GetVerticalPosition(VerticalAlignment verticalAlignment, Size size, Rectangle targetRectangle)
+ {
+ switch (verticalAlignment)
+ {
+ case VerticalAlignment.Stretch:
+ case VerticalAlignment.Top:
+ return targetRectangle.Y;
+ case VerticalAlignment.Bottom:
+ return targetRectangle.Bottom - size.Height;
+ case VerticalAlignment.Centre:
+ return targetRectangle.Y + targetRectangle.Height / 2 - size.Height / 2;
+ default:
+ throw new ArgumentOutOfRangeException(nameof(verticalAlignment), verticalAlignment, $"{verticalAlignment} is not supported");
+ }
+ }
+ }
+} \ No newline at end of file