Skip to content

Commit

Permalink
Merge pull request #14 from thiagomvas/ui-system
Browse files Browse the repository at this point in the history
UI system
  • Loading branch information
thiagomvas authored May 29, 2024
2 parents f8f5725 + 8e79515 commit 05c2597
Show file tree
Hide file tree
Showing 23 changed files with 656 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Basalt.Core/Basalt.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
2 changes: 1 addition & 1 deletion Basalt.Core/Common/Abstractions/Engine/IEventBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IEventBus : IEngineComponent
/// <param name="eventName">The name of the event to unsubscribe from.</param>
/// <param name="handler">The handler to remove from the event.</param>
void Unsubscribe(string eventName, EventHandler handler);

/// <summary>
/// Triggers an event on the event bus.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Basalt.Raylib/Basalt.Raylib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageIcon>BasaltLogoBg.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
112 changes: 112 additions & 0 deletions Basalt.Raylib/Components/Button.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Basalt.Math;
using Raylib_cs;
using System.Numerics;
using static Raylib_cs.Raylib;

namespace Basalt.Raylib.Components
{
/// <summary>
/// Represents a button UI component.
/// </summary>
public class Button : UIComponent
{
/// <summary>
/// Gets or sets the text displayed on the button.
/// </summary>
public string Text { get; set; }

/// <summary>
/// Gets or sets the font size of the button text.
/// </summary>
public float FontSize { get; set; } = 12f;

/// <summary>
/// Gets or sets the spacing between characters in the button text.
/// </summary>
public float Spacing { get; set; } = 1f;

/// <summary>
/// Gets or sets the size of the button.
/// </summary>
public Vector2 Size { get; set; } = Vector2.One;

/// <summary>
/// Gets or sets the background color of the button.
/// </summary>
public Color BackgroundColor { get; set; } = Color.White;

/// <summary>
/// Gets or sets the color of the button when hovered over.
/// </summary>
public Color OnHoverColor { get; set; } = Color.Gray;

/// <summary>
/// Gets or sets the color of the button when clicked.
/// </summary>
public Color OnClickColor { get; set; } = Color.DarkGray;

/// <summary>
/// Gets or sets the color of the button text.
/// </summary>
public Color TextColor { get; set; } = Color.Black;

/// <summary>
/// Gets or sets the action to be performed when the button is clicked.
/// </summary>
public Action OnClick { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="Button"/> class.
/// </summary>
/// <param name="entity">The entity that the button belongs to.</param>
public Button(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
{
var position = GetPivotedPosition(new(GetScreenWidth(), GetScreenHeight())) + Offset;
var mousePos = GetMousePosition();
var rect = new Rectangle(position.X, position.Y, Size.X, Size.Y);

if (BasaltMath.IsBetween(mousePos.X, position.X - Size.X * 0.5f, position.X + Size.X * 0.5f) && BasaltMath.IsBetween(mousePos.Y, position.Y - Size.Y * 0.5f, position.Y + Size.Y * 0.5f))
{
if (IsMouseButtonPressed(MouseButton.Left))
OnClick?.Invoke();

if (IsMouseButtonDown(MouseButton.Left))
DrawRectanglePro(rect, Size / 2, Rotation, OnClickColor);
else
DrawRectanglePro(rect, Size / 2, Rotation, OnHoverColor);
}
else
{
DrawRectanglePro(rect, Size / 2, Rotation, BackgroundColor);
}

DrawTextPro(GetFontDefault(),
Text,
position,
MeasureTextEx(GetFontDefault(), Text, FontSize, Spacing) / 2,
Rotation,
FontSize,
Spacing,
Color.White);
}
}
}
57 changes: 57 additions & 0 deletions Basalt.Raylib/Components/Image.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Basalt.Common.Utils;
using Basalt.Raylib.Graphics;
using Raylib_cs;
using static Raylib_cs.Raylib;
using System.Numerics;

namespace Basalt.Raylib.Components
{
public class Image : UIComponent
{
private string _textureKey = string.Empty;
public string TextureKey
{
get => _textureKey;
set
{
_textureKey = value;
_texture = null;
}
}
public float Scale { get; set; } = 1f;
public Color Tint { get; set; } = Color.White;

private Texture2D? _texture;

/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
/// <param name="entity">The entity.</param>
public Image(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
{
if (_texture == null)
_texture = ResourceCache.Instance.GetTexture(TextureKey);
var position = GetPivotedPosition(new Vector2(GetScreenWidth(), GetScreenHeight())) + Offset;
DrawTextureEx(_texture!.Value, position - new Vector2(_texture.Value.Width, _texture.Value.Height) * Scale * 0.5f, Rotation, Scale, Tint);
}
}
}
68 changes: 68 additions & 0 deletions Basalt.Raylib/Components/Label.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Raylib_cs;
using System.Numerics;

namespace Basalt.Raylib.Components
{
/// <summary>
/// Represents a label UI component.
/// </summary>
public class Label : UIComponent
{
/// <summary>
/// Gets or sets the text of the label.
/// </summary>
public string Text { get; set; } = string.Empty;

/// <summary>
/// Gets or sets the font size of the label.
/// </summary>
public float FontSize { get; set; } = 20;

/// <summary>
/// Gets or sets the spacing between characters in the label.
/// </summary>
public float Spacing { get; set; } = 1;

/// <summary>
/// Gets or sets the color of the text in the label.
/// </summary>
public Color TextColor { get; set; } = Color.White;

/// <summary>
/// Initializes a new instance of the <see cref="Label"/> class.
/// </summary>
/// <param name="entity">The entity that the label belongs to.</param>
public Label(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
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,
TextColor);
}
}
}
48 changes: 48 additions & 0 deletions Basalt.Raylib/Components/Panel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Raylib_cs;
using System.Numerics;
using static Raylib_cs.Raylib;
namespace Basalt.Raylib.Components
{
/// <summary>
/// Represents a panel UI component.
/// </summary>
public class Panel : UIComponent
{
/// <summary>
/// Gets or sets the size of the panel.
/// </summary>
public Vector2 Size { get; set; } = Vector2.One;

/// <summary>
/// Gets or sets the color of the panel.
/// </summary>
public Color Color { get; set; } = Color.White;

/// <summary>
/// Initializes a new instance of the <see cref="Panel"/> class.
/// </summary>
/// <param name="entity">The entity that the panel belongs to.</param>
public Panel(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{
}

/// <inheritdoc/>
public override void OnUpdate()
{
}

/// <inheritdoc/>
public override void OnUIRender()
{
var position = GetPivotedPosition(new(GetScreenWidth(), GetScreenHeight())) + Offset;
DrawRectanglePro(new Rectangle(position.X, position.Y, Size.X, Size.Y), Size / 2, Rotation, Color);
}
}
}
62 changes: 62 additions & 0 deletions Basalt.Raylib/Components/ProgressBar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Basalt.Common.Components;
using Basalt.Common.Entities;
using Raylib_cs;
using System.Numerics;
using static Raylib_cs.Raylib;

namespace Basalt.Raylib.Components
{
/// <summary>
/// Represents a progress bar UI component.
/// </summary>
public class ProgressBar : UIComponent
{
/// <summary>
/// Gets or sets the size of the progress bar.
/// </summary>
public Vector2 Size { get; set; } = Vector2.One;

/// <summary>
/// Gets or sets the background color of the progress bar.
/// </summary>
public Color BackgroundColor { get; set; } = Color.White;

/// <summary>
/// Gets or sets the foreground color of the progress bar.
/// </summary>
public Color ForegroundColor { get; set; } = Color.Green;

/// <summary>
/// Gets or sets the progress value of the progress bar.
/// </summary>
public float Progress { get; set; } = 0.5f;

/// <summary>
/// Initializes a new instance of the <see cref="ProgressBar"/> class.
/// </summary>
/// <param name="entity">The entity that the progress bar belongs to.</param>
public ProgressBar(Entity entity) : base(entity)
{
}

/// <inheritdoc/>
public override void OnStart()
{

}

/// <inheritdoc/>
public override void OnUpdate()
{

}

/// <inheritdoc/>
public override void OnUIRender()
{
var position = GetPivotedPosition(new(GetScreenWidth(), GetScreenHeight())) + Offset;
DrawRectanglePro(new Rectangle(position.X, position.Y, Size.X, Size.Y), Size / 2, Rotation, BackgroundColor);
DrawRectanglePro(new Rectangle(position.X, position.Y, Size.X * Progress, Size.Y), Size / 2, Rotation, ForegroundColor);
}
}
}
Loading

0 comments on commit 05c2597

Please sign in to comment.