1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Added ElementHelper.MakeGrid

This commit is contained in:
Ell 2022-10-17 10:57:41 +02:00
parent d3fade27e5
commit 627350ca31
2 changed files with 22 additions and 2 deletions

View file

@ -37,6 +37,7 @@ Additions
- Added Element.OnAddedToUi and Element.OnRemovedFromUi - Added Element.OnAddedToUi and Element.OnRemovedFromUi
- Added ScrollBar.MouseDragScrolling - Added ScrollBar.MouseDragScrolling
- Added Panel.ScrollToElement - Added Panel.ScrollToElement
- Added ElementHelper.MakeGrid
Improvements Improvements
- Allow elements to auto-adjust their size even when their children are aligned oddly - Allow elements to auto-adjust their size even when their children are aligned oddly

View file

@ -68,12 +68,31 @@ namespace MLEM.Ui.Elements {
for (var i = 0; i < amount; i++) { for (var i = 0; i < amount; i++) {
var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline; var anchor = i == amount - 1 ? Anchor.AutoInlineIgnoreOverflow : Anchor.AutoInline;
cols[i] = new Group(anchor, new Vector2(totalSize.X / amount, totalSize.Y), setHeightBasedOnChildren); 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; return cols;
} }
/// <summary>
/// Creates an array of groups with a fixed width and height that can be used to create a grid with equally sized boxes.
/// </summary>
/// <param name="parent">The element the groups should be added to, can be <see langword="null"/>.</param>
/// <param name="totalSize">The total size that the grid should take up.</param>
/// <param name="width">The width of the grid, or the amount of columns it should have.</param>
/// <param name="height">The height of the grid, or the amount of rows it should have.</param>
/// <returns>The created grid.</returns>
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;
}
/// <summary> /// <summary>
/// Creates a <see cref="TextField"/> with a + and a - button next to it, to allow for easy number input. /// Creates a <see cref="TextField"/> with a + and a - button next to it, to allow for easy number input.
/// </summary> /// </summary>