| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "InputCommon/ControllerEmu/ControlGroup/Attachments.h" |
| 5 | |
| 6 | namespace ControllerEmu |
| 7 | { |
| 8 | |
| 9 | void AttachedController::LoadDefaults() |
| 10 | { |
| 11 | } |
| 12 | |
| 13 | Attachments::Attachments(const std::string& name_) : ControlGroup(name_, GroupType::Attachments) |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | void Attachments::AddAttachment(std::unique_ptr<AttachedController> att) |
| 18 | { |
| 19 | m_attachments.emplace_back(std::move(att)); |
| 20 | } |
| 21 | |
| 22 | u32 Attachments::GetSelectedAttachment() const |
| 23 | { |
| 24 | // This is originally an int, treat it as such |
| 25 | const int value = m_selection_value.GetValue(); |
| 26 | |
| 27 | if (value > 0 && static_cast<size_t>(value) < m_attachments.size()) |
| 28 | return u32(value); |
| 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | void Attachments::SetSelectedAttachment(u32 val) |
| 34 | { |
| 35 | m_selection_setting.SetValue(val); |
| 36 | } |
| 37 | |
| 38 | NumericSetting<int>& Attachments::GetSelectionSetting() |
| 39 | { |
| 40 | return m_selection_setting; |
| 41 | } |
| 42 | |
| 43 | SubscribableSettingValue<int>& Attachments::GetAttachmentSetting() |
| 44 | { |
| 45 | return m_selection_value; |
| 46 | } |
| 47 | |
| 48 | const std::vector<std::unique_ptr<AttachedController>>& Attachments::GetAttachmentList() const |
| 49 | { |
| 50 | return m_attachments; |
| 51 | } |
| 52 | |
| 53 | void Attachments::LoadConfig(Common::IniFile::Section* sec, const std::string& base) |
| 54 | { |
| 55 | ControlGroup::LoadConfig(sec, base); |
| 56 | |
| 57 | SetSelectedAttachment(0); |
| 58 | |
| 59 | std::string attachment_text; |
| 60 | sec->Get(base + name, &attachment_text, ""); |
| 61 | |
| 62 | // First assume attachment string is a valid expression. |
| 63 | // If it instead matches one of the names of our attachments it is overridden below. |
| 64 | GetSelectionSetting().GetInputReference().SetExpression(attachment_text); |
| 65 | |
| 66 | u32 n = 0; |
| 67 | for (auto& ai : GetAttachmentList()) |
| 68 | { |
| 69 | ai->LoadGroupsConfig(sec, base + ai->GetName() + "/"); |
| 70 | |
| 71 | if (ai->GetName() == attachment_text) |
| 72 | SetSelectedAttachment(n); |
| 73 | |
| 74 | ++n; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void Attachments::SaveConfig(Common::IniFile::Section* sec, const std::string& base) |
| 79 | { |
| 80 | ControlGroup::SaveConfig(sec, base); |
| 81 | |
| 82 | if (GetSelectionSetting().IsSimpleValue()) |
| 83 | { |
| 84 | sec->Set(base + name, GetAttachmentList()[GetSelectedAttachment()]->GetName(), "None"); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | std::string expression = GetSelectionSetting().GetInputReference().GetExpression(); |
| 89 | ReplaceBreaksWithSpaces(expression); |
| 90 | sec->Set(base + name, expression, "None"); |
| 91 | } |
| 92 | |
| 93 | for (auto& ai : GetAttachmentList()) |
| 94 | ai->SaveGroupsConfig(sec, base + ai->GetName() + "/"); |
| 95 | } |
| 96 | |
| 97 | void Attachments::UpdateReferences(ciface::ExpressionParser::ControlEnvironment& env) |
| 98 | { |
| 99 | ControlGroup::UpdateReferences(env); |
| 100 | |
| 101 | GetSelectionSetting().GetInputReference().UpdateReference(env); |
| 102 | |
| 103 | for (auto& attachment : GetAttachmentList()) |
| 104 | attachment->UpdateGroupsReferences(env); |
| 105 | } |
| 106 | |
| 107 | } // namespace ControllerEmu |