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

@ -330,8 +330,7 @@ void EmulateIMUCursor(IMUCursorState* state, ControllerEmu::IMUCursor* imu_ir_gr
auto target_yaw = std::clamp(yaw, -max_yaw, max_yaw);
// Handle the "Recenter" button being pressed.
if (imu_ir_group->controls[0]->control_ref->State() >
ControllerEmu::Buttons::ACTIVATION_THRESHOLD)
if (imu_ir_group->controls[0]->control_ref->GetState<bool>())
{
state->recentered_pitch = std::asin((inv_rotation * Common::Vec3{0, 1, 0}).z);
target_yaw = 0;

View File

@ -132,11 +132,9 @@ void MappingButton::UpdateIndicator()
if (!isActiveWindow())
return;
const auto state = m_reference->State();
QFont f = m_parent->font();
if (state > ControllerEmu::Buttons::ACTIVATION_THRESHOLD)
if (m_reference->GetState<bool>())
f.setBold(true);
setFont(f);

View File

@ -4,6 +4,8 @@
#include "DolphinQt/Config/Mapping/MappingNumeric.h"
#include <limits>
#include "DolphinQt/Config/Mapping/MappingWidget.h"
#include "InputCommon/ControllerEmu/ControllerEmu.h"
@ -12,24 +14,20 @@
MappingDouble::MappingDouble(MappingWidget* parent, ControllerEmu::NumericSetting<double>* setting)
: QDoubleSpinBox(parent), m_setting(*setting)
{
setRange(m_setting.GetMinValue(), m_setting.GetMaxValue());
setDecimals(2);
setFixedWidth(WIDGET_MAX_WIDTH);
if (const auto ui_suffix = m_setting.GetUISuffix())
setSuffix(QLatin1Char{' '} + tr(ui_suffix));
if (const auto ui_description = m_setting.GetUIDescription())
setToolTip(tr(ui_description));
connect(this, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
[this, parent](double value) {
m_setting.SetValue(value);
ConfigChanged();
parent->SaveSettings();
});
connect(parent, &MappingWidget::ConfigChanged, this, &MappingDouble::ConfigChanged);
connect(parent, &MappingWidget::Update, this, &MappingDouble::Update);
}
// Overriding QDoubleSpinBox's fixup to set the default value when input is cleared.
@ -41,6 +39,36 @@ void MappingDouble::fixup(QString& input) const
void MappingDouble::ConfigChanged()
{
const QSignalBlocker blocker(this);
QString suffix;
if (const auto ui_suffix = m_setting.GetUISuffix())
suffix += QLatin1Char{' '} + tr(ui_suffix);
if (m_setting.IsSimpleValue())
{
setRange(m_setting.GetMinValue(), m_setting.GetMaxValue());
setButtonSymbols(ButtonSymbols::UpDownArrows);
}
else
{
constexpr auto inf = std::numeric_limits<double>::infinity();
setRange(-inf, inf);
setButtonSymbols(ButtonSymbols::NoButtons);
suffix += QString::fromUtf8(" 🎮");
}
setSuffix(suffix);
setValue(m_setting.GetValue());
}
void MappingDouble::Update()
{
if (m_setting.IsSimpleValue() || hasFocus())
return;
const QSignalBlocker blocker(this);
setValue(m_setting.GetValue());
}
@ -49,14 +77,31 @@ MappingBool::MappingBool(MappingWidget* parent, ControllerEmu::NumericSetting<bo
{
connect(this, &QCheckBox::stateChanged, this, [this, parent](int value) {
m_setting.SetValue(value != 0);
ConfigChanged();
parent->SaveSettings();
});
connect(parent, &MappingWidget::ConfigChanged, this, &MappingBool::ConfigChanged);
connect(parent, &MappingWidget::Update, this, &MappingBool::Update);
}
void MappingBool::ConfigChanged()
{
const QSignalBlocker blocker(this);
if (m_setting.IsSimpleValue())
setText({});
else
setText(QString::fromUtf8("🎮"));
setChecked(m_setting.GetValue());
}
void MappingBool::Update()
{
if (m_setting.IsSimpleValue())
return;
const QSignalBlocker blocker(this);
setChecked(m_setting.GetValue());
}

View File

@ -21,6 +21,7 @@ private:
void fixup(QString& input) const override;
void ConfigChanged();
void Update();
ControllerEmu::NumericSetting<double>& m_setting;
};
@ -32,6 +33,7 @@ public:
private:
void ConfigChanged();
void Update();
ControllerEmu::NumericSetting<bool>& m_setting;
};

View File

@ -11,6 +11,7 @@
#include <QPushButton>
#include <QTimer>
#include "DolphinQt/Config/Mapping/IOWindow.h"
#include "DolphinQt/Config/Mapping/MappingButton.h"
#include "DolphinQt/Config/Mapping/MappingIndicator.h"
#include "DolphinQt/Config/Mapping/MappingNumeric.h"
@ -141,7 +142,31 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
}
if (setting_widget)
form_layout->addRow(tr(setting->GetUIName()), setting_widget);
{
const auto hbox = new QHBoxLayout;
hbox->addWidget(setting_widget);
const auto advanced_button = new QPushButton(tr("..."));
advanced_button->setFixedWidth(
QFontMetrics(font()).boundingRect(advanced_button->text()).width() * 2);
hbox->addWidget(advanced_button);
advanced_button->connect(
advanced_button, &QPushButton::clicked, [this, &setting = *setting.get()]() {
setting.SetExpressionFromValue();
IOWindow io(this, GetController(), &setting.GetInputReference(), IOWindow::Type::Input);
io.exec();
setting.SimplifyIfPossible();
ConfigChanged();
SaveSettings();
});
form_layout->addRow(tr(setting->GetUIName()), hbox);
}
}
if (group->can_be_disabled)

View File

@ -14,7 +14,6 @@ constexpr int WIDGET_MAX_WIDTH = 112;
class ControlGroupBox;
class InputConfig;
class IOWindow;
class MappingButton;
class MappingNumeric;
class MappingWindow;

View File

@ -7,6 +7,7 @@
#include <memory>
#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControlReference/FunctionExpression.h"
#include "InputCommon/ControllerInterface/Device.h"
// ControlReference
@ -30,6 +31,9 @@ public:
virtual ControlState State(const ControlState state = 0) = 0;
virtual bool IsInput() const = 0;
template <typename T>
T GetState();
int BoundCount() const;
ciface::ExpressionParser::ParseStatus GetParseStatus() const;
void UpdateReference(ciface::ExpressionParser::ControlEnvironment& env);
@ -45,6 +49,18 @@ protected:
ciface::ExpressionParser::ParseStatus m_parse_status;
};
template <>
inline bool ControlReference::GetState<bool>()
{
return State() > ciface::ExpressionParser::CONDITION_THRESHOLD;
}
template <typename T>
T ControlReference::GetState()
{
return State();
}
//
// InputReference
//

View File

@ -10,8 +10,6 @@
namespace ciface::ExpressionParser
{
constexpr ControlState CONDITION_THRESHOLD = 0.5;
using Clock = std::chrono::steady_clock;
using FSec = std::chrono::duration<ControlState>;

View File

@ -15,6 +15,8 @@
namespace ciface::ExpressionParser
{
constexpr ControlState CONDITION_THRESHOLD = 0.5;
class FunctionExpression : public Expression
{
public:

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