Implements JoystickState and Capabilities setters

This commit is contained in:
Stefanos A 2013-12-24 14:21:37 +01:00 committed by thefiddler
parent 91b54cfbf3
commit 02fb6bf2f9
2 changed files with 86 additions and 15 deletions

View file

@ -38,20 +38,35 @@ namespace OpenTK.Input
byte axis_count;
byte button_count;
byte dpad_count;
byte trackball_count;
bool is_connected;
#region Constructors
public JoystickCapabilities(int axis_count, int button_count, bool is_connected)
{
if (axis_count < 0 || axis_count >= JoystickState.MaxAxes)
throw new ArgumentOutOfRangeException("axis_count");
if (button_count < 0 || button_count >= JoystickState.MaxButtons)
throw new ArgumentOutOfRangeException("axis_count");
this.axis_count = (byte)axis_count;
this.button_count = (byte)button_count;
this.dpad_count = 0; // Todo: either remove dpad_count or add it as a parameter
this.is_connected = is_connected;
}
#endregion
#region Public Members
public int AxisCount
{
get { return axis_count; }
set { axis_count = (byte)value; }
}
public int ButtonCount
{
get { return button_count; }
set { button_count = (byte)value; }
}
#endregion
@ -61,15 +76,14 @@ namespace OpenTK.Input
int DPadCount
{
get { return dpad_count; }
set { dpad_count = (byte)value; }
}
int TrackballCount
{
get { return trackball_count; }
set { trackball_count = (byte)value; }
}
#endregion
public bool IsConnected
{
get { return is_connected; }
}
}
}

View file

@ -36,10 +36,18 @@ namespace OpenTK.Input
{
public struct JoystickState
{
const int MaxAxes = 10; // JoystickAxis defines 10 axes
const float ConversionFactor = 1.0f / short.MaxValue;
// If we ever add more values to JoystickAxis or JoystickButton
// then we'll need to increase these limits.
internal const int MaxAxes = 10;
internal const int MaxButtons = 32;
const float ConversionFactor = 1.0f / (short.MaxValue + 1);
unsafe fixed short axes[MaxAxes];
JoystickButton buttons;
int buttons;
bool is_connected;
#region Public Members
public float GetAxis(JoystickAxis axis)
{
@ -66,14 +74,63 @@ namespace OpenTK.Input
return value;
}
public ButtonState GetButton(JoystickButton button)
{
return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released;
}
public bool IsButtonDown(JoystickButton button)
{
return (buttons & button) != 0;
return (buttons & (1 << (int)button)) != 0;
}
public bool IsButtonUp(JoystickButton button)
{
return (buttons & button) == 0;
return (buttons & (1 << (int)button)) == 0;
}
public bool IsConnected
{
get { return is_connected; }
}
#endregion
#region Internal Members
internal void SetAxis(JoystickAxis axis, short value)
{
int index = (int)axis;
if (index < 0 || index >= MaxAxes)
throw new ArgumentOutOfRangeException("axis");
unsafe
{
fixed (short* paxes = axes)
{
*(paxes + index) = value;
}
}
}
internal void SetButton(JoystickButton button, bool value)
{
int index = 1 << (int)button;
if (value)
{
buttons |= index;
}
else
{
buttons &= ~index;
}
}
internal void SetIsConnected(bool value)
{
is_connected = value;
}
#endregion
}
}