| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h" |
| 5 | |
| 6 | #include <QCheckBox> |
| 7 | #include <QDialogButtonBox> |
| 8 | #include <QLabel> |
| 9 | #include <QTimer> |
| 10 | #include <QVBoxLayout> |
| 11 | |
| 12 | #include "Core/Config/MainSettings.h" |
| 13 | |
| 14 | #include "InputCommon/GCAdapter.h" |
| 15 | |
| 16 | GCPadWiiUConfigDialog::GCPadWiiUConfigDialog(int port, QWidget* parent) |
| 17 | : QDialog(parent), m_port{port} |
| 18 | { |
| 19 | CreateLayout(); |
| 20 | |
| 21 | LoadSettings(); |
| 22 | ConnectWidgets(); |
| 23 | } |
| 24 | |
| 25 | void GCPadWiiUConfigDialog::CreateLayout() |
| 26 | { |
| 27 | setWindowTitle(tr("GameCube Controller Adapter at Port %1").arg(m_port + 1)); |
| 28 | |
| 29 | m_layout = new QVBoxLayout(); |
| 30 | m_status_label = new QLabel(); |
| 31 | m_poll_rate_label = new QLabel; |
| 32 | m_rumble = new QCheckBox(tr("Enable Rumble")); |
| 33 | m_simulate_bongos = new QCheckBox(tr("Simulate DK Bongos")); |
| 34 | m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok); |
| 35 | |
| 36 | UpdateAdapterStatus(); |
| 37 | |
| 38 | auto* const timer = new QTimer{this}; |
| 39 | connect(timer, &QTimer::timeout, this, &GCPadWiiUConfigDialog::UpdateAdapterStatus); |
| 40 | timer->start(std::chrono::milliseconds{500}); |
| 41 | |
| 42 | m_layout->addWidget(m_status_label); |
| 43 | m_layout->addWidget(m_poll_rate_label); |
| 44 | m_layout->addWidget(m_rumble); |
| 45 | m_layout->addWidget(m_simulate_bongos); |
| 46 | m_layout->addWidget(m_button_box); |
| 47 | |
| 48 | setLayout(m_layout); |
| 49 | } |
| 50 | |
| 51 | void GCPadWiiUConfigDialog::ConnectWidgets() |
| 52 | { |
| 53 | connect(m_rumble, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); |
| 54 | connect(m_simulate_bongos, &QCheckBox::toggled, this, &GCPadWiiUConfigDialog::SaveSettings); |
| 55 | connect(m_button_box, &QDialogButtonBox::accepted, this, &GCPadWiiUConfigDialog::accept); |
| 56 | } |
| 57 | |
| 58 | void GCPadWiiUConfigDialog::UpdateAdapterStatus() |
| 59 | { |
| 60 | const char* error_message = nullptr; |
| 61 | const bool detected = GCAdapter::IsDetected(&error_message); |
| 62 | QString status_text; |
| 63 | |
| 64 | if (detected) |
| 65 | { |
| 66 | status_text = tr("Adapter Detected"); |
| 67 | } |
| 68 | else if (error_message) |
| 69 | { |
| 70 | status_text = tr("Error Opening Adapter: %1").arg(QString::fromUtf8(error_message)); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | status_text = tr("No Adapter Detected"); |
| 75 | } |
| 76 | |
| 77 | m_status_label->setText(status_text); |
| 78 | |
| 79 | const auto poll_rate = GCAdapter::GetCurrentPollRate(); |
| 80 | if (poll_rate != 0) |
| 81 | m_poll_rate_label->setText(tr("Poll Rate: %1 Hz").arg(poll_rate, 0, 'f', 2)); |
| 82 | else |
| 83 | m_poll_rate_label->clear(); |
| 84 | |
| 85 | m_rumble->setEnabled(detected); |
| 86 | m_simulate_bongos->setEnabled(detected); |
| 87 | } |
| 88 | |
| 89 | void GCPadWiiUConfigDialog::LoadSettings() |
| 90 | { |
| 91 | m_rumble->setChecked(Config::Get(Config::GetInfoForAdapterRumble(m_port))); |
| 92 | m_simulate_bongos->setChecked(Config::Get(Config::GetInfoForSimulateKonga(m_port))); |
| 93 | } |
| 94 | |
| 95 | void GCPadWiiUConfigDialog::SaveSettings() |
| 96 | { |
| 97 | Config::SetBaseOrCurrent(Config::GetInfoForAdapterRumble(m_port), m_rumble->isChecked()); |
| 98 | Config::SetBaseOrCurrent(Config::GetInfoForSimulateKonga(m_port), m_simulate_bongos->isChecked()); |
| 99 | } |