using System;
using Chernobyl.Collections.Generic.Event;
using Chernobyl.Graphics.Drawing;
using Chernobyl.Graphics.Material.Shader.Parameters.Semantics;
using Chernobyl.Graphics.Texture;
using Chernobyl.Interface.Input;
using Chernobyl.Interface.Tool;
using Chernobyl.Mathematics.Mechanics;
using Chernobyl.Mathematics.Movement;
namespace Chernobyl.Interface
{
///
/// An interface button that performs an action when it is clicked on.
///
public class Button : Sprite, IButton
{
///
/// Constructor that creates a button that uses a simple sprite for
/// rendering.
///
/// The service holder instance that takes
/// services and gives them out.
/// The texture to use as the button's image.
public Button(IEventCollection services, ITexture texture)
: this(services, texture, null)
{ }
///
/// Constructor that creates a button that uses a simple sprite for
/// rendering.
///
/// The service holder instance that takes
/// services and gives them out.
/// The texture to use as the button's image.
/// The instance that can click on the button or
/// null if the default should be used.
public Button(IEventCollection services, ITexture texture, IPointerControl pointer)
: this(services, texture, pointer, null)
{ }
///
/// Constructor that creates a button that uses a simple sprite for
/// rendering.
///
/// The service holder instance that takes
/// services and gives them out.
/// The texture parameter to use in the draw graph
/// of this sprite. This texture will become the sprites image.
/// The instance that can click on the button or
/// null if the default should be used.
/// The that handles the
/// transformations for the instance created by this
/// or null if the 's default
/// should be used.
public Button(IEventCollection services, ITexture texture, IPointerControl pointer, ITransform transform)
: base(services, new ColorTextureParameter(services, texture), null, null, transform)
{
// give this button some borders
Borders = new Borders(services);
// Make sure this button can be clicked on.
ClickableRegion = new ClickableRegion(services, pointer, this);
ClickableRegion.PressedDown += PressDown;
ClickableRegion.LetUp += LetUp;
}
///
/// The borders on this button. By default, this button is given borders
/// along all it's edges. Note: you do not need to parent this Button
/// with the new Border or un-parent the old border; this property will
/// do that for you.
///
public Borders Borders
{
get { return _borders; }
set
{
if(_borders != null)
Transform.SeperateParentChild(this, _borders);
_borders = value;
Transform.MakeParentChild(this, _borders);
}
}
///
/// The cursor click extension that is used to provide clicking capabilities
/// for this cursor.
///
public ClickableRegion ClickableRegion { get; protected set; }
///
/// True if the button was down, false if otherwise (set to the previous
/// value of IButton.IsDown).
///
public bool WasDown { get; private set; }
///
/// True if the button is down, false if otherwise.
///
public bool IsDown
{
get { return _isDown; }
protected set
{
if(_isDown == true)
{
if (value == false)
{
if(OnUp != null)
OnUp(this, EventArgs.Empty);
if (OnChange != null)
OnChange(this, EventArgs.Empty);
}
}
else
{
if (value == true)
{
if (OnDown != null)
OnDown(this, EventArgs.Empty);
if (OnChange != null)
OnChange(this, EventArgs.Empty);
}
}
WasDown = _isDown;
_isDown = value;
}
}
///
/// An event handler that can be attached to an event. This event handler
/// will press this button down if it is already up. Note that this
/// method will not let the button back up - it will stay down.
///
/// The sender of the event.
/// The events arguments.
public void PressDown(object sender, EventArgs e)
{
IsDown = true;
}
///
/// An event handler that can be attached to an event. This event handler
/// will let the button up if it is already down. Note that this method
/// will not push the button back down - it will stay up.
///
/// The sender of the event.
/// The events arguments.
public void LetUp(object sender, EventArgs e)
{
IsDown = false;
}
///
/// An event handler that can be attached to an event. This event handler
/// will press toggle the buttons current state. If the button is down
/// the button will be let up, if it is up the button will be pressed
/// down.
///
/// The sender of the event.
/// The events arguments.
public void Toggle(object sender, EventArgs e)
{
IsDown = !IsDown;
}
///
/// An event that is raised when this button is pushed down or turned
/// on.
///
public event EventHandler OnDown;
///
/// An event that is raised when this button has stopped being pushed
/// down or is off.
///
public event EventHandler OnUp;
///
/// This event is fired when a state has just been left and right before
/// a state has been entered. You should only attach to this event if
/// you want to know about every state change. If you want to know about
/// individual state changes (as in, when particular states are entered
/// or left) then attach to the StateChange event of that particular
/// state. This event should only be invoked when the current state
/// is not equal to the state being changed to.
///
public event EventHandler OnChange;
///
/// The backing field to .
///
Borders _borders;
///
/// The backing field to .
///
bool _isDown;
}
}