Fix any remaining bugs with anchor positioning system, use LocalPosition for UIElement, and make containers use that for arrangement.
This commit is contained in:
@@ -7,7 +7,9 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
{
|
||||
public bool Visible { get; set; } = true;
|
||||
public bool IgnoreInput { get; set; } = false;
|
||||
public Vector2 Position { get; set; } = Vector2.Zero;
|
||||
public Vector2 LocalPosition { get; set; } = Vector2.Zero;
|
||||
public Vector2 GlobalPosition => _parent?.GlobalPosition + LocalPosition ?? LocalPosition;
|
||||
|
||||
public Rect Size
|
||||
{
|
||||
get => _size;
|
||||
@@ -36,6 +38,11 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
|
||||
public virtual void MarkDirty() => _dirty = true;
|
||||
|
||||
public void SetParent(UIElement parent)
|
||||
{
|
||||
_parent = parent;
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
if (!_dirty) return;
|
||||
@@ -46,9 +53,9 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
|
||||
OnUpdate();
|
||||
|
||||
if (_parentRect != Rect.Zero)
|
||||
if (_parent is not null && _parent.Size != Rect.Zero)
|
||||
{
|
||||
ApplyAnchor(_parentPosition, _parentRect);
|
||||
ApplyAnchor(_parent.GlobalPosition, _parent.Size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +64,7 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
|
||||
public void DrawSize(RenderSystem renderer)
|
||||
{
|
||||
renderer.SetTransform(Position, Vector2.Zero);
|
||||
renderer.SetTransform(GlobalPosition, Vector2.Zero);
|
||||
renderer.DrawRectangleOutline(new Vector2(Size.Width, Size.Height), Color.Red, 2.0f);
|
||||
}
|
||||
|
||||
@@ -68,9 +75,9 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
/// <returns>True if the point is inside the widget; otherwise, false.</returns>
|
||||
public bool ContainsPoint(Vector2 point)
|
||||
{
|
||||
return point.X >= Position.X && point.Y >= Position.Y &&
|
||||
point.X <= Position.X + Size.Width &&
|
||||
point.Y <= Position.Y + Size.Height;
|
||||
return point.X >= GlobalPosition.X && point.Y >= GlobalPosition.Y &&
|
||||
point.X <= GlobalPosition.X + Size.Width &&
|
||||
point.Y <= GlobalPosition.Y + Size.Height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -78,14 +85,11 @@ public abstract class UIElement : IElement, IRenderableElement, IResizeableEleme
|
||||
/// </summary>
|
||||
public virtual void ApplyAnchor(Vector2 parentPosition, Rect parentRect)
|
||||
{
|
||||
_parentPosition = parentPosition;
|
||||
_parentRect = parentRect;
|
||||
Position = Anchor.Calculate(parentPosition, parentRect, Size) + new Vector2(AnchorOffset.X, AnchorOffset.Y);
|
||||
LocalPosition = Anchor.Calculate(parentPosition, parentRect, Size) + new Vector2(AnchorOffset.X, AnchorOffset.Y);
|
||||
}
|
||||
|
||||
private bool _dirty = true;
|
||||
private Rect _size = Rect.Zero;
|
||||
|
||||
private Vector2 _parentPosition = Vector2.Zero;
|
||||
private Rect _parentRect = Rect.Zero;
|
||||
private UIElement? _parent;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user