2019-11-24 15:49:53 -05:00
|
|
|
|
using RecrownedAthenaeum.Types;
|
2018-12-04 07:45:09 -06:00
|
|
|
|
using RecrownedAthenaeum.Input;
|
2019-12-28 15:41:06 -06:00
|
|
|
|
using RecrownedAthenaeum.Graphics.UI.SkinSystem.Definitions;
|
|
|
|
|
using RecrownedAthenaeum.Graphics.UI.SkinSystem;
|
2019-12-28 14:35:01 -06:00
|
|
|
|
using RecrownedAthenaeum.Graphics.Render;
|
2018-11-29 20:41:06 -06:00
|
|
|
|
|
2019-12-28 15:41:06 -06:00
|
|
|
|
namespace RecrownedAthenaeum.Graphics.UI.Modular.Modules.Interactive
|
2018-11-29 20:41:06 -06:00
|
|
|
|
{
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Function to be called when button is clicked.
|
|
|
|
|
/// </summary>
|
2019-01-27 17:59:55 -06:00
|
|
|
|
/// <param name="button">The button that was clicked.</param>
|
|
|
|
|
public delegate void Clicked(Button button);
|
2018-11-29 20:41:06 -06:00
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A very primitive button containing all the basic functions.
|
|
|
|
|
/// </summary>
|
2018-11-29 20:41:06 -06:00
|
|
|
|
public class Button : UIModule
|
|
|
|
|
{
|
2018-12-14 00:43:38 -06:00
|
|
|
|
private ButtonSkinDefinition skinDefinition;
|
|
|
|
|
private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture;
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Click event listeners.
|
|
|
|
|
/// </summary>
|
2018-11-29 20:41:06 -06:00
|
|
|
|
public event Clicked Listeners;
|
2018-12-14 00:43:38 -06:00
|
|
|
|
private bool pressed;
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether or not this button should be currently disabled.
|
|
|
|
|
/// </summary>
|
2018-12-14 00:43:38 -06:00
|
|
|
|
public bool disabled = false;
|
2019-01-14 00:34:35 -06:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether or not this button is currently being hovered on.
|
|
|
|
|
/// </summary>
|
2018-12-14 00:43:38 -06:00
|
|
|
|
public bool Highlighted { get; private set; }
|
2018-11-29 20:41:06 -06:00
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs this button using <see cref="ISpecialDrawable"/>s for the different states it could be in.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="down">Button being pressed.</param>
|
|
|
|
|
/// <param name="up">Button not being pressed.</param>
|
|
|
|
|
/// <param name="disabled">Disabled button.</param>
|
|
|
|
|
/// <param name="selected">Button being highlighted.</param>
|
2018-12-14 00:43:38 -06:00
|
|
|
|
public Button(ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null)
|
2018-11-29 20:41:06 -06:00
|
|
|
|
{
|
2018-12-14 00:43:38 -06:00
|
|
|
|
this.downTexture = down;
|
|
|
|
|
this.upTexture = up;
|
|
|
|
|
this.disabledTexture = disabled;
|
|
|
|
|
this.highlightedTexture = selected;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs this button using the skin system.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="skin">The skin containing the information of the textures and design to follow.</param>
|
|
|
|
|
/// <param name="definitionName">The name of the definition in the skin. Can be null to select the default.</param>
|
2019-01-27 18:14:10 -06:00
|
|
|
|
public Button(ISkin skin, string definitionName = null)
|
2018-12-14 00:43:38 -06:00
|
|
|
|
{
|
2019-01-28 19:43:41 -06:00
|
|
|
|
skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName);
|
2019-03-07 23:43:25 -06:00
|
|
|
|
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion, true);
|
|
|
|
|
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion, true);
|
2019-01-20 01:08:38 -06:00
|
|
|
|
disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);
|
|
|
|
|
highlightedTexture = skin.GetTextureAtlasRegion(skinDefinition.selectedRegion);
|
2018-11-29 20:41:06 -06:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 16:00:13 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Instantiates a button using a definition.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="skin">The skin the definition is defined in.</param>
|
|
|
|
|
/// <param name="skinDefinition">The definition itself.</param>
|
2019-03-09 00:57:29 -06:00
|
|
|
|
public Button(ISkin skin, ButtonSkinDefinition skinDefinition) :
|
|
|
|
|
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
|
|
|
|
|
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
|
|
|
|
|
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
|
2019-01-29 16:00:13 -06:00
|
|
|
|
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
|
2019-03-09 00:57:29 -06:00
|
|
|
|
{ }
|
2019-01-29 16:00:13 -06:00
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Draws the button.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="batch">Batch used to draw the button.</param>
|
2019-03-23 19:04:43 -05:00
|
|
|
|
public override void Draw(ConsistentSpriteBatch batch)
|
2018-11-29 20:41:06 -06:00
|
|
|
|
{
|
2018-12-14 00:43:38 -06:00
|
|
|
|
if (disabled)
|
|
|
|
|
{
|
2019-03-09 00:57:29 -06:00
|
|
|
|
disabledTexture?.Draw(batch, Boundaries, color);
|
2018-12-14 00:43:38 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pressed)
|
|
|
|
|
{
|
2019-03-09 00:57:29 -06:00
|
|
|
|
downTexture.Draw(batch, Boundaries, color);
|
|
|
|
|
}
|
|
|
|
|
else if (Highlighted)
|
|
|
|
|
{
|
|
|
|
|
highlightedTexture?.Draw(batch, Boundaries, color);
|
2018-12-14 00:43:38 -06:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-03-09 00:57:29 -06:00
|
|
|
|
upTexture.Draw(batch, Boundaries, color);
|
2018-12-14 00:43:38 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-05 21:42:28 -06:00
|
|
|
|
|
2018-11-29 20:41:06 -06:00
|
|
|
|
base.Draw(batch);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the mouse changes state.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The new state.</param>
|
|
|
|
|
/// <returns>Whether or not to continue calling the next mouse change listener.</returns>
|
2018-11-29 20:41:06 -06:00
|
|
|
|
public sealed override bool MouseStateChanged(MouseState state)
|
|
|
|
|
{
|
2019-03-09 00:57:29 -06:00
|
|
|
|
if (InputUtilities.MouseWithinBoundries(Boundaries))
|
2018-11-29 20:41:06 -06:00
|
|
|
|
{
|
2018-12-14 00:43:38 -06:00
|
|
|
|
if (state.LeftButton == ButtonState.Pressed)
|
|
|
|
|
{
|
|
|
|
|
pressed = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pressed = false;
|
|
|
|
|
}
|
2018-11-29 20:41:06 -06:00
|
|
|
|
if (InputUtilities.MouseClicked())
|
|
|
|
|
{
|
|
|
|
|
OnClick();
|
|
|
|
|
}
|
|
|
|
|
Highlighted = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Highlighted = false;
|
2018-12-14 00:43:38 -06:00
|
|
|
|
pressed = false;
|
2018-11-29 20:41:06 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.MouseStateChanged(state);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when the state of the keyboard changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The new state.</param>
|
|
|
|
|
/// <returns>Whether or not the next keyboard change listener should be called.</returns>
|
2018-11-29 20:41:06 -06:00
|
|
|
|
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
return base.KeyboardStateChanged(state);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 00:34:35 -06:00
|
|
|
|
internal void OnClick()
|
2018-11-29 20:41:06 -06:00
|
|
|
|
{
|
2019-01-27 17:59:55 -06:00
|
|
|
|
Listeners?.Invoke(this);
|
2018-11-29 20:41:06 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|