diff --git a/Basalt.Raylib/Components/Label.cs b/Basalt.Raylib/Components/Label.cs new file mode 100644 index 0000000..bff2832 --- /dev/null +++ b/Basalt.Raylib/Components/Label.cs @@ -0,0 +1,42 @@ +using Basalt.Common.Components; +using Basalt.Common.Entities; +using System.Numerics; + +namespace Basalt.Raylib.Components +{ + public class Label : UIComponent + { + public string Text { get; set; } = string.Empty; + public float FontSize { get; set; } = 20; + public float Spacing { get; set; } = 1; + public float Rotation { get; set; } = 0; + public Label(Entity entity) : base(entity) + { + } + + public override void OnStart() + { + + } + + public override void OnUpdate() + { + + } + + public override void OnUIRender() + { + var position = GetPivotedPosition(new(Raylib_cs.Raylib.GetScreenWidth(), Raylib_cs.Raylib.GetScreenHeight())) + Offset; + var origin = new Vector2(0, 0); + Raylib_cs.Raylib.DrawTextPro(Raylib_cs.Raylib.GetFontDefault(), + Text, + position, + Raylib_cs.Raylib.MeasureTextEx(Raylib_cs.Raylib.GetFontDefault(), Text, FontSize, Spacing) / 2, + Rotation, + FontSize, + Spacing, + Raylib_cs.Color.White); + Engine.Instance.Logger?.LogDebug($"Drawing label at {position}"); + } + } +} diff --git a/Basalt.TestField/Components/TestUiComponent.cs b/Basalt.TestField/Components/TestUiComponent.cs new file mode 100644 index 0000000..9ab5445 --- /dev/null +++ b/Basalt.TestField/Components/TestUiComponent.cs @@ -0,0 +1,28 @@ +using Basalt.Common.Components; +using Basalt.Common.Entities; +using Raylib_cs; + +namespace Basalt.TestField.Components +{ + internal class TestUiComponent : UIComponent + { + public TestUiComponent(Entity entity) : base(entity) + { + } + + public override void OnStart() + { + + } + + public override void OnUpdate() + { + + } + + public override void OnUIRender() + { + Raylib_cs.Raylib.DrawText("Hello, world!", 10, 10, 20, Color.White); + } + } +} diff --git a/Basalt.TestField/Program.cs b/Basalt.TestField/Program.cs index a91c0b9..4ef29e7 100644 --- a/Basalt.TestField/Program.cs +++ b/Basalt.TestField/Program.cs @@ -13,6 +13,7 @@ using Basalt.Raylib.Input; using Basalt.Raylib.Utils; using Basalt.TestField; +using Basalt.TestField.Components; using Basalt.Types; using Raylib_cs; using System.Numerics; @@ -57,6 +58,7 @@ player.AddComponent(new Basalt.TestField.Components.PlayerController(player)); player.AddComponent(new LightSource(player, "lighting") { Color = Color.Red, Type = LightType.Point }); //player.AddComponent(new TrailRenderer(player) { StartRadius = 0.5f, EndRadius = 0.1f, Color = Color.Red, TrailSegmentCount = 25, Offset = offset, TrailRefreshRate = 0.025f }); +player.AddComponent(new Label(player) { Text = "This is a test label ", Pivot = UIPivot.MiddleLeft, Offset = new(100, 25)}); Engine.CreateEntity(player); diff --git a/Basalt/Common/Components/UIComponent.cs b/Basalt/Common/Components/UIComponent.cs index a55b515..0ff760e 100644 --- a/Basalt/Common/Components/UIComponent.cs +++ b/Basalt/Common/Components/UIComponent.cs @@ -8,9 +8,9 @@ namespace Basalt.Common.Components { public abstract class UIComponent : Component { - public UIPivot Pivot { get; set; } - public Vector3 Offset { get; set; } - public Vector3 Scale { get; set; } + public UIPivot Pivot { get; set; } = UIPivot.TopLeft; + public Vector2 Offset { get; set; } + public float ZIndex { get; set; } protected UIComponent(Entity entity) : base(entity) { } @@ -37,6 +37,32 @@ private protected override void UnsubscribeFromEvents() Engine.Instance.GetEngineComponent()!.Unsubscribe(BasaltConstants.UiRenderEventKey, OnUIRenderEvent); } + protected Vector2 GetPivotedPosition(Vector2 screenSize) + { + switch (Pivot) + { + case UIPivot.TopLeft: + return new Vector2(0, 0); + case UIPivot.TopCenter: + return new Vector2(screenSize.X / 2, 0); + case UIPivot.TopRight: + return new Vector2(screenSize.X, 0); + case UIPivot.MiddleLeft: + return new Vector2(0, screenSize.Y / 2); + case UIPivot.MiddleCenter: + return new Vector2(screenSize.X / 2, screenSize.Y / 2); + case UIPivot.MiddleRight: + return new Vector2(screenSize.X, screenSize.Y / 2); + case UIPivot.BottomLeft: + return new Vector2(0, screenSize.Y); + case UIPivot.BottomCenter: + return new Vector2(screenSize.X / 2, screenSize.Y); + case UIPivot.BottomRight: + return new Vector2(screenSize.X, screenSize.Y); + default: + return Vector2.Zero; + } + } } }