NumericSetting: Stop values from binding to numbered input names.

This commit is contained in:
Jordan Woyak 2024-03-20 22:40:01 -05:00
parent 5039072ae9
commit e9fe0d3d5b
3 changed files with 36 additions and 34 deletions

View File

@ -9,6 +9,12 @@
#include "InputCommon/ControlReference/ExpressionParser.h" #include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControllerInterface/CoreDevice.h" #include "InputCommon/ControllerInterface/CoreDevice.h"
namespace ControllerEmu
{
template <typename T>
T ControlStateCast(ControlState value);
}
// ControlReference // ControlReference
// //
// These are what you create to actually use the inputs, InputReference or OutputReference. // These are what you create to actually use the inputs, InputReference or OutputReference.
@ -31,7 +37,10 @@ public:
virtual bool IsInput() const = 0; virtual bool IsInput() const = 0;
template <typename T> template <typename T>
T GetState(); T GetState()
{
return ControllerEmu::ControlStateCast<T>(State());
}
int BoundCount() const; int BoundCount() const;
ciface::ExpressionParser::ParseStatus GetParseStatus() const; ciface::ExpressionParser::ParseStatus GetParseStatus() const;
@ -51,24 +60,27 @@ protected:
ciface::ExpressionParser::ParseStatus::EmptyExpression; ciface::ExpressionParser::ParseStatus::EmptyExpression;
}; };
namespace ControllerEmu
{
template <> template <>
inline bool ControlReference::GetState<bool>() inline bool ControlStateCast<bool>(ControlState value)
{ {
// Round to nearest of 0 or 1. // Round to nearest of 0 or 1.
return std::lround(State()) > 0; return std::lround(value) > 0;
} }
template <> template <>
inline int ControlReference::GetState<int>() inline int ControlStateCast<int>(ControlState value)
{ {
return std::lround(State()); return std::lround(value);
} }
template <> template <>
inline ControlState ControlReference::GetState<ControlState>() inline ControlState ControlStateCast<ControlState>(ControlState value)
{ {
return State(); return value;
} }
} // namespace ControllerEmu
// //
// InputReference // InputReference

View File

@ -3,7 +3,7 @@
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h" #include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
#include <sstream> #include <fmt/format.h>
namespace ControllerEmu namespace ControllerEmu
{ {
@ -11,6 +11,11 @@ NumericSettingBase::NumericSettingBase(const NumericSettingDetails& details) : m
{ {
} }
// Explicit instantiations so generic definitions can exist outside of the header.
template class NumericSetting<int>;
template class NumericSetting<double>;
template class NumericSetting<bool>;
const char* NumericSettingBase::GetININame() const const char* NumericSettingBase::GetININame() const
{ {
return m_details.ini_name; return m_details.ini_name;
@ -36,28 +41,20 @@ SettingVisibility NumericSettingBase::GetVisibility() const
return m_details.visibility; return m_details.visibility;
} }
template <> template <typename T>
void NumericSetting<int>::SetExpressionFromValue() void NumericSetting<T>::SetExpressionFromValue()
{ {
m_value.m_input.SetExpression(ValueToString(GetValue())); // Always include -/+ sign to prevent CoalesceExpression binding.
// e.g. 1 is a valid input name for keyboard devices, +1 is not.
m_value.m_input.SetExpression(fmt::format("{:+g}", ControlState(GetValue())));
} }
template <> template <typename T>
void NumericSetting<double>::SetExpressionFromValue() void NumericSetting<T>::SimplifyIfPossible()
{ {
// We must use a dot decimal separator for expression parser. ValueType value;
std::ostringstream ss; if (TryParse(std::string(StripWhitespace(m_value.m_input.GetExpression())), &value))
ss.imbue(std::locale::classic()); m_value.SetValue(value);
ss << GetValue();
m_value.m_input.SetExpression(ss.str());
}
template <>
void NumericSetting<bool>::SetExpressionFromValue()
{
// Cast bool to prevent "true"/"false" strings.
m_value.m_input.SetExpression(ValueToString(int(GetValue())));
} }
template <> template <>

View File

@ -143,14 +143,7 @@ public:
} }
bool IsSimpleValue() const override { return m_value.IsSimpleValue(); } bool IsSimpleValue() const override { return m_value.IsSimpleValue(); }
void SimplifyIfPossible() override;
void SimplifyIfPossible() override
{
ValueType value;
if (TryParse(m_value.m_input.GetExpression(), &value))
m_value.SetValue(value);
}
void SetExpressionFromValue() override; void SetExpressionFromValue() override;
InputReference& GetInputReference() override { return m_value.m_input; } InputReference& GetInputReference() override { return m_value.m_input; }
const InputReference& GetInputReference() const override { return m_value.m_input; } const InputReference& GetInputReference() const override { return m_value.m_input; }