From f0c721bb0f9d65a22232f6c73ddf5afc478851d5 Mon Sep 17 00:00:00 2001 From: dnesov Date: Fri, 20 Jun 2025 20:08:17 +0200 Subject: [PATCH] Add ConfineToChildren to Container, containers will use MinimumRect as a size if a given size is smaller than minimum, update documentation for Container. --- TestGame/TestGame.cs | 1 + Voile/Source/UI/Containers/Container.cs | 38 +++++++++++++++++++------ Voile/Source/UI/Rect.cs | 14 +++++++++ 3 files changed, 44 insertions(+), 9 deletions(-) diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index 1f91408..df5443b 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -127,6 +127,7 @@ public class TestGame : Game private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new()) { + ConfineToContents = false, Size = new Rect(500, 300), Direction = FlexDirection.Row, Justify = JustifyContent.Start, diff --git a/Voile/Source/UI/Containers/Container.cs b/Voile/Source/UI/Containers/Container.cs index b0d6eb6..c702130 100644 --- a/Voile/Source/UI/Containers/Container.cs +++ b/Voile/Source/UI/Containers/Container.cs @@ -21,6 +21,17 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen set { _size = value; + + if (value.Width < MinimumRect.Width) + { + _size.Width = MinimumRect.Width; + } + + if (value.Height < MinimumRect.Height) + { + _size.Height = MinimumRect.Height; + } + MarkDirty(); } } @@ -29,6 +40,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen /// public bool Dirty => _isDirty; + /// + /// Specifies if this 's minimum size will be confined to contents. + /// + public bool ConfineToContents { get; set; } = false; + public Container() { MarkDirty(); @@ -62,7 +78,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen } Arrange(); - CalculateMinimumSize(); + + if (ConfineToContents) + { + RecalculateSizes(); + } } public void MarkDirty() => _isDirty = true; @@ -73,16 +93,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen public abstract void Arrange(); /// - /// Recalculates a minimum size for this . + /// Recalculates sizes for this . + /// This is done automatically if is true. /// - public void CalculateMinimumSize() + public void RecalculateSizes() { - if (Children.Count == 0) - { - _size = MinimumRect; - return; - } - float minX = float.MaxValue; float minY = float.MaxValue; float maxX = float.MinValue; @@ -108,6 +123,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height); MinimumRect = new Rect(finalWidth, finalHeight); + + if (MinimumRect > _size) + { + _size = MinimumRect; + } } public void AddChild(IElement child) diff --git a/Voile/Source/UI/Rect.cs b/Voile/Source/UI/Rect.cs index de1b7ad..9a39753 100644 --- a/Voile/Source/UI/Rect.cs +++ b/Voile/Source/UI/Rect.cs @@ -5,5 +5,19 @@ namespace Voile.UI; /// public record Rect(float Width = 0.0f, float Height = 0.0f) { + public float Width { get; set; } = Width; + public float Height { get; set; } = Height; public static Rect Zero => new Rect(0.0f, 0.0f); + public float Area => Width * Height; + + public int CompareTo(Rect? other) + { + if (other is null) return 1; + return Area.CompareTo(other.Area); + } + + public static bool operator >(Rect left, Rect right) => left.CompareTo(right) > 0; + public static bool operator <(Rect left, Rect right) => left.CompareTo(right) < 0; + public static bool operator >=(Rect left, Rect right) => left.CompareTo(right) >= 0; + public static bool operator <=(Rect left, Rect right) => left.CompareTo(right) <= 0; } \ No newline at end of file