InputCommon: Allow controller settings specified with input expresions.

This commit is contained in:
Jordan Woyak
2019-10-18 14:54:02 -05:00
parent 6a857df219
commit e8152b700f
13 changed files with 172 additions and 28 deletions

View File

@ -24,13 +24,11 @@ public:
{
for (auto& control : controls)
{
if (control->control_ref->State() > ACTIVATION_THRESHOLD)
if (control->control_ref->GetState<bool>())
*buttons |= *bitmasks;
bitmasks++;
}
}
static constexpr ControlState ACTIVATION_THRESHOLD = 0.5;
};
} // namespace ControllerEmu

View File

@ -35,19 +35,19 @@ void ModifySettingsButton::GetState()
{
for (size_t i = 0; i < controls.size(); ++i)
{
ControlState state = controls[i]->control_ref->State();
const bool state = controls[i]->control_ref->GetState<bool>();
if (!associated_settings_toggle[i])
{
// not toggled
associated_settings[i] = state > ACTIVATION_THRESHOLD;
associated_settings[i] = state;
}
else
{
// toggle (loading savestates does not en-/disable toggle)
// after we passed the threshold, we en-/disable. but after that, we don't change it
// anymore
if (!threshold_exceeded[i] && state > ACTIVATION_THRESHOLD)
if (!threshold_exceeded[i] && state)
{
associated_settings[i] = !associated_settings[i];
@ -59,7 +59,7 @@ void ModifySettingsButton::GetState()
threshold_exceeded[i] = true;
}
if (state < ACTIVATION_THRESHOLD)
if (!state)
threshold_exceeded[i] = false;
}
}

View File

@ -14,6 +14,7 @@
#include "InputCommon/ControllerEmu/Control/Control.h"
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
namespace ControllerEmu
@ -54,6 +55,9 @@ void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvir
for (auto& control : ctrlGroup->controls)
control->control_ref->UpdateReference(env);
for (auto& setting : ctrlGroup->numeric_settings)
setting->GetInputReference().UpdateReference(env);
// Attachments:
if (ctrlGroup->type == GroupType::Attachments)
{

View File

@ -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<double>(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<T>;
public:
ValueType GetValue() const { return m_value; }
ValueType GetValue() const
{
if (IsSimpleValue())
return m_value;
else
return m_input.GetState<ValueType>();
}
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<ValueType> m_value = {};
// Unfortunately InputReference's state grabbing is non-const requiring mutable here.
mutable InputReference m_input;
};
} // namespace ControllerEmu