From a11a7017a7a31db3a31cd5030242669bad850bd3 Mon Sep 17 00:00:00 2001 From: James Groom Date: Sun, 30 Aug 2020 04:39:48 +1000 Subject: [PATCH] Fix scaling of axes (#2334) * Cleanup NormalizeAxes no behaviour change yet * Use a more sensible scaling algorithm in NormalizeAxes * Replace AxisSpec.FloatRange with AxisSpec.Range (ints) * Use 0..255 for GBA light sensor, and set its Mid to 0 (fixes #2323) --- src/BizHawk.Client.Common/Controller.cs | 57 ++++++------------- .../tools/TAStudio/TAStudio.ListView.cs | 3 +- .../Base Implementations/Axes/AxisSpec.cs | 2 - .../Consoles/Nintendo/GBA/MGBAHawk.cs | 2 +- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/src/BizHawk.Client.Common/Controller.cs b/src/BizHawk.Client.Common/Controller.cs index bb20b495d3..973d54de0a 100644 --- a/src/BizHawk.Client.Common/Controller.cs +++ b/src/BizHawk.Client.Common/Controller.cs @@ -49,52 +49,31 @@ namespace BizHawk.Client.Common .SelectMany(kvp => kvp.Value) .Any(boundButton => boundButton == button); - public void NormalizeAxes(IController controller) + public void NormalizeAxes() { foreach (var kvp in _axisBindings) { - var input = (float) _axes[kvp.Key]; - string outKey = kvp.Key; - float multiplier = kvp.Value.Mult; - float deadZone = kvp.Value.Deadzone; - if (_axisRanges.TryGetValue(outKey, out var range)) - { - // input range is assumed to be -10000,0,10000 + if (!_axisRanges.TryGetValue(kvp.Key, out var range)) continue; //TODO throw (or use indexer instead of TryGetValue)? this `continue` should never be hit --yoshi - // first, modify for deadZone - float absInput = Math.Abs(input); - float zeroPoint = deadZone * 10000.0f; - if (absInput < zeroPoint) - { - input = 0.0f; - } - else - { - absInput -= zeroPoint; - absInput *= 10000.0f; - absInput /= 10000.0f - zeroPoint; - input = absInput * Math.Sign(input); - } + // values of _axes are ints in -10000..10000 (or 0..10000), so scale to -1..1, using floats to keep fractional part + var value = _axes[kvp.Key] / 10000.0f; - // zero 09-mar-2015 - not sure if adding + 1 here is correct.. but... maybe? - float output; + // apply deadzone (and scale diminished range back up to -1..1) + var deadzone = kvp.Value.Deadzone; + if (value < -deadzone) value += deadzone; + else if (value < deadzone) value = 0.0f; + else value -= deadzone; + value /= 1.0f - deadzone; - if (range.IsReversed) - { - output = (((input * multiplier) + 10000.0f) * (range.Min - range.Max + 1) / 20000.0f) + range.Max; - } - else - { - output = (((input * multiplier) + 10000.0f) * (range.Max - range.Min + 1) / 20000.0f) + range.Min; - } + // scale by user-set multiplier (which is 0..1, i.e. value can only shrink and is therefore still in -1..1) + value *= kvp.Value.Mult; - // zero 09-mar-2015 - at this point, we should only have integers, since that's all 100% of consoles ever see - // if this becomes a problem we can add flags to the range and update GUIs to be able to display floats + // -1..1 -> range + value += range.Mid; + value *= Math.Max(range.Mid - range.Min, range.Max - range.Mid); - // fixed maybe? --yoshi - - _axes[outKey] = (int) output.ConstrainWithin(range.FloatRange); - } + // finally, constrain to range again in case the original value was unexpectedly large, or the deadzone and scale made it so, or the axis is lopsided + _axes[kvp.Key] = ((int) value).ConstrainWithin(range.Range); } } @@ -129,7 +108,7 @@ namespace BizHawk.Client.Common } // it's not sure where this should happen, so for backwards compatibility.. do it every time - NormalizeAxes(controller); + NormalizeAxes(); } public void ApplyAxisConstraints(string constraintClass) diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs index 476fced6e5..83ac3c76ed 100644 --- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs +++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.ListView.cs @@ -1267,7 +1267,8 @@ namespace BizHawk.Client.EmuHawk string prevTyped = _axisTypedValue; var range = ControllerType.Axes[_axisEditColumn]; - var (rMin, rMax) = range.FloatRange; + float rMin = range.Min; + float rMax = range.Max; // feos: typing past max digits overwrites existing value, not touching the sign // but doesn't handle situations where the range is like -50 through 100, where minimum is negative and has less digits diff --git a/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisSpec.cs b/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisSpec.cs index 491a1f3a56..229da39724 100644 --- a/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisSpec.cs +++ b/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisSpec.cs @@ -15,8 +15,6 @@ namespace BizHawk.Emulation.Common /// public readonly AxisConstraint? Constraint; - public Range FloatRange => ((float) Min).RangeTo(Max); - public readonly bool IsReversed; public int Max => Range.EndInclusive; diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs index 65ce72e702..3a914d1422 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/GBA/MGBAHawk.cs @@ -267,6 +267,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.GBA Name = "GBA Controller", BoolButtons = { "Up", "Down", "Left", "Right", "Start", "Select", "B", "A", "L", "R", "Power" } }.AddXYZTriple("Tilt {0}", (-32767).RangeTo(32767), 0) - .AddAxis("Light Sensor", 0.RangeTo(200), 100); + .AddAxis("Light Sensor", 0.RangeTo(255), 0); } }