/*
* Util.InputEvent.cs
* Authors: Adam Nabinger
* Copyright (c) 2007-2008 Cornell University
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
using System.Globalization;
using Microsoft.Xna.Framework.Input;
namespace Util
{
///
/// An item of input that was collected for processing.
///
public class InputEvent
{
#region Fields
// Whether or not the event has been logged
private bool logged;
// The Event Code that triggered this event.
private readonly int eventCode;
// The timestamp at the moment this event was triggered, in CPU Ticks.
private readonly long timestamp;
// The time at the moment this event was triggered.
private readonly uint time;
// The trigger that caused this event.
private readonly Trigger trigger;
// Whether the event has been handled or not
private bool handled;
// Any event arguments that have been carried by the event
private object mEventArgs;
#endregion
#region Properties
///
/// Gets the Event Code that triggered this event.
///
public int EventCode { get { return eventCode; } }
///
/// Gets the timestamp at the moment this event was triggered, in CPU Ticks.
///
public long Timestamp { get { return timestamp; } }
///
/// Gets the time at the moment this event was triggered.
///
public uint Time { get { return time; } }
///
/// Gets the trigger that caused this event.
///
public Trigger Trigger { get { return trigger; } }
///
/// Gets whether the event has been handled or not.
///
public bool Handled { get { return handled; } set { handled = value; } }
///
/// Has the event been logged.
///
public bool Logged { get { return logged; } }
///
/// Gets or sets the arguments for the event. These can be set either
/// before the event is handled or afterwards for feedback.
///
public object EventArgs
{
get { return mEventArgs; }
set { mEventArgs = value; }
}
#endregion
#region Creation
///
/// Constructor
///
/// The associated event code number
/// The time stamp for when the event occured
/// The current time
/// What caused the event
internal InputEvent(int code, long tStamp, uint mt, Trigger t)
{
eventCode = code;
timestamp = tStamp;
time = mt;
trigger = t;
}
#endregion
#region WriteToLog
internal void WriteToLog(Logger log)
{
if (logged)
{
return;
}
logged = true;
log.Write(this);
}
///
/// Write this Util.InputEvent out to the given Util.Logger.
/// This function will return immediately without waiting for the IO to finish.
///
/// The logger used for recording the various events
/// The associated information for the event
public void WriteToLog(Logger log, string info)
{
if (logged)
{
return;
}
logged = true;
log.Write(this, info);
}
///
/// Write this Util.InputEvent out to the given Util.Logger.
/// This function will return immediately without waiting for the IO to finish.
///
/// The logger used for recording the various events
/// The type of enum to use for the event
public void WriteToLog(Logger log, System.Type enumType)
{
if (logged)
{
return;
}
logged = true;
log.Write(this, System.Enum.ToObject(enumType, EventCode).ToString());
}
#endregion
}
///
/// An input event that was triggered by the keyboard.
///
public sealed class KeyInputEvent : InputEvent
{
#region
// The Key that triggered this event.
private readonly Keys key;
// Whether shift/caps lock was active or not.
// Inputhandler must be set to record this information, otherwise it will always be false.
private readonly bool capital;
#endregion
#region Properties
///
/// Gets the Key that triggered this event.
///
public Keys Key { get { return key; } }
///
/// Gets whether shift/caps lock was active or not.
/// Inputhandler must be set to record this information, otherwise it will always be false.
///
public bool Capital { get { return capital; } }
#endregion
#region Creation
///
/// Constructor
///
/// The associated event code number
/// The time stamp for when the event occured
/// The current time
/// What caused the event
/// The specific key that has been pressed
internal KeyInputEvent(int code, long time, uint mt, Trigger t, Keys k)
: base(code, time, mt, t)
{
key = k;
}
///
/// Constructor
///
/// The associated event code number
/// The time stamp for when the event occured
/// The current time
/// What caused the event
/// The specific key that has been pressed
/// Whether or not capital is on
internal KeyInputEvent(int code, long time, uint mt, Trigger t, Keys k, bool cap)
: base(code, time, mt, t)
{
key = k;
capital = cap;
}
#endregion
#region Converters
///
/// Convert the key to a string.
///
/// A string representation of the key
public override string ToString()
{
return Capital ? Key.ToString().ToUpper(CultureInfo.CurrentCulture) : Key.ToString().ToLower(CultureInfo.CurrentCulture);
}
///
/// Return the key that triggered this event as a char.
///
/// The single key character
public char ToChar()
{
return (char)Key;
}
///
/// If this Util.InputEvent was as alphanumeric key, returns a string containing that character.
/// Else, returns string.Empty;
///
/// 0-9|a-z|A-Z
public string ToAlphanumericString()
{
if ((int)Key >= 65 && (int)Key <= 106)
{
return Capital ? ((char)Key).ToString() : ((char)((int)Key + 32)).ToString();
}
if ((int)Key >= 48 && (int)Key <= 57)
{
return ((char)Key).ToString();
}
return string.Empty;
}
///
/// Convert the key pressed to a letter, number, or symbol, if recognised.
/// TODO: Cleanup
///
/// The converter character
public string ToCodeString()
{
if ((int)Key >= 65 && (int)Key <= 106)
{
return Capital ? ((char)Key).ToString() : ((char)((int)Key + 32)).ToString();
}
if ((int)Key >= 48 && (int)Key <= 57)
{
if (!Capital)
{
return ((char)Key).ToString();
}
if (Key == Keys.D0)
{
return ")";
}
if (Key == Keys.D1)
{
return "!";
}
if (Key == Keys.D2)
{
return "@";
}
if (Key == Keys.D3)
{
return "#";
}
if (Key == Keys.D4)
{
return "$";
}
if (Key == Keys.D5)
{
return "%";
}
if (Key == Keys.D6)
{
return "^";
}
if (Key == Keys.D7)
{
return "&";
}
if (Key == Keys.D8)
{
return "*";
}
if (Key == Keys.D9)
{
return "(";
}
}
else switch (Key)
{
case Keys.Space:
return " ";
case Keys.OemSemicolon:
return Capital ? ":" : ";";
case Keys.OemQuotes:
return Capital ? "\"" : "'";
case Keys.OemComma:
return Capital ? "<" : ",";
case Keys.OemOpenBrackets:
return Capital ? "{" : "[";
case Keys.OemCloseBrackets:
return Capital ? "}" : "]";
case Keys.OemPeriod:
return Capital ? ">" : ".";
case Keys.OemPipe:
return Capital ? "|" : @"\";
case Keys.OemPlus:
return Capital ? "+" : "=";
case Keys.OemMinus:
return Capital ? "_" : "-";
case Keys.Multiply:
return "*";
case Keys.Divide:
return "/";
case Keys.OemQuestion:
return Capital ? "?" : "/";
}
return string.Empty;
}
#endregion
}
///
/// An input event that was triggered from the mouse.
///
public sealed class MouseInputEvent : InputEvent
{
#region Fields
// The distance, in pixels, the mouse was from the left edge of the window.
private readonly int x;
// The distance, in pixels, the mouse was from the top edge of the window.
private readonly int y;
#endregion
#region Properties
///
/// Gets the distance, in pixels, the mouse was from the left edge of the window.
///
public int X { get { return x; } }
///
/// Gets the distance, in pixels, the mouse was from the top edge of the window.
///
public int Y { get { return y; } }
///
/// Gets the position of the mouse at the moment this input event fired, relative to the top-left corner of the window.
///
public Microsoft.Xna.Framework.Point Location { get { return new Microsoft.Xna.Framework.Point(X, Y); } }
#endregion
#region Creation
///
/// Constructor
///
/// The associated event code number
/// The time stamp for when the event occured
/// The current time
/// What caused the event
/// The position of the mouse from the left side of the screen
/// The position of the mouse from the top of the screen
internal MouseInputEvent(int code, long time, uint mt, Trigger t, int xLoc, int yLoc)
: base(code, time, mt, t)
{
x = xLoc;
y = yLoc;
}
#endregion
}
}