mirror of
https://github.com/Stone-Red-Code/RemSox.git
synced 2026-09-04 09:06:23 +02:00
Reorganize project structure
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
using RemSox.Kernel.UI.GUI.UIEelements;
|
||||
using RemSox.Kernel.UI.GUI.Windows;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.Kernel.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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using RemSox.Kernel.UI.GUI.Windows;
|
||||
|
||||
namespace RemSox.Kernel.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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
using RemSox.Kernel.UI.GUI.UIEelements;
|
||||
using RemSox.Kernel.UI.GUI.Windows;
|
||||
|
||||
using System.Drawing;
|
||||
|
||||
namespace RemSox.Kernel.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user