Add StackLayout and GridLayout classes for window UI element layouts

This commit is contained in:
Stone_Red
2026-06-16 18:20:20 +02:00
parent 99c98e74a2
commit e3ece1fb4a
4 changed files with 205 additions and 11 deletions
+37 -11
View File
@@ -1,4 +1,5 @@
using RemSox.Processing;
using RemSox.UI.GUI.Layout;
using RemSox.UI.GUI.Rendering;
using RemSox.UI.GUI.UIEelements.Controls;
using RemSox.UI.GUI.Windows;
@@ -21,13 +22,15 @@ internal class DesktopProcess() : Process("Desktop Manager")
WindowManager.InvalidateAll();
// Test window 1: interactive controls
// Test window 1: interactive controls using stack layout
Window testWindow = WindowManager.CreateWindow(this, "UI Controls", new Size(280, 260));
testWindow.AutoFlush = true;
Button button = testWindow.CreateUIElement<Button>(b =>
StackLayout stack = testWindow.CreateStackLayout(20, 30, 6);
stack.UniformWidth = 240;
Button button = stack.Add<Button>(b =>
{
b.Position = new Point(20, 30);
b.Size = new Size(240, 25);
b.Text = "Click Me";
b.BackgroundColor = Color.LightBlue;
@@ -39,9 +42,8 @@ internal class DesktopProcess() : Process("Desktop Manager")
button.BackgroundColor = Color.LightGreen;
};
CheckBox check = testWindow.CreateUIElement<CheckBox>(c =>
CheckBox check = stack.Add<CheckBox>(c =>
{
c.Position = new Point(20, 65);
c.Size = new Size(240, 20);
c.Text = "Check Me";
c.IsChecked = true;
@@ -52,25 +54,22 @@ internal class DesktopProcess() : Process("Desktop Manager")
check.Text = check.IsChecked ? "Checked!" : "Unchecked!";
};
RadioButton radio = testWindow.CreateUIElement<RadioButton>(r =>
RadioButton radio = stack.Add<RadioButton>(r =>
{
r.Position = new Point(20, 95);
r.Size = new Size(240, 20);
r.Text = "Radio Option";
r.IsChecked = true;
});
Slider slider = testWindow.CreateUIElement<Slider>(s =>
Slider slider = stack.Add<Slider>(s =>
{
s.Position = new Point(20, 130);
s.Size = new Size(240, 24);
s.BackgroundColor = Color.SteelBlue;
s.Value = 60;
});
ProgressBar progress = testWindow.CreateUIElement<ProgressBar>(p =>
ProgressBar progress = stack.Add<ProgressBar>(p =>
{
p.Position = new Point(20, 170);
p.Size = new Size(240, 20);
p.BackgroundColor = Color.DimGray;
p.FillColor = Color.LimeGreen;
@@ -131,6 +130,33 @@ internal class DesktopProcess() : Process("Desktop Manager")
});
shapesCheck.OnCheckedChanged += (_, _) => { };
// Test window 3: grid layout
Window gridWin = WindowManager.CreateWindow(this, "Grid Layout", new Size(260, 160));
gridWin.AutoFlush = true;
GridLayout grid = gridWin.CreateGridLayout(10, 25,
new int[] { 70, 70, 70 },
new int[] { 25, 25, 25 },
6);
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
int r = row, c = col;
Button btn = grid.Add<Button>(col, row, b =>
{
b.Text = $"[{c},{r}]";
b.BackgroundColor = (c + r) % 2 == 0 ? Color.SteelBlue : Color.DimGray;
});
btn.OnClick += (_, _) =>
{
btn.Text = "X";
};
}
}
}
internal override void Tick()
+70
View File
@@ -0,0 +1,70 @@
using RemSox.UI.GUI.UIEelements;
using RemSox.UI.GUI.Windows;
using System.Drawing;
namespace RemSox.UI.GUI.Layout;
public sealed class GridLayout
{
private readonly Window window;
private readonly int originX;
private readonly int originY;
private readonly int[] colWidths;
private readonly int[] rowHeights;
private readonly int spacing;
public GridLayout(Window window, int x, int y, int[] colWidths, int[] rowHeights, int spacing = 0)
{
this.window = window;
originX = x;
originY = y;
this.colWidths = colWidths;
this.rowHeights = rowHeights;
this.spacing = spacing;
}
public T Add<T>(int col, int row, Action<T>? options = null) where T : UIElement, new()
{
bool wasAutoFlush = window.AutoFlush;
window.AutoFlush = false;
T element = window.CreateUIElement<T>(options);
PositionElement(element, col, row);
if (wasAutoFlush)
{
window.AutoFlush = true;
window.Flush();
}
else
{
window.AutoFlush = false;
}
return element;
}
private void PositionElement(UIElement element, int col, int row)
{
int cellX = CellOrigin(colWidths, col);
int cellY = CellOrigin(rowHeights, row);
element.Position = new Point(originX + cellX, originY + cellY);
if (element is Control control)
{
control.Size = new Size(colWidths[col], rowHeights[row]);
}
}
private int CellOrigin(int[] sizes, int index)
{
int offset = 0;
for (int i = 0; i < index; i++)
{
offset += sizes[i] + spacing;
}
return offset;
}
}
+16
View File
@@ -0,0 +1,16 @@
using RemSox.UI.GUI.Windows;
namespace RemSox.UI.GUI.Layout;
public static class LayoutExtensions
{
public static StackLayout CreateStackLayout(this Window window, int x, int y, int spacing = 0, StackOrientation orientation = StackOrientation.Vertical)
{
return new StackLayout(window, x, y, spacing, orientation);
}
public static GridLayout CreateGridLayout(this Window window, int x, int y, int[] colWidths, int[] rowHeights, int spacing = 0)
{
return new GridLayout(window, x, y, colWidths, rowHeights, spacing);
}
}
+82
View File
@@ -0,0 +1,82 @@
using RemSox.UI.GUI.UIEelements;
using RemSox.UI.GUI.Windows;
using System.Drawing;
namespace RemSox.UI.GUI.Layout;
public enum StackOrientation
{
Vertical,
Horizontal,
}
public sealed class StackLayout
{
private readonly Window window;
private int nextX;
private int nextY;
private readonly int spacing;
private readonly StackOrientation orientation;
public int? UniformWidth { get; set; }
public int? UniformHeight { get; set; }
public StackLayout(Window window, int x, int y, int spacing = 0, StackOrientation orientation = StackOrientation.Vertical)
{
this.window = window;
nextX = x;
nextY = y;
this.spacing = spacing;
this.orientation = orientation;
}
public T Add<T>(Action<T>? options = null) where T : UIElement, new()
{
bool wasAutoFlush = window.AutoFlush;
window.AutoFlush = false;
T element = window.CreateUIElement<T>(options);
PositionElement(element);
if (wasAutoFlush)
{
window.AutoFlush = true;
window.Flush();
}
else
{
window.AutoFlush = false;
}
return element;
}
private void PositionElement(UIElement element)
{
element.Position = new Point(nextX, nextY);
if (element is Control control)
{
if (UniformWidth.HasValue)
{
control.Size = new Size(UniformWidth.Value, control.Size.Height);
}
if (UniformHeight.HasValue)
{
control.Size = new Size(control.Size.Width, UniformHeight.Value);
}
}
if (orientation == StackOrientation.Vertical)
{
int h = element is Control c ? c.Size.Height : 0;
nextY += h + spacing;
}
else
{
int w = element is Control c ? c.Size.Width : 0;
nextX += w + spacing;
}
}
}