Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label-dependent button #23

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
147 changes: 66 additions & 81 deletions Basalt.Raylib/Components/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,95 +7,80 @@

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>
/// Represents a button UI component.
/// </summary>
public class Button : UIComponent
{
/// <summary>
/// Gets or sets the Label (text, font size, spacing, and text color) displayed on the button.
/// </summary>
public Label ButtonLabel { 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 size of the button.
/// </summary>
public Vector2 Size { get; set; } = Vector2.One;

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

/// <summary>
/// Gets or sets the size of the button.
/// </summary>
public Vector2 Size { get; set; } = Vector2.One;
/// <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 background color of the button.
/// </summary>
public Color BackgroundColor { get; set; } = Color.White;
/// <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 when hovered over.
/// </summary>
public Color OnHoverColor { get; set; } = Color.Gray;
/// <summary>
/// Gets or sets the action to be performed when the button is clicked.
/// </summary>
public Action OnClick { get; set; }

/// <summary>
/// Gets or sets the color of the button when clicked.
/// </summary>
public Color OnClickColor { get; set; } = Color.DarkGray;
/// <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)
{
}

/// <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; }
/// <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);

/// <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)
{
}
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);
}

/// <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);
}
}
}
DrawTextPro(GetFontDefault(),
ButtonLabel.Text,
position,
MeasureTextEx(GetFontDefault(), ButtonLabel.Text, ButtonLabel.FontSize, ButtonLabel.Spacing) / 2,
Rotation,
ButtonLabel.FontSize,
ButtonLabel.Spacing,
ButtonLabel.TextColor);
}
}
}
Loading