From e8152b700f76da97d346b93662ef8492bc93190c Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Fri, 18 Oct 2019 14:54:02 -0500 Subject: InputCommon: Allow controller settings specified with input expresions. --- .../ControllerEmu/Setting/NumericSetting.h | 70 ++++++++++++++++++++-- 1 file changed, 64 insertions(+), 6 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h') diff --git a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h index 108b228049..6884e7756b 100644 --- a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h +++ b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h @@ -9,6 +9,7 @@ #include "Common/CommonTypes.h" #include "Common/IniFile.h" +#include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControllerInterface/Device.h" namespace ControllerEmu @@ -52,6 +53,15 @@ public: virtual void LoadFromIni(const IniFile::Section& section, const std::string& group_name) = 0; virtual void SaveToIni(IniFile::Section& section, const std::string& group_name) const = 0; + virtual InputReference& GetInputReference() = 0; + virtual const InputReference& GetInputReference() const = 0; + + // Convert a literal expression e.g. "7.0" to a regular value. (disables expression parsing) + virtual void SimplifyIfPossible() = 0; + + // Convert a regular value to an expression. (used before expression editing) + virtual void SetExpressionFromValue() = 0; + virtual SettingType GetType() const = 0; const char* GetUIName() const; @@ -84,16 +94,47 @@ public: void LoadFromIni(const IniFile::Section& section, const std::string& group_name) override { - ValueType value; - section.Get(group_name + m_details.ini_name, &value, m_default_value); - SetValue(value); + std::string str_value; + if (section.Get(group_name + m_details.ini_name, &str_value)) + { + m_value.m_input.SetExpression(std::move(str_value)); + SimplifyIfPossible(); + } + else + { + SetValue(m_default_value); + } } void SaveToIni(IniFile::Section& section, const std::string& group_name) const override { - section.Set(group_name + m_details.ini_name, GetValue(), m_default_value); + if (IsSimpleValue()) + section.Set(group_name + m_details.ini_name, GetValue(), m_default_value); + else + section.Set(group_name + m_details.ini_name, m_value.m_input.GetExpression(), ""); } + bool IsSimpleValue() const { return m_value.IsSimpleValue(); } + + void SimplifyIfPossible() override + { + ValueType value; + if (TryParse(m_value.m_input.GetExpression(), &value)) + m_value.SetValue(value); + } + + void SetExpressionFromValue() override + { + if (!IsSimpleValue()) + return; + + // Cast to double to prevent bool -> "true"/"false" strings. + m_value.m_input.SetExpression(ValueToString(static_cast(GetValue()))); + } + + InputReference& GetInputReference() override { return m_value.m_input; } + const InputReference& GetInputReference() const override { return m_value.m_input; } + ValueType GetValue() const { return m_value.GetValue(); } void SetValue(ValueType value) { m_value.SetValue(value); } @@ -119,13 +160,30 @@ class SettingValue friend class NumericSetting; public: - ValueType GetValue() const { return m_value; } + ValueType GetValue() const + { + if (IsSimpleValue()) + return m_value; + else + return m_input.GetState(); + } + + bool IsSimpleValue() const { return m_input.GetExpression().empty(); } private: - void SetValue(ValueType value) { m_value = value; } + void SetValue(ValueType value) + { + m_value = value; + + // Clear the expression to use our new "simple" value. + m_input.SetExpression(""); + } // Values are R/W by both UI and CPU threads. std::atomic m_value = {}; + + // Unfortunately InputReference's state grabbing is non-const requiring mutable here. + mutable InputReference m_input; }; } // namespace ControllerEmu -- cgit v1.2.3 From 0a1634bedf17f7b27a3781d8e5ac6bafb20cd546 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sat, 18 Jan 2020 11:19:32 -0600 Subject: InputCommon: Allow Wii remote extension to be set with an input expression. --- .../ControllerEmu/Setting/NumericSetting.h | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h') diff --git a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h index 6884e7756b..c7298391f4 100644 --- a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h +++ b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h @@ -16,6 +16,7 @@ namespace ControllerEmu { enum class SettingType { + Int, Double, Bool, }; @@ -56,6 +57,8 @@ public: virtual InputReference& GetInputReference() = 0; virtual const InputReference& GetInputReference() const = 0; + virtual bool IsSimpleValue() const = 0; + // Convert a literal expression e.g. "7.0" to a regular value. (disables expression parsing) virtual void SimplifyIfPossible() = 0; @@ -76,13 +79,14 @@ template class SettingValue; template -class NumericSetting : public NumericSettingBase +class NumericSetting final : public NumericSettingBase { public: using ValueType = T; - static_assert(std::is_same() || std::is_same(), - "NumericSetting is only implemented for double and bool."); + static_assert(std::is_same() || std::is_same() || + std::is_same(), + "NumericSetting is only implemented for int, double, and bool."); NumericSetting(SettingValue* value, const NumericSettingDetails& details, ValueType default_value, ValueType min_value, ValueType max_value) @@ -114,7 +118,7 @@ public: section.Set(group_name + m_details.ini_name, m_value.m_input.GetExpression(), ""); } - bool IsSimpleValue() const { return m_value.IsSimpleValue(); } + bool IsSimpleValue() const override { return m_value.IsSimpleValue(); } void SimplifyIfPossible() override { @@ -123,15 +127,7 @@ public: m_value.SetValue(value); } - void SetExpressionFromValue() override - { - if (!IsSimpleValue()) - return; - - // Cast to double to prevent bool -> "true"/"false" strings. - m_value.m_input.SetExpression(ValueToString(static_cast(GetValue()))); - } - + void SetExpressionFromValue() override; InputReference& GetInputReference() override { return m_value.m_input; } const InputReference& GetInputReference() const override { return m_value.m_input; } -- cgit v1.2.3