using System; using Chernobyl.Collections.Generic.Event; using Chernobyl.Input.Controls; using Microsoft.Xna.Framework.Input; namespace Chernobyl.Input.Xna.Controls.Keyboard { /// /// Represents an XNA keyboard key /// public class KeyboardKey : ButtonControl { /// /// Constructor. /// /// The services class to use when injecting this class with dependencies. /// The XNA key that this keyboard key represents. /// The this /// is a part of. public KeyboardKey(IEventCollection services, Keys key, IKeyboard keyboard) : base(services) { Key = key; Name = Key.ToString(); Keyboard = keyboard; } /// /// True if the button is down, false /// if otherwise. /// public override bool IsDown { get { return Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Key); } protected set { throw new NotImplementedException("You cannot set the state of an XNA button."); } } /// /// An event that is raised when this button /// is pushed down or turned on. /// public override event EventHandler OnDown { add { // ensure we get updated by making ourself the keyboards child. if (this.ControlParent == null) Control.MakeParentChild(Keyboard, this); base.OnDownMethod += value; } remove { base.OnDownMethod -= value; CheckForPotentialParentSeperation(); } } /// /// An event that is raised when this button /// has stopped being pushed down or is off. /// public override event EventHandler OnUp { add { // ensure we get updated by making ourself the keyboards child. if (this.ControlParent == null) Control.MakeParentChild(Keyboard, this); base.OnUpMethod += value; } remove { base.OnUpMethod -= value; CheckForPotentialParentSeperation(); } } /// /// An event that is raised when this button's /// state has changed. /// public override event EventHandler OnChange { add { // ensure we get updated by making ourself the keyboards child. if (this.ControlParent == null) Control.MakeParentChild(Keyboard, this); base.OnChangeMethod += value; } remove { base.OnChangeMethod -= value; CheckForPotentialParentSeperation(); } } /// /// Checks the events to to see if they need updating (i.e. they have /// event handlers assigned to them). If they don't, then this instance /// is separated from its parent to prevent to many keyboard keys from /// having to be updated. /// public void CheckForPotentialParentSeperation() { if(base.OnChangeMethod == null && base.OnUpMethod == null && base.OnDownMethod == null) Control.SeparateParentChild(Keyboard, this); } /// /// The XNA keyboard key that this KeyboardKey represents. /// public Keys Key { get; protected set; } /// /// The this is a part /// of. /// IKeyboard Keyboard { get; set; } } }