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

View File

@ -3,7 +3,7 @@
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
#include <sstream>
#include <fmt/format.h>
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
{
return m_details.ini_name;
@ -36,28 +41,20 @@ SettingVisibility NumericSettingBase::GetVisibility() const
return m_details.visibility;
}
template <>
void NumericSetting<int>::SetExpressionFromValue()
template <typename T>
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 <>
void NumericSetting<double>::SetExpressionFromValue()
template <typename T>
void NumericSetting<T>::SimplifyIfPossible()
{
// We must use a dot decimal separator for expression parser.
std::ostringstream ss;
ss.imbue(std::locale::classic());
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())));
ValueType value;
if (TryParse(std::string(StripWhitespace(m_value.m_input.GetExpression())), &value))
m_value.SetValue(value);
}
template <>

View File

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