InputCommon: Allow Wii remote extension to be set with an input expression.

This commit is contained in:
Jordan Woyak
2020-01-18 11:19:32 -06:00
parent e8152b700f
commit 0a1634bedf
12 changed files with 172 additions and 64 deletions

View File

@ -17,12 +17,22 @@ void Attachments::AddAttachment(std::unique_ptr<EmulatedController> att)
u32 Attachments::GetSelectedAttachment() const
{
return m_selected_attachment;
const u32 value = m_selection_value.GetValue();
if (value < m_attachments.size())
return value;
return 0;
}
void Attachments::SetSelectedAttachment(u32 val)
{
m_selected_attachment = val;
m_selection_setting.SetValue(val);
}
NumericSetting<int>& Attachments::GetSelectionSetting()
{
return m_selection_setting;
}
const std::vector<std::unique_ptr<EmulatedController>>& Attachments::GetAttachmentList() const

View File

@ -12,6 +12,7 @@
#include "Common/CommonTypes.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
namespace ControllerEmu
{
@ -27,11 +28,14 @@ public:
u32 GetSelectedAttachment() const;
void SetSelectedAttachment(u32 val);
NumericSetting<int>& GetSelectionSetting();
const std::vector<std::unique_ptr<EmulatedController>>& GetAttachmentList() const;
private:
std::vector<std::unique_ptr<EmulatedController>> m_attachments;
SettingValue<int> m_selection_value;
NumericSetting<int> m_selection_setting = {&m_selection_value, {""}, 0, 0, 0};
std::atomic<u32> m_selected_attachment = {};
std::vector<std::unique_ptr<EmulatedController>> m_attachments;
};
} // namespace ControllerEmu

View File

@ -74,15 +74,19 @@ void ControlGroup::LoadConfig(IniFile::Section* sec, const std::string& defdev,
ext->SetSelectedAttachment(0);
u32 n = 0;
std::string extname;
sec->Get(base + name, &extname, "");
std::string attachment_text;
sec->Get(base + name, &attachment_text, "");
// First assume attachment string is a valid expression.
// If it instead matches one of the names of our attachments it is overridden below.
ext->GetSelectionSetting().GetInputReference().SetExpression(attachment_text);
for (auto& ai : ext->GetAttachmentList())
{
ai->SetDefaultDevice(defdev);
ai->LoadConfig(sec, base + ai->GetName() + "/");
if (ai->GetName() == extname)
if (ai->GetName() == attachment_text)
ext->SetSelectedAttachment(n);
n++;
@ -114,8 +118,16 @@ void ControlGroup::SaveConfig(IniFile::Section* sec, const std::string& defdev,
if (type == GroupType::Attachments)
{
auto* const ext = static_cast<Attachments*>(this);
sec->Set(base + name, ext->GetAttachmentList()[ext->GetSelectedAttachment()]->GetName(),
"None");
if (ext->GetSelectionSetting().IsSimpleValue())
{
sec->Set(base + name, ext->GetAttachmentList()[ext->GetSelectedAttachment()]->GetName(),
"None");
}
else
{
sec->Set(base + name, ext->GetSelectionSetting().GetInputReference().GetExpression(), "None");
}
for (auto& ai : ext->GetAttachmentList())
ai->SaveConfig(sec, base + ai->GetName() + "/");

View File

@ -61,7 +61,11 @@ void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvir
// Attachments:
if (ctrlGroup->type == GroupType::Attachments)
{
for (auto& attachment : static_cast<Attachments*>(ctrlGroup.get())->GetAttachmentList())
auto* const attachments = static_cast<Attachments*>(ctrlGroup.get());
attachments->GetSelectionSetting().GetInputReference().UpdateReference(env);
for (auto& attachment : attachments->GetAttachmentList())
attachment->UpdateReferences(env);
}
}

View File

@ -4,6 +4,8 @@
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
#include <sstream>
namespace ControllerEmu
{
NumericSettingBase::NumericSettingBase(const NumericSettingDetails& details) : m_details(details)
@ -25,6 +27,36 @@ const char* NumericSettingBase::GetUIDescription() const
return m_details.ui_description;
}
template <>
void NumericSetting<int>::SetExpressionFromValue()
{
m_value.m_input.SetExpression(ValueToString(GetValue()));
}
template <>
void NumericSetting<double>::SetExpressionFromValue()
{
// 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())));
}
template <>
SettingType NumericSetting<int>::GetType() const
{
return SettingType::Int;
}
template <>
SettingType NumericSetting<double>::GetType() const
{

View File

@ -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 <typename T>
class SettingValue;
template <typename T>
class NumericSetting : public NumericSettingBase
class NumericSetting final : public NumericSettingBase
{
public:
using ValueType = T;
static_assert(std::is_same<ValueType, double>() || std::is_same<ValueType, bool>(),
"NumericSetting is only implemented for double and bool.");
static_assert(std::is_same<ValueType, int>() || std::is_same<ValueType, double>() ||
std::is_same<ValueType, bool>(),
"NumericSetting is only implemented for int, double, and bool.");
NumericSetting(SettingValue<ValueType>* 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<double>(GetValue())));
}
void SetExpressionFromValue() override;
InputReference& GetInputReference() override { return m_value.m_input; }
const InputReference& GetInputReference() const override { return m_value.m_input; }