This commit is contained in:
2023-09-25 22:18:23 +02:00
parent 092d01dcae
commit c96db94de4
14 changed files with 323 additions and 47 deletions

View File

@@ -7,14 +7,26 @@ namespace DaggerFramework.Rendering
{
public class RaylibRenderer : Renderer
{
public override Vector2 WindowSize => _windowSize;
public override Vector2 WindowSize => new Vector2(Raylib.GetScreenWidth(), Raylib.GetScreenHeight());
public override bool ShouldRun => !WindowShouldClose();
public override void CreateWindow(string title, Vector2 size)
// public override void CreateWindow(string title, Vector2 size)
// {
// Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
// _windowSize = size;
// Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
// }
public override void CreateWindow(WindowSettings windowSettings)
{
Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = size;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
_windowSize = windowSettings.Size;
ConfigFlags windowFlags = 0;
windowFlags |= windowSettings.Resizable ? ConfigFlags.FLAG_WINDOW_RESIZABLE : 0;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, windowSettings.Title);
Raylib.SetWindowState(windowFlags);
}
public override void SetWindowTitle(string title)
@@ -179,6 +191,7 @@ namespace DaggerFramework.Rendering
// Fullscreen
flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0;
Raylib.SetConfigFlags(flags);
}

View File

@@ -32,15 +32,10 @@ namespace DaggerFramework.Rendering
/// </summary>
public abstract Vector2 WindowSize { get; }
/// <summary>
/// Creates the window with a given title and size.
/// Creates a window.
/// </summary>
/// <param name="title">Title of the window.</param>
/// <param name="size">Vector2 representing size.</param>
public abstract void CreateWindow(string title, Vector2 size);
public void CreateWindow(WindowSettings windowSettings)
{
CreateWindow(windowSettings.Title, windowSettings.Size);
}
/// <param name="windowSettings">Window settings to use to create the window.</param>
public abstract void CreateWindow(WindowSettings windowSettings);
// TODO: use properties for these.
public abstract void SetWindowTitle(string title);
@@ -151,6 +146,7 @@ namespace DaggerFramework.Rendering
{
public string Title;
public Vector2 Size = new Vector2(640, 480);
public bool Resizable { get; set; }
public WindowSettings(string title, Vector2 size)
{

View File

@@ -49,9 +49,6 @@ namespace DaggerFramework.Rendering
_gl.Clear((uint)ClearBufferMask.ColorBufferBit);
}
/// <inheritdoc />
public override void CreateWindow(string title, Vector2 size) => CreateWindowUnsafe(title, size);
/// <inheritdoc />
public override void Shutdown()
{
@@ -176,6 +173,10 @@ namespace DaggerFramework.Rendering
throw new NotImplementedException();
}
public override void CreateWindow(WindowSettings windowSettings)
{
throw new NotImplementedException();
}
private GL _gl;
private Glfw _glfw;

View File

@@ -77,7 +77,7 @@ namespace DaggerFramework.SceneGraph
private void SetupRenderer()
{
Renderer.CreateWindow("Game", new Vector2(1280, 720));
// Renderer.CreateWindow("Game", new Vector2(1280, 720));
Renderer.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
Renderer.SetTargetFps(60);
}

View 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);
}
}
}

View 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;
}
}

View 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);
}
}

View File

@@ -0,0 +1,6 @@
namespace DaggerFramework.UI;
public struct PanelStyle
{
public Color BackgroundColor { get; set; }
}

View 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; }
}

View 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;
}

View 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
}

View 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);
}
}