-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from thiagomvas/ui-system
UI system
- Loading branch information
Showing
23 changed files
with
656 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.