Add ConfineToChildren to Container, containers will use MinimumRect as a size if a given size is smaller than minimum, update documentation for Container.
This commit is contained in:
@@ -127,6 +127,7 @@ public class TestGame : Game
|
|||||||
|
|
||||||
private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new())
|
private FlexContainer _container = new(minimumSize: new Rect(64.0f, 64.0f), new())
|
||||||
{
|
{
|
||||||
|
ConfineToContents = false,
|
||||||
Size = new Rect(500, 300),
|
Size = new Rect(500, 300),
|
||||||
Direction = FlexDirection.Row,
|
Direction = FlexDirection.Row,
|
||||||
Justify = JustifyContent.Start,
|
Justify = JustifyContent.Start,
|
||||||
|
|||||||
@@ -21,6 +21,17 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_size = value;
|
_size = value;
|
||||||
|
|
||||||
|
if (value.Width < MinimumRect.Width)
|
||||||
|
{
|
||||||
|
_size.Width = MinimumRect.Width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value.Height < MinimumRect.Height)
|
||||||
|
{
|
||||||
|
_size.Height = MinimumRect.Height;
|
||||||
|
}
|
||||||
|
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,6 +40,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public bool Dirty => _isDirty;
|
public bool Dirty => _isDirty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Specifies if this <see cref="Container"/>'s minimum size will be confined to contents.
|
||||||
|
/// </summary>
|
||||||
|
public bool ConfineToContents { get; set; } = false;
|
||||||
|
|
||||||
public Container()
|
public Container()
|
||||||
{
|
{
|
||||||
MarkDirty();
|
MarkDirty();
|
||||||
@@ -62,7 +78,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
Arrange();
|
Arrange();
|
||||||
CalculateMinimumSize();
|
|
||||||
|
if (ConfineToContents)
|
||||||
|
{
|
||||||
|
RecalculateSizes();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MarkDirty() => _isDirty = true;
|
public void MarkDirty() => _isDirty = true;
|
||||||
@@ -73,16 +93,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
public abstract void Arrange();
|
public abstract void Arrange();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Recalculates a minimum size for this <see cref="Container"/>.
|
/// Recalculates sizes for this <see cref="Container"/>.
|
||||||
|
/// This is done automatically if <see cref="ConfineToContents"/> is true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void CalculateMinimumSize()
|
public void RecalculateSizes()
|
||||||
{
|
{
|
||||||
if (Children.Count == 0)
|
|
||||||
{
|
|
||||||
_size = MinimumRect;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float minX = float.MaxValue;
|
float minX = float.MaxValue;
|
||||||
float minY = float.MaxValue;
|
float minY = float.MaxValue;
|
||||||
float maxX = float.MinValue;
|
float maxX = float.MinValue;
|
||||||
@@ -108,6 +123,11 @@ public abstract class Container : IElement, IParentableElement, IUpdatableElemen
|
|||||||
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
|
float finalHeight = MathF.Max(occupiedHeight, MinimumRect.Height);
|
||||||
|
|
||||||
MinimumRect = new Rect(finalWidth, finalHeight);
|
MinimumRect = new Rect(finalWidth, finalHeight);
|
||||||
|
|
||||||
|
if (MinimumRect > _size)
|
||||||
|
{
|
||||||
|
_size = MinimumRect;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddChild(IElement child)
|
public void AddChild(IElement child)
|
||||||
|
|||||||
@@ -5,5 +5,19 @@ namespace Voile.UI;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public record Rect(float Width = 0.0f, float Height = 0.0f)
|
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 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;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user