Use ResourceRefs for RenderSystem, add IReloadableSystem, remove UI package.

This commit is contained in:
2024-10-15 21:51:26 +02:00
parent 4d5c6bbe9a
commit 7dff8076b9
14 changed files with 64 additions and 248 deletions

View File

@@ -83,6 +83,12 @@ namespace Voile.Rendering
// Raylib.SetWindowState(windowFlags);
}
// TODO
protected override void ReloadResources()
{
throw new NotImplementedException();
}
protected override void SetWindowTitle(string title)
{
Raylib.SetWindowTitle(title);
@@ -158,8 +164,10 @@ namespace Voile.Rendering
Raylib.DrawCircle((int)transformPosition.X, (int)transformPosition.Y, radius, VoileColorToRaylibColor(color));
}
public override void DrawTexture(Texture2d texture, Color tint)
public override void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint)
{
var texture = textureResource.Value;
if (texture.Handle == -1)
{
LoadTexture(texture);
@@ -214,14 +222,17 @@ namespace Voile.Rendering
return new Raylib_cs.Color { r = (byte)Math.Round(color.R * 255f), g = (byte)Math.Round(color.G * 255f), b = (byte)Math.Round(color.B * 255f), a = (byte)Math.Round(color.A * 255f) };
}
public override void DrawText(Font font, string text, Color color)
public override void DrawText(ResourceRef<Font> fontResource, string text, Color color)
{
var font = fontResource.Value;
if (font.Handle == -1)
{
LoadFont(font);
}
var rayFont = _fontPool[font.Handle];
Raylib.DrawTextPro(rayFont, text, transformPosition, transformPivot, transformRotation, font.Size, 0.0f, VoileColorToRaylibColor(color));
}

View File

@@ -37,13 +37,15 @@ namespace Voile.Rendering
/// <summary>
/// An abstract class representing the graphics renderer.
/// </summary>
public abstract class RenderSystem : IStartableSystem<RendererSettings>, IDisposable
public abstract class RenderSystem : IStartableSystem<RendererSettings>, IReloadableSystem, IDisposable
{
public void Start(RendererSettings settings)
{
CreateAndInitializeWithWindow(settings);
}
public void Reload() => ReloadResources();
/// <inheritdoc/>
public void Dispose()
{
@@ -89,6 +91,8 @@ namespace Voile.Rendering
/// </summary>
public abstract Vector2 MonitorSize { get; }
protected abstract void ReloadResources();
/// <summary>
/// Creates a window.
/// </summary>
@@ -178,15 +182,14 @@ namespace Voile.Rendering
/// <param name="color">Color of the text.</param>
public abstract void DrawSdfText(string text, int fontSize, Color color);
public abstract void DrawText(Font font, string text, Color color);
public abstract void DrawText(ResourceRef<Font> font, string text, Color color);
/// <summary>
/// Draws the texture.
/// </summary>
/// <param name="id">Texture to draw.</param>
/// <param name="tint">Texture tint.</param>
public abstract void DrawTexture(Texture2d texture, Color tint);
public abstract void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint);
protected Vector2 transformPosition, transformPivot;
protected float transformRotation;

View File

@@ -78,7 +78,7 @@ namespace Voile.Rendering
_clearColor = color;
}
public override void DrawText(Font font, string text, Color color)
public override void DrawText(ResourceRef<Font> fontResource, string text, Color color)
{
throw new NotImplementedException();
}
@@ -117,7 +117,7 @@ namespace Voile.Rendering
}
/// <inheritdoc />
public override void DrawTexture(Texture2d texture, Color tint)
public override void DrawTexture(ResourceRef<Texture2d> textureResource, Color tint)
{
throw new NotImplementedException();
}
@@ -394,6 +394,10 @@ namespace Voile.Rendering
throw new NotImplementedException();
}
protected override void ReloadResources()
{
throw new NotImplementedException();
}
private Vector2 _windowSize = Vector2.Zero;
private IWindow? _window;

View File

@@ -17,6 +17,17 @@ public interface IStartableSystem<T>
void Start(T configuration);
}
/// <summary>
/// A system that has the ability to reload its internal state.
/// </summary>
public interface IReloadableSystem
{
/// <summary>
/// Reloads internal system state.
/// </summary>
void Reload();
}
public interface IUpdatableSystem
{
/// <summary>

View File

@@ -1,63 +0,0 @@
using System.Numerics;
using Voile.Rendering;
namespace Voile.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(RenderSystem 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

@@ -1,24 +0,0 @@
using System.Numerics;
namespace Voile.UI;
public class MarginPanel : Panel
{
public Vector2 RelativeMargin { get; set; }
public Vector2 AbsoluteMargin { get; set; }
public MarginPanel(PanelStyle style) : base(style)
{
}
protected override void RearrangeChild(int idx, UIElement child)
{
base.RearrangeChild(idx, child);
var rect = child.Rect;
var absoluteMargin = Rect.Size * RelativeMargin + AbsoluteMargin;
rect.Position = Rect.Position + absoluteMargin;
rect.Size = rect.Size - absoluteMargin * 2;
}
}

View File

@@ -1,20 +0,0 @@
using System.Numerics;
using Voile.Rendering;
namespace Voile.UI;
public class Panel : Container
{
public PanelStyle Style { get; set; }
public Panel(PanelStyle style)
{
Style = style;
}
protected override void OnRender(RenderSystem renderer)
{
base.OnRender(renderer);
renderer.DrawRectangle(Rect.Size, Style.BackgroundColor);
}
}

View File

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

View File

@@ -1,10 +0,0 @@
using System.Numerics;
namespace Voile.UI;
public class Rect
{
public Vector2 Position { get; set; }
public Vector2 Size { get; set; }
public Vector2 Scale { get; set; }
}

View File

@@ -1,31 +0,0 @@
using Voile.Rendering;
namespace Voile.UI;
public class TextLabel : UIElement
{
public string Text { get; set; } = string.Empty;
public Font Font
{
get => _font; set
{
_font = value;
}
}
public int FontSize { get; set; } = 16;
public Color FontColor { get; set; } = Color.White;
protected override void OnRender(RenderSystem renderer)
{
if (_font == null)
{
renderer.DrawDebugText(Text, FontSize, FontColor);
}
else
{
renderer.DrawText(_font, Text, FontColor);
}
}
private Font? _font;
}

View File

@@ -1,55 +0,0 @@
using System.Numerics;
using Voile.Rendering;
using Voile.Utils;
namespace Voile.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(RenderSystem renderer)
{
Vector2 parentPos = parent != null ? parent.Rect.Position : Vector2.Zero;
renderer.SetTransform(Rect.Position + parentPos, 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(RenderSystem renderer);
protected List<UIElement> children;
protected UIElement? parent;
private Logger _logger = new(nameof(UIElement));
}
[Flags]
public enum SizeFlags
{
ShrinkBegin,
ShrinkCenter,
ShrinkEnd,
Fill
}

View File

@@ -1,22 +0,0 @@
using System.Numerics;
namespace Voile.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);
}
}