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 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 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); Raylib.SetTraceLogLevel(TraceLogLevel.LOG_NONE);
_windowSize = size; _windowSize = windowSettings.Size;
Raylib.InitWindow((int)_windowSize.X, (int)_windowSize.Y, title);
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) public override void SetWindowTitle(string title)
@@ -179,6 +191,7 @@ namespace DaggerFramework.Rendering
// Fullscreen // Fullscreen
flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0; flags |= settings.Fullscreen ? ConfigFlags.FLAG_FULLSCREEN_MODE : 0;
Raylib.SetConfigFlags(flags); Raylib.SetConfigFlags(flags);
} }

View File

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

View File

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

View File

@@ -77,7 +77,7 @@ namespace DaggerFramework.SceneGraph
private void SetupRenderer() 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.Initialize(new RendererSettings { Msaa = Msaa.Msaa4x, UseVSync = true });
Renderer.SetTargetFps(60); 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);
}
}

View File

@@ -20,7 +20,8 @@ public class TestGame : Game
_renderer.CreateAndInitialize(new WindowSettings() _renderer.CreateAndInitialize(new WindowSettings()
{ {
Title = "Test Game", Title = "Test Game",
Size = new Vector2(1280, 720) Size = new Vector2(1280, 720),
Resizable = true,
}, new RendererSettings() }, new RendererSettings()
{ {
UseVSync = true UseVSync = true
@@ -73,25 +74,6 @@ public class TestGame : Game
{ {
while (!_scene.ShouldStop()) while (!_scene.ShouldStop())
{ {
// _renderer.BeginFrame();
// _renderer.ClearBackground(Color.Black);
// if (_inputHandler.IsActionJustPressed("play"))
// {
// var instance = _audioBackend.CreateInstance(_testSound)
// .PitchVariation(min: 0.9f, max: 1.2f)
// .VolumeVariation(min: 0.90f, max: 1.0f);
// instance.Play();
// }
// _audioBackend.Update();
// _renderer.SetTransform(new Vector2(32, 32));
// _renderer.DrawCircle(16f, Color.Chocolate);
// _renderer.DrawText(_fontHandle, $"{(int)(1 / _renderer.GetFrameTime())} FPS", 20, Color.White);
// _renderer.EndFrame();
_scene.Update(); _scene.Update();
} }
} }

View File

@@ -1,34 +1,80 @@
using System.Numerics; using System.Numerics;
using DaggerFramework; using DaggerFramework;
using DaggerFramework.Rendering;
using DaggerFramework.SceneGraph; using DaggerFramework.SceneGraph;
using DaggerFramework.UI;
using DaggerFramework.Utils;
public class UiLayer : EntityLayer public class UiLayer : Layer
{ {
protected override void OnStart() protected override void OnStart()
{ {
base.OnStart(); base.OnStart();
GetResources(); GetResources();
CreateAndAddEntities(); CreateUiElements();
} }
protected override void OnUpdate(double dt) protected override void OnUpdate(double dt)
{ {
base.OnUpdate(dt); base.OnUpdate(dt);
// _textLabel.Text = $"{MathF.Round(1 / (float)dt)} FPS";
_fpsText.Text = $"{MathF.Round(1 / (float)dt)} FPS"; _screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize);
} }
private void CreateAndAddEntities() protected override void OnBeginDraw(Renderer renderer)
{ {
_fpsText = new Text2d()
}
protected override void OnDraw(Renderer renderer)
{ {
Font = _defaultFont, _screenContainer.Render(renderer);
Position = Vector2.One * 16, }
FontSize = 20
protected override void OnEndDraw(Renderer renderer)
{
}
private void CreateUiElements()
{
_screenContainer = new Container();
_screenContainer.UpdateRect(Vector2.Zero, Scene.Renderer.WindowSize);
var style = new PanelStyle()
{
BackgroundColor = new Color(0.25f, 0.25f, 0.25f, 1.0f)
}; };
AddEntity(_fpsText); _panel = new MarginPanel(style)
{
ExpandRatio = new Vector2(0.5f, 1.0f),
RelativeMargin = Vector2.One * 0.01f
};
_screenContainer.AddChild(_panel);
var exampleText = new TextLabel()
{
Font = _defaultFont,
FontSize = 30,
Text = "This Panel will occupy 50% of the screen.\nHow cool is that?"
};
_panel.AddChild(exampleText);
// var verticalPanel = new VerticalPanel(new PanelStyle()
// {
// BackgroundColor = Color.White,
// });
// verticalPanel.ExpandRatio = new Vector2(1f, 0.5f);
// _panel.AddChild(verticalPanel);
// verticalPanel.AddChild(new Panel(new PanelStyle() { BackgroundColor = Color.Red }));
// verticalPanel.AddChild(new Panel(new PanelStyle() { BackgroundColor = Color.Green }));
// verticalPanel.AddChild(new Panel(new PanelStyle() { BackgroundColor = Color.Blue }));
} }
private void GetResources() private void GetResources()
@@ -36,6 +82,11 @@ public class UiLayer : EntityLayer
ResourceManager.TryGetResource("inter_regular", out _defaultFont); ResourceManager.TryGetResource("inter_regular", out _defaultFont);
} }
private Text2d _fpsText;
private Font _defaultFont; private Font _defaultFont;
private Container _screenContainer;
private Panel _panel, _contentPanel;
private TextLabel _textLabel;
private Logger _logger = new(nameof(UiLayer));
} }