#region License // // The Open Toolkit Library License // // Copyright (c) 2006 - 2008 the Open Toolkit library, except where noted. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights to // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of // the Software, and to permit persons to whom the Software is furnished to do // so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // #endregion using System; using System.Collections.Generic; namespace OpenTK.Input { /// /// Represents a joystick device and provides methods to query its status. /// public abstract class JoystickDevice : IInputDevice { #region Fields int id; string description; JoystickAxisCollection axis_collection; JoystickButtonCollection button_collection; JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0); JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false); #endregion #region Constructors internal JoystickDevice(int id, int axes, int buttons) { if (axes < 0) throw new ArgumentOutOfRangeException("axes"); if (buttons < 0) throw new ArgumentOutOfRangeException("buttons"); Id = id; axis_collection = new JoystickAxisCollection(axes); button_collection = new JoystickButtonCollection(buttons); } #endregion #region Public Members /// /// Gets a JoystickAxisCollection containing the state of each axis on this instance. Values are normalized in the [-1, 1] range. /// public JoystickAxisCollection Axis { get { return axis_collection; } } /// /// Gets JoystickButtonCollection containing the state of each button on this instance. True indicates that the button is pressed. /// public JoystickButtonCollection Button { get { return button_collection; } } #endregion #region IInputDevice Members /// /// Gets a System.String containing a unique description for this instance. /// public string Description { get { return description; } internal set { description = value; } } /// /// Gets a value indicating the InputDeviceType of this InputDevice. /// public InputDeviceType DeviceType { get { return InputDeviceType.Hid; } } #endregion #region Events /// /// Occurs when an axis of this JoystickDevice instance is moved. /// public EventHandler Move = delegate(object sender, JoystickMoveEventArgs e) { }; /// /// Occurs when a button of this JoystickDevice instance is pressed. /// public EventHandler ButtonDown = delegate(object sender, JoystickButtonEventArgs e) { }; /// /// Occurs when a button of this JoystickDevice is released. /// public EventHandler ButtonUp = delegate(object sender, JoystickButtonEventArgs e) { }; #endregion #region Internal Members internal int Id { get { return id; } set { id = value; } } internal void SetAxis(JoystickAxis axis, float @value) { move_args.Axis = axis; move_args.Delta = move_args.Value - @value; axis_collection[axis] = move_args.Value = @value; Move(this, move_args); } internal void SetButton(JoystickButton button, bool @value) { if (button_collection[button] != @value) { button_args.Button = button; button_collection[button] = button_args.Pressed = @value; if (@value) ButtonDown(this, button_args); else ButtonUp(this, button_args); } } #endregion } #region JoystickDevice : JoystickDevice // Provides platform-specific information about the relevant JoystickDevice. internal sealed class JoystickDevice : JoystickDevice where TDetail : new() { internal JoystickDevice(int id, int axes, int buttons) : base(id, axes, buttons) { } internal TDetail Details = new TDetail(); } #endregion #region Event Arguments /// /// The base class for JoystickDevice event arguments. /// public class JoystickEventArgs : EventArgs { } /// /// Provides data for the and events. /// This class is cached for performance reasons - avoid storing references outside the scope of the event. /// public class JoystickButtonEventArgs : EventArgs { #region Fields JoystickButton button; bool pressed; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The index of the joystick button for the event. /// The current state of the button. internal JoystickButtonEventArgs(JoystickButton button, bool pressed) { this.button = button; this.pressed = pressed; } #endregion #region Public Members /// /// The index of the joystick button for the event. /// public JoystickButton Button { get { return this.button; } internal set { this.button = value; } } /// /// Gets a System.Boolean representing the state of the button for the event. /// public bool Pressed { get { return pressed; } internal set { this.pressed = value; } } #endregion } /// /// Provides data for the event. /// This class is cached for performance reasons - avoid storing references outside the scope of the event. /// public class JoystickMoveEventArgs : JoystickEventArgs { #region Fields JoystickAxis axis; float value; float delta; #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// The index of the joystick axis that was moved. /// The absolute value of the joystick axis. /// The relative change in value of the joystick axis. public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta) { this.axis = axis; this.value = value; this.delta = delta; } #endregion #region Public Members /// /// Gets a System.Int32 representing the index of the axis that was moved. /// public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } } /// /// Gets a System.Single representing the absolute position of the axis. /// public float Value { get { return value; } internal set { this.value = value; } } /// /// Gets a System.Single representing the relative change in the position of the axis. /// public float Delta { get { return delta; } internal set { this.delta = value; } } #endregion } #endregion #region JoystickButtonCollection /// /// Defines a collection of JoystickButtons. /// public sealed class JoystickButtonCollection { #region Fields bool[] button_state; #endregion #region Constructors internal JoystickButtonCollection(int numButtons) { if (numButtons < 0) throw new ArgumentOutOfRangeException("numButtons"); button_state = new bool[numButtons]; } #endregion #region Public Members /// /// Gets a System.Boolean indicating whether the JoystickButton with the specified index is pressed. /// /// The index of the JoystickButton to check. /// True if the JoystickButton is pressed; false otherwise. public bool this[int index] { get { return button_state[index]; } internal set { button_state[index] = value; } } /// /// Gets a System.Boolean indicating whether the specified JoystickButton is pressed. /// /// The JoystickButton to check. /// True if the JoystickButton is pressed; false otherwise. public bool this[JoystickButton button] { get { return button_state[(int)button]; } internal set { button_state[(int)button] = value; } } /// /// Gets a System.Int32 indicating the available amount of JoystickButtons. /// public int Count { get { return button_state.Length; } } #endregion } #endregion #region JoystickAxisCollection /// /// Defines a collection of JoystickAxes. /// public sealed class JoystickAxisCollection { #region Fields float[] axis_state; #endregion #region Constructors internal JoystickAxisCollection(int numAxes) { if (numAxes < 0) throw new ArgumentOutOfRangeException("numAxes"); axis_state = new float[numAxes]; } #endregion #region Public Members /// /// Gets a System.Single indicating the absolute position of the JoystickAxis with the specified index. /// /// The index of the JoystickAxis to check. /// A System.Single in the range [-1, 1]. public float this[int index] { get { return axis_state[index]; } internal set { axis_state[index] = value; } } /// /// Gets a System.Single indicating the absolute position of the JoystickAxis. /// /// The JoystickAxis to check. /// A System.Single in the range [-1, 1]. public float this[JoystickAxis axis] { get { return axis_state[(int)axis]; } internal set { axis_state[(int)axis] = value; } } /// /// Gets a System.Int32 indicating the available amount of JoystickAxes. /// public int Count { get { return axis_state.Length; } } #endregion } #endregion }