WIP: dirty flag for UI.
This commit is contained in:
@@ -8,41 +8,65 @@ namespace Voile.UI.Containers;
|
||||
/// </summary>
|
||||
public abstract class Container : IElement, IParentableElement, IUpdatableElement, IResizeableElement, IRenderableElement
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public IReadOnlyList<IElement> Children => _children;
|
||||
/// <inheritdoc />
|
||||
public Vector2 Position { get; set; }
|
||||
/// <inheritdoc />
|
||||
public Rect MinimumRect { get; set; } = Rect.Zero;
|
||||
/// <inheritdoc />
|
||||
public Rect Size { get; set; } = Rect.Zero;
|
||||
/// <inheritdoc />
|
||||
public bool Visible { get; set; } = true;
|
||||
/// <inheritdoc />
|
||||
public bool Dirty => _isDirty;
|
||||
|
||||
public Container()
|
||||
{
|
||||
|
||||
MarkDirty();
|
||||
}
|
||||
|
||||
public Container(Rect minimumSize)
|
||||
{
|
||||
MinimumRect = minimumSize;
|
||||
|
||||
MarkDirty();
|
||||
}
|
||||
|
||||
public Container(Rect minimumSize, List<IElement> children)
|
||||
{
|
||||
MinimumRect = minimumSize;
|
||||
_children = children;
|
||||
|
||||
MarkDirty();
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
Arrange();
|
||||
CalculateMinimumSize();
|
||||
if (!_isDirty) return;
|
||||
_isDirty = false;
|
||||
|
||||
|
||||
foreach (var child in _children)
|
||||
{
|
||||
if (child is not IUpdatableElement updatable) continue;
|
||||
updatable.Update();
|
||||
}
|
||||
|
||||
Arrange();
|
||||
CalculateMinimumSize();
|
||||
}
|
||||
|
||||
public void MarkDirty() => _isDirty = true;
|
||||
|
||||
/// <summary>
|
||||
/// Called when this <see cref="Container"/> has to rearrange its children.
|
||||
/// </summary>
|
||||
public abstract void Arrange();
|
||||
|
||||
/// <summary>
|
||||
/// Recalculates a minimum size for this <see cref="Container"/>.
|
||||
/// </summary>
|
||||
public void CalculateMinimumSize()
|
||||
{
|
||||
if (Children.Count == 0)
|
||||
@@ -81,12 +105,16 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
||||
public void AddChild(IElement child)
|
||||
{
|
||||
_children.Add(child);
|
||||
|
||||
MarkDirty();
|
||||
Update();
|
||||
}
|
||||
|
||||
public void RemoveChild(IElement child)
|
||||
{
|
||||
_children.Remove(child);
|
||||
|
||||
MarkDirty();
|
||||
Update();
|
||||
}
|
||||
|
||||
@@ -106,4 +134,5 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
||||
}
|
||||
|
||||
private List<IElement> _children = new();
|
||||
private bool _isDirty;
|
||||
}
|
||||
Reference in New Issue
Block a user