recrownedgtk/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs

63 lines
1.5 KiB
C#
Raw Normal View History

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.UI.Modular;
using RecrownedAthenaeum.DataTypes;
using RecrownedAthenaeum.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{
public delegate bool Clicked();
public class Button : UIModule
{
private NinePatch background;
public event Clicked Listeners;
public Button(NinePatch background = null)
{
this.background = background;
}
public override void Draw(SpriteBatch batch)
{
background?.Draw(batch, bounds);
base.Draw(batch);
}
public sealed override bool MouseStateChanged(MouseState state)
{
if (InputUtilities.MouseWithinBoundries(bounds))
{
if (InputUtilities.MouseClicked())
{
OnClick();
}
Highlighted = true;
}
else
{
Highlighted = false;
}
return base.MouseStateChanged(state);
}
public sealed override bool KeyboardStateChanged(KeyboardState state)
{
return base.KeyboardStateChanged(state);
}
protected void OnClick()
{
Listeners?.Invoke();
}
public bool Highlighted { get; private set; }
}
}