From 7ef7c12c2bb11e8c41efae43bc22ad5836c1f943 Mon Sep 17 00:00:00 2001 From: Christopher Lees Date: Wed, 23 Nov 2016 14:11:00 +0000 Subject: [PATCH] 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) --- src/OpenTK/Input/GamePadConfiguration.cs | 6 +- src/OpenTK/Input/GamePadConfigurationSource.cs | 6 +- src/OpenTK/Input/JoystickButton.cs | 108 ------------------------- src/OpenTK/Input/JoystickDevice.cs | 21 ++--- src/OpenTK/Input/JoystickState.cs | 39 +++++---- src/OpenTK/OpenTK.csproj | 1 - src/OpenTK/Platform/LegacyJoystickDriver.cs | 3 +- src/OpenTK/Platform/Linux/LinuxJoystick.cs | 8 +- src/OpenTK/Platform/MacOS/HIDInput.cs | 4 +- src/OpenTK/Platform/MappedGamePadDriver.cs | 2 +- src/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs | 4 +- src/OpenTK/Platform/Windows/WinRawJoystick.cs | 18 ++--- src/OpenTK/Platform/Windows/XInputJoystick.cs | 22 ++--- 13 files changed, 59 insertions(+), 183 deletions(-) delete mode 100644 src/OpenTK/Input/JoystickButton.cs diff --git a/src/OpenTK/Input/GamePadConfiguration.cs b/src/OpenTK/Input/GamePadConfiguration.cs index c7e9877..92e9c43 100644 --- a/src/OpenTK/Input/GamePadConfiguration.cs +++ b/src/OpenTK/Input/GamePadConfiguration.cs @@ -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) diff --git a/src/OpenTK/Input/GamePadConfigurationSource.cs b/src/OpenTK/Input/GamePadConfigurationSource.cs index c312f52..1f4b6f4 100644 --- a/src/OpenTK/Input/GamePadConfigurationSource.cs +++ b/src/OpenTK/Input/GamePadConfigurationSource.cs @@ -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; } diff --git a/src/OpenTK/Input/JoystickButton.cs b/src/OpenTK/Input/JoystickButton.cs deleted file mode 100644 index f858619..0000000 --- a/src/OpenTK/Input/JoystickButton.cs +++ /dev/null @@ -1,108 +0,0 @@ -#region License -// -// JoystickButton.cs -// -// Author: -// Stefanos A. -// -// 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 -{ - /// - /// Defines available JoystickDevice buttons. - /// - public enum JoystickButton - { - /// The first button of the JoystickDevice. - Button0 = 0, - /// The second button of the JoystickDevice. - Button1, - /// The third button of the JoystickDevice. - Button2, - /// The fourth button of the JoystickDevice. - Button3, - /// The fifth button of the JoystickDevice. - Button4, - /// The sixth button of the JoystickDevice. - Button5, - /// The seventh button of the JoystickDevice. - Button6, - /// The eighth button of the JoystickDevice. - Button7, - /// The ninth button of the JoystickDevice. - Button8, - /// The tenth button of the JoystickDevice. - Button9, - /// The eleventh button of the JoystickDevice. - Button10, - /// The twelfth button of the JoystickDevice. - Button11, - /// The thirteenth button of the JoystickDevice. - Button12, - /// The fourteenth button of the JoystickDevice. - Button13, - /// The fifteenth button of the JoystickDevice. - Button14, - /// The sixteenth button of the JoystickDevice. - Button15, - /// The seventeenth button of the JoystickDevice. - Button16, - /// The eighteenth button of the JoystickDevice. - Button17, - /// The nineteenth button of the JoystickDevice. - Button18, - /// The twentieth button of the JoystickDevice. - Button19, - /// The twentyfirst button of the JoystickDevice. - Button20, - /// The twentysecond button of the JoystickDevice. - Button21, - /// The twentythird button of the JoystickDevice. - Button22, - /// The twentyfourth button of the JoystickDevice. - Button23, - /// The twentyfifth button of the JoystickDevice. - Button24, - /// The twentysixth button of the JoystickDevice. - Button25, - /// The twentyseventh button of the JoystickDevice. - Button26, - /// The twentyeighth button of the JoystickDevice. - Button27, - /// The twentynineth button of the JoystickDevice. - Button28, - /// The thirtieth button of the JoystickDevice. - Button29, - /// The thirtyfirst button of the JoystickDevice. - Button30, - /// The thirtysecond button of the JoystickDevice. - Button31, - /// The last supported button of the JoystickDevice. - Last = Button31, - } -} diff --git a/src/OpenTK/Input/JoystickDevice.cs b/src/OpenTK/Input/JoystickDevice.cs index 0467ce2..2155a7b 100644 --- a/src/OpenTK/Input/JoystickDevice.cs +++ b/src/OpenTK/Input/JoystickDevice.cs @@ -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 /// /// The index of the joystick button for the event. /// The current state of the button. - 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 /// /// The index of the joystick button for the event. /// - public JoystickButton Button { get { return this.button; } internal set { this.button = value; } } + public int Button { get { return this.button; } internal set { this.button = value; } } /// /// Gets a System.Boolean representing the state of the button for the event. @@ -317,17 +317,6 @@ namespace OpenTK.Input } /// - /// Gets a System.Boolean indicating whether the specified JoystickButton is pressed. - /// - /// The JoystickButton to check. - /// True if the JoystickButton is pressed; false otherwise. - public bool this[JoystickButton button] - { - get { return button_state[(int)button]; } - internal set { button_state[(int)button] = value; } - } - - /// /// Gets a System.Int32 indicating the available amount of JoystickButtons. /// public int Count diff --git a/src/OpenTK/Input/JoystickState.cs b/src/OpenTK/Input/JoystickState.cs index 540d5c7..aca286b 100644 --- a/src/OpenTK/Input/JoystickState.cs +++ b/src/OpenTK/Input/JoystickState.cs @@ -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 } /// - /// Gets the current of the specified . + /// Gets the current of the specified button. /// /// if the specified button is pressed; otherwise, . - /// The to query. - public ButtonState GetButton(JoystickButton button) + /// The button to query. + 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; } /// @@ -104,23 +104,23 @@ namespace OpenTK.Input } /// - /// Gets a value indicating whether the specified is currently pressed. + /// Gets a value indicating whether the specified button is currently pressed. /// /// true if the specified button is pressed; otherwise, false. - /// The to query. - public bool IsButtonDown(JoystickButton button) + /// The button to query. + public bool IsButtonDown(int button) { - return (buttons & (1 << (int)button)) != 0; + return (buttons & ((long)1 << button)) != 0; } /// - /// Gets a value indicating whether the specified is currently released. + /// Gets a value indicating whether the specified button is currently released. /// /// true if the specified button is released; otherwise, false. - /// The to query. - public bool IsButtonUp(JoystickButton button) + /// The button to query. + public bool IsButtonUp(int button) { - return (buttons & (1 << (int)button)) == 0; + return (buttons & ((long)1 << button)) == 0; } /// @@ -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); } } diff --git a/src/OpenTK/OpenTK.csproj b/src/OpenTK/OpenTK.csproj index aaa0989..7c2828c 100644 --- a/src/OpenTK/OpenTK.csproj +++ b/src/OpenTK/OpenTK.csproj @@ -112,7 +112,6 @@ - diff --git a/src/OpenTK/Platform/LegacyJoystickDriver.cs b/src/OpenTK/Platform/LegacyJoystickDriver.cs index 202b998..d85ecf8 100644 --- a/src/OpenTK/Platform/LegacyJoystickDriver.cs +++ b/src/OpenTK/Platform/LegacyJoystickDriver.cs @@ -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++) { diff --git a/src/OpenTK/Platform/Linux/LinuxJoystick.cs b/src/OpenTK/Platform/Linux/LinuxJoystick.cs index 348e3e3..fa47bdf 100644 --- a/src/OpenTK/Platform/Linux/LinuxJoystick.cs +++ b/src/OpenTK/Platform/Linux/LinuxJoystick.cs @@ -52,8 +52,8 @@ namespace OpenTK.Platform.Linux public readonly Dictionary AxisMap = new Dictionary(); - public readonly Dictionary ButtonMap = - new Dictionary(); + public readonly Dictionary ButtonMap = + new Dictionary(); } 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); diff --git a/src/OpenTK/Platform/MacOS/HIDInput.cs b/src/OpenTK/Platform/MacOS/HIDInput.cs index 88c3b8f..ca6eecd 100644 --- a/src/OpenTK/Platform/MacOS/HIDInput.cs +++ b/src/OpenTK/Platform/MacOS/HIDInput.cs @@ -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); } diff --git a/src/OpenTK/Platform/MappedGamePadDriver.cs b/src/OpenTK/Platform/MappedGamePadDriver.cs index aab0f3e..a15676f 100644 --- a/src/OpenTK/Platform/MappedGamePadDriver.cs +++ b/src/OpenTK/Platform/MappedGamePadDriver.cs @@ -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) diff --git a/src/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs b/src/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs index 396c336..606cd1a 100644 --- a/src/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs +++ b/src/OpenTK/Platform/SDL2/Sdl2JoystickDriver.cs @@ -399,7 +399,7 @@ namespace OpenTK.Platform.SDL2 { int index = sdl_instanceid_to_joysticks[id]; JoystickDevice joystick = (JoystickDevice)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++) diff --git a/src/OpenTK/Platform/Windows/WinRawJoystick.cs b/src/OpenTK/Platform/Windows/WinRawJoystick.cs index 82f13c6..1d224a0 100644 --- a/src/OpenTK/Platform/Windows/WinRawJoystick.cs +++ b/src/OpenTK/Platform/Windows/WinRawJoystick.cs @@ -55,8 +55,8 @@ namespace OpenTK.Platform.Windows readonly Dictionary axes = new Dictionary(); - readonly Dictionary buttons = - new Dictionary(); + readonly Dictionary buttons = + new Dictionary(); readonly Dictionary hats = new Dictionary(); @@ -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); } diff --git a/src/OpenTK/Platform/Windows/XInputJoystick.cs b/src/OpenTK/Platform/Windows/XInputJoystick.cs index 0ffed2a..4df1e88 100644 --- a/src/OpenTK/Platform/Windows/XInputJoystick.cs +++ b/src/OpenTK/Platform/Windows/XInputJoystick.cs @@ -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))); } -- 2.7.4