using System;
using System.Text;
using Chernobyl.Collections.Generic.Event;
using Chernobyl.Event;
using Chernobyl.Input.Controls;
using Microsoft.Xna.Framework.Input;
namespace Chernobyl.Input.Xna.Controls.Keyboard
{
///
/// Represents an XNA keyboard
///
public class XnaKeyboard : Control, IKeyboard
{
///
/// Constructor
///
/// The object to use to inject dependencies
/// into this instance.
public XnaKeyboard(IEventCollection services)
{
Services = services;
if(WKI == null)
WKI = new WindowsKeyboardInput(services);
EnteredText = new StringBuilder();
WKI.CharEntered += OnTextEntered;
}
///
/// Gets the with the specified key name.
///
public IButtonControl this[string keyName]
{
get
{
object obj = Enum.Parse(typeof(Keys), keyName);
if (obj == null)
return null;
return new KeyboardKey(Services, (Keys)obj, this);
}
}
///
/// An event that is raised when text has been "entered" or typed on
/// the keyboard.
///
public event EventHandler> TextEntered;
///
/// Updates this keyboard. Grabs the recently pressed keys and
///
/// The amount of time that has
/// passed since this update was last called.
public override void Update(TimeSpan deltaTime)
{
if(EnteredText.Length != 0 && TextEntered != null)
{
TextEntered(this, new ItemsEventArgs(EnteredText.ToString().ToCharArray()));
EnteredText.Remove(0, EnteredText.Length);
}
base.Update(deltaTime);
}
#region Keys
///
/// The backspace key or null if it does not exist on the keyboard.
///
public IButtonControl Backspace { get { return GetKey(ref _Backspace, Keys.Back); } }
///
/// The delete key or null if it does not exist on the keyboard.
///
public IButtonControl Delete { get { return GetKey(ref _Delete, Keys.Delete); } }
///
/// The down arrow or null if it does not exist on the keyboard.
///
public IButtonControl Down { get { return GetKey(ref _Down, Keys.Down); } }
///
/// The End key or null if it does not exist on the keyboard.
///
public IButtonControl End { get { return GetKey(ref _End, Keys.End); } }
///
/// The Home key or null if it does not exist on the keyboard.
///
public IButtonControl Home { get { return GetKey(ref _Home, Keys.Home); } }
///
/// The up arrow or null if it does not exist on the keyboard.
///
public IButtonControl Up { get { return GetKey(ref _Up, Keys.Up); } }
///
/// The up arrow or null if it does not exist on the keyboard.
///
public IButtonControl Left { get { return GetKey(ref _Left, Keys.Left); } }
///
/// The right arrow or null if it does not exist on the keyboard.
///
public IButtonControl Right { get { return GetKey(ref _Right, Keys.Right); } }
#endregion // Keys
///
/// Due to the fact that the engineers at Microsoft are spastic retards
/// who got their degrees by attending English classes, XNA cannot
/// handle buffered input. Therefore, this event handler is provided to
/// allow "third party" applications to inform this class when text has
/// been entered.
///
public void OnTextEntered(object sender, ItemsEventArgs e)
{
EnteredText.Append(e.Items);
}
///
/// The that receives input from the
/// method.
///
StringBuilder EnteredText { get; set; }
///
/// Gets the specified .
///
/// The that should be assigned
/// to.
/// The XNA key enum that matches the key you want
/// to get.
/// The key requested
IButtonControl GetKey(ref IButtonControl key, Keys xnaKey)
{
if (key == null)
key = new KeyboardKey(Services, xnaKey, this);
return key;
}
///
/// Checks the content of two arrays for equality.
///
/// The first array.
/// The second array.
/// True if the arrays contain the same objects, false if
/// otherwise.
bool Equals(char[] values1, char[] values2)
{
if (values1 == values2)
return true;
if (values1 == null || values2 == null)
return false;
if (values1.Length != values2.Length)
return false;
for (int i = 0; i < values1.Length; ++i)
{
if (values1[i] != values2[i])
return false;
}
return true;
}
///
/// The object to use to inject dependencies into this instance.
///
IEventCollection Services { get; set; }
///
/// The class that injects Window's keyboard input into Chernobyl.
///
static WindowsKeyboardInput WKI { get; set; }
#region Keyboard Key Backing Fields
///
/// The backing field to .
///
IButtonControl _Backspace;
///
/// The backing field to
///
IButtonControl _Delete;
///
/// The backing field to .
///
IButtonControl _Down;
///
/// The backing field to .
///
IButtonControl _End;
///
/// The backing field to .
///
IButtonControl _Home;
///
/// The backing field to .
///
IButtonControl _Up;
///
/// The backing field to .
///
IButtonControl _Left;
///
/// The backing field to .
///
IButtonControl _Right;
#endregion
}
}