diff --git a/TestGame/TestGame.cs b/TestGame/TestGame.cs index d2a9714..b284338 100644 --- a/TestGame/TestGame.cs +++ b/TestGame/TestGame.cs @@ -117,5 +117,5 @@ public class TestGame : Game private ResourceRef _sound; private ResourceRef _icon; - private HorizontalContainer _container = new(spacing: 8.0f); + private GridContainer _container = new(); } \ No newline at end of file diff --git a/Voile/Source/UI/Containers/GridContainer.cs b/Voile/Source/UI/Containers/GridContainer.cs new file mode 100644 index 0000000..972c1f6 --- /dev/null +++ b/Voile/Source/UI/Containers/GridContainer.cs @@ -0,0 +1,70 @@ +using System.Numerics; +using Voile.Rendering; + +namespace Voile.UI.Containers; + +public class GridContainer : Container, IRenderableElement +{ + public int Columns { get; set; } = 2; + public float ColumnSpacing { get; set; } = 16.0f; + public float RowSpacing { get; set; } = 16.0f; + public bool Visible { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + + public GridContainer(List children, int columns = 2, float colSpacing = 16.0f, float rowSpacing = 16.0f) + : base(children) + { + Columns = columns; + ColumnSpacing = colSpacing; + RowSpacing = rowSpacing; + } + + public GridContainer(int columns = 2, float colSpacing = 16.0f, float rowSpacing = 16.0f) + { + Columns = columns; + ColumnSpacing = colSpacing; + RowSpacing = rowSpacing; + } + + public override void Arrange() + { + float startX = Position.X; + float startY = Position.Y; + + float currentX = startX; + float currentY = startY; + int colIndex = 0; + + foreach (var child in Children) + { + child.Position = new Vector2(currentX, currentY); + + float childWidth = 0.0f; + float childHeight = 0.0f; + + childWidth = child.Size.Width; + childHeight = child.Size.Height; + + colIndex++; + + if (colIndex >= Columns) + { + colIndex = 0; + currentX = startX; + currentY += childHeight + RowSpacing; + } + else + { + currentX += childWidth + ColumnSpacing; + } + } + } + + public void Render(RenderSystem renderer, Style style) + { + foreach (var child in Children) + { + if (child is not IRenderableElement renderable) continue; + renderable.Render(renderer, style); + } + } +} \ No newline at end of file