| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" |
| 5 | |
| 6 | #include "Common/IniFile.h" |
| 7 | |
| 8 | #include "InputCommon/ControlReference/ControlReference.h" |
| 9 | #include "InputCommon/ControllerEmu/Control/Input.h" |
| 10 | #include "InputCommon/ControllerEmu/Control/Output.h" |
| 11 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 12 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 13 | |
| 14 | namespace ControllerEmu |
| 15 | { |
| 16 | ControlGroup::ControlGroup(const std::string& name_, const GroupType type_, |
| 17 | DefaultValue default_value_) |
| 18 | : ControlGroup{name_, name_, type_, default_value_} |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | ControlGroup::ControlGroup(std::string name_, std::string ui_name_, const GroupType type_, |
| 23 | DefaultValue default_value_) |
| 24 | : name(std::move(name_)), ui_name(std::move(ui_name_)), type(type_), |
| 25 | default_value(default_value_) |
| 26 | { |
| 27 | if (default_value_ != DefaultValue::AlwaysEnabled) |
| 28 | { |
| 29 | enabled_setting = std::make_unique<NumericSetting<bool>>( |
| 30 | &enabled, NumericSettingDetails{_trans("Enabled")}, |
| 31 | (default_value_ == DefaultValue::Enabled), false, true); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | void ControlGroup::AddVirtualNotchSetting(SettingValue<double>* value, double max_virtual_notch_deg) |
| 36 | { |
| 37 | AddSetting(value, |
| 38 | {_trans("Virtual Notches"), |
| 39 | // i18n: The degrees symbol. |
| 40 | _trans("°"), _trans("Snap the thumbstick position to the nearest octagonal axis."), |
| 41 | nullptr, SettingVisibility::Advanced}, |
| 42 | 0, 0, max_virtual_notch_deg); |
| 43 | } |
| 44 | |
| 45 | void ControlGroup::AddDeadzoneSetting(SettingValue<double>* value, double maximum_deadzone) |
| 46 | { |
| 47 | AddSetting(value, |
| 48 | {_trans("Dead Zone"), |
| 49 | // i18n: The percent symbol. |
| 50 | _trans("%"), |
| 51 | // i18n: Refers to the dead-zone setting of gamepad inputs. |
| 52 | _trans("Input strength to ignore and remap.")}, |
| 53 | 0, 0, maximum_deadzone); |
| 54 | } |
| 55 | |
| 56 | ControlGroup::~ControlGroup() = default; |
| 57 | |
| 58 | void ControlGroup::LoadConfig(Common::IniFile::Section* sec, const std::string& base) |
| 59 | { |
| 60 | const std::string group(base + name + "/"); |
| 61 | |
| 62 | // enabled |
| 63 | if (HasEnabledSetting()) |
| 64 | enabled_setting->LoadFromIni(*sec, group); |
| 65 | |
| 66 | for (auto& setting : numeric_settings) |
| 67 | setting->LoadFromIni(*sec, group); |
| 68 | |
| 69 | for (auto& c : controls) |
| 70 | { |
| 71 | { |
| 72 | // control expression |
| 73 | std::string expression; |
| 74 | sec->Get(group + c->name, &expression, ""); |
| 75 | c->control_ref->SetExpression(std::move(expression)); |
| 76 | } |
| 77 | |
| 78 | // range |
| 79 | sec->Get(group + c->name + "/Range", &c->control_ref->range, 100.0); |
| 80 | c->control_ref->range /= 100; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void ControlGroup::SaveConfig(Common::IniFile::Section* sec, const std::string& base) |
| 85 | { |
| 86 | const std::string group(base + name + "/"); |
| 87 | |
| 88 | // enabled |
| 89 | if (HasEnabledSetting()) |
| 90 | enabled_setting->SaveToIni(*sec, group); |
| 91 | |
| 92 | for (auto& setting : numeric_settings) |
| 93 | setting->SaveToIni(*sec, group); |
| 94 | |
| 95 | for (auto& c : controls) |
| 96 | { |
| 97 | // control expression |
| 98 | std::string expression = c->control_ref->GetExpression(); |
| 99 | // We can't save line breaks in a single line config. Restoring them is too complicated. |
| 100 | ReplaceBreaksWithSpaces(expression); |
| 101 | sec->Set(group + c->name, expression, ""); |
| 102 | |
| 103 | // range |
| 104 | sec->Set(group + c->name + "/Range", c->control_ref->range * 100.0, 100.0); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void ControlGroup::UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env) |
| 109 | { |
| 110 | if (HasEnabledSetting()) |
| 111 | enabled_setting->GetInputReference().UpdateReference(env); |
| 112 | |
| 113 | for (auto& control : controls) |
| 114 | control->control_ref->UpdateReference(env); |
| 115 | |
| 116 | for (auto& setting : numeric_settings) |
| 117 | setting->GetInputReference().UpdateReference(env); |
| 118 | } |
| 119 | |
| 120 | void ControlGroup::SetControlExpression(int index, const std::string& expression) |
| 121 | { |
| 122 | controls.at(index)->control_ref->SetExpression(expression); |
| 123 | } |
| 124 | |
| 125 | void ControlGroup::AddInput(Translatability translate, std::string name_) |
| 126 | { |
| 127 | controls.emplace_back(std::make_unique<Input>(translate, std::move(name_))); |
| 128 | } |
| 129 | |
| 130 | void ControlGroup::AddInput(Translatability translate, std::string name_, std::string ui_name_) |
| 131 | { |
| 132 | controls.emplace_back(std::make_unique<Input>(translate, std::move(name_), std::move(ui_name_))); |
| 133 | } |
| 134 | |
| 135 | void ControlGroup::AddOutput(Translatability translate, std::string name_) |
| 136 | { |
| 137 | controls.emplace_back(std::make_unique<Output>(translate, std::move(name_))); |
| 138 | } |
| 139 | |
| 140 | bool ControlGroup::HasEnabledSetting() const |
| 141 | { |
| 142 | return enabled_setting != nullptr; |
| 143 | } |
| 144 | |
| 145 | } // namespace ControllerEmu |