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)
This commit is contained in:
James Groom 2020-08-30 04:39:48 +10:00 committed by GitHub
parent 4456e84bf0
commit a11a7017a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 43 deletions

View File

@ -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)

View File

@ -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

View File

@ -15,8 +15,6 @@ namespace BizHawk.Emulation.Common
/// </summary>
public readonly AxisConstraint? Constraint;
public Range<float> FloatRange => ((float) Min).RangeTo(Max);
public readonly bool IsReversed;
public int Max => Range.EndInclusive;

View File

@ -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);
}
}