input: clamp 0-1 in NormalizeDirectedInput

If you called this function with a value smaller than threshold, you
would get an overflow. This never happened, because we always passed
values bigger than threshold. Let's better fix this anyway.
This commit is contained in:
Megamouse 2023-08-26 09:56:50 +02:00
parent f4c2b4cc7e
commit c727795881
1 changed files with 10 additions and 15 deletions

View File

@ -55,15 +55,14 @@ u16 PadHandlerBase::NormalizeTriggerInput(u16 value, int threshold) const
{ {
return static_cast<u16>(0); return static_cast<u16>(0);
} }
else if (threshold <= trigger_min)
if (threshold <= trigger_min)
{ {
return static_cast<u16>(ScaledInput(value, trigger_min, trigger_max)); return static_cast<u16>(ScaledInput(value, trigger_min, trigger_max));
} }
else
{ const s32 val = static_cast<s32>(static_cast<f32>(trigger_max) * (value - threshold) / (trigger_max - threshold));
const s32 val = static_cast<s32>(static_cast<f32>(trigger_max) * (value - threshold) / (trigger_max - threshold)); return static_cast<u16>(ScaledInput(val, trigger_min, trigger_max));
return static_cast<u16>(ScaledInput(val, trigger_min, trigger_max));
}
} }
// normalizes a directed input, meaning it will correspond to a single "button" and not an axis with two directions // normalizes a directed input, meaning it will correspond to a single "button" and not an axis with two directions
@ -81,11 +80,9 @@ u16 PadHandlerBase::NormalizeDirectedInput(s32 raw_value, s32 threshold, s32 max
{ {
return static_cast<u16>(255.0f * val); return static_cast<u16>(255.0f * val);
} }
else
{ const f32 thresh = static_cast<f32>(threshold) / maximum; // threshold converted to [0, 1]
const f32 thresh = static_cast<f32>(threshold) / maximum; // threshold converted to [0, 1] return static_cast<u16>(255.0f * std::clamp((val - thresh) / (1.0f - thresh), 0.0f, 1.0f));
return static_cast<u16>(255.0f * std::min(1.0f, (val - thresh) / (1.0f - thresh)));
}
} }
u16 PadHandlerBase::NormalizeStickInput(u16 raw_value, int threshold, int multiplier, bool ignore_threshold) const u16 PadHandlerBase::NormalizeStickInput(u16 raw_value, int threshold, int multiplier, bool ignore_threshold) const
@ -96,10 +93,8 @@ u16 PadHandlerBase::NormalizeStickInput(u16 raw_value, int threshold, int multip
{ {
return static_cast<u16>(ScaledInput(scaled_value, 0, thumb_max)); return static_cast<u16>(ScaledInput(scaled_value, 0, thumb_max));
} }
else
{ return NormalizeDirectedInput(scaled_value, threshold, thumb_max);
return NormalizeDirectedInput(scaled_value, threshold, thumb_max);
}
} }
// This function normalizes stick deadzone based on the DS3's deadzone, which is ~13% // This function normalizes stick deadzone based on the DS3's deadzone, which is ~13%