Merge pull request from leezer3/JoystickAxis

Change: Bump joystick axis limit to 64 & remove JoystickAxis enum
This commit is contained in:
Jarl Gullberg 2017-07-16 22:31:16 +02:00 committed by GitHub
commit ee09c798e4
13 changed files with 114 additions and 162 deletions

View file

@ -27,8 +27,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace OpenTK.Input
{
@ -53,9 +51,13 @@ namespace OpenTK.Input
return configuration_items.GetEnumerator();
}
// Parses a GamePad configuration string. The string
// follows the rules for SDL2 GameController, outlined here:
// http://wiki.libsdl.org/SDL_GameControllerAddMapping
/// <summary>
/// Parses a GamePad configuration string.
/// This string must follow the rules for SDL2
/// GameController outlined here:
/// http://wiki.libsdl.org/SDL_GameControllerAddMapping
/// </summary>
/// <param name="configuration"></param>
void ParseConfiguration(string configuration)
{
if (String.IsNullOrEmpty(configuration))
@ -86,6 +88,11 @@ namespace OpenTK.Input
}
}
/// <summary>
/// Parses a gamepad configuration target string
/// </summary>
/// <param name="target">The string to parse</param>
/// <returns>The configuration target (Button index, axis index etc.)</returns>
static GamePadConfigurationTarget ParseTarget(string target)
{
switch (target)
@ -145,6 +152,11 @@ namespace OpenTK.Input
}
}
/// <summary>
/// Creates a new gamepad configuration source from the given string
/// </summary>
/// <param name="item">The string to parse</param>
/// <returns>The new gamepad configuration source</returns>
static GamePadConfigurationSource ParseSource(string item)
{
if (String.IsNullOrEmpty(item))
@ -155,10 +167,10 @@ namespace OpenTK.Input
switch (item[0])
{
case 'a':
return new GamePadConfigurationSource(ParseAxis(item));
return new GamePadConfigurationSource(isAxis:true, index:ParseIndex(item));
case 'b':
return new GamePadConfigurationSource(ParseButton(item));
return new GamePadConfigurationSource(isAxis:false, index:ParseIndex(item));
case 'h':
{
@ -172,25 +184,28 @@ namespace OpenTK.Input
}
}
static JoystickAxis ParseAxis(string item)
/// <summary>
/// Parses a string in the format a#" where:
/// - # is a zero-based integer number
/// </summary>
/// <param name="item">The string to parse</param>
/// <returns>The index of the axis or button</returns>
static int ParseIndex(string item)
{
// item is in the format "a#" where # a zero-based integer number
JoystickAxis axis = JoystickAxis.Axis0;
int id = Int32.Parse(item.Substring(1));
return axis + id;
}
static int ParseButton(string item)
{
// item is in the format "b#" where # a zero-based integer number
return Int32.Parse(item.Substring(1));
return Int32.Parse(item.Substring(1)); ;
}
/// <summary>
/// Parses a string in the format "h#.#" where:
/// - the 1st # is the zero-based hat id
/// - the 2nd # is a bit-flag defining the hat position
/// </summary>
/// <param name="item">The string to parse</param>
/// <param name="position">The hat position assigned via 'out'</param>
/// <returns>The new joystick hat</returns>
static JoystickHat ParseHat(string item, out HatPosition position)
{
// item is in the format "h#.#" where:
// - the 1st # is the zero-based hat id
// - the 2nd # is a bit-flag defining the hat position
JoystickHat hat = JoystickHat.Hat0;
int id = Int32.Parse(item.Substring(1, 1));
int pos = Int32.Parse(item.Substring(3));

View file

@ -25,31 +25,40 @@
// THE SOFTWARE.
//
using System;
namespace OpenTK.Input
{
struct GamePadConfigurationSource
{
int? map_button;
JoystickAxis? map_axis;
int? map_axis;
JoystickHat? map_hat;
HatPosition? map_hat_position;
public GamePadConfigurationSource(JoystickAxis axis)
/// <summary>
/// Creates a new gamepad configuration source from an axis or a button
/// </summary>
/// <param name="isAxis">Whether this source is an axis or a button</param>
/// <param name="index">The index of this source</param>
public GamePadConfigurationSource(bool isAxis, int index)
: this()
{
Type = ConfigurationType.Axis;
Axis = axis;
}
public GamePadConfigurationSource(int button)
: this()
{
Type = ConfigurationType.Button;
Button = button;
if (isAxis)
{
Type = ConfigurationType.Axis;
Axis = index;
}
else
{
Type = ConfigurationType.Button;
Button = index;
}
}
/// <summary>
/// Creates a new gamepad configuration source from a hat
/// </summary>
/// <param name="hat">The hat</param>
/// <param name="pos">The starting hat position</param>
public GamePadConfigurationSource(JoystickHat hat, HatPosition pos)
: this()
{
@ -60,24 +69,36 @@ namespace OpenTK.Input
public ConfigurationType Type { get; private set; }
public JoystickAxis Axis
/// <summary>
/// Represents a gamepad axis
/// </summary>
public int Axis
{
get { return map_axis.Value; }
private set { map_axis = value; }
}
/// <summary>
/// Represents a gamepad button
/// </summary>
public int Button
{
get { return map_button.Value; }
private set { map_button = value; }
}
/// <summary>
/// Represents a gamepad hat
/// </summary>
public JoystickHat Hat
{
get { return map_hat.Value; }
private set { map_hat = value; }
}
/// <summary>
/// Represents the position of a gamepad hat
/// </summary>
public HatPosition HatPosition
{
get { return map_hat_position.Value; }

View file

@ -1,64 +0,0 @@
//
// JoystickAxis.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2014 Stefanos Apostolopoulos
//
// 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.
//
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Input
{
/// <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,
/// <summary>The eleventh axis of the JoystickDevice.</summary>
Axis10,
/// <summary>The highest supported axis of the JoystickDevice.</summary>
Last = Axis10,
}
}

View file

@ -92,7 +92,7 @@ namespace OpenTK.Input
internal int Id { get; set; }
internal void SetAxis(JoystickAxis axis, float @value)
internal void SetAxis(int axis, float @value)
{
if ((int)axis < Axis.Count)
{
@ -178,7 +178,7 @@ namespace OpenTK.Input
/// <param name="axis">The index of the joystick axis that was moved.</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>
public JoystickMoveEventArgs(JoystickAxis axis, float value, float delta)
public JoystickMoveEventArgs(int axis, float value, float delta)
{
this.Axis = axis;
this.Value = value;
@ -188,7 +188,7 @@ namespace OpenTK.Input
/// <summary>
/// Gets a System.Int32 representing the index of the axis that was moved.
/// </summary>
public JoystickAxis Axis { get; internal set; }
public int Axis { get; internal set; }
/// <summary>
/// Gets a System.Single representing the absolute position of the axis.
@ -262,17 +262,6 @@ namespace OpenTK.Input
internal set { axis_state[index] = value; }
}
/// <summary>
/// Gets a System.Single indicating the absolute position of the JoystickAxis.
/// </summary>
/// <param name="axis">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>

View file

@ -38,7 +38,7 @@ namespace OpenTK.Input
{
// If we ever add more values to JoystickAxis or JoystickButton
// then we'll need to increase these limits.
internal const int MaxAxes = (int)JoystickAxis.Last + 1;
internal const int MaxAxes = 64;
internal const int MaxButtons = 64;
internal const int MaxHats = (int)JoystickHat.Last + 1;
@ -60,7 +60,7 @@ namespace OpenTK.Input
/// to query the number of available axes.
/// </returns>
/// <param name="axis">The <see cref="JoystickAxis"/> to query.</param>
public float GetAxis(JoystickAxis axis)
public float GetAxis(int axis)
{
return GetAxisRaw(axis) * ConversionFactor;
}
@ -146,7 +146,7 @@ namespace OpenTK.Input
for (int i = 0; i < MaxAxes; i++)
{
sb.Append(" ");
sb.Append(String.Format("{0:f4}", GetAxis(JoystickAxis.Axis0 + i)));
sb.Append(String.Format("{0:f4}", GetAxis(i)));
}
return String.Format(
"{{Axes:{0}; Buttons: {1}; Hat: {2}; IsConnected: {3}}}",
@ -186,11 +186,6 @@ namespace OpenTK.Input
internal int PacketNumber { get; private set; }
internal short GetAxisRaw(JoystickAxis axis)
{
return GetAxisRaw((int)axis);
}
internal short GetAxisRaw(int axis)
{
short value = 0;
@ -205,9 +200,9 @@ namespace OpenTK.Input
return value;
}
internal void SetAxis(JoystickAxis axis, short value)
internal void SetAxis(int axis, short value)
{
int index = (int)axis;
int index = axis;
if (index < 0 || index >= MaxAxes)
throw new ArgumentOutOfRangeException("axis");

View file

@ -112,7 +112,6 @@
<Compile Include="Input\IKeyboardDriver2.cs" />
<Compile Include="Input\IMouseDriver2.cs" />
<Compile Include="Input\Joystick.cs" />
<Compile Include="Input\JoystickAxis.cs" />
<Compile Include="Input\JoystickCapabilities.cs" />
<Compile Include="Input\JoystickState.cs" />
<Compile Include="InteropHelper.cs" />

View file

@ -56,7 +56,7 @@ namespace OpenTK.Platform.Common
return (int)(temp / (value_max - value_min) + result_min);
}
public static JoystickAxis TranslateJoystickAxis(HIDPage page, int usage)
public static int TranslateJoystickAxis(HIDPage page, int usage)
{
switch (page)
{
@ -64,26 +64,23 @@ namespace OpenTK.Platform.Common
switch ((HIDUsageGD)usage)
{
case HIDUsageGD.X:
return JoystickAxis.Axis0;
return 0;
case HIDUsageGD.Y:
return JoystickAxis.Axis1;
return 1;
case HIDUsageGD.Z:
return JoystickAxis.Axis2;
return 2;
case HIDUsageGD.Rz:
return JoystickAxis.Axis3;
return 3;
case HIDUsageGD.Rx:
return JoystickAxis.Axis4;
return 4;
case HIDUsageGD.Ry:
return JoystickAxis.Axis5;
return 5;
case HIDUsageGD.Slider:
return JoystickAxis.Axis6;
return 6;
case HIDUsageGD.Dial:
return JoystickAxis.Axis7;
return 7;
case HIDUsageGD.Wheel:
return JoystickAxis.Axis8;
return 8;
}
break;
@ -91,9 +88,9 @@ namespace OpenTK.Platform.Common
switch ((HIDUsageSim)usage)
{
case HIDUsageSim.Rudder:
return JoystickAxis.Axis9;
return 9;
case HIDUsageSim.Throttle:
return JoystickAxis.Axis10;
return 10;
}
break;
}

View file

@ -35,7 +35,7 @@ namespace OpenTK.Platform.Linux
{
struct AxisInfo
{
public JoystickAxis Axis;
public int Axis;
public InputAbsInfo Info;
}
@ -230,7 +230,7 @@ namespace OpenTK.Platform.Linux
// Analogue hat
stick.AxisMap.Add(axis, new AxisInfo
{
Axis = (JoystickAxis)(JoystickHat)hats++,
Axis = (int)(JoystickHat)hats++,
Info = info
});
}
@ -239,7 +239,7 @@ namespace OpenTK.Platform.Linux
// Regular axis
stick.AxisMap.Add(axis, new AxisInfo
{
Axis = (JoystickAxis)axes++,
Axis = axes++,
Info = info
});
}

View file

@ -850,8 +850,8 @@ namespace OpenTK.Platform.MacOS
case HIDUsageGD.Dial:
case HIDUsageGD.Wheel:
short offset = GetJoystickAxis(val, elem);
JoystickAxis axis = JoystickAxis.Axis0 + joy.Elements[cookie].Index;
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
int axis = joy.Elements[cookie].Index;
if (axis >= 0 && axis <= JoystickState.MaxAxes)
{
joy.State.SetAxis(axis, offset);
}
@ -874,8 +874,8 @@ namespace OpenTK.Platform.MacOS
case HIDUsageSim.Rudder:
case HIDUsageSim.Throttle:
short offset = GetJoystickAxis(val, elem);
JoystickAxis axis = JoystickAxis.Axis0 + joy.Elements[cookie].Index;
if (axis >= JoystickAxis.Axis0 && axis <= JoystickAxis.Last)
int axis = joy.Elements[cookie].Index;
if (axis >= 0 && axis <= JoystickState.MaxAxes)
{
joy.State.SetAxis(axis, offset);
}
@ -887,7 +887,7 @@ namespace OpenTK.Platform.MacOS
{
bool pressed = GetJoystickButton(val, elem);
int button = joy.Elements[cookie].Index;
if (button >= 0 && button <= 64)
if (button >= 0 && button <= JoystickState.MaxButtons)
{
joy.State.SetButton(button, pressed);
}

View file

@ -74,7 +74,7 @@ namespace OpenTK.Platform
case ConfigurationType.Axis:
{
// JoystickAxis -> Buttons/GamePadAxes mapping
JoystickAxis source_axis = map.Source.Axis;
int source_axis = map.Source.Axis;
short value = joy.GetAxisRaw(source_axis);
switch (map.Target.Type)

View file

@ -359,7 +359,7 @@ namespace OpenTK.Platform.SDL2
int index = sdl_instanceid_to_joysticks[id];
JoystickDevice<Sdl2JoystickDetails> joystick = (JoystickDevice<Sdl2JoystickDetails>)joysticks[index];
float value = ev.Value * RangeMultiplier;
joystick.SetAxis((JoystickAxis)ev.Axis, value);
joystick.SetAxis(ev.Axis, value);
joystick.Details.PacketNumber = Math.Max(0, unchecked(joystick.Details.PacketNumber + 1));
}
else
@ -591,7 +591,7 @@ namespace OpenTK.Platform.SDL2
for (int i = 0; i < joystick.Axis.Count; i++)
{
state.SetAxis(JoystickAxis.Axis0 + i, (short)(joystick.Axis[i] * short.MaxValue + 0.5f));
state.SetAxis(i, (short)(joystick.Axis[i] * short.MaxValue + 0.5f));
}
for (int i = 0; i < joystick.Button.Count; i++)

View file

@ -51,8 +51,8 @@ namespace OpenTK.Platform.Windows
internal readonly bool IsXInput;
internal readonly int XInputIndex;
readonly Dictionary<int, JoystickAxis> axes =
new Dictionary<int,JoystickAxis>();
readonly Dictionary<int, int> axes =
new Dictionary<int,int>();
readonly Dictionary<int, int> buttons =
new Dictionary<int, int>();
readonly Dictionary<int, JoystickHat> hats =
@ -79,7 +79,7 @@ namespace OpenTK.Platform.Windows
//return an invalid HID page of 1, so
if ((int)usage != 1)
{
JoystickAxis axis = GetAxis(collection, page, usage);
int axis = GetAxis(collection, page, usage);
State.SetAxis(axis, value);
}
}
@ -133,12 +133,12 @@ namespace OpenTK.Platform.Windows
return (coll_byte << 24) | (page_byte << 16) | unchecked((ushort)usage);
}
JoystickAxis GetAxis(short collection, HIDPage page, short usage)
int GetAxis(short collection, HIDPage page, short usage)
{
int key = MakeKey(collection, page, usage);
if (!axes.ContainsKey(key))
{
JoystickAxis axis = HidHelper.TranslateJoystickAxis(page, usage);
int axis = HidHelper.TranslateJoystickAxis(page, usage);
axes.Add(key, axis);
}
return axes[key];
@ -556,7 +556,7 @@ namespace OpenTK.Platform.Windows
case HIDUsageGD.Dial:
case HIDUsageGD.Wheel:
Debug.Print("Found axis {0} ({1} / {2})",
JoystickAxis.Axis0 + stick.GetCapabilities().AxisCount,
stick.GetCapabilities().AxisCount,
page, (HIDUsageGD)stick.AxisCaps[i].NotRange.Usage);
stick.SetAxis(collection, page, stick.AxisCaps[i].NotRange.Usage, 0);
break;
@ -581,7 +581,7 @@ namespace OpenTK.Platform.Windows
case HIDUsageSim.Rudder:
case HIDUsageSim.Throttle:
Debug.Print("Found simulation axis {0} ({1} / {2})",
JoystickAxis.Axis0 + stick.GetCapabilities().AxisCount,
stick.GetCapabilities().AxisCount,
page, (HIDUsageSim)stick.AxisCaps[i].NotRange.Usage);
stick.SetAxis(collection, page, stick.AxisCaps[i].NotRange.Usage, 0);
break;

View file

@ -54,12 +54,12 @@ namespace OpenTK.Platform.Windows
{
state.SetIsConnected(true);
state.SetAxis(JoystickAxis.Axis0, (short)xstate.GamePad.ThumbLX);
state.SetAxis(JoystickAxis.Axis1, (short)Math.Min(short.MaxValue, -xstate.GamePad.ThumbLY));
state.SetAxis(JoystickAxis.Axis2, (short)Common.HidHelper.ScaleValue(xstate.GamePad.LeftTrigger, 0, byte.MaxValue, short.MinValue, short.MaxValue));
state.SetAxis(JoystickAxis.Axis3, (short)xstate.GamePad.ThumbRX);
state.SetAxis(JoystickAxis.Axis4, (short)Math.Min(short.MaxValue, -xstate.GamePad.ThumbRY));
state.SetAxis(JoystickAxis.Axis5, (short)Common.HidHelper.ScaleValue(xstate.GamePad.RightTrigger, 0, byte.MaxValue, short.MinValue, short.MaxValue));
state.SetAxis(0, (short)xstate.GamePad.ThumbLX);
state.SetAxis(1, (short)Math.Min(short.MaxValue, -xstate.GamePad.ThumbLY));
state.SetAxis(2, (short)Common.HidHelper.ScaleValue(xstate.GamePad.LeftTrigger, 0, byte.MaxValue, short.MinValue, short.MaxValue));
state.SetAxis(3, (short)xstate.GamePad.ThumbRX);
state.SetAxis(4, (short)Math.Min(short.MaxValue, -xstate.GamePad.ThumbRY));
state.SetAxis(5, (short)Common.HidHelper.ScaleValue(xstate.GamePad.RightTrigger, 0, byte.MaxValue, short.MinValue, short.MaxValue));
state.SetButton(0, (xstate.GamePad.Buttons & XInputButtons.A) != 0);
state.SetButton(1, (xstate.GamePad.Buttons & XInputButtons.B) != 0);