| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Mapping/MappingWidget.h" |
| 5 | |
| 6 | #include <fmt/core.h> |
| 7 | |
| 8 | #include <QCheckBox> |
| 9 | #include <QDialogButtonBox> |
| 10 | #include <QFormLayout> |
| 11 | #include <QGroupBox> |
| 12 | #include <QHBoxLayout> |
| 13 | #include <QLabel> |
| 14 | #include <QPushButton> |
| 15 | |
| 16 | #include "DolphinQt/Config/Mapping/IOWindow.h" |
| 17 | #include "DolphinQt/Config/Mapping/MappingButton.h" |
| 18 | #include "DolphinQt/Config/Mapping/MappingIndicator.h" |
| 19 | #include "DolphinQt/Config/Mapping/MappingNumeric.h" |
| 20 | #include "DolphinQt/Config/Mapping/MappingWindow.h" |
| 21 | |
| 22 | #include "InputCommon/ControllerEmu/Control/Control.h" |
| 23 | #include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h" |
| 24 | #include "InputCommon/ControllerEmu/ControlGroup/MixedTriggers.h" |
| 25 | #include "InputCommon/ControllerEmu/ControllerEmu.h" |
| 26 | #include "InputCommon/ControllerEmu/Setting/NumericSetting.h" |
| 27 | #include "InputCommon/ControllerEmu/StickGate.h" |
| 28 | #include "InputCommon/ControllerInterface/ControllerInterface.h" |
| 29 | |
| 30 | MappingWidget::MappingWidget(MappingWindow* parent) : m_parent(parent) |
| 31 | { |
| 32 | connect(parent, &MappingWindow::Update, this, &MappingWidget::Update); |
| 33 | connect(parent, &MappingWindow::Save, this, &MappingWidget::SaveSettings); |
| 34 | connect(parent, &MappingWindow::ConfigChanged, this, &MappingWidget::ConfigChanged); |
| 35 | } |
| 36 | |
| 37 | MappingWindow* MappingWidget::GetParent() const |
| 38 | { |
| 39 | return m_parent; |
| 40 | } |
| 41 | |
| 42 | int MappingWidget::GetPort() const |
| 43 | { |
| 44 | return m_parent->GetPort(); |
| 45 | } |
| 46 | |
| 47 | QGroupBox* MappingWidget::CreateGroupBox(ControllerEmu::ControlGroup* group) |
| 48 | { |
| 49 | return CreateGroupBox(tr(group->ui_name.c_str()), group); |
| 50 | } |
| 51 | |
| 52 | QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::ControlGroup* group) |
| 53 | { |
| 54 | QGroupBox* group_box = new QGroupBox(name); |
| 55 | QFormLayout* form_layout = new QFormLayout(); |
| 56 | |
| 57 | group_box->setLayout(form_layout); |
| 58 | |
| 59 | MappingIndicator* indicator = nullptr; |
| 60 | |
| 61 | switch (group->type) |
| 62 | { |
| 63 | case ControllerEmu::GroupType::Shake: |
| 64 | indicator = new ShakeMappingIndicator(*static_cast<ControllerEmu::Shake*>(group)); |
| 65 | break; |
| 66 | |
| 67 | case ControllerEmu::GroupType::MixedTriggers: |
| 68 | indicator = new MixedTriggersIndicator(*static_cast<ControllerEmu::MixedTriggers*>(group)); |
| 69 | break; |
| 70 | |
| 71 | case ControllerEmu::GroupType::Tilt: |
| 72 | indicator = new TiltIndicator(*static_cast<ControllerEmu::Tilt*>(group)); |
| 73 | break; |
| 74 | |
| 75 | case ControllerEmu::GroupType::Cursor: |
| 76 | indicator = new CursorIndicator(*static_cast<ControllerEmu::Cursor*>(group)); |
| 77 | break; |
| 78 | |
| 79 | case ControllerEmu::GroupType::Force: |
| 80 | indicator = new SwingIndicator(*static_cast<ControllerEmu::Force*>(group)); |
| 81 | break; |
| 82 | |
| 83 | case ControllerEmu::GroupType::IMUAccelerometer: |
| 84 | indicator = |
| 85 | new AccelerometerMappingIndicator(*static_cast<ControllerEmu::IMUAccelerometer*>(group)); |
| 86 | break; |
| 87 | |
| 88 | case ControllerEmu::GroupType::IMUGyroscope: |
| 89 | indicator = new GyroMappingIndicator(*static_cast<ControllerEmu::IMUGyroscope*>(group)); |
| 90 | break; |
| 91 | |
| 92 | case ControllerEmu::GroupType::Stick: |
| 93 | indicator = new AnalogStickIndicator(*static_cast<ControllerEmu::ReshapableInput*>(group)); |
| 94 | break; |
| 95 | |
| 96 | case ControllerEmu::GroupType::IRPassthrough: |
| 97 | indicator = |
| 98 | new IRPassthroughMappingIndicator(*static_cast<ControllerEmu::IRPassthrough*>(group)); |
| 99 | break; |
| 100 | |
| 101 | default: |
| 102 | break; |
| 103 | } |
| 104 | |
| 105 | // Mapping indicator. |
| 106 | if (indicator != nullptr) |
| 107 | { |
| 108 | auto* const indicator_layout = new QBoxLayout(QBoxLayout::Direction::Down); |
| 109 | indicator_layout->addWidget(indicator); |
| 110 | indicator_layout->setAlignment(Qt::AlignCenter); |
| 111 | form_layout->addRow(indicator_layout); |
| 112 | |
| 113 | connect(this, &MappingWidget::Update, indicator, qOverload<>(&MappingIndicator::update)); |
| 114 | } |
| 115 | |
| 116 | // "Enabled" checkbox. |
| 117 | if (group->HasEnabledSetting()) |
| 118 | { |
| 119 | AddSettingWidget(form_layout, group->enabled_setting.get()); |
| 120 | |
| 121 | auto enable_labels = [form_layout, start_index = form_layout->rowCount()](bool enabled) { |
| 122 | // Skipping the "Enabled" checkbox row itself and the mapping indicator, if it exists. |
| 123 | for (int i = start_index; i < form_layout->count(); ++i) |
| 124 | { |
| 125 | auto* const item = form_layout->itemAt(i, QFormLayout::LabelRole); |
| 126 | if (item == nullptr) |
| 127 | continue; |
| 128 | |
| 129 | auto* const widget = item->widget(); |
| 130 | if (widget == nullptr) |
| 131 | continue; |
| 132 | |
| 133 | widget->setEnabled(enabled); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | connect(this, &MappingWidget::Update, this, [group, enable_labels, enabled = true]() mutable { |
| 138 | if (enabled == group->enabled_setting->GetValue()) |
| 139 | return; |
| 140 | enable_labels(enabled = !enabled); |
| 141 | }); |
| 142 | } |
| 143 | |
| 144 | // "Calibrate" button. |
| 145 | if (indicator != nullptr) |
| 146 | { |
| 147 | const bool need_calibration = group->type == ControllerEmu::GroupType::Cursor || |
| 148 | group->type == ControllerEmu::GroupType::Stick || |
| 149 | group->type == ControllerEmu::GroupType::Tilt || |
| 150 | group->type == ControllerEmu::GroupType::Force; |
| 151 | |
| 152 | if (need_calibration) |
| 153 | { |
| 154 | const auto calibrate = |
| 155 | new CalibrationWidget(*this, *static_cast<ControllerEmu::ReshapableInput*>(group), |
| 156 | *static_cast<ReshapableInputIndicator*>(indicator)); |
| 157 | |
| 158 | form_layout->addRow(calibrate); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | for (auto& control : group->controls) |
| 163 | CreateControl(control.get(), form_layout, !indicator); |
| 164 | |
| 165 | AddSettingWidgets(form_layout, group, ControllerEmu::SettingVisibility::Normal); |
| 166 | |
| 167 | const auto advanced_setting_count = |
| 168 | std::ranges::count(group->numeric_settings, ControllerEmu::SettingVisibility::Advanced, |
| 169 | &ControllerEmu::NumericSettingBase::GetVisibility); |
| 170 | |
| 171 | if (advanced_setting_count != 0) |
| 172 | { |
| 173 | const auto advanced_button = new QPushButton(tr("Advanced")); |
| 174 | form_layout->addRow(advanced_button); |
| 175 | connect(advanced_button, &QPushButton::clicked, |
| 176 | [this, group] { ShowAdvancedControlGroupDialog(group); }); |
| 177 | } |
| 178 | |
| 179 | if (group->type == ControllerEmu::GroupType::Cursor) |
| 180 | { |
| 181 | QPushButton* mouse_button = new QPushButton(tr("Use Mouse Controlled Pointing")); |
| 182 | form_layout->insertRow(2, mouse_button); |
| 183 | |
| 184 | using ControllerEmu::Cursor; |
| 185 | connect(mouse_button, &QCheckBox::clicked, [this, grp = static_cast<Cursor*>(group)] { |
| 186 | std::string default_device = g_controller_interface.GetDefaultDeviceString() + ":"; |
| 187 | const std::string controller_device = GetController()->GetDefaultDevice().ToString() + ":"; |
| 188 | if (default_device == controller_device) |
| 189 | { |
| 190 | default_device.clear(); |
| 191 | } |
| 192 | grp->SetControlExpression(0, fmt::format("`{}Cursor Y-`", default_device)); |
| 193 | grp->SetControlExpression(1, fmt::format("`{}Cursor Y+`", default_device)); |
| 194 | grp->SetControlExpression(2, fmt::format("`{}Cursor X-`", default_device)); |
| 195 | grp->SetControlExpression(3, fmt::format("`{}Cursor X+`", default_device)); |
| 196 | |
| 197 | grp->SetRelativeInput(false); |
| 198 | |
| 199 | emit ConfigChanged(); |
| 200 | GetController()->UpdateReferences(g_controller_interface); |
| 201 | }); |
| 202 | } |
| 203 | |
| 204 | return group_box; |
| 205 | } |
| 206 | |
| 207 | void MappingWidget::AddSettingWidgets(QFormLayout* layout, ControllerEmu::ControlGroup* group, |
| 208 | ControllerEmu::SettingVisibility visibility) |
| 209 | { |
| 210 | for (auto& setting : group->numeric_settings) |
| 211 | { |
| 212 | if (setting->GetVisibility() != visibility) |
| 213 | continue; |
| 214 | |
| 215 | AddSettingWidget(layout, setting.get()); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | void MappingWidget::AddSettingWidget(QFormLayout* layout, |
| 220 | ControllerEmu::NumericSettingBase* setting) |
| 221 | { |
| 222 | QWidget* setting_widget = nullptr; |
| 223 | |
| 224 | switch (setting->GetType()) |
| 225 | { |
| 226 | case ControllerEmu::SettingType::Double: |
| 227 | setting_widget = |
| 228 | new MappingDouble(this, static_cast<ControllerEmu::NumericSetting<double>*>(setting)); |
| 229 | break; |
| 230 | |
| 231 | case ControllerEmu::SettingType::Bool: |
| 232 | setting_widget = |
| 233 | new MappingBool(this, static_cast<ControllerEmu::NumericSetting<bool>*>(setting)); |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | // FYI: Widgets for additional types can be implemented as needed. |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | if (setting_widget != nullptr) |
| 242 | { |
| 243 | auto* const hbox = new QHBoxLayout; |
| 244 | |
| 245 | hbox->addWidget(setting_widget); |
| 246 | setting_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 247 | |
| 248 | hbox->addWidget(CreateSettingAdvancedMappingButton(*setting)); |
| 249 | |
| 250 | layout->addRow(tr(setting->GetUIName()), hbox); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void MappingWidget::ShowAdvancedControlGroupDialog(ControllerEmu::ControlGroup* group) |
| 255 | { |
| 256 | QDialog dialog{this}; |
| 257 | dialog.setWindowTitle(tr(group->ui_name.c_str())); |
| 258 | |
| 259 | const auto group_box = new QGroupBox(tr("Advanced Settings")); |
| 260 | |
| 261 | QFormLayout* form_layout = new QFormLayout(); |
| 262 | |
| 263 | AddSettingWidgets(form_layout, group, ControllerEmu::SettingVisibility::Advanced); |
| 264 | |
| 265 | const auto reset_button = new QPushButton(tr("Reset All")); |
| 266 | form_layout->addRow(reset_button); |
| 267 | |
| 268 | connect(reset_button, &QPushButton::clicked, [this, group] { |
| 269 | for (auto& setting : group->numeric_settings) |
| 270 | { |
| 271 | if (setting->GetVisibility() != ControllerEmu::SettingVisibility::Advanced) |
| 272 | continue; |
| 273 | |
| 274 | setting->SetToDefault(); |
| 275 | } |
| 276 | |
| 277 | emit ConfigChanged(); |
| 278 | }); |
| 279 | |
| 280 | const auto main_layout = new QVBoxLayout(); |
| 281 | const auto button_box = new QDialogButtonBox(QDialogButtonBox::Close); |
| 282 | |
| 283 | group_box->setLayout(form_layout); |
| 284 | |
| 285 | main_layout->addWidget(group_box); |
| 286 | main_layout->addWidget(button_box); |
| 287 | |
| 288 | dialog.setLayout(main_layout); |
| 289 | |
| 290 | // Focusing something else by default instead of the first spin box. |
| 291 | // Dynamically changing expression-backed settings pause when taking input. |
| 292 | // This just avoids that weird edge case behavior when the dialog is first open. |
| 293 | button_box->setFocus(); |
| 294 | |
| 295 | // Signal the newly created numeric setting widgets to display the current values. |
| 296 | emit ConfigChanged(); |
| 297 | |
| 298 | // Enable "Close" button functionality. |
| 299 | connect(button_box, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); |
| 300 | |
| 301 | dialog.exec(); |
| 302 | } |
| 303 | |
| 304 | QGroupBox* MappingWidget::CreateControlsBox(const QString& name, ControllerEmu::ControlGroup* group, |
| 305 | int columns) |
| 306 | { |
| 307 | auto* group_box = new QGroupBox(name); |
| 308 | auto* hbox_layout = new QHBoxLayout(); |
| 309 | |
| 310 | group_box->setLayout(hbox_layout); |
| 311 | |
| 312 | std::vector<QFormLayout*> form_layouts; |
| 313 | for (int i = 0; i < columns; ++i) |
| 314 | { |
| 315 | form_layouts.push_back(new QFormLayout()); |
| 316 | hbox_layout->addLayout(form_layouts[i]); |
| 317 | } |
| 318 | |
| 319 | for (size_t i = 0; i < group->controls.size(); ++i) |
| 320 | { |
| 321 | CreateControl(group->controls[i].get(), form_layouts[i % columns], true); |
| 322 | } |
| 323 | |
| 324 | return group_box; |
| 325 | } |
| 326 | |
| 327 | void MappingWidget::CreateControl(const ControllerEmu::Control* control, QFormLayout* layout, |
| 328 | bool indicator) |
| 329 | { |
| 330 | // I know this check is terrible, but it's just UI code. |
| 331 | const bool is_modifier = control->name == "Modifier"; |
| 332 | |
| 333 | using ControlType = MappingButton::ControlType; |
| 334 | const auto control_type = |
| 335 | control->control_ref->IsInput() ? |
| 336 | (is_modifier ? ControlType::ModifierInput : ControlType::NormalInput) : |
| 337 | ControlType::Output; |
| 338 | |
| 339 | auto* const button = new MappingButton(this, control->control_ref.get(), control_type); |
| 340 | |
| 341 | if (control->control_ref->IsInput()) |
| 342 | { |
| 343 | if (m_previous_mapping_button) |
| 344 | { |
| 345 | connect(m_previous_mapping_button, &MappingButton::QueueNextButtonMapping, |
| 346 | [this, button] { m_parent->QueueInputDetection(button); }); |
| 347 | } |
| 348 | m_previous_mapping_button = button; |
| 349 | } |
| 350 | |
| 351 | button->setMinimumWidth(100); |
| 352 | button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); |
| 353 | |
| 354 | const bool translate = control->translate == ControllerEmu::Translatability::Translate; |
| 355 | const QString translated_name = |
| 356 | translate ? tr(control->ui_name.c_str()) : QString::fromStdString(control->ui_name); |
| 357 | |
| 358 | if (indicator && control->control_ref->IsInput()) |
| 359 | { |
| 360 | auto* const button_indicator = new ButtonIndicator{control->control_ref.get()}; |
| 361 | connect(this, &MappingWidget::Update, button_indicator, qOverload<>(&MappingIndicator::update)); |
| 362 | |
| 363 | auto* const hbox = new QHBoxLayout; |
| 364 | hbox->setSpacing(0); |
| 365 | hbox->addWidget(button_indicator); |
| 366 | hbox->addWidget(button); |
| 367 | layout->addRow(translated_name, hbox); |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | layout->addRow(translated_name, button); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | ControllerEmu::EmulatedController* MappingWidget::GetController() const |
| 376 | { |
| 377 | return m_parent->GetController(); |
| 378 | } |
| 379 | |
| 380 | QPushButton* |
| 381 | MappingWidget::CreateSettingAdvancedMappingButton(ControllerEmu::NumericSettingBase& setting) |
| 382 | { |
| 383 | const auto button = new QPushButton(tr("...")); |
| 384 | button->setFixedWidth(QFontMetrics(font()).boundingRect(button->text()).width() * 2); |
| 385 | |
| 386 | button->connect(button, &QPushButton::clicked, [this, &setting] { |
| 387 | if (setting.IsSimpleValue()) |
| 388 | setting.SetExpressionFromValue(); |
| 389 | |
| 390 | // Ensure the UI has the game-controller indicator while editing the expression. |
| 391 | // And cancel in-progress mappings. |
| 392 | ConfigChanged(); |
| 393 | |
| 394 | IOWindow io(GetParent(), GetController(), &setting.GetInputReference(), IOWindow::Type::Input); |
| 395 | io.exec(); |
| 396 | |
| 397 | setting.SimplifyIfPossible(); |
| 398 | |
| 399 | ConfigChanged(); |
| 400 | SaveSettings(); |
| 401 | }); |
| 402 | |
| 403 | return button; |
| 404 | } |