Replaced color usage with OpenTK's implementation.
This commit is contained in:
@@ -0,0 +1,155 @@
|
||||
using RecrownedAthenaeum.Types;
|
||||
using RecrownedAthenaeum.Input;
|
||||
using RecrownedAthenaeum.Graphics.UI.SkinSystem.Definitions;
|
||||
using RecrownedAthenaeum.Graphics.UI.SkinSystem;
|
||||
using RecrownedAthenaeum.Graphics.Render;
|
||||
|
||||
namespace RecrownedAthenaeum.Graphics.UI.Modular.Modules.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Function to be called when button is clicked.
|
||||
/// </summary>
|
||||
/// <param name="button">The button that was clicked.</param>
|
||||
public delegate void Clicked(Button button);
|
||||
|
||||
/// <summary>
|
||||
/// A very primitive button containing all the basic functions.
|
||||
/// </summary>
|
||||
public class Button : UIModule
|
||||
{
|
||||
private ButtonSkinDefinition skinDefinition;
|
||||
private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture;
|
||||
/// <summary>
|
||||
/// Click event listeners.
|
||||
/// </summary>
|
||||
public event Clicked Listeners;
|
||||
private bool pressed;
|
||||
/// <summary>
|
||||
/// Whether or not this button should be currently disabled.
|
||||
/// </summary>
|
||||
public bool disabled = false;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not this button is currently being hovered on.
|
||||
/// </summary>
|
||||
public bool Highlighted { get; private set; }
|
||||
|
||||
/// <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>
|
||||
public Button(ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null)
|
||||
{
|
||||
this.downTexture = down;
|
||||
this.upTexture = up;
|
||||
this.disabledTexture = disabled;
|
||||
this.highlightedTexture = selected;
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public Button(ISkin skin, string definitionName = null)
|
||||
{
|
||||
skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName);
|
||||
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion, true);
|
||||
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion, true);
|
||||
disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);
|
||||
highlightedTexture = skin.GetTextureAtlasRegion(skinDefinition.selectedRegion);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
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))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Draws the button.
|
||||
/// </summary>
|
||||
/// <param name="batch">Batch used to draw the button.</param>
|
||||
public override void Draw(ConsistentSpriteBatch batch)
|
||||
{
|
||||
if (disabled)
|
||||
{
|
||||
disabledTexture?.Draw(batch, Boundaries, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (pressed)
|
||||
{
|
||||
downTexture.Draw(batch, Boundaries, color);
|
||||
}
|
||||
else if (Highlighted)
|
||||
{
|
||||
highlightedTexture?.Draw(batch, Boundaries, color);
|
||||
}
|
||||
else
|
||||
{
|
||||
upTexture.Draw(batch, Boundaries, color);
|
||||
}
|
||||
}
|
||||
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public sealed override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
if (InputUtilities.MouseWithinBoundries(Boundaries))
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
/// <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>
|
||||
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
return base.KeyboardStateChanged(state);
|
||||
}
|
||||
|
||||
internal void OnClick()
|
||||
{
|
||||
Listeners?.Invoke(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,92 @@
|
||||
using RecrownedAthenaeum.Graphics.Render;
|
||||
using OpenTK.Graphics;
|
||||
using RecrownedAthenaeum.Graphics.UI.SkinSystem;
|
||||
using RecrownedAthenaeum.Graphics.UI.SkinSystem.Definitions;
|
||||
|
||||
namespace RecrownedAthenaeum.Graphics.UI.Modular.Modules.Interactive
|
||||
{
|
||||
/// <summary>
|
||||
/// Button that holds a string.
|
||||
/// </summary>
|
||||
public class TextButton : Button
|
||||
{
|
||||
/// <summary>
|
||||
/// The text that is used to display the string.
|
||||
/// </summary>
|
||||
public readonly Text text;
|
||||
|
||||
/// <summary>
|
||||
/// The color the font should be rendered in.
|
||||
/// </summary>
|
||||
public Color4 FontColor { get { return text.color; } set { text.color = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs text button with the positions represented by <see cref="ISpecialDrawable"/>
|
||||
/// </summary>
|
||||
/// <param name="text">The string representing the text to be displayed.</param>
|
||||
/// <param name="font">The font to be used to display the text.</param>
|
||||
/// <param name="down">What to draw as button is pushed down.</param>
|
||||
/// <param name="up">What to draw as button is not pushed.</param>
|
||||
/// <param name="disabled">What to draw as button is disabled.</param>
|
||||
/// <param name="selected">What to draw as button is selected.</param>
|
||||
public TextButton(string text, SpriteFont font, ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null) : base(down, up, disabled, selected)
|
||||
{
|
||||
this.text = new Text(font, text);
|
||||
this.text.autoScale = true;
|
||||
this.text.centered = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a text button using a skin and definition.
|
||||
/// </summary>
|
||||
/// <param name="text">The text to display.</param>
|
||||
/// <param name="font">The font to be used.</param>
|
||||
/// <param name="skin">The skin to use.</param>
|
||||
/// <param name="definitionName">Name of the definition for this type in the skin given.</param>
|
||||
public TextButton(string text, SpriteFont font, ISkin skin, string definitionName = null) : base(skin, skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName))
|
||||
{
|
||||
TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName);
|
||||
this.text = new Text(font, text);
|
||||
this.text.autoScale = true;
|
||||
this.text.centered = true;
|
||||
FontColor = skin.GetColor(skinDefinition.fontColor);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a text button with a given definition.
|
||||
/// </summary>
|
||||
/// <param name="text">The text to be displayed on this button.</param>
|
||||
/// <param name="font">The font to use for this button.</param>
|
||||
/// <param name="skin">The skin the definition is from.</param>
|
||||
/// <param name="skinDefinition">The definition to use.</param>
|
||||
public TextButton(string text, SpriteFont font, ISkin skin, TextButtonSkinDefinition skinDefinition) :
|
||||
this(text,
|
||||
font,
|
||||
skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
|
||||
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
|
||||
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
|
||||
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
/// Updates the text button.
|
||||
/// </summary>
|
||||
/// <param name="gameTime">Snapshot of information about time for game.</param>
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
text.SetPositionAndDimensions(Boundaries);
|
||||
text.Update(gameTime);
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called whenever game wants to render this button.
|
||||
/// </summary>
|
||||
/// <param name="batch">Batch to use. Batch should already be started.</param>
|
||||
public override void Draw(ConsistentSpriteBatch batch)
|
||||
{
|
||||
base.Draw(batch);
|
||||
text.Draw(batch);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user