UI WIP
This commit is contained in:
63
DaggerFramework/Source/UI/Container.cs
Normal file
63
DaggerFramework/Source/UI/Container.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using System.Numerics;
|
||||
using DaggerFramework.Rendering;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
/// <summary>
|
||||
/// A basic container for UI elements. All container's children will update their constraints based on container's sizing and positioning.
|
||||
/// </summary>
|
||||
public class Container : UIElement
|
||||
{
|
||||
/// <summary>
|
||||
/// Updates the sizes of the container and rearranges its children.
|
||||
/// </summary>
|
||||
/// <param name="position"></param>
|
||||
/// <param name="size"></param>
|
||||
public void UpdateRect(Vector2 position, Vector2 size)
|
||||
{
|
||||
Rect.Position = position;
|
||||
|
||||
UpdateSize(size);
|
||||
RearrangeChildren();
|
||||
}
|
||||
|
||||
protected void RearrangeChildren()
|
||||
{
|
||||
int idx = 0;
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child is Container container)
|
||||
{
|
||||
container.UpdateRect(Rect.Position, Rect.Size);
|
||||
}
|
||||
RearrangeChild(idx, child);
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual void RearrangeChild(int idx, UIElement child) { }
|
||||
|
||||
protected override void OnRender(Renderer renderer)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void UpdateSize(Vector2 baseSize)
|
||||
{
|
||||
if (parent == null)
|
||||
{
|
||||
Rect.Size = baseSize;
|
||||
return;
|
||||
}
|
||||
|
||||
if (VerticalSizeFlags == SizeFlags.Fill)
|
||||
{
|
||||
Rect.Size = new Vector2(Rect.Size.X, baseSize.Y * ExpandRatio.Y);
|
||||
}
|
||||
|
||||
if (HorizontalSizeFlags == SizeFlags.Fill)
|
||||
{
|
||||
Rect.Size = new Vector2(baseSize.X * ExpandRatio.X, Rect.Size.Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
DaggerFramework/Source/UI/MarginPanel.cs
Normal file
19
DaggerFramework/Source/UI/MarginPanel.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public class MarginPanel : Panel
|
||||
{
|
||||
public Vector2 RelativeMargin { get; set; }
|
||||
public MarginPanel(PanelStyle style) : base(style)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void RearrangeChild(int idx, UIElement child)
|
||||
{
|
||||
base.RearrangeChild(idx, child);
|
||||
|
||||
var rect = child.Rect;
|
||||
rect.Position = Rect.Size * RelativeMargin;
|
||||
}
|
||||
}
|
||||
20
DaggerFramework/Source/UI/Panel.cs
Normal file
20
DaggerFramework/Source/UI/Panel.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System.Numerics;
|
||||
using DaggerFramework.Rendering;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public class Panel : Container
|
||||
{
|
||||
public PanelStyle Style { get; set; }
|
||||
|
||||
public Panel(PanelStyle style)
|
||||
{
|
||||
Style = style;
|
||||
}
|
||||
|
||||
protected override void OnRender(Renderer renderer)
|
||||
{
|
||||
base.OnRender(renderer);
|
||||
renderer.DrawRectangle(Rect.Size, Style.BackgroundColor);
|
||||
}
|
||||
}
|
||||
6
DaggerFramework/Source/UI/PanelStyle.cs
Normal file
6
DaggerFramework/Source/UI/PanelStyle.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public struct PanelStyle
|
||||
{
|
||||
public Color BackgroundColor { get; set; }
|
||||
}
|
||||
10
DaggerFramework/Source/UI/Rect.cs
Normal file
10
DaggerFramework/Source/UI/Rect.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public class Rect
|
||||
{
|
||||
public Vector2 Position { get; set; }
|
||||
public Vector2 Size { get; set; }
|
||||
public Vector2 Scale { get; set; }
|
||||
}
|
||||
39
DaggerFramework/Source/UI/TextLabel.cs
Normal file
39
DaggerFramework/Source/UI/TextLabel.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using DaggerFramework.Rendering;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public class TextLabel : UIElement
|
||||
{
|
||||
public string Text { get; set; } = string.Empty;
|
||||
public Font Font
|
||||
{
|
||||
get => _font; set
|
||||
{
|
||||
_isDirty = true;
|
||||
_font = value;
|
||||
}
|
||||
}
|
||||
public int FontSize { get; set; } = 16;
|
||||
public Color FontColor { get; set; } = Color.White;
|
||||
protected override void OnRender(Renderer renderer)
|
||||
{
|
||||
if (_isDirty && _font != null)
|
||||
{
|
||||
_fontHandle = renderer.LoadFont(_font);
|
||||
_isDirty = false;
|
||||
}
|
||||
|
||||
if (_font == null)
|
||||
{
|
||||
renderer.DrawDebugText(Text, FontSize, FontColor);
|
||||
}
|
||||
else
|
||||
{
|
||||
renderer.DrawText(_fontHandle, Text, FontSize, FontColor);
|
||||
}
|
||||
}
|
||||
|
||||
private Font? _font;
|
||||
private int _fontHandle;
|
||||
private bool _isDirty;
|
||||
}
|
||||
54
DaggerFramework/Source/UI/UIElement.cs
Normal file
54
DaggerFramework/Source/UI/UIElement.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System.Numerics;
|
||||
using DaggerFramework.Rendering;
|
||||
using DaggerFramework.Utils;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public abstract class UIElement
|
||||
{
|
||||
public Rect Rect { get; set; } = new Rect();
|
||||
public SizeFlags VerticalSizeFlags { get; set; } = SizeFlags.Fill;
|
||||
public SizeFlags HorizontalSizeFlags { get; set; } = SizeFlags.Fill;
|
||||
|
||||
public Vector2 ExpandRatio { get; set; } = Vector2.One;
|
||||
|
||||
public UIElement()
|
||||
{
|
||||
children = new();
|
||||
}
|
||||
|
||||
public void AddChild(UIElement child)
|
||||
{
|
||||
children.Add(child);
|
||||
child.parent = this;
|
||||
}
|
||||
|
||||
public void Render(Renderer renderer)
|
||||
{
|
||||
renderer.SetTransform(Rect.Position, Vector2.Zero, 0);
|
||||
OnRender(renderer);
|
||||
|
||||
foreach (UIElement child in children)
|
||||
{
|
||||
renderer.SetTransform(child.Rect.Position, Vector2.Zero, 0);
|
||||
child.Render(renderer);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void OnRender(Renderer renderer);
|
||||
|
||||
protected List<UIElement> children;
|
||||
protected UIElement? parent;
|
||||
|
||||
private Logger _logger = new(nameof(UIElement));
|
||||
}
|
||||
|
||||
|
||||
[Flags]
|
||||
public enum SizeFlags
|
||||
{
|
||||
ShrinkBegin,
|
||||
ShrinkCenter,
|
||||
ShrinkEnd,
|
||||
Fill
|
||||
}
|
||||
22
DaggerFramework/Source/UI/VerticalPanel.cs
Normal file
22
DaggerFramework/Source/UI/VerticalPanel.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace DaggerFramework.UI;
|
||||
|
||||
public class VerticalPanel : Panel
|
||||
{
|
||||
public float Spacing { get; set; } = 16;
|
||||
public VerticalPanel(PanelStyle style) : base(style)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void RearrangeChild(int idx, UIElement child)
|
||||
{
|
||||
base.RearrangeChild(idx, child);
|
||||
|
||||
var yOffset = idx * Spacing;
|
||||
var rect = child.Rect;
|
||||
|
||||
rect.Position = Rect.Position;
|
||||
rect.Position = new Vector2(rect.Position.X, yOffset);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user