#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;
List axis = new List();
List button = new List();
IList axis_readonly;
IList button_readonly;
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.AddRange(new float[axes]);
button.AddRange(new bool[buttons]);
axis_readonly = axis.AsReadOnly();
button_readonly = button.AsReadOnly();
}
#endregion
#region Public Members
///
/// Gets a readonly IList containing the state of each axis on this instance. Values are normalized in the [-1, 1] range.
///
public IList Axis { get { return axis_readonly; } }
///
/// Gets a readonly IList containing the state of each button on this instance. True indicates that the button is pressed.
///
public IList Button { get { return button_readonly; } }
#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(int num, float @value)
{
move_args.Index = num;
move_args.Delta = move_args.Value - @value;
axis[num] = move_args.Value = @value;
Move(this, move_args);
}
internal void SetButton(int num, bool @value)
{
if (button[num] != @value)
{
button_args.Button = num;
button[num] = 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
{
TDetail details;
internal JoystickDevice(int id, int axes, int buttons)
: base(id, axes, buttons)
{ }
internal TDetail Details { get { return details; } set { details = value; } }
}
#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
int 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(int button, bool pressed)
{
this.button = button;
this.pressed = pressed;
}
#endregion
#region Public Members
///
/// The index of the joystick button for the event.
///
public int 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
int index;
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(int index, float value, float delta)
{
this.index = index;
this.value = value;
this.delta = delta;
}
#endregion
#region Public Members
///
/// Gets a System.Int32 representing the index of the axis that was moved.
///
public int Index { get { return index; } internal set { this.index = 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 = delta; } }
#endregion
}
#endregion
}