Implements JoystickState and Capabilities setters

This commit is contained in:
Stefanos A. 2013-12-24 14:21:37 +01:00
parent 58b67d31e3
commit 5c73a3ea74
2 changed files with 86 additions and 15 deletions

View file

@ -38,20 +38,35 @@ namespace OpenTK.Input
byte axis_count; byte axis_count;
byte button_count; byte button_count;
byte dpad_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 #region Public Members
public int AxisCount public int AxisCount
{ {
get { return axis_count; } get { return axis_count; }
set { axis_count = (byte)value; }
} }
public int ButtonCount public int ButtonCount
{ {
get { return button_count; } get { return button_count; }
set { button_count = (byte)value; }
} }
#endregion #endregion
@ -61,15 +76,14 @@ namespace OpenTK.Input
int DPadCount int DPadCount
{ {
get { return dpad_count; } get { return dpad_count; }
set { dpad_count = (byte)value; }
}
int TrackballCount
{
get { return trackball_count; }
set { trackball_count = (byte)value; }
} }
#endregion #endregion
public bool IsConnected
{
get { return is_connected; }
}
} }
} }

View file

@ -36,10 +36,18 @@ namespace OpenTK.Input
{ {
public struct JoystickState public struct JoystickState
{ {
const int MaxAxes = 10; // JoystickAxis defines 10 axes // If we ever add more values to JoystickAxis or JoystickButton
const float ConversionFactor = 1.0f / short.MaxValue; // 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]; unsafe fixed short axes[MaxAxes];
JoystickButton buttons; int buttons;
bool is_connected;
#region Public Members
public float GetAxis(JoystickAxis axis) public float GetAxis(JoystickAxis axis)
{ {
@ -66,14 +74,63 @@ namespace OpenTK.Input
return value; return value;
} }
public ButtonState GetButton(JoystickButton button)
{
return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released;
}
public bool IsButtonDown(JoystickButton button) public bool IsButtonDown(JoystickButton button)
{ {
return (buttons & button) != 0; return (buttons & (1 << (int)button)) != 0;
} }
public bool IsButtonUp(JoystickButton button) 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
} }
} }