Added JoystickAxis and JoystickButton enumerations, as well as their respective collections.

Implemented necessary Poll() functions.
This commit is contained in:
the_fiddler 2009-03-01 01:17:24 +00:00
parent 0dca74a232
commit f6f196c90b
5 changed files with 243 additions and 52 deletions

View file

@ -39,10 +39,8 @@ namespace OpenTK.Input
int id; int id;
string description; string description;
List<float> axis = new List<float>(); JoystickAxisCollection axis_collection;
List<bool> button = new List<bool>(); JoystickButtonCollection button_collection;
IList<float> axis_readonly;
IList<bool> button_readonly;
JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0); JoystickMoveEventArgs move_args = new JoystickMoveEventArgs(0, 0, 0);
JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false); JoystickButtonEventArgs button_args = new JoystickButtonEventArgs(0, false);
@ -59,11 +57,8 @@ namespace OpenTK.Input
throw new ArgumentOutOfRangeException("buttons"); throw new ArgumentOutOfRangeException("buttons");
Id = id; Id = id;
axis.AddRange(new float[axes]); axis_collection = new JoystickAxisCollection(axes);
button.AddRange(new bool[buttons]); button_collection = new JoystickButtonCollection(buttons);
axis_readonly = axis.AsReadOnly();
button_readonly = button.AsReadOnly();
} }
#endregion #endregion
@ -71,14 +66,14 @@ namespace OpenTK.Input
#region Public Members #region Public Members
/// <summary> /// <summary>
/// Gets a readonly IList containing the state of each axis on this instance. Values are normalized in the [-1, 1] range. /// Gets a JoystickAxisCollection containing the state of each axis on this instance. Values are normalized in the [-1, 1] range.
/// </summary> /// </summary>
public IList<float> Axis { get { return axis_readonly; } } public JoystickAxisCollection Axis { get { return axis_collection; } }
/// <summary> /// <summary>
/// Gets a readonly IList containing the state of each button on this instance. True indicates that the button is pressed. /// Gets JoystickButtonCollection containing the state of each button on this instance. True indicates that the button is pressed.
/// </summary> /// </summary>
public IList<bool> Button { get { return button_readonly; } } public JoystickButtonCollection Button { get { return button_collection; } }
#endregion #endregion
@ -133,20 +128,20 @@ namespace OpenTK.Input
set { id = value; } set { id = value; }
} }
internal void SetAxis(int num, float @value) internal void SetAxis(JoystickAxis axis, float @value)
{ {
move_args.Index = num; move_args.Axis = axis;
move_args.Delta = move_args.Value - @value; move_args.Delta = move_args.Value - @value;
axis[num] = move_args.Value = @value; axis_collection[axis] = move_args.Value = @value;
Move(this, move_args); Move(this, move_args);
} }
internal void SetButton(int num, bool @value) internal void SetButton(JoystickButton button, bool @value)
{ {
if (button[num] != @value) if (button_collection[button] != @value)
{ {
button_args.Button = num; button_args.Button = button;
button[num] = button_args.Pressed = @value; button_collection[button] = button_args.Pressed = @value;
if (@value) if (@value)
ButtonDown(this, button_args); ButtonDown(this, button_args);
else else
@ -190,7 +185,7 @@ namespace OpenTK.Input
{ {
#region Fields #region Fields
int button; JoystickButton button;
bool pressed; bool pressed;
#endregion #endregion
@ -202,7 +197,7 @@ namespace OpenTK.Input
/// </summary> /// </summary>
/// <param name="button">The index of the joystick button for the event.</param> /// <param name="button">The index of the joystick button for the event.</param>
/// <param name="pressed">The current state of the button.</param> /// <param name="pressed">The current state of the button.</param>
internal JoystickButtonEventArgs(int button, bool pressed) internal JoystickButtonEventArgs(JoystickButton button, bool pressed)
{ {
this.button = button; this.button = button;
this.pressed = pressed; this.pressed = pressed;
@ -215,7 +210,7 @@ namespace OpenTK.Input
/// <summary> /// <summary>
/// The index of the joystick button for the event. /// The index of the joystick button for the event.
/// </summary> /// </summary>
public int Button { get { return this.button; } internal set { this.button = value; } } public JoystickButton Button { get { return this.button; } internal set { this.button = value; } }
/// <summary> /// <summary>
/// Gets a System.Boolean representing the state of the button for the event. /// Gets a System.Boolean representing the state of the button for the event.
@ -233,7 +228,7 @@ namespace OpenTK.Input
{ {
#region Fields #region Fields
int index; JoystickAxis axis;
float value; float value;
float delta; float delta;
@ -247,9 +242,9 @@ namespace OpenTK.Input
/// <param name="index">The index of the joystick axis that was moved.</param> /// <param name="index">The index of the joystick axis that was moved.</param>
/// <param name="value">The absolute value of the joystick axis.</param> /// <param name="value">The absolute value of the joystick axis.</param>
/// <param name="delta">The relative change in value of the joystick axis.</param> /// <param name="delta">The relative change in value of the joystick axis.</param>
public JoystickMoveEventArgs(int index, float value, float delta) public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta)
{ {
this.index = index; this.axis = axis;
this.value = value; this.value = value;
this.delta = delta; this.delta = delta;
} }
@ -261,7 +256,7 @@ namespace OpenTK.Input
/// <summary> /// <summary>
/// Gets a System.Int32 representing the index of the axis that was moved. /// Gets a System.Int32 representing the index of the axis that was moved.
/// </summary> /// </summary>
public int Index { get { return index; } internal set { this.index = value; } } public JoystickAxis Axis { get { return axis; } internal set { this.axis = value; } }
/// <summary> /// <summary>
/// Gets a System.Single representing the absolute position of the axis. /// Gets a System.Single representing the absolute position of the axis.
@ -271,7 +266,205 @@ namespace OpenTK.Input
/// <summary> /// <summary>
/// Gets a System.Single representing the relative change in the position of the axis. /// Gets a System.Single representing the relative change in the position of the axis.
/// </summary> /// </summary>
public float Delta { get { return delta; } internal set { this.delta = delta; } } public float Delta { get { return delta; } internal set { this.delta = value; } }
#endregion
}
#endregion
#region JoystickButton
/// <summary>
/// Defines available JoystickDevice buttons.
/// </summary>
public enum JoystickButton
{
/// <summary>The first button of the JoystickDevice.</summary>
Button0 = 0,
/// <summary>The second button of the JoystickDevice.</summary>
Button1,
/// <summary>The third button of the JoystickDevice.</summary>
Button2,
/// <summary>The fourth button of the JoystickDevice.</summary>
Button3,
/// <summary>The fifth button of the JoystickDevice.</summary>
Button4,
/// <summary>The sixth button of the JoystickDevice.</summary>
Button5,
/// <summary>The seventh button of the JoystickDevice.</summary>
Button6,
/// <summary>The eighth button of the JoystickDevice.</summary>
Button7,
/// <summary>The ninth button of the JoystickDevice.</summary>
Button8,
/// <summary>The tenth button of the JoystickDevice.</summary>
Button9,
/// <summary>The eleventh button of the JoystickDevice.</summary>
Button10,
/// <summary>The twelfth button of the JoystickDevice.</summary>
Button11,
/// <summary>The thirteenth button of the JoystickDevice.</summary>
Button12,
/// <summary>The fourteenth button of the JoystickDevice.</summary>
Button13,
/// <summary>The fifteenth button of the JoystickDevice.</summary>
Button14,
/// <summary>The sixteenth button of the JoystickDevice.</summary>
Button15,
}
#endregion
#region JoystickButtonCollection
/// <summary>
/// Defines a collection of JoystickButtons.
/// </summary>
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
/// <summary>
/// Gets a System.Boolean indicating whether the JoystickButton with the specified index is pressed.
/// </summary>
/// <param name="index">The index of the JoystickButton to check.</param>
/// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
public bool this[int index]
{
get { return button_state[index]; }
internal set { button_state[index] = value; }
}
/// <summary>
/// Gets a System.Boolean indicating whether the specified JoystickButton is pressed.
/// </summary>
/// <param name="index">The JoystickButton to check.</param>
/// <returns>True if the JoystickButton is pressed; false otherwise.</returns>
public bool this[JoystickButton button]
{
get { return button_state[(int)button]; }
internal set { button_state[(int)button] = value; }
}
/// <summary>
/// Gets a System.Int32 indicating the available amount of JoystickButtons.
/// </summary>
public int Count
{
get { return button_state.Length; }
}
#endregion
}
#endregion
#region JoystickAxis
/// <summary>
/// Defines available JoystickDevice axes.
/// </summary>
public enum JoystickAxis
{
/// <summary>The first axis of the JoystickDevice.</summary>
Axis0 = 0,
/// <summary>The second axis of the JoystickDevice.</summary>
Axis1,
/// <summary>The third axis of the JoystickDevice.</summary>
Axis2,
/// <summary>The fourth axis of the JoystickDevice.</summary>
Axis3,
/// <summary>The fifth axis of the JoystickDevice.</summary>
Axis4,
/// <summary>The sixth axis of the JoystickDevice.</summary>
Axis5,
/// <summary>The seventh axis of the JoystickDevice.</summary>
Axis6,
/// <summary>The eighth axis of the JoystickDevice.</summary>
Axis7,
/// <summary>The ninth axis of the JoystickDevice.</summary>
Axis8,
/// <summary>The tenth axis of the JoystickDevice.</summary>
Axis9,
}
#endregion
#region JoystickAxisCollection
/// <summary>
/// Defines a collection of JoystickAxes.
/// </summary>
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
/// <summary>
/// Gets a System.Single indicating the absolute position of the JoystickAxis with the specified index.
/// </summary>
/// <param name="index">The index of the JoystickAxis to check.</param>
/// <returns>A System.Single in the range [-1, 1].</returns>
public float this[int index]
{
get { return axis_state[index]; }
internal set { axis_state[index] = value; }
}
/// <summary>
/// Gets a System.Single indicating the absolute position of the JoystickAxis.
/// </summary>
/// <param name="index">The JoystickAxis to check.</param>
/// <returns>A System.Single in the range [-1, 1].</returns>
public float this[JoystickAxis axis]
{
get { return axis_state[(int)axis]; }
internal set { axis_state[(int)axis] = value; }
}
/// <summary>
/// Gets a System.Int32 indicating the available amount of JoystickAxes.
/// </summary>
public int Count
{
get { return axis_state.Length; }
}
#endregion #endregion
} }

View file

@ -233,7 +233,7 @@ namespace OpenTK.Platform.Windows
public void Poll() public void Poll()
{ {
// No polling needed. joystick_driver.Poll();
} }
#endregion #endregion

View file

@ -55,9 +55,12 @@ namespace OpenTK.Platform.Windows
{ {
JoystickDevice<WinMMJoyDetails> stick = OpenJoystick(number++); JoystickDevice<WinMMJoyDetails> stick = OpenJoystick(number++);
if (stick != null) if (stick != null)
{
stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count);
sticks.Add(stick); sticks.Add(stick);
} }
} }
}
#endregion #endregion
@ -116,22 +119,22 @@ namespace OpenTK.Platform.Windows
int axis = 0; int axis = 0;
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.XPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.XPos, axis)); axis++; }
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.YPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.YPos, axis)); axis++; }
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.ZPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.ZPos, axis)); axis++; }
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.RPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.RPos, axis)); axis++; }
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.UPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.UPos, axis)); axis++; }
if (axis < js.Axis.Count) if (axis < js.Axis.Count)
{ js.SetAxis(axis, js.Details.CalculateOffset((float)info.VPos, axis)); axis++; } { js.SetAxis((JoystickAxis)axis, js.Details.CalculateOffset((float)info.VPos, axis)); axis++; }
int button = 0; int button = 0;
while (button < js.Button.Count) while (button < js.Button.Count)
{ {
js.SetButton(button, (info.Buttons & (1 << button)) != 0); js.SetButton((JoystickButton)button, (info.Buttons & (1 << button)) != 0);
button++; button++;
} }
} }

View file

@ -20,6 +20,7 @@ namespace OpenTK.Platform.X11
/// </summary> /// </summary>
internal sealed class X11Input : IInputDriver internal sealed class X11Input : IInputDriver
{ {
X11Joystick joystick_driver = new X11Joystick();
//X11WindowInfo window; //X11WindowInfo window;
KeyboardDevice keyboard = new KeyboardDevice(); KeyboardDevice keyboard = new KeyboardDevice();
MouseDevice mouse = new MouseDevice(); MouseDevice mouse = new MouseDevice();
@ -199,15 +200,6 @@ namespace OpenTK.Platform.X11
#region --- IInputDriver Members --- #region --- IInputDriver Members ---
#region public IList<IInputDevice> InputDevices
public IList<IInputDevice> InputDevices
{
get { throw new Exception("The method or operation is not implemented."); }
}
#endregion
#region public IList<Keyboard> Keyboard #region public IList<Keyboard> Keyboard
public IList<KeyboardDevice> Keyboard public IList<KeyboardDevice> Keyboard
@ -230,7 +222,7 @@ namespace OpenTK.Platform.X11
public IList<JoystickDevice> Joysticks public IList<JoystickDevice> Joysticks
{ {
get { throw new NotImplementedException(); } get { return joystick_driver.Joysticks; }
} }
#endregion #endregion
@ -242,7 +234,7 @@ namespace OpenTK.Platform.X11
/// </summary> /// </summary>
public void Poll() public void Poll()
{ {
//mouseDriver.Poll(); joystick_driver.Poll();
} }
#endregion #endregion

View file

@ -35,7 +35,7 @@ namespace OpenTK.Platform.X11
{ {
struct X11JoyDetails { } struct X11JoyDetails { }
sealed class X11JoystickDriver : IJoystickDriver sealed class X11Joystick : IJoystickDriver
{ {
#region Fields #region Fields
@ -48,7 +48,7 @@ namespace OpenTK.Platform.X11
#region Constructors #region Constructors
public X11JoystickDriver() public X11Joystick()
{ {
sticks_readonly = sticks.AsReadOnly(); sticks_readonly = sticks.AsReadOnly();
@ -57,8 +57,11 @@ namespace OpenTK.Platform.X11
{ {
JoystickDevice stick = OpenJoystick(JoystickPath, number++); JoystickDevice stick = OpenJoystick(JoystickPath, number++);
if (stick != null) if (stick != null)
{
stick.Description = String.Format("USB Joystick {0} ({1} axes, {2} buttons)", number, stick.Axis.Count, stick.Button.Count);
sticks.Add(stick); sticks.Add(stick);
} }
}
number = 0; number = 0;
while (number < max_sticks) while (number < max_sticks)
@ -98,11 +101,11 @@ namespace OpenTK.Platform.X11
switch (e.Type) switch (e.Type)
{ {
case JoystickEventType.Axis: case JoystickEventType.Axis:
js.SetAxis(e.Number, e.Value / 32767.0f); js.SetAxis((JoystickAxis)e.Number, e.Value / 32767.0f);
break; break;
case JoystickEventType.Button: case JoystickEventType.Button:
js.SetButton(e.Number, e.Value != 0); js.SetButton((JoystickButton)e.Number, e.Value != 0);
break; break;
} }
} }
@ -233,7 +236,7 @@ namespace OpenTK.Platform.X11
} }
} }
~X11JoystickDriver() ~X11Joystick()
{ {
Dispose(false); Dispose(false);
} }