Initial implementation of UI anchors, update TestGame.

This commit is contained in:
2025-06-21 22:23:19 +02:00
parent ae1b612524
commit 683656dee8
8 changed files with 148 additions and 20 deletions

View File

@@ -1,4 +1,5 @@
using System.Numerics;
using System.Xml.Serialization;
using Voile.Input;
using Voile.Rendering;
@@ -7,7 +8,7 @@ namespace Voile.UI.Widgets;
/// <summary>
/// A base class for all UI widgets.
/// </summary>
public abstract class Widget : IElement, IRenderableElement, IInputElement, IResizeableElement, IUpdatableElement
public abstract class Widget : IElement, IRenderableElement, IInputElement, IResizeableElement, IUpdatableElement, IAnchorableElement
{
public bool Visible { get; set; } = true;
public bool IgnoreInput { get; set; }
@@ -36,6 +37,36 @@ public abstract class Widget : IElement, IRenderableElement, IInputElement, IRes
public bool Dirty => _isDirty;
public Anchor Anchor { get; set; }
public Vector2 AnchorOffset { get; set; }
public void ApplyAnchor(Vector2 parentPosition, Rect parentRect)
{
switch (Anchor)
{
case Anchor.TopLeft:
Position = parentPosition + AnchorOffset;
break;
case Anchor.TopCenter:
var topCenterX = parentRect.Width / 2;
var topCenterY = parentPosition.Y;
Position = new Vector2(parentPosition.X + topCenterX, topCenterY) + AnchorOffset;
break;
case Anchor.TopRight:
Position = new Vector2(parentPosition.X + parentRect.Width, parentPosition.Y);
break;
case Anchor.Fill:
Position = parentPosition;
Size = parentRect;
break;
default:
throw new NotImplementedException("This anchor type is not implemented!");
}
}
/// <summary>
/// Called when its time to draw this widget.
/// </summary>