dolphin/Source/Core/DolphinQt/Config/ControllersWindow.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

228 lines
6.6 KiB
C++
Raw Normal View History

2017-05-09 16:49:10 +00:00
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
2018-07-06 22:40:15 +00:00
#include "DolphinQt/Config/ControllersWindow.h"
#include <QApplication>
2017-05-09 16:49:10 +00:00
#include <QBoxLayout>
#include <QCheckBox>
#include <QComboBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QGridLayout>
2017-05-09 16:49:10 +00:00
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QScreen>
2017-05-09 16:49:10 +00:00
#include <QVBoxLayout>
#include <map>
#include <optional>
2017-05-09 16:49:10 +00:00
#include "Core/ConfigManager.h"
#include "Core/Core.h"
2017-05-09 16:49:10 +00:00
#include "Core/HW/SI/SI.h"
#include "Core/HW/SI/SI_Device.h"
2017-05-09 16:49:10 +00:00
#include "Core/IOS/IOS.h"
#include "DolphinQt/Config/CommonControllersWidget.h"
2018-07-06 22:40:15 +00:00
#include "DolphinQt/Config/Mapping/GCPadWiiUConfigDialog.h"
#include "DolphinQt/Config/Mapping/MappingWindow.h"
#include "DolphinQt/Config/WiimoteControllersWidget.h"
2018-07-06 22:40:15 +00:00
#include "DolphinQt/QtUtils/WrapInScrollArea.h"
#include "DolphinQt/Settings.h"
2017-05-09 16:49:10 +00:00
#include "InputCommon/GCAdapter.h"
2018-05-28 01:48:04 +00:00
#include "UICommon/UICommon.h"
2017-05-09 16:49:10 +00:00
static const std::map<SerialInterface::SIDevices, int> s_gc_types = {
2017-05-09 16:49:10 +00:00
{SerialInterface::SIDEVICE_NONE, 0}, {SerialInterface::SIDEVICE_GC_CONTROLLER, 1},
{SerialInterface::SIDEVICE_WIIU_ADAPTER, 2}, {SerialInterface::SIDEVICE_GC_STEERING, 3},
{SerialInterface::SIDEVICE_DANCEMAT, 4}, {SerialInterface::SIDEVICE_GC_TARUKONGA, 5},
{SerialInterface::SIDEVICE_GC_GBA, 6}, {SerialInterface::SIDEVICE_GC_KEYBOARD, 7}};
static std::optional<int> ToGCMenuIndex(const SerialInterface::SIDevices sidevice)
2017-05-09 16:49:10 +00:00
{
auto it = s_gc_types.find(sidevice);
return it != s_gc_types.end() ? it->second : std::optional<int>();
2017-05-09 16:49:10 +00:00
}
static std::optional<SerialInterface::SIDevices> FromGCMenuIndex(const int menudevice)
2017-05-09 16:49:10 +00:00
{
auto it = std::find_if(s_gc_types.begin(), s_gc_types.end(),
[=](auto pair) { return pair.second == menudevice; });
return it != s_gc_types.end() ? it->first : std::optional<SerialInterface::SIDevices>();
2017-05-09 16:49:10 +00:00
}
ControllersWindow::ControllersWindow(QWidget* parent) : QDialog(parent)
2017-05-09 16:49:10 +00:00
{
setWindowTitle(tr("Controller Settings"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2017-05-09 16:49:10 +00:00
CreateGamecubeLayout();
m_wiimote_controllers = new WiimoteControllersWidget(this);
m_common = new CommonControllersWidget(this);
2017-05-09 16:49:10 +00:00
CreateMainLayout();
LoadSettings();
ConnectWidgets();
}
void ControllersWindow::CreateGamecubeLayout()
{
m_gc_box = new QGroupBox(tr("GameCube Controllers"));
m_gc_layout = new QGridLayout();
m_gc_layout->setVerticalSpacing(7);
m_gc_layout->setColumnStretch(1, 1);
2017-05-09 16:49:10 +00:00
for (size_t i = 0; i < m_gc_groups.size(); i++)
{
auto* gc_label = new QLabel(tr("Port %1").arg(i + 1));
2017-05-09 16:49:10 +00:00
auto* gc_box = m_gc_controller_boxes[i] = new QComboBox();
auto* gc_button = m_gc_buttons[i] = new QPushButton(tr("Configure"));
2017-05-09 16:49:10 +00:00
for (const auto& item :
{tr("None"), tr("Standard Controller"), tr("GameCube Adapter for Wii U"),
tr("Steering Wheel"), tr("Dance Mat"), tr("DK Bongos"), tr("GBA"), tr("Keyboard")})
{
gc_box->addItem(item);
}
int controller_row = m_gc_layout->rowCount();
m_gc_layout->addWidget(gc_label, controller_row, 0);
m_gc_layout->addWidget(gc_box, controller_row, 1);
m_gc_layout->addWidget(gc_button, controller_row, 2);
2017-05-09 16:49:10 +00:00
}
m_gc_box->setLayout(m_gc_layout);
}
void ControllersWindow::CreateMainLayout()
{
2018-03-17 15:07:51 +00:00
auto* layout = new QVBoxLayout();
2018-03-20 09:12:11 +00:00
m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
2017-05-09 16:49:10 +00:00
2018-03-17 15:07:51 +00:00
layout->addWidget(m_gc_box);
layout->addWidget(m_wiimote_controllers);
layout->addWidget(m_common);
layout->addStretch();
2018-03-17 15:07:51 +00:00
layout->addWidget(m_button_box);
2017-05-09 16:49:10 +00:00
2018-03-17 15:07:51 +00:00
WrapInScrollArea(this, layout);
2017-05-09 16:49:10 +00:00
}
void ControllersWindow::ConnectWidgets()
{
2018-03-20 09:12:11 +00:00
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
2017-05-09 16:49:10 +00:00
for (size_t i = 0; i < m_gc_groups.size(); i++)
{
connect(m_gc_controller_boxes[i], qOverload<int>(&QComboBox::currentIndexChanged), this,
2017-05-09 16:49:10 +00:00
&ControllersWindow::SaveSettings);
connect(m_gc_controller_boxes[i], qOverload<int>(&QComboBox::currentIndexChanged), this,
2017-05-09 16:49:10 +00:00
&ControllersWindow::OnGCTypeChanged);
connect(m_gc_buttons[i], &QPushButton::clicked, this, &ControllersWindow::OnGCPadConfigure);
}
}
void ControllersWindow::OnGCTypeChanged(int type)
{
const auto* box = static_cast<QComboBox*>(QObject::sender());
for (size_t i = 0; i < m_gc_groups.size(); i++)
{
if (m_gc_controller_boxes[i] == box)
{
const int index = box->currentIndex();
m_gc_buttons[i]->setEnabled(index != 0 && index != 6);
return;
}
}
SaveSettings();
2017-05-09 16:49:10 +00:00
}
void ControllersWindow::OnGCPadConfigure()
{
size_t index;
for (index = 0; index < m_gc_groups.size(); index++)
{
if (m_gc_buttons[index] == QObject::sender())
break;
}
2017-05-20 15:53:17 +00:00
MappingWindow::Type type;
2017-05-09 16:49:10 +00:00
switch (m_gc_controller_boxes[index]->currentIndex())
{
case 0: // None
case 6: // GBA
return;
case 1: // Standard Controller
2017-05-20 15:53:17 +00:00
type = MappingWindow::Type::MAPPING_GCPAD;
break;
2017-05-09 16:49:10 +00:00
case 2: // GameCube Adapter for Wii U
GCPadWiiUConfigDialog(static_cast<int>(index), this).exec();
return;
2017-05-09 16:49:10 +00:00
case 3: // Steering Wheel
2017-05-20 15:53:17 +00:00
type = MappingWindow::Type::MAPPING_GC_STEERINGWHEEL;
break;
2017-05-09 16:49:10 +00:00
case 4: // Dance Mat
2017-05-20 15:53:17 +00:00
type = MappingWindow::Type::MAPPING_GC_DANCEMAT;
break;
2017-05-09 16:49:10 +00:00
case 5: // DK Bongos
2017-05-20 15:53:17 +00:00
type = MappingWindow::Type::MAPPING_GC_BONGOS;
break;
2017-05-09 16:49:10 +00:00
case 7: // Keyboard
2017-05-20 15:53:17 +00:00
type = MappingWindow::Type::MAPPING_GC_KEYBOARD;
break;
default:
2017-05-09 16:49:10 +00:00
return;
}
MappingWindow* window = new MappingWindow(this, type, static_cast<int>(index));
window->setAttribute(Qt::WA_DeleteOnClose, true);
window->setWindowModality(Qt::WindowModality::WindowModal);
window->show();
2017-05-09 16:49:10 +00:00
}
void ControllersWindow::LoadSettings()
{
for (size_t i = 0; i < m_gc_groups.size(); i++)
2017-05-09 16:49:10 +00:00
{
const std::optional<int> gc_index = ToGCMenuIndex(SConfig::GetInstance().m_SIDevice[i]);
if (gc_index)
{
m_gc_controller_boxes[i]->setCurrentIndex(*gc_index);
m_gc_buttons[i]->setEnabled(*gc_index != 0 && *gc_index != 6);
}
2017-05-09 16:49:10 +00:00
}
}
void ControllersWindow::SaveSettings()
{
for (size_t i = 0; i < m_gc_groups.size(); i++)
{
const int index = m_gc_controller_boxes[i]->currentIndex();
const std::optional<SerialInterface::SIDevices> si_device = FromGCMenuIndex(index);
if (si_device)
{
SConfig::GetInstance().m_SIDevice[i] = *si_device;
if (Core::IsRunning())
SerialInterface::ChangeDevice(*si_device, static_cast<s32>(i));
}
2017-05-09 16:49:10 +00:00
m_gc_buttons[i]->setEnabled(index != 0 && index != 6);
}
if (GCAdapter::UseAdapter())
GCAdapter::StartScanThread();
else
GCAdapter::StopScanThread();
SConfig::GetInstance().SaveSettings();
2017-05-09 16:49:10 +00:00
}