Skip to content

Commit

Permalink
Add basic UI calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomvas committed May 24, 2024
1 parent de7f029 commit 59095ba
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
42 changes: 42 additions & 0 deletions Basalt.Raylib/Components/Label.cs
Original file line number Diff line number Diff line change
@@ -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}");
}
}
}
28 changes: 28 additions & 0 deletions Basalt.TestField/Components/TestUiComponent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
2 changes: 2 additions & 0 deletions Basalt.TestField/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
32 changes: 29 additions & 3 deletions Basalt/Common/Components/UIComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand All @@ -37,6 +37,32 @@ private protected override void UnsubscribeFromEvents()
Engine.Instance.GetEngineComponent<IEventBus>()!.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;
}
}

}
}

0 comments on commit 59095ba

Please sign in to comment.