CommonHostInterface: Scale full axis to half axis bindings

This commit is contained in:
Connor McLaughlin 2021-07-03 21:09:10 +10:00
parent 4ddab3609d
commit 3da6c604d7

View file

@ -1914,16 +1914,39 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
StringUtil::FromChars<int>(axis.substr(axis[0] == '+' || axis[0] == '-' ? 5 : 4)); StringUtil::FromChars<int>(axis.substr(axis[0] == '+' || axis[0] == '-' ? 5 : 4));
if (axis_index) if (axis_index)
{ {
const bool inverted = StringUtil::EndsWith(axis, "-");
ControllerInterface::AxisSide axis_side = ControllerInterface::AxisSide::Full; ControllerInterface::AxisSide axis_side = ControllerInterface::AxisSide::Full;
if (axis[0] == '+') if (axis[0] == '+')
{
axis_side = ControllerInterface::AxisSide::Positive; axis_side = ControllerInterface::AxisSide::Positive;
}
else if (axis[0] == '-') else if (axis[0] == '-')
{
axis_side = ControllerInterface::AxisSide::Negative; axis_side = ControllerInterface::AxisSide::Negative;
}
const bool inverted = StringUtil::EndsWith(axis, "-"); if (axis_type == Controller::AxisType::Half && axis_side == ControllerInterface::Full)
{
// full axis [-1..1] -> half axis [0..1]
if (inverted)
{
m_controller_interface->BindControllerAxis(
*controller_index, *axis_index, axis_side,
[cb = std::move(handler)](float value) { cb(((-value) + 1.0f) * 0.5f); });
}
else
{
m_controller_interface->BindControllerAxis(
*controller_index, *axis_index, axis_side,
[cb = std::move(handler)](float value) { cb((value + 1.0f) * 0.5f); });
}
}
else
{
if (!inverted) if (!inverted)
{ {
if (m_controller_interface->BindControllerAxis(*controller_index, *axis_index, axis_side, std::move(handler))) if (m_controller_interface->BindControllerAxis(*controller_index, *axis_index, axis_side,
std::move(handler)))
{ {
return true; return true;
} }
@ -1937,6 +1960,7 @@ bool CommonHostInterface::AddAxisToInputMap(const std::string& binding, const st
} }
} }
} }
}
Log_WarningPrintf("Failed to bind controller axis '%s' to axis", binding.c_str()); Log_WarningPrintf("Failed to bind controller axis '%s' to axis", binding.c_str());
return false; return false;
} }