| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Mapping/WiimoteEmuGeneral.h" |
| 5 | |
| 6 | #include <QComboBox> |
| 7 | #include <QFormLayout> |
| 8 | #include <QGridLayout> |
| 9 | #include <QGroupBox> |
| 10 | #include <QLabel> |
| 11 | #include <QPushButton> |
| 12 | |
| 13 | #include "Core/HW/Wiimote.h" |
| 14 | #include "Core/HW/WiimoteEmu/WiimoteEmu.h" |
| 15 | |
| 16 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 17 | #include "DolphinQt/Config/Mapping/WiimoteEmuExtension.h" |
| 18 | |
| 19 | #include "InputCommon/ControllerEmu/ControlGroup/Attachments.h" |
| 20 | #include "InputCommon/InputConfig.h" |
| 21 | |
| 22 | WiimoteEmuGeneral::WiimoteEmuGeneral(MappingWindow* window, WiimoteEmuExtension* extension) |
| 23 | : MappingWidget(window), m_extension_widget(extension) |
| 24 | { |
| 25 | CreateMainLayout(); |
| 26 | Connect(); |
| 27 | } |
| 28 | |
| 29 | void WiimoteEmuGeneral::CreateMainLayout() |
| 30 | { |
| 31 | auto* layout = new QGridLayout; |
| 32 | |
| 33 | layout->addWidget( |
| 34 | CreateGroupBox(tr("Buttons"), |
| 35 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Buttons)), |
| 36 | 0, 0, -1, 1); |
| 37 | layout->addWidget(CreateGroupBox(tr("D-Pad"), Wiimote::GetWiimoteGroup( |
| 38 | GetPort(), WiimoteEmu::WiimoteGroup::DPad)), |
| 39 | 0, 1, -1, 1); |
| 40 | layout->addWidget( |
| 41 | CreateGroupBox(tr("Hotkeys"), |
| 42 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Hotkeys)), |
| 43 | 0, 2, -1, 1); |
| 44 | |
| 45 | auto* extension_group = |
| 46 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments); |
| 47 | auto* extension = CreateGroupBox(tr("Extension"), extension_group); |
| 48 | auto* ce_extension = static_cast<ControllerEmu::Attachments*>(extension_group); |
| 49 | |
| 50 | const auto combo_hbox = new QHBoxLayout; |
| 51 | combo_hbox->addWidget(m_extension_combo = new QComboBox()); |
| 52 | combo_hbox->addWidget(m_extension_combo_dynamic_indicator = new QLabel(QString::fromUtf8("🎮"))); |
| 53 | combo_hbox->addWidget(CreateSettingAdvancedMappingButton(ce_extension->GetSelectionSetting())); |
| 54 | |
| 55 | m_extension_combo_dynamic_indicator->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored); |
| 56 | |
| 57 | for (const auto& attachment : ce_extension->GetAttachmentList()) |
| 58 | m_extension_combo->addItem(tr(attachment->GetDisplayName().c_str())); |
| 59 | |
| 60 | extension->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 61 | |
| 62 | auto* const ext_layout = static_cast<QFormLayout*>(extension->layout()); |
| 63 | ext_layout->insertRow(0, combo_hbox); |
| 64 | |
| 65 | m_configure_ext_button = new QPushButton(tr("Configure Extension")); |
| 66 | m_configure_ext_button->setDisabled(true); |
| 67 | m_configure_ext_button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 68 | ext_layout->insertRow(1, m_configure_ext_button); |
| 69 | |
| 70 | layout->addWidget(extension, 0, 3); |
| 71 | layout->addWidget(CreateGroupBox(tr("Rumble"), Wiimote::GetWiimoteGroup( |
| 72 | GetPort(), WiimoteEmu::WiimoteGroup::Rumble)), |
| 73 | 1, 3); |
| 74 | |
| 75 | layout->addWidget( |
| 76 | CreateGroupBox(tr("Options"), |
| 77 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Options)), |
| 78 | 2, 3); |
| 79 | |
| 80 | setLayout(layout); |
| 81 | } |
| 82 | |
| 83 | void WiimoteEmuGeneral::Connect() |
| 84 | { |
| 85 | connect(m_extension_combo, &QComboBox::currentIndexChanged, this, |
| 86 | &WiimoteEmuGeneral::OnAttachmentChanged); |
| 87 | connect(m_extension_combo, &QComboBox::activated, this, &WiimoteEmuGeneral::OnAttachmentSelected); |
| 88 | connect(this, &MappingWidget::ConfigChanged, this, &WiimoteEmuGeneral::ConfigChanged); |
| 89 | connect(this, &MappingWidget::Update, this, &WiimoteEmuGeneral::Update); |
| 90 | connect(m_configure_ext_button, &QPushButton::clicked, GetParent(), |
| 91 | &MappingWindow::ActivateExtensionTab); |
| 92 | } |
| 93 | |
| 94 | void WiimoteEmuGeneral::OnAttachmentChanged(int extension) |
| 95 | { |
| 96 | GetParent()->ShowExtensionMotionTabs(extension == WiimoteEmu::ExtensionNumber::NUNCHUK); |
| 97 | |
| 98 | m_extension_widget->ChangeExtensionType(extension); |
| 99 | |
| 100 | m_configure_ext_button->setEnabled(extension != WiimoteEmu::ExtensionNumber::NONE); |
| 101 | } |
| 102 | |
| 103 | void WiimoteEmuGeneral::OnAttachmentSelected(int extension) |
| 104 | { |
| 105 | auto* ce_extension = static_cast<ControllerEmu::Attachments*>( |
| 106 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); |
| 107 | |
| 108 | ce_extension->SetSelectedAttachment(extension); |
| 109 | |
| 110 | ConfigChanged(); |
| 111 | SaveSettings(); |
| 112 | } |
| 113 | |
| 114 | void WiimoteEmuGeneral::ConfigChanged() |
| 115 | { |
| 116 | auto* ce_extension = static_cast<ControllerEmu::Attachments*>( |
| 117 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); |
| 118 | |
| 119 | m_extension_combo->setCurrentIndex(ce_extension->GetSelectedAttachment()); |
| 120 | |
| 121 | m_extension_combo_dynamic_indicator->setVisible( |
| 122 | !ce_extension->GetSelectionSetting().IsSimpleValue()); |
| 123 | } |
| 124 | |
| 125 | void WiimoteEmuGeneral::Update() |
| 126 | { |
| 127 | auto* ce_extension = static_cast<ControllerEmu::Attachments*>( |
| 128 | Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments)); |
| 129 | |
| 130 | m_extension_combo->setCurrentIndex(ce_extension->GetSelectedAttachment()); |
| 131 | } |
| 132 | |
| 133 | void WiimoteEmuGeneral::LoadSettings() |
| 134 | { |
| 135 | Wiimote::LoadConfig(); |
| 136 | } |
| 137 | |
| 138 | void WiimoteEmuGeneral::SaveSettings() |
| 139 | { |
| 140 | Wiimote::GetConfig()->SaveConfig(); |
| 141 | } |
| 142 | |
| 143 | InputConfig* WiimoteEmuGeneral::GetConfig() |
| 144 | { |
| 145 | return Wiimote::GetConfig(); |
| 146 | } |