Add GridContainer.

This commit is contained in:
2025-06-19 15:13:25 +02:00
parent 6affded730
commit 3460c124b8
2 changed files with 71 additions and 1 deletions

View File

@@ -117,5 +117,5 @@ public class TestGame : Game
private ResourceRef<Sound> _sound;
private ResourceRef<Texture2d> _icon;
private HorizontalContainer _container = new(spacing: 8.0f);
private GridContainer _container = new();
}

View File

@@ -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<IElement> 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);
}
}
}