using System; using System.Collections.Generic; using Chernobyl.Collections.Generic.Event; using Chernobyl.Event; using Chernobyl.Mathematics.Mechanics; namespace Chernobyl.Input.Controls { /// /// Makes creating new IButton classes easier by implementing /// some of the properties and methods and making allowing for /// the button to be (de)serialized to/from an XML file. Do NOT /// work with the class "ButtonControl" (like use it as a method parameter) /// because not every button will derive from "ButtonControl". Instead, /// work with "IButton". /// public abstract class ButtonControl : Control, IButtonControl { /// /// Initializes a new instance of the class. /// /// The instance that gives out services for use /// by this type and takes services from this type for use by other systems. protected ButtonControl(IEventCollection services) : this(services, new List()) { } /// /// Constructor. /// /// The instance that gives out services for use /// by this type and takes services from this type for use by other systems. /// The list to use to hold the children controls. protected ButtonControl(IEventCollection services, ICollection childList) : base(childList) { } /// /// Checks to see if a buttons state has been changed and fires the /// necessary events if so. Also calls update on the children buttons. /// /// The amount of time that has passed /// since the last call to update. public override void Update(TimeSpan deltaTime) { // update the previous state bool previousState = WasDown; WasDown = IsDown; // check if the new button state has changed if (IsDown != previousState) { // yes it has EventArgs e = new EventArgs(this); if (OnChangeMethod != null) OnChangeMethod(this, EventArgs.Empty); if (IsDown == true && OnDownMethod != null) OnDownMethod(this, EventArgs.Empty); else if (OnUpMethod != null) OnUpMethod(this, e); } base.Update(deltaTime); } /// /// True if the button was down, false /// if otherwise (set to the previous /// value of "IsDown"). /// public virtual bool WasDown { get; protected set; } /// /// True if the button is down, false /// if otherwise. /// public abstract bool IsDown { get; protected set; } /// /// An event that is raised when this button /// is pushed down or turned on. /// public virtual event EventHandler OnDown { add { OnDownMethod += value; } remove { OnDownMethod -= value; } } /// /// An event that is raised when this button /// has stopped being pushed down or is off. /// public virtual event EventHandler OnUp { add { OnUpMethod += value; } remove { OnUpMethod -= value; } } /// /// An event that is raised when this button's /// state has changed. /// public virtual event EventHandler OnChange { add { OnChangeMethod += value; } remove { OnChangeMethod -= value; } } /// /// Allows derived classes access to the event. /// protected EventHandler OnDownMethod { get; set; } /// /// Allows derived classes access to the event. /// protected EventHandler OnUpMethod { get; set; } /// /// Allows derived classes access to the event. /// protected EventHandler OnChangeMethod { get; set; } } }