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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user