From 2f344f8ffd6b202c8293901dd2e429dcf64b5874 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 20 Aug 2021 08:18:03 +1000 Subject: [PATCH] Clean up OTK_GamePad.GetAxes --- src/BizHawk.Bizware.OpenTK3/OTK_Gamepad.cs | 75 ++++++++++--------- .../OpenTKInputAdapter.cs | 2 +- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/src/BizHawk.Bizware.OpenTK3/OTK_Gamepad.cs b/src/BizHawk.Bizware.OpenTK3/OTK_Gamepad.cs index 851b7bf631..f85aa6df6e 100644 --- a/src/BizHawk.Bizware.OpenTK3/OTK_Gamepad.cs +++ b/src/BizHawk.Bizware.OpenTK3/OTK_Gamepad.cs @@ -80,14 +80,6 @@ namespace BizHawk.Bizware.OpenTK3 } } - /// The things that use (analog controls) appear to require values -10000.0..10000.0 rather than the -1.0..1.0 that OpenTK returns (although even then the results may be slightly outside of these bounds) - private static float ConstrainFloatInput(float num) - { - if (num > 1) return 10000.0f; - if (num < -1) return -10000.0f; - return num * 10000.0f; - } - /// The OpenTK device index private readonly int _deviceIndex; @@ -287,40 +279,51 @@ namespace BizHawk.Bizware.OpenTK3 if (!tmpJstate.Equals(jState)) Console.WriteLine($"Joystick State:\t{tmpJstate}"); } - public IEnumerable<(string AxisID, float Value)> GetAxes() + public IReadOnlyCollection<(string AxisID, int Value)> GetAxes() { + // The host input stack appears to require values -10000.0..10000.0 rather than the -1.0..1.0 that OpenTK returns (although even then the results may be slightly outside of these bounds) + static int ConstrainFloatInput(float num) => num switch + { + > 1 => 10000, + < -1 => -10000, + _ => (int) (num * 10000.0f) + }; + if (MappedGamePad) { // automapping identified - use OpenTKGamePad - yield return ("LeftThumbX", ConstrainFloatInput(state.ThumbSticks.Left.X)); - yield return ("LeftThumbY", ConstrainFloatInput(state.ThumbSticks.Left.Y)); - yield return ("RightThumbX", ConstrainFloatInput(state.ThumbSticks.Right.X)); - yield return ("RightThumbY", ConstrainFloatInput(state.ThumbSticks.Right.Y)); - yield return ("LeftTrigger", ConstrainFloatInput(state.Triggers.Left)); - yield return ("RightTrigger", ConstrainFloatInput(state.Triggers.Right)); - yield break; - } - else - { - // use Joystick - yield return ("X", ConstrainFloatInput(jState.GetAxis(0))); - yield return ("Y", ConstrainFloatInput(jState.GetAxis(1))); - yield return ("Z", ConstrainFloatInput(jState.GetAxis(2))); - yield return ("W", ConstrainFloatInput(jState.GetAxis(3))); - yield return ("V", ConstrainFloatInput(jState.GetAxis(4))); - yield return ("S", ConstrainFloatInput(jState.GetAxis(5))); - yield return ("Q", ConstrainFloatInput(jState.GetAxis(6))); - yield return ("P", ConstrainFloatInput(jState.GetAxis(7))); - yield return ("N", ConstrainFloatInput(jState.GetAxis(8))); - - for (var i = 9; i < 64; i++) + return new[] { - var j = i; - yield return ($"Axis{j.ToString()}", ConstrainFloatInput(jState.GetAxis(j))); - } - - yield break; + ("LeftThumbX", ConstrainFloatInput(state.ThumbSticks.Left.X)), + ("LeftThumbY", ConstrainFloatInput(state.ThumbSticks.Left.Y)), + ("RightThumbX", ConstrainFloatInput(state.ThumbSticks.Right.X)), + ("RightThumbY", ConstrainFloatInput(state.ThumbSticks.Right.Y)), + ("LeftTrigger", ConstrainFloatInput(state.Triggers.Left)), + ("RightTrigger", ConstrainFloatInput(state.Triggers.Right)), + }; } + + // else use Joystick + List<(string AxisID, int Value)> values = new() + { + ("X", ConstrainFloatInput(jState.GetAxis(0))), + ("Y", ConstrainFloatInput(jState.GetAxis(1))), + ("Z", ConstrainFloatInput(jState.GetAxis(2))), + ("W", ConstrainFloatInput(jState.GetAxis(3))), + ("V", ConstrainFloatInput(jState.GetAxis(4))), + ("S", ConstrainFloatInput(jState.GetAxis(5))), + ("Q", ConstrainFloatInput(jState.GetAxis(6))), + ("P", ConstrainFloatInput(jState.GetAxis(7))), + ("N", ConstrainFloatInput(jState.GetAxis(8))), + }; + + for (var i = 9; i < 64; i++) + { + var j = i; + values.Add(($"Axis{j.ToString()}", ConstrainFloatInput(jState.GetAxis(j)))); + } + + return values; } /// Contains name and delegate function for all buttons, hats and axis diff --git a/src/BizHawk.Bizware.OpenTK3/OpenTKInputAdapter.cs b/src/BizHawk.Bizware.OpenTK3/OpenTKInputAdapter.cs index 70b4d1dc71..9692e354ba 100644 --- a/src/BizHawk.Bizware.OpenTK3/OpenTKInputAdapter.cs +++ b/src/BizHawk.Bizware.OpenTK3/OpenTKInputAdapter.cs @@ -34,7 +34,7 @@ namespace BizHawk.Bizware.OpenTK3 foreach (var pad in OTK_GamePad.EnumerateDevices()) { foreach (var but in pad.ButtonGetters) handleButton(pad.InputNamePrefix + but.ButtonName, but.GetIsPressed(), ClientInputFocus.Pad); - foreach (var (axisID, f) in pad.GetAxes()) handleAxis($"{pad.InputNamePrefix}{axisID} Axis", (int) f); + foreach (var (axisID, f) in pad.GetAxes()) handleAxis($"{pad.InputNamePrefix}{axisID} Axis", f); #if DEBUG // effectively no-op as OpenTK 3 doesn't seem to actually support haptic feedback foreach (var channel in pad.HapticsChannels) {