diff --git a/Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp b/Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp index 1c0da74a0f..dbecadb1d8 100644 --- a/Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp +++ b/Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp @@ -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()) { state->recentered_pitch = std::asin((inv_rotation * Common::Vec3{0, 1, 0}).z); target_yaw = 0; diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp b/Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp index 9dcb9d0766..f6f68d15ff 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp @@ -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()) f.setBold(true); setFont(f); diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp b/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp index a0921ec9f3..0d6570e87d 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp @@ -4,6 +4,8 @@ #include "DolphinQt/Config/Mapping/MappingNumeric.h" +#include + #include "DolphinQt/Config/Mapping/MappingWidget.h" #include "InputCommon/ControllerEmu/ControllerEmu.h" @@ -12,24 +14,20 @@ MappingDouble::MappingDouble(MappingWidget* parent, ControllerEmu::NumericSetting* 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::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::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::NumericSettingSaveSettings(); }); 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()); } diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.h b/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.h index 49817ea40c..267f556e7e 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.h +++ b/Source/Core/DolphinQt/Config/Mapping/MappingNumeric.h @@ -21,6 +21,7 @@ private: void fixup(QString& input) const override; void ConfigChanged(); + void Update(); ControllerEmu::NumericSetting& m_setting; }; @@ -32,6 +33,7 @@ public: private: void ConfigChanged(); + void Update(); ControllerEmu::NumericSetting& m_setting; }; diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp index 1e2aabd3ec..5ba6e70e29 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp @@ -11,6 +11,7 @@ #include #include +#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) diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h index a88c09b4ed..707332ccd8 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h @@ -14,7 +14,6 @@ constexpr int WIDGET_MAX_WIDTH = 112; class ControlGroupBox; class InputConfig; -class IOWindow; class MappingButton; class MappingNumeric; class MappingWindow; diff --git a/Source/Core/InputCommon/ControlReference/ControlReference.h b/Source/Core/InputCommon/ControlReference/ControlReference.h index 539c7f4927..80c4a65d51 100644 --- a/Source/Core/InputCommon/ControlReference/ControlReference.h +++ b/Source/Core/InputCommon/ControlReference/ControlReference.h @@ -7,6 +7,7 @@ #include #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 + 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() +{ + return State() > ciface::ExpressionParser::CONDITION_THRESHOLD; +} + +template +T ControlReference::GetState() +{ + return State(); +} + // // InputReference // diff --git a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp index 75f78526e6..4d8e2866db 100644 --- a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp +++ b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp @@ -10,8 +10,6 @@ namespace ciface::ExpressionParser { -constexpr ControlState CONDITION_THRESHOLD = 0.5; - using Clock = std::chrono::steady_clock; using FSec = std::chrono::duration; diff --git a/Source/Core/InputCommon/ControlReference/FunctionExpression.h b/Source/Core/InputCommon/ControlReference/FunctionExpression.h index e247d9a623..3b29baa9ae 100644 --- a/Source/Core/InputCommon/ControlReference/FunctionExpression.h +++ b/Source/Core/InputCommon/ControlReference/FunctionExpression.h @@ -15,6 +15,8 @@ namespace ciface::ExpressionParser { +constexpr ControlState CONDITION_THRESHOLD = 0.5; + class FunctionExpression : public Expression { public: diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h index c2311415e5..9560a37843 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h @@ -24,13 +24,11 @@ public: { for (auto& control : controls) { - if (control->control_ref->State() > ACTIVATION_THRESHOLD) + if (control->control_ref->GetState()) *buttons |= *bitmasks; bitmasks++; } } - - static constexpr ControlState ACTIVATION_THRESHOLD = 0.5; }; } // namespace ControllerEmu diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/ModifySettingsButton.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/ModifySettingsButton.cpp index 908de81a96..a902034795 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/ModifySettingsButton.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/ModifySettingsButton.cpp @@ -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(); 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; } } diff --git a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp index c5ff0705a2..5d4d16fe76 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp @@ -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) { 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