using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using RecrownedAthenaeum.UI.SkinSystem;
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{
///
/// Function to be called when button is clicked.
///
/// The button that was clicked.
public delegate void Clicked(Button button);
///
/// A very primitive button containing all the basic functions.
///
public class Button : UIModule
{
private ButtonSkinDefinition skinDefinition;
private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture;
///
/// Click event listeners.
///
public event Clicked Listeners;
private bool pressed;
///
/// Whether or not this button should be currently disabled.
///
public bool disabled = false;
///
/// Whether or not this button is currently being hovered on.
///
public bool Highlighted { get; private set; }
///
/// Constructs this button using s for the different states it could be in.
///
/// Button being pressed.
/// Button not being pressed.
/// Disabled button.
/// Button being highlighted.
public Button(ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null)
{
this.downTexture = down;
this.upTexture = up;
this.disabledTexture = disabled;
this.highlightedTexture = selected;
}
///
/// Constructs this button using the skin system.
///
/// The skin containing the information of the textures and design to follow.
/// The name of the definition in the skin. Can be null to select the default.
public Button(ISkin skin, string definitionName = null)
{
skinDefinition = skin.ObtainDefinition(definitionName);
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion, true);
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion, true);
disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);
highlightedTexture = skin.GetTextureAtlasRegion(skinDefinition.selectedRegion);
}
///
/// Instantiates a button using a definition.
///
/// The skin the definition is defined in.
/// The definition itself.
public Button(ISkin skin, ButtonSkinDefinition skinDefinition) :
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
{}
///
/// Draws the button.
///
/// Batch used to draw the button.
public override void Draw(SpriteBatch batch)
{
if (disabled)
{
disabledTexture?.Draw(batch, bounds, color);
}
else
{
if (pressed)
{
downTexture.Draw(batch, bounds, color);
}
else
{
upTexture.Draw(batch, bounds, color);
}
}
base.Draw(batch);
}
///
/// Called when the mouse changes state.
///
/// The new state.
/// Whether or not to continue calling the next mouse change listener.
public sealed override bool MouseStateChanged(MouseState state)
{
if (InputUtilities.MouseWithinBoundries(bounds))
{
if (state.LeftButton == ButtonState.Pressed)
{
pressed = true;
}
else
{
pressed = false;
}
if (InputUtilities.MouseClicked())
{
OnClick();
}
Highlighted = true;
}
else
{
Highlighted = false;
pressed = false;
}
return base.MouseStateChanged(state);
}
///
/// Called when the state of the keyboard changes.
///
/// The new state.
/// Whether or not the next keyboard change listener should be called.
public sealed override bool KeyboardStateChanged(KeyboardState state)
{
return base.KeyboardStateChanged(state);
}
internal void OnClick()
{
Listeners?.Invoke(this);
}
}
}