Joystick Buttons:

* Remove JoystickButton enum (Use zero-based int as button index instead)
* Change to using a long to store button states
* Max buttons now 64 (Unable to go further without using an array)
This commit is contained in:
Christopher Lees 2016-11-23 14:11:00 +00:00
parent c6ca549923
commit 7ef7c12c2b
13 changed files with 59 additions and 183 deletions

View file

@ -194,12 +194,10 @@ namespace OpenTK.Input
return axis + id;
}
static JoystickButton ParseButton(string item)
static int ParseButton(string item)
{
// item is in the format "b#" where # a zero-based integer number
JoystickButton button = JoystickButton.Button0;
int id = Int32.Parse(item.Substring(1));
return button + id;
return Int32.Parse(item.Substring(1));
}
static JoystickHat ParseHat(string item, out HatPosition position)

View file

@ -34,7 +34,7 @@ namespace OpenTK.Input
struct GamePadConfigurationSource
{
ConfigurationType map_type;
JoystickButton? map_button;
int? map_button;
JoystickAxis? map_axis;
JoystickHat? map_hat;
HatPosition? map_hat_position;
@ -46,7 +46,7 @@ namespace OpenTK.Input
Axis = axis;
}
public GamePadConfigurationSource(JoystickButton button)
public GamePadConfigurationSource(int button)
: this()
{
Type = ConfigurationType.Button;
@ -73,7 +73,7 @@ namespace OpenTK.Input
private set { map_axis = value; }
}
public JoystickButton Button
public int Button
{
get { return map_button.Value; }
private set { map_button = value; }

View file

@ -1,108 +0,0 @@
#region License
//
// JoystickButton.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2013 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.
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace OpenTK.Input
{
/// <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,
/// <summary>The seventeenth button of the JoystickDevice.</summary>
Button16,
/// <summary>The eighteenth button of the JoystickDevice.</summary>
Button17,
/// <summary>The nineteenth button of the JoystickDevice.</summary>
Button18,
/// <summary>The twentieth button of the JoystickDevice.</summary>
Button19,
/// <summary>The twentyfirst button of the JoystickDevice.</summary>
Button20,
/// <summary>The twentysecond button of the JoystickDevice.</summary>
Button21,
/// <summary>The twentythird button of the JoystickDevice.</summary>
Button22,
/// <summary>The twentyfourth button of the JoystickDevice.</summary>
Button23,
/// <summary>The twentyfifth button of the JoystickDevice.</summary>
Button24,
/// <summary>The twentysixth button of the JoystickDevice.</summary>
Button25,
/// <summary>The twentyseventh button of the JoystickDevice.</summary>
Button26,
/// <summary>The twentyeighth button of the JoystickDevice.</summary>
Button27,
/// <summary>The twentynineth button of the JoystickDevice.</summary>
Button28,
/// <summary>The thirtieth button of the JoystickDevice.</summary>
Button29,
/// <summary>The thirtyfirst button of the JoystickDevice.</summary>
Button30,
/// <summary>The thirtysecond button of the JoystickDevice.</summary>
Button31,
/// <summary>The last supported button of the JoystickDevice.</summary>
Last = Button31,
}
}

View file

@ -139,9 +139,9 @@ namespace OpenTK.Input
}
}
internal void SetButton(JoystickButton button, bool @value)
internal void SetButton(int button, bool @value)
{
if ((int)button < button_collection.Count)
if (button < button_collection.Count)
{
if (button_collection[button] != @value)
{
@ -190,7 +190,7 @@ namespace OpenTK.Input
{
#region Fields
JoystickButton button;
int button;
bool pressed;
#endregion
@ -202,7 +202,7 @@ namespace OpenTK.Input
/// </summary>
/// <param name="button">The index of the joystick button for the event.</param>
/// <param name="pressed">The current state of the button.</param>
internal JoystickButtonEventArgs(JoystickButton button, bool pressed)
internal JoystickButtonEventArgs(int button, bool pressed)
{
this.button = button;
this.pressed = pressed;
@ -215,7 +215,7 @@ namespace OpenTK.Input
/// <summary>
/// The index of the joystick button for the event.
/// </summary>
public JoystickButton Button { get { return this.button; } internal set { this.button = value; } }
public int Button { get { return this.button; } internal set { this.button = value; } }
/// <summary>
/// Gets a System.Boolean representing the state of the button for the event.
@ -316,17 +316,6 @@ namespace OpenTK.Input
internal set { button_state[index] = value; }
}
/// <summary>
/// Gets a System.Boolean indicating whether the specified JoystickButton is pressed.
/// </summary>
/// <param name="button">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>

View file

@ -41,13 +41,13 @@ 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 MaxButtons = (int)JoystickButton.Last + 1;
internal const int MaxButtons = 64;
internal const int MaxHats = (int)JoystickHat.Last + 1;
const float ConversionFactor = 1.0f / (short.MaxValue + 0.5f);
int packet_number;
int buttons;
long buttons;
unsafe fixed short axes[MaxAxes];
JoystickHatState hat0;
JoystickHatState hat1;
@ -72,13 +72,13 @@ namespace OpenTK.Input
}
/// <summary>
/// Gets the current <see cref="ButtonState"/> of the specified <see cref="JoystickButton"/>.
/// Gets the current <see cref="ButtonState"/> of the specified button.
/// </summary>
/// <returns><see cref="ButtonState.Pressed"/> if the specified button is pressed; otherwise, <see cref="ButtonState.Released"/>.</returns>
/// <param name="button">The <see cref="JoystickButton"/> to query.</param>
public ButtonState GetButton(JoystickButton button)
/// <param name="button">The button to query.</param>
public ButtonState GetButton(int button)
{
return (buttons & (1 << (int)button)) != 0 ? ButtonState.Pressed : ButtonState.Released;
return (buttons & ((long)1 << button)) != 0 ? ButtonState.Pressed : ButtonState.Released;
}
/// <summary>
@ -104,23 +104,23 @@ namespace OpenTK.Input
}
/// <summary>
/// Gets a value indicating whether the specified <see cref="JoystickButton"/> is currently pressed.
/// Gets a value indicating whether the specified button is currently pressed.
/// </summary>
/// <returns>true if the specified button is pressed; otherwise, false.</returns>
/// <param name="button">The <see cref="JoystickButton"/> to query.</param>
public bool IsButtonDown(JoystickButton button)
/// <param name="button">The button to query.</param>
public bool IsButtonDown(int button)
{
return (buttons & (1 << (int)button)) != 0;
return (buttons & ((long)1 << button)) != 0;
}
/// <summary>
/// Gets a value indicating whether the specified <see cref="JoystickButton"/> is currently released.
/// Gets a value indicating whether the specified button is currently released.
/// </summary>
/// <returns>true if the specified button is released; otherwise, false.</returns>
/// <param name="button">The <see cref="JoystickButton"/> to query.</param>
public bool IsButtonUp(JoystickButton button)
/// <param name="button">The button to query.</param>
public bool IsButtonUp(int button)
{
return (buttons & (1 << (int)button)) == 0;
return (buttons & ((long)1 << button)) == 0;
}
/// <summary>
@ -160,7 +160,7 @@ namespace OpenTK.Input
return String.Format(
"{{Axes:{0}; Buttons: {1}; Hat: {2}; IsConnected: {3}}}",
sb.ToString(),
Convert.ToString((int)buttons, 2).PadLeft(16, '0'),
Convert.ToString(buttons, 2).PadLeft(16, '0'),
hat0,
IsConnected);
}
@ -241,19 +241,18 @@ namespace OpenTK.Input
buttons = 0;
}
internal void SetButton(JoystickButton button, bool value)
internal void SetButton(int button, bool value)
{
int index = (int)button;
if (index < 0 || index >= MaxButtons)
if (button < 0 || button >= MaxButtons)
throw new ArgumentOutOfRangeException("button");
if (value)
{
buttons |= 1 << index;
buttons |= (long)1 << button;
}
else
{
buttons &= ~(1 << index);
buttons &= ~((long)1 << button);
}
}

View file

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

View file

@ -89,8 +89,7 @@ namespace OpenTK.Platform
}
for (int button_index = 0; button_index < caps.ButtonCount; button_index++)
{
JoystickButton button = JoystickButton.Button0 + button_index;
joysticks[i].SetButton(button, state.GetButton(button) == ButtonState.Pressed);
joysticks[i].SetButton(button_index, state.GetButton(button_index) == ButtonState.Pressed);
}
for (int hat_index = 0; hat_index < caps.HatCount; hat_index++)
{

View file

@ -52,8 +52,8 @@ namespace OpenTK.Platform.Linux
public readonly Dictionary<EvdevAxis, AxisInfo> AxisMap =
new Dictionary<EvdevAxis, AxisInfo>();
public readonly Dictionary<EvdevButton, JoystickButton> ButtonMap =
new Dictionary<EvdevButton, JoystickButton>();
public readonly Dictionary<EvdevButton, int> ButtonMap =
new Dictionary<EvdevButton, int>();
}
sealed class LinuxJoystick : IJoystickDriver2
@ -258,7 +258,7 @@ namespace OpenTK.Platform.Linux
{
if (TestBit(keybit, (int)button))
{
stick.ButtonMap.Add(button, (JoystickButton)buttons++);
stick.ButtonMap.Add(button, buttons++);
}
}
}
@ -436,7 +436,7 @@ namespace OpenTK.Platform.Linux
case EvdevType.KEY:
{
JoystickButton button;
int button;
if (js.ButtonMap.TryGetValue((EvdevButton)e->Code, out button))
{
js.State.SetButton(button, e->Value != 0);

View file

@ -916,8 +916,8 @@ namespace OpenTK.Platform.MacOS
case HIDPage.Button:
{
bool pressed = GetJoystickButton(val, elem);
JoystickButton button = JoystickButton.Button0 + joy.Elements[cookie].Index;
if (button >= JoystickButton.Button0 && button <= JoystickButton.Last)
int button = joy.Elements[cookie].Index;
if (button >= 0 && button <= 64)
{
joy.State.SetButton(button, pressed);
}

View file

@ -99,7 +99,7 @@ namespace OpenTK.Platform
case ConfigurationType.Button:
{
// JoystickButton -> Buttons/GamePadAxes mapping
JoystickButton source_button = map.Source.Button;
int source_button = map.Source.Button;
bool pressed = joy.GetButton(source_button) == ButtonState.Pressed;
switch (map.Target.Type)

View file

@ -399,7 +399,7 @@ namespace OpenTK.Platform.SDL2
{
int index = sdl_instanceid_to_joysticks[id];
JoystickDevice<Sdl2JoystickDetails> joystick = (JoystickDevice<Sdl2JoystickDetails>)joysticks[index];
joystick.SetButton((JoystickButton)ev.Button, ev.State == State.Pressed);
joystick.SetButton(ev.Button, ev.State == State.Pressed);
joystick.Details.PacketNumber = Math.Max(0, unchecked(joystick.Details.PacketNumber + 1));
}
else
@ -612,7 +612,7 @@ namespace OpenTK.Platform.SDL2
for (int i = 0; i < joystick.Button.Count; i++)
{
state.SetButton(JoystickButton.Button0 + i, joystick.Button[i]);
state.SetButton(i, joystick.Button[i]);
}
for (int i = 0; i < joystick.Details.HatCount; i++)

View file

@ -55,8 +55,8 @@ namespace OpenTK.Platform.Windows
readonly Dictionary<int, JoystickAxis> axes =
new Dictionary<int,JoystickAxis>();
readonly Dictionary<int, JoystickButton> buttons =
new Dictionary<int, JoystickButton>();
readonly Dictionary<int, int> buttons =
new Dictionary<int, int>();
readonly Dictionary<int, JoystickHat> hats =
new Dictionary<int, JoystickHat>();
@ -90,7 +90,7 @@ namespace OpenTK.Platform.Windows
public void SetButton(short collection, HIDPage page, short usage, bool value)
{
JoystickButton button = GetButton(collection, page, usage);
int button = GetButton(collection, page, usage);
State.SetButton(button, value);
}
@ -151,12 +151,12 @@ namespace OpenTK.Platform.Windows
return axes[key];
}
JoystickButton GetButton(short collection, HIDPage page, short usage)
int GetButton(short collection, HIDPage page, short usage)
{
int key = MakeKey(collection, page, usage);
if (!buttons.ContainsKey(key))
{
buttons.Add(key, JoystickButton.Button0 + buttons.Count);
buttons.Add(key, buttons.Count);
}
return buttons[key];
}
@ -415,8 +415,8 @@ namespace OpenTK.Platform.Windows
for (int i = 0; i < stick.ButtonCaps.Count; i++)
{
short* usage_list = stackalloc short[(int)JoystickButton.Last + 1];
int usage_length = (int)JoystickButton.Last;
short* usage_list = stackalloc short[64];
int usage_length = 64;
HIDPage page = stick.ButtonCaps[i].UsagePage;
short collection = stick.ButtonCaps[i].LinkCollection;
@ -591,7 +591,7 @@ namespace OpenTK.Platform.Windows
for (short usage = stick.ButtonCaps[i].Range.UsageMin; usage <= stick.ButtonCaps[i].Range.UsageMax; usage++)
{
Debug.Print("Found button {0} ({1} / {2})",
JoystickButton.Button0 + stick.GetCapabilities().ButtonCount,
stick.GetCapabilities().ButtonCount,
page, usage);
stick.SetButton(collection, page, usage, false);
}
@ -599,7 +599,7 @@ namespace OpenTK.Platform.Windows
else
{
Debug.Print("Found button {0} ({1} / {2})",
JoystickButton.Button0 + stick.GetCapabilities().ButtonCount,
stick.GetCapabilities().ButtonCount,
page, stick.ButtonCaps[i].NotRange.Usage);
stick.SetButton(collection, page, stick.ButtonCaps[i].NotRange.Usage, false);
}

View file

@ -65,17 +65,17 @@ namespace OpenTK.Platform.Windows
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.SetButton(JoystickButton.Button0, (xstate.GamePad.Buttons & XInputButtons.A) != 0);
state.SetButton(JoystickButton.Button1, (xstate.GamePad.Buttons & XInputButtons.B) != 0);
state.SetButton(JoystickButton.Button2, (xstate.GamePad.Buttons & XInputButtons.X) != 0);
state.SetButton(JoystickButton.Button3, (xstate.GamePad.Buttons & XInputButtons.Y) != 0);
state.SetButton(JoystickButton.Button4, (xstate.GamePad.Buttons & XInputButtons.LeftShoulder) != 0);
state.SetButton(JoystickButton.Button5, (xstate.GamePad.Buttons & XInputButtons.RightShoulder) != 0);
state.SetButton(JoystickButton.Button6, (xstate.GamePad.Buttons & XInputButtons.Back) != 0);
state.SetButton(JoystickButton.Button7, (xstate.GamePad.Buttons & XInputButtons.Start) != 0);
state.SetButton(JoystickButton.Button8, (xstate.GamePad.Buttons & XInputButtons.LeftThumb) != 0);
state.SetButton(JoystickButton.Button9, (xstate.GamePad.Buttons & XInputButtons.RightThumb) != 0);
state.SetButton(JoystickButton.Button10, (xstate.GamePad.Buttons & XInputButtons.Guide) != 0);
state.SetButton(0, (xstate.GamePad.Buttons & XInputButtons.A) != 0);
state.SetButton(1, (xstate.GamePad.Buttons & XInputButtons.B) != 0);
state.SetButton(2, (xstate.GamePad.Buttons & XInputButtons.X) != 0);
state.SetButton(3, (xstate.GamePad.Buttons & XInputButtons.Y) != 0);
state.SetButton(4, (xstate.GamePad.Buttons & XInputButtons.LeftShoulder) != 0);
state.SetButton(5, (xstate.GamePad.Buttons & XInputButtons.RightShoulder) != 0);
state.SetButton(6, (xstate.GamePad.Buttons & XInputButtons.Back) != 0);
state.SetButton(7, (xstate.GamePad.Buttons & XInputButtons.Start) != 0);
state.SetButton(8, (xstate.GamePad.Buttons & XInputButtons.LeftThumb) != 0);
state.SetButton(9, (xstate.GamePad.Buttons & XInputButtons.RightThumb) != 0);
state.SetButton(10, (xstate.GamePad.Buttons & XInputButtons.Guide) != 0);
state.SetHat(JoystickHat.Hat0, new JoystickHatState(TranslateHat(xstate.GamePad.Buttons)));
}