2018-11-30 02:41:06 +00:00
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
2018-12-04 13:45:09 +00:00
|
|
|
|
using RecrownedAthenaeum.UI.Modular;
|
|
|
|
|
using RecrownedAthenaeum.DataTypes;
|
|
|
|
|
using RecrownedAthenaeum.Input;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2018-12-14 06:43:38 +00:00
|
|
|
|
using RecrownedAthenaeum.UI.Skin.Definitions;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
2018-12-04 13:45:09 +00:00
|
|
|
|
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
|
|
|
|
public delegate bool Clicked();
|
|
|
|
|
|
|
|
|
|
public class Button : UIModule
|
|
|
|
|
{
|
2018-12-14 06:43:38 +00:00
|
|
|
|
private ButtonSkinDefinition skinDefinition;
|
|
|
|
|
private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
public event Clicked Listeners;
|
2018-12-14 06:43:38 +00:00
|
|
|
|
private bool pressed;
|
|
|
|
|
public bool disabled = false;
|
|
|
|
|
public bool Highlighted { get; private set; }
|
2018-11-30 02:41:06 +00:00
|
|
|
|
|
2018-12-14 06:43:38 +00:00
|
|
|
|
public Button(ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null)
|
2018-11-30 02:41:06 +00:00
|
|
|
|
{
|
2018-12-14 06:43:38 +00:00
|
|
|
|
this.downTexture = down;
|
|
|
|
|
this.upTexture = up;
|
|
|
|
|
this.disabledTexture = disabled;
|
|
|
|
|
this.highlightedTexture = selected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Button(Skin.Skin skin, string definitionName = null)
|
|
|
|
|
{
|
|
|
|
|
this.skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName, GetType());
|
|
|
|
|
downTexture = skin.textureAtlas[skinDefinition.downRegion];
|
|
|
|
|
upTexture = skin.textureAtlas[skinDefinition.upRegion];
|
|
|
|
|
disabledTexture = skin.textureAtlas[skinDefinition.disabledRegion];
|
|
|
|
|
highlightedTexture = skin.textureAtlas[skinDefinition.selectedRegion];
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(SpriteBatch batch)
|
|
|
|
|
{
|
2018-12-14 06:43:38 +00:00
|
|
|
|
if (disabled)
|
|
|
|
|
{
|
|
|
|
|
disabledTexture?.Draw(batch, bounds, color);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (pressed)
|
|
|
|
|
{
|
|
|
|
|
downTexture.Draw(batch, bounds, color);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
upTexture.Draw(batch, bounds, color);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
base.Draw(batch);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed override bool MouseStateChanged(MouseState state)
|
|
|
|
|
{
|
|
|
|
|
if (InputUtilities.MouseWithinBoundries(bounds))
|
|
|
|
|
{
|
2018-12-14 06:43:38 +00:00
|
|
|
|
if (state.LeftButton == ButtonState.Pressed)
|
|
|
|
|
{
|
|
|
|
|
pressed = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pressed = false;
|
|
|
|
|
}
|
2018-11-30 02:41:06 +00:00
|
|
|
|
if (InputUtilities.MouseClicked())
|
|
|
|
|
{
|
|
|
|
|
OnClick();
|
|
|
|
|
}
|
|
|
|
|
Highlighted = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Highlighted = false;
|
2018-12-14 06:43:38 +00:00
|
|
|
|
pressed = false;
|
2018-11-30 02:41:06 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return base.MouseStateChanged(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
|
|
|
|
{
|
|
|
|
|
return base.KeyboardStateChanged(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnClick()
|
|
|
|
|
{
|
|
|
|
|
Listeners?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|