From 627350ca3185c64b453e70b0d2eaeba59f75fe93 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 17 Oct 2022 10:57:41 +0200 Subject: [PATCH] Added ElementHelper.MakeGrid --- CHANGELOG.md | 1 + MLEM.Ui/Elements/ElementHelper.cs | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dcbe089..1b90597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Additions - Added Element.OnAddedToUi and Element.OnRemovedFromUi - Added ScrollBar.MouseDragScrolling - Added Panel.ScrollToElement +- Added ElementHelper.MakeGrid Improvements - Allow elements to auto-adjust their size even when their children are aligned oddly diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 47d565d..3fd55a3 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -68,12 +68,31 @@ namespace MLEM.Ui.Elements { for (var i = 0; i < amount; i++) { var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline; cols[i] = new Group(anchor, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren); - if (parent != null) - parent.AddChild(cols[i]); + parent?.AddChild(cols[i]); } return cols; } + /// + /// Creates an array of groups with a fixed width and height that can be used to create a grid with equally sized boxes. + /// + /// The element the groups should be added to, can be . + /// The total size that the grid should take up. + /// The width of the grid, or the amount of columns it should have. + /// The height of the grid, or the amount of rows it should have. + /// The created grid. + public static Group[,] MakeGrid(Element parent, Vector2 totalSize, int width, int height) { + var grid = new Group[width, height]; + for (var y = 0; y < height; y++) { + for (var x = 0; x < width; x++) { + var anchor = x == 0 ? Anchor.AutoLeft : Anchor.AutoInlineIgnoreOverflow; + grid[x, y] = new Group(anchor, new Vector2(totalSize.X / width, totalSize.Y / height), false); + parent?.AddChild(grid[x, y]); + } + } + return grid; + } + /// /// Creates a with a + and a - button next to it, to allow for easy number input. ///