2023-12-06 21:15:20 +00:00
|
|
|
using System;
|
|
|
|
using Microsoft.Xna.Framework;
|
2023-12-06 22:38:38 +00:00
|
|
|
using Microsoft.Xna.Framework.Content;
|
2023-12-06 21:15:20 +00:00
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
using Vector2 = Microsoft.Xna.Framework.Vector2;
|
|
|
|
|
|
|
|
namespace GUI.UIElements;
|
|
|
|
|
|
|
|
public class ButtonData
|
|
|
|
{
|
|
|
|
public Color bgColor;
|
|
|
|
public Color fgColor;
|
|
|
|
|
|
|
|
public Color hoverBgColor;
|
|
|
|
public Color hoverFgColor;
|
|
|
|
|
|
|
|
public Color pressedBgColor;
|
|
|
|
public Color pressedFgColor;
|
2023-12-06 22:38:38 +00:00
|
|
|
|
|
|
|
public Color idleBgColor;
|
|
|
|
public Color idleFgColor;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
public bool isPressed = false;
|
|
|
|
public bool isReleased = false;
|
|
|
|
public bool isHovered = false;
|
2023-12-06 22:38:38 +00:00
|
|
|
public bool isIdle = false;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
public Action<Button>? onClick;
|
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
public string text = "";
|
|
|
|
public Vector2 textPosition;
|
|
|
|
public Vector2 textResolution;
|
|
|
|
|
2023-12-06 21:15:20 +00:00
|
|
|
public ButtonData(Action<Button>? onClick)
|
|
|
|
{
|
|
|
|
this.onClick = onClick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Button : Drawable
|
|
|
|
{
|
|
|
|
private DrawableData _drawableData;
|
2023-12-06 22:38:38 +00:00
|
|
|
public ButtonData _buttonData;
|
|
|
|
|
|
|
|
private SpriteFont buttonFont;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
public Button(DrawableData data, ButtonData buttonData)
|
|
|
|
{
|
|
|
|
_drawableData = data;
|
|
|
|
_buttonData = buttonData;
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
public override void Initialize()
|
2023-12-06 21:15:20 +00:00
|
|
|
{
|
2023-12-06 22:38:38 +00:00
|
|
|
buttonFont = Scene.content.Load<SpriteFont>("Fonts/default");
|
2023-12-06 21:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void onPressed()
|
|
|
|
{
|
|
|
|
if (_buttonData.isPressed)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Console.WriteLine("I'm being pressed!");
|
|
|
|
_buttonData.bgColor = Color.Red;
|
2023-12-06 22:38:38 +00:00
|
|
|
_buttonData.fgColor = Color.Black;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
if (_buttonData.onClick is not null)
|
|
|
|
_buttonData.onClick(this);
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
_buttonData.isReleased = false;
|
|
|
|
_buttonData.isPressed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onReleased()
|
|
|
|
{
|
|
|
|
if (!_buttonData.isPressed)
|
|
|
|
return;
|
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
_buttonData.bgColor = Color.Yellow;
|
|
|
|
_buttonData.fgColor = Color.Red;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
_buttonData.isPressed = false;
|
|
|
|
_buttonData.isReleased = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void onHovered()
|
|
|
|
{
|
|
|
|
if (_buttonData.isHovered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Console.WriteLine("I'm being hovered!");
|
|
|
|
_buttonData.bgColor = Color.Yellow;
|
2023-12-06 22:38:38 +00:00
|
|
|
_buttonData.fgColor = Color.Red;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
_buttonData.isHovered = true;
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
void onIdle()
|
2023-12-06 21:15:20 +00:00
|
|
|
{
|
|
|
|
_buttonData.bgColor = Color.Red;
|
2023-12-06 22:38:38 +00:00
|
|
|
_buttonData.fgColor = Color.Yellow;
|
2023-12-06 21:15:20 +00:00
|
|
|
|
|
|
|
_buttonData.isPressed = false;
|
|
|
|
_buttonData.isReleased = false;
|
|
|
|
_buttonData.isHovered = false;
|
|
|
|
}
|
|
|
|
|
2023-12-06 22:38:38 +00:00
|
|
|
public void buttonLogic()
|
2023-12-06 21:15:20 +00:00
|
|
|
{
|
|
|
|
var mstate = Mouse.GetState();
|
|
|
|
Vector2 mousePosition = mstate.Position.ToVector2();
|
|
|
|
|
|
|
|
bool outsideHorizontalBoundary = (mousePosition.X < _drawableData.position.X) || (mousePosition.X > (_drawableData.position.X + _drawableData.scale.X));
|
|
|
|
bool outsideVerticalBoundary = (mousePosition.Y < _drawableData.position.Y) || (mousePosition.Y > (_drawableData.position.Y + _drawableData.scale.Y));
|
|
|
|
|
|
|
|
if (outsideHorizontalBoundary || outsideVerticalBoundary)
|
|
|
|
{
|
2023-12-06 22:38:38 +00:00
|
|
|
onIdle();
|
2023-12-06 21:15:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mstate.LeftButton == ButtonState.Pressed)
|
|
|
|
{
|
|
|
|
onPressed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mstate.LeftButton == ButtonState.Released)
|
|
|
|
{
|
|
|
|
onReleased();
|
|
|
|
}
|
2023-12-06 22:38:38 +00:00
|
|
|
|
2023-12-06 21:15:20 +00:00
|
|
|
onHovered();
|
2023-12-06 22:38:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Update(GameTime gameTime)
|
|
|
|
{
|
|
|
|
buttonLogic();
|
|
|
|
|
|
|
|
_buttonData.textResolution = buttonFont.MeasureString(_buttonData.text);
|
|
|
|
|
|
|
|
// we want to center the text so
|
|
|
|
Vector2 buttonCenter = (_drawableData.position + (_drawableData.scale / 2.0f));
|
|
|
|
Vector2 textHalfResolution = _buttonData.textResolution / 2.0f;
|
|
|
|
|
|
|
|
_buttonData.textPosition = buttonCenter - textHalfResolution;
|
2023-12-06 21:15:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Draw(GameTime gameTime)
|
|
|
|
{
|
|
|
|
Scene.spriteBatch.Draw(
|
|
|
|
_drawableData.texture,
|
|
|
|
_drawableData.position,
|
|
|
|
null,
|
|
|
|
_buttonData.bgColor,
|
2023-12-06 22:38:38 +00:00
|
|
|
_drawableData.rotation,
|
2023-12-06 21:15:20 +00:00
|
|
|
Vector2.Zero,
|
|
|
|
_drawableData.scale,
|
|
|
|
SpriteEffects.None,
|
|
|
|
0f
|
|
|
|
);
|
2023-12-06 22:38:38 +00:00
|
|
|
|
|
|
|
Scene.spriteBatch.DrawString(
|
|
|
|
buttonFont,
|
|
|
|
_buttonData.text,
|
|
|
|
_buttonData.textPosition,
|
|
|
|
_buttonData.fgColor,
|
|
|
|
_drawableData.rotation,
|
|
|
|
Vector2.Zero,
|
|
|
|
Vector2.One,
|
|
|
|
SpriteEffects.None,
|
|
|
|
0f,
|
|
|
|
false
|
|
|
|
);
|
2023-12-06 21:15:20 +00:00
|
|
|
}
|
|
|
|
}
|