using System;
using Gwen.Input;
namespace Gwen.Control
{
///
/// Button control.
///
public class Button : Label
{
private bool m_Depressed;
private bool m_Toggle;
private bool m_ToggleStatus;
private bool m_CenterImage;
private ImagePanel m_Image;
///
/// Invoked when the button is released.
///
public event GwenEventHandler Clicked;
///
/// Invoked when the button is pressed.
///
public event GwenEventHandler Pressed;
///
/// Invoked when the button is released.
///
public event GwenEventHandler Released;
///
/// Invoked when the button's toggle state has changed.
///
public event GwenEventHandler Toggled;
///
/// Invoked when the button's toggle state has changed to On.
///
public event GwenEventHandler ToggledOn;
///
/// Invoked when the button's toggle state has changed to Off.
///
public event GwenEventHandler ToggledOff;
///
/// Invoked when the button has been double clicked.
///
public event GwenEventHandler DoubleClickedLeft;
///
/// Indicates whether the button is depressed.
///
public bool IsDepressed
{
get { return m_Depressed; }
set
{
if (m_Depressed == value)
return;
m_Depressed = value;
Redraw();
}
}
///
/// Indicates whether the button is toggleable.
///
public bool IsToggle { get { return m_Toggle; } set { m_Toggle = value; } }
///
/// Determines the button's toggle state.
///
public bool ToggleState
{
get { return m_ToggleStatus; }
set
{
if (!m_Toggle) return;
if (m_ToggleStatus == value) return;
m_ToggleStatus = value;
if (Toggled != null)
Toggled.Invoke(this);
if (m_ToggleStatus)
{
if (ToggledOn != null)
ToggledOn.Invoke(this);
}
else
{
if (ToggledOff != null)
ToggledOff.Invoke(this);
}
Redraw();
}
}
///
/// Control constructor.
///
/// Parent control.
public Button(Base parent)
: base(parent)
{
SetSize(100, 20);
MouseInputEnabled = true;
Alignment = Pos.Center;
TextPadding = new Padding(3, 3, 3, 3);
}
///
/// Toggles the button.
///
public virtual void Toggle()
{
ToggleState = !ToggleState;
}
///
/// "Clicks" the button.
///
public virtual void Press(Base control = null)
{
OnClicked();
}
///
/// Renders the control using specified skin.
///
/// Skin to use.
protected override void Render(Skin.Base skin)
{
base.Render(skin);
if (ShouldDrawBackground)
{
bool drawDepressed = IsDepressed && IsHovered;
if (IsToggle)
drawDepressed = drawDepressed || ToggleState;
bool bDrawHovered = IsHovered && ShouldDrawHover;
skin.DrawButton(this, drawDepressed, bDrawHovered, IsDisabled);
}
}
///
/// Handler invoked on mouse click (left) event.
///
/// X coordinate.
/// Y coordinate.
/// If set to true mouse button is down.
protected override void OnMouseClickedLeft(int x, int y, bool down)
{
//base.OnMouseClickedLeft(x, y, down);
if (down)
{
IsDepressed = true;
InputHandler.MouseFocus = this;
if (Pressed != null)
Pressed.Invoke(this);
}
else
{
if (IsHovered && m_Depressed)
{
OnClicked();
}
IsDepressed = false;
InputHandler.MouseFocus = null;
if (Released != null)
Released.Invoke(this);
}
Redraw();
}
///
/// Internal OnPressed implementation.
///
protected virtual void OnClicked()
{
if (IsToggle)
{
Toggle();
}
if (Clicked != null)
Clicked.Invoke(this);
}
///
/// Sets the button's image.
///
/// Texture name. Null to remove.
/// Determines whether the image should be centered.
public virtual void SetImage(String textureName, bool center = false)
{
if (String.IsNullOrEmpty(textureName))
{
if (m_Image != null)
m_Image.Dispose();
m_Image = null;
return;
}
if (m_Image == null)
{
m_Image = new ImagePanel(this);
}
m_Image.ImageName = textureName;
m_Image.SizeToContents( );
m_Image.SetPosition(Math.Max(Padding.Left, 2), 2);
m_CenterImage = center;
TextPadding = new Padding(m_Image.Right + 2, TextPadding.Top, TextPadding.Right, TextPadding.Bottom);
}
///
/// Sizes to contents.
///
public override void SizeToContents()
{
base.SizeToContents();
if (m_Image != null)
{
int height = m_Image.Height + 4;
if (Height < height)
{
Height = height;
}
}
}
///
/// Handler for Space keyboard event.
///
/// Indicates whether the key was pressed or released.
///
/// True if handled.
///
protected override bool OnKeySpace(bool down)
{
if (down)
OnClicked();
return true;
}
///
/// Default accelerator handler.
///
protected override void OnAccelerator()
{
OnClicked();
}
///
/// Lays out the control's interior according to alignment, padding, dock etc.
///
/// Skin to use.
protected override void Layout(Skin.Base skin)
{
base.Layout(skin);
if (m_Image != null)
{
Align.CenterVertically(m_Image);
if (m_CenterImage)
Align.CenterHorizontally(m_Image);
}
}
///
/// Updates control colors.
///
public override void UpdateColors()
{
if (IsDisabled)
{
TextColor = Skin.Colors.Button.Disabled;
return;
}
if (IsDepressed || ToggleState)
{
TextColor = Skin.Colors.Button.Down;
return;
}
if (IsHovered)
{
TextColor = Skin.Colors.Button.Hover;
return;
}
TextColor = Skin.Colors.Button.Normal;
}
///
/// Handler invoked on mouse double click (left) event.
///
/// X coordinate.
/// Y coordinate.
protected override void OnMouseDoubleClickedLeft(int x, int y)
{
OnMouseClickedLeft(x, y, true);
if (DoubleClickedLeft != null)
DoubleClickedLeft.Invoke(this);
}
}
}