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..6079bd4881 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,33 @@ MappingBool::MappingBool(MappingWidget* parent, ControllerEmu::NumericSettingSaveSettings(); }); connect(parent, &MappingWidget::ConfigChanged, this, &MappingBool::ConfigChanged); + connect(parent, &MappingWidget::Update, this, &MappingBool::Update); + + setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored); } 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..eb39bac64f 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp @@ -9,8 +9,8 @@ #include #include #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" @@ -28,14 +28,6 @@ MappingWidget::MappingWidget(MappingWindow* parent) : m_parent(parent) connect(parent, &MappingWindow::Update, this, &MappingWidget::Update); connect(parent, &MappingWindow::Save, this, &MappingWidget::SaveSettings); connect(parent, &MappingWindow::ConfigChanged, this, &MappingWidget::ConfigChanged); - - const auto timer = new QTimer(this); - connect(timer, &QTimer::timeout, this, [this] { - const auto lock = m_parent->GetController()->GetStateLock(); - emit Update(); - }); - - timer->start(1000 / INDICATOR_UPDATE_FREQ); } MappingWindow* MappingWidget::GetParent() const @@ -138,10 +130,21 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con setting_widget = new MappingBool(this, static_cast*>(setting.get())); break; + + default: + // FYI: Widgets for additional types can be implemented as needed. + break; } if (setting_widget) - form_layout->addRow(tr(setting->GetUIName()), setting_widget); + { + const auto hbox = new QHBoxLayout; + + hbox->addWidget(setting_widget); + hbox->addWidget(CreateSettingAdvancedMappingButton(*setting)); + + form_layout->addRow(tr(setting->GetUIName()), hbox); + } } if (group->can_be_disabled) @@ -173,3 +176,25 @@ ControllerEmu::EmulatedController* MappingWidget::GetController() const { return m_parent->GetController(); } + +QPushButton* +MappingWidget::CreateSettingAdvancedMappingButton(ControllerEmu::NumericSettingBase& setting) +{ + const auto button = new QPushButton(tr("...")); + button->setFixedWidth(QFontMetrics(font()).boundingRect(button->text()).width() * 2); + + button->connect(button, &QPushButton::clicked, [this, &setting]() { + if (setting.IsSimpleValue()) + setting.SetExpressionFromValue(); + + IOWindow io(this, GetController(), &setting.GetInputReference(), IOWindow::Type::Input); + io.exec(); + + setting.SimplifyIfPossible(); + + ConfigChanged(); + SaveSettings(); + }); + + return button; +} diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h index a88c09b4ed..851d2c12ce 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWidget.h @@ -14,10 +14,10 @@ constexpr int WIDGET_MAX_WIDTH = 112; class ControlGroupBox; class InputConfig; -class IOWindow; class MappingButton; class MappingNumeric; class MappingWindow; +class QPushButton; class QGroupBox; namespace ControllerEmu @@ -25,13 +25,9 @@ namespace ControllerEmu class Control; class ControlGroup; class EmulatedController; +class NumericSettingBase; } // namespace ControllerEmu -namespace ciface::Core -{ -class Device; -} // namespace ciface::Core - constexpr int INDICATOR_UPDATE_FREQ = 30; class MappingWidget : public QWidget @@ -57,6 +53,7 @@ protected: QGroupBox* CreateGroupBox(ControllerEmu::ControlGroup* group); QGroupBox* CreateGroupBox(const QString& name, ControllerEmu::ControlGroup* group); + QPushButton* CreateSettingAdvancedMappingButton(ControllerEmu::NumericSettingBase& setting); private: MappingWindow* m_parent; diff --git a/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp b/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp index 2b38fea2aa..dd5768e3f2 100644 --- a/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include "Core/Core.h" @@ -62,6 +63,15 @@ MappingWindow::MappingWindow(QWidget* parent, Type type, int port_num) ConnectWidgets(); SetMappingType(type); + const auto timer = new QTimer(this); + connect(timer, &QTimer::timeout, this, [this] { + const auto lock = GetController()->GetStateLock(); + emit Update(); + }); + + timer->start(1000 / INDICATOR_UPDATE_FREQ); + + GetController()->GetStateLock(); emit ConfigChanged(); } @@ -235,6 +245,7 @@ void MappingWindow::OnLoadProfilePressed() m_controller->LoadConfig(ini.GetOrCreateSection("Profile")); m_controller->UpdateReferences(g_controller_interface); + GetController()->GetStateLock(); emit ConfigChanged(); } @@ -426,6 +437,8 @@ void MappingWindow::OnDefaultFieldsPressed() { m_controller->LoadDefaults(g_controller_interface); m_controller->UpdateReferences(g_controller_interface); + + GetController()->GetStateLock(); emit ConfigChanged(); emit Save(); } @@ -441,6 +454,8 @@ void MappingWindow::OnClearFieldsPressed() m_controller->SetDefaultDevice(default_device); m_controller->UpdateReferences(g_controller_interface); + + GetController()->GetStateLock(); emit ConfigChanged(); emit Save(); } diff --git a/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.cpp b/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.cpp index 633245a324..0084bc5bee 100644 --- a/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include "Core/HW/Wiimote.h" #include "Core/HW/WiimoteEmu/WiimoteEmu.h" @@ -22,7 +24,7 @@ WiimoteEmuGeneral::WiimoteEmuGeneral(MappingWindow* window, WiimoteEmuExtension* : MappingWidget(window), m_extension_widget(extension) { CreateMainLayout(); - Connect(window); + Connect(); } void WiimoteEmuGeneral::CreateMainLayout() @@ -45,14 +47,20 @@ void WiimoteEmuGeneral::CreateMainLayout() Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments); auto* extension = CreateGroupBox(tr("Extension"), extension_group); auto* ce_extension = static_cast(extension_group); - m_extension_combo = new QComboBox(); + + const auto combo_hbox = new QHBoxLayout; + combo_hbox->addWidget(m_extension_combo = new QComboBox()); + combo_hbox->addWidget(m_extension_combo_dynamic_indicator = new QLabel(QString::fromUtf8("🎮"))); + combo_hbox->addWidget(CreateSettingAdvancedMappingButton(ce_extension->GetSelectionSetting())); + + m_extension_combo_dynamic_indicator->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored); for (const auto& attachment : ce_extension->GetAttachmentList()) m_extension_combo->addItem(tr(attachment->GetDisplayName().c_str())); extension->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); - static_cast(extension->layout())->insertRow(0, m_extension_combo); + static_cast(extension->layout())->insertRow(0, combo_hbox); layout->addWidget(extension, 0, 3); layout->addWidget(CreateGroupBox(tr("Rumble"), Wiimote::GetWiimoteGroup( @@ -67,11 +75,14 @@ void WiimoteEmuGeneral::CreateMainLayout() setLayout(layout); } -void WiimoteEmuGeneral::Connect(MappingWindow* window) +void WiimoteEmuGeneral::Connect() { - connect(m_extension_combo, static_cast(&QComboBox::currentIndexChanged), - this, &WiimoteEmuGeneral::OnAttachmentChanged); - connect(window, &MappingWindow::ConfigChanged, this, &WiimoteEmuGeneral::ConfigChanged); + connect(m_extension_combo, QOverload::of(&QComboBox::currentIndexChanged), this, + &WiimoteEmuGeneral::OnAttachmentChanged); + connect(m_extension_combo, QOverload::of(&QComboBox::activated), this, + &WiimoteEmuGeneral::OnAttachmentSelected); + connect(this, &MappingWidget::ConfigChanged, this, &WiimoteEmuGeneral::ConfigChanged); + connect(this, &MappingWidget::Update, this, &WiimoteEmuGeneral::Update); } void WiimoteEmuGeneral::OnAttachmentChanged(int extension) @@ -79,15 +90,31 @@ void WiimoteEmuGeneral::OnAttachmentChanged(int extension) GetParent()->ShowExtensionMotionTabs(extension == WiimoteEmu::ExtensionNumber::NUNCHUK); m_extension_widget->ChangeExtensionType(extension); +} +void WiimoteEmuGeneral::OnAttachmentSelected(int extension) +{ auto* ce_extension = static_cast( Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); + ce_extension->SetSelectedAttachment(extension); + ConfigChanged(); SaveSettings(); } void WiimoteEmuGeneral::ConfigChanged() +{ + auto* ce_extension = static_cast( + Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); + + m_extension_combo->setCurrentIndex(ce_extension->GetSelectedAttachment()); + + m_extension_combo_dynamic_indicator->setVisible( + !ce_extension->GetSelectionSetting().IsSimpleValue()); +} + +void WiimoteEmuGeneral::Update() { auto* ce_extension = static_cast( Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); diff --git a/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.h b/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.h index d692f9d9dd..01a11fa8da 100644 --- a/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.h +++ b/Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.h @@ -7,6 +7,7 @@ #include "DolphinQt/Config/Mapping/MappingWidget.h" class QComboBox; +class QLabel; class WiimoteEmuExtension; class WiimoteEmuGeneral final : public MappingWidget @@ -21,12 +22,19 @@ private: void LoadSettings() override; void SaveSettings() override; void CreateMainLayout(); - void Connect(MappingWindow* window); + void Connect(); + + // Index changed by code/expression. void OnAttachmentChanged(int index); + // Selection chosen by user. + void OnAttachmentSelected(int index); + void ConfigChanged(); + void Update(); // Extensions QComboBox* m_extension_combo; + QLabel* m_extension_combo_dynamic_indicator; WiimoteEmuExtension* m_extension_widget; }; 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/Attachments.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp index e104918fe2..6f980d8c60 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.cpp @@ -17,12 +17,22 @@ void Attachments::AddAttachment(std::unique_ptr 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& Attachments::GetSelectionSetting() +{ + return m_selection_setting; } const std::vector>& Attachments::GetAttachmentList() const diff --git a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.h b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.h index 81e76eccfe..f92bd8b1fd 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.h +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/Attachments.h @@ -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& GetSelectionSetting(); + const std::vector>& GetAttachmentList() const; private: - std::vector> m_attachments; + SettingValue m_selection_value; + NumericSetting m_selection_setting = {&m_selection_value, {""}, 0, 0, 0}; - std::atomic m_selected_attachment = {}; + std::vector> m_attachments; }; } // namespace ControllerEmu 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/ControlGroup.cpp b/Source/Core/InputCommon/ControllerEmu/ControlGroup/ControlGroup.cpp index 40ccae24f7..1a262a8dfd 100644 --- a/Source/Core/InputCommon/ControllerEmu/ControlGroup/ControlGroup.cpp +++ b/Source/Core/InputCommon/ControllerEmu/ControlGroup/ControlGroup.cpp @@ -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(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() + "/"); 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..a9c5844b48 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,10 +55,17 @@ 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) { - for (auto& attachment : static_cast(ctrlGroup.get())->GetAttachmentList()) + auto* const attachments = static_cast(ctrlGroup.get()); + + attachments->GetSelectionSetting().GetInputReference().UpdateReference(env); + + for (auto& attachment : attachments->GetAttachmentList()) attachment->UpdateReferences(env); } } diff --git a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.cpp b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.cpp index 3459040824..ab45b64595 100644 --- a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.cpp +++ b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.cpp @@ -4,6 +4,8 @@ #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" +#include + 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::SetExpressionFromValue() +{ + m_value.m_input.SetExpression(ValueToString(GetValue())); +} + +template <> +void NumericSetting::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::SetExpressionFromValue() +{ + // Cast bool to prevent "true"/"false" strings. + m_value.m_input.SetExpression(ValueToString(int(GetValue()))); +} + +template <> +SettingType NumericSetting::GetType() const +{ + return SettingType::Int; +} + template <> SettingType NumericSetting::GetType() const { diff --git a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h index 108b228049..c7298391f4 100644 --- a/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h +++ b/Source/Core/InputCommon/ControllerEmu/Setting/NumericSetting.h @@ -9,12 +9,14 @@ #include "Common/CommonTypes.h" #include "Common/IniFile.h" +#include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControllerInterface/Device.h" namespace ControllerEmu { enum class SettingType { + Int, Double, Bool, }; @@ -52,6 +54,17 @@ 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; + + virtual bool IsSimpleValue() 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; @@ -66,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) @@ -84,16 +98,39 @@ 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 override { return m_value.IsSimpleValue(); } + + void SimplifyIfPossible() override + { + ValueType value; + if (TryParse(m_value.m_input.GetExpression(), &value)) + m_value.SetValue(value); + } + + void SetExpressionFromValue() override; + 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 +156,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