Skip to content

Commit

Permalink
Add button
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagomvas committed May 28, 2024
1 parent d1e34dc commit 74742dc
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Basalt.Raylib/Components/Button.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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
{
public class Button : UIComponent
{
public string Text { get; set; }
public float FontSize { get; set; } = 12f;
public float Spacing { get; set; } = 1f;
public Vector2 Size { get; set; } = Vector2.One;
public Color BackgroundColor { get; set; } = Color.White;
public Color OnHoverColor { get; set; } = Color.Gray;
public Color OnClickColor { get; set; } = Color.DarkGray;
public Color TextColor { get; set; } = Color.Black;

public Action OnClick { get; set; }
public Button(Entity entity) : base(entity)
{
}

public override void OnStart()
{

}

public override void OnUpdate()
{

}

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


}
}
5 changes: 5 additions & 0 deletions Basalt/Math/BasaltMath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,10 @@ public static float AngleBetweenVectors(Vector3 vector1, Vector3 vector2)
float angle = (float)System.Math.Acos(dotProduct / (magnitude1 * magnitude2));
return angle;
}

public static bool IsBetween(float value, float min, float max)
{
return value >= min && value <= max;
}
}
}

0 comments on commit 74742dc

Please sign in to comment.