2018-09-08 19:01:30 +02:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
2016-07-29 14:45:49 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2017-01-22 20:02:29 +01:00
|
|
|
#include <algorithm>
|
2016-07-29 14:45:49 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
2018-12-29 01:24:56 +01:00
|
|
|
#include <QInputDialog>
|
2018-08-26 10:23:12 +02:00
|
|
|
#include <QMenu>
|
2017-12-06 05:26:29 +01:00
|
|
|
#include <QMessageBox>
|
2016-09-20 17:21:23 +02:00
|
|
|
#include <QTimer>
|
2016-12-22 05:49:36 +01:00
|
|
|
#include "citra_qt/configuration/config.h"
|
|
|
|
#include "citra_qt/configuration/configure_input.h"
|
2018-08-11 05:52:13 +02:00
|
|
|
#include "citra_qt/configuration/configure_motion_touch.h"
|
2017-01-22 20:02:29 +01:00
|
|
|
#include "common/param_package.h"
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2017-01-22 20:02:29 +01:00
|
|
|
const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
|
|
|
|
ConfigureInput::analog_sub_buttons{{
|
2018-03-09 18:54:43 +01:00
|
|
|
"up",
|
|
|
|
"down",
|
|
|
|
"left",
|
|
|
|
"right",
|
|
|
|
"modifier",
|
2017-01-22 20:02:29 +01:00
|
|
|
}};
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
static QString GetKeyName(int key_code) {
|
2016-12-10 00:59:09 +01:00
|
|
|
switch (key_code) {
|
|
|
|
case Qt::Key_Shift:
|
|
|
|
return QObject::tr("Shift");
|
|
|
|
case Qt::Key_Control:
|
|
|
|
return QObject::tr("Ctrl");
|
|
|
|
case Qt::Key_Alt:
|
|
|
|
return QObject::tr("Alt");
|
|
|
|
case Qt::Key_Meta:
|
2019-07-26 21:11:14 +02:00
|
|
|
return QString{};
|
2016-12-10 00:59:09 +01:00
|
|
|
default:
|
|
|
|
return QKeySequence(key_code).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 05:26:29 +01:00
|
|
|
static void SetAnalogButton(const Common::ParamPackage& input_param,
|
|
|
|
Common::ParamPackage& analog_param, const std::string& button_name) {
|
2017-01-22 20:02:29 +01:00
|
|
|
if (analog_param.Get("engine", "") != "analog_from_button") {
|
|
|
|
analog_param = {
|
2018-03-09 18:54:43 +01:00
|
|
|
{"engine", "analog_from_button"},
|
|
|
|
{"modifier_scale", "0.5"},
|
2017-01-22 20:02:29 +01:00
|
|
|
};
|
|
|
|
}
|
2017-12-06 05:26:29 +01:00
|
|
|
analog_param.Set(button_name, input_param.Serialize());
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 19:06:14 +01:00
|
|
|
static QString ButtonToText(const Common::ParamPackage& param) {
|
|
|
|
if (!param.Has("engine")) {
|
|
|
|
return QObject::tr("[not set]");
|
2019-07-26 21:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "keyboard") {
|
2019-05-26 06:39:23 +02:00
|
|
|
return GetKeyName(param.Get("code", 0));
|
2019-07-26 21:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "sdl") {
|
2017-12-13 19:06:14 +01:00
|
|
|
if (param.Has("hat")) {
|
2019-07-26 21:11:14 +02:00
|
|
|
const QString hat_str = QString::fromStdString(param.Get("hat", ""));
|
|
|
|
const QString direction_str = QString::fromStdString(param.Get("direction", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Hat %1 %2").arg(hat_str, direction_str);
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
2017-12-13 19:06:14 +01:00
|
|
|
if (param.Has("axis")) {
|
2019-07-26 21:11:14 +02:00
|
|
|
const QString axis_str = QString::fromStdString(param.Get("axis", ""));
|
|
|
|
const QString direction_str = QString::fromStdString(param.Get("direction", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Axis %1%2").arg(axis_str, direction_str);
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
2017-12-13 19:06:14 +01:00
|
|
|
if (param.Has("button")) {
|
2019-07-26 21:11:14 +02:00
|
|
|
const QString button_str = QString::fromStdString(param.Get("button", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Button %1").arg(button_str);
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
|
|
|
return {};
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
|
|
|
return QObject::tr("[unknown]");
|
|
|
|
}
|
2017-12-13 19:06:14 +01:00
|
|
|
|
|
|
|
static QString AnalogToText(const Common::ParamPackage& param, const std::string& dir) {
|
|
|
|
if (!param.Has("engine")) {
|
|
|
|
return QObject::tr("[not set]");
|
2019-07-26 21:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "analog_from_button") {
|
2017-12-13 19:06:14 +01:00
|
|
|
return ButtonToText(Common::ParamPackage{param.Get(dir, "")});
|
2019-07-26 21:11:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "sdl") {
|
2017-12-13 19:06:14 +01:00
|
|
|
if (dir == "modifier") {
|
2019-07-26 21:11:14 +02:00
|
|
|
return QObject::tr("[unused]");
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dir == "left" || dir == "right") {
|
2019-07-26 21:11:14 +02:00
|
|
|
const QString axis_x_str = QString::fromStdString(param.Get("axis_x", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Axis %1").arg(axis_x_str);
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
|
|
|
if (dir == "up" || dir == "down") {
|
|
|
|
const QString axis_y_str = QString::fromStdString(param.Get("axis_y", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Axis %1").arg(axis_y_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
2017-12-13 19:06:14 +01:00
|
|
|
}
|
2019-07-26 21:11:14 +02:00
|
|
|
|
|
|
|
return QObject::tr("[unknown]");
|
|
|
|
}
|
2017-12-13 19:06:14 +01:00
|
|
|
|
2016-09-18 02:38:01 +02:00
|
|
|
ConfigureInput::ConfigureInput(QWidget* parent)
|
2016-12-10 00:59:09 +01:00
|
|
|
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
|
2017-12-06 05:26:29 +01:00
|
|
|
timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()) {
|
2016-07-29 14:45:49 +02:00
|
|
|
ui->setupUi(this);
|
2016-12-10 00:59:09 +01:00
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
for (const auto& profile : Settings::values.input_profiles) {
|
2018-12-29 03:00:09 +01:00
|
|
|
ui->profile->addItem(QString::fromStdString(profile.name));
|
|
|
|
}
|
2018-12-29 01:24:56 +01:00
|
|
|
|
2018-12-29 05:31:55 +01:00
|
|
|
ui->profile->setCurrentIndex(Settings::values.current_input_profile_index);
|
2017-01-22 20:02:29 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
button_map = {
|
2018-12-28 22:13:16 +01:00
|
|
|
ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY,
|
|
|
|
ui->buttonDpadUp, ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight,
|
|
|
|
ui->buttonL, ui->buttonR, ui->buttonStart, ui->buttonSelect,
|
2018-12-29 00:13:37 +01:00
|
|
|
ui->buttonDebug, ui->buttonGpio14, ui->buttonZL, ui->buttonZR,
|
|
|
|
ui->buttonHome,
|
2018-12-29 03:26:50 +01:00
|
|
|
};
|
2017-12-06 05:26:29 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
analog_map_buttons = {{
|
|
|
|
{
|
|
|
|
ui->buttonCircleUp,
|
|
|
|
ui->buttonCircleDown,
|
|
|
|
ui->buttonCircleLeft,
|
|
|
|
ui->buttonCircleRight,
|
|
|
|
ui->buttonCircleMod,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ui->buttonCStickUp,
|
|
|
|
ui->buttonCStickDown,
|
|
|
|
ui->buttonCStickLeft,
|
|
|
|
ui->buttonCStickRight,
|
|
|
|
nullptr,
|
|
|
|
},
|
|
|
|
}};
|
2018-12-29 03:13:57 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
analog_map_stick = {ui->buttonCircleAnalog, ui->buttonCStickAnalog};
|
2018-12-29 03:13:57 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
|
|
|
|
if (!button_map[button_id])
|
2018-08-26 10:23:12 +02:00
|
|
|
continue;
|
2018-12-29 03:26:50 +01:00
|
|
|
button_map[button_id]->setContextMenuPolicy(Qt::CustomContextMenu);
|
2019-07-22 23:28:10 +02:00
|
|
|
connect(button_map[button_id], &QPushButton::clicked, [=]() {
|
2019-05-26 06:39:23 +02:00
|
|
|
HandleClick(button_map[button_id],
|
2018-12-29 15:31:03 +01:00
|
|
|
[=](const Common::ParamPackage& params) {
|
|
|
|
buttons_param[button_id] = params;
|
2018-12-29 19:15:31 +01:00
|
|
|
// If the user closes the dialog, the changes are reverted in
|
|
|
|
// `GMainWindow::OnConfigure()`
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 15:31:03 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
},
|
|
|
|
InputCommon::Polling::DeviceType::Button);
|
2018-08-26 10:23:12 +02:00
|
|
|
});
|
2018-12-29 03:26:50 +01:00
|
|
|
connect(button_map[button_id], &QPushButton::customContextMenuRequested,
|
|
|
|
[=](const QPoint& menu_location) {
|
2018-08-26 10:23:12 +02:00
|
|
|
QMenu context_menu;
|
|
|
|
context_menu.addAction(tr("Clear"), [&] {
|
2018-12-29 03:26:50 +01:00
|
|
|
buttons_param[button_id].Clear();
|
|
|
|
button_map[button_id]->setText(tr("[not set]"));
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 01:24:56 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
2018-08-26 10:23:12 +02:00
|
|
|
});
|
|
|
|
context_menu.addAction(tr("Restore Default"), [&] {
|
2018-12-29 03:26:50 +01:00
|
|
|
buttons_param[button_id] = Common::ParamPackage{
|
|
|
|
InputCommon::GenerateKeyboardParam(Config::default_buttons[button_id])};
|
|
|
|
button_map[button_id]->setText(ButtonToText(buttons_param[button_id]));
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 03:26:50 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
});
|
|
|
|
context_menu.exec(button_map[button_id]->mapToGlobal(menu_location));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
|
|
|
|
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
|
|
|
|
if (!analog_map_buttons[analog_id][sub_button_id])
|
|
|
|
continue;
|
|
|
|
analog_map_buttons[analog_id][sub_button_id]->setContextMenuPolicy(
|
|
|
|
Qt::CustomContextMenu);
|
2019-07-22 23:28:10 +02:00
|
|
|
connect(analog_map_buttons[analog_id][sub_button_id], &QPushButton::clicked, [=]() {
|
2019-05-26 06:39:23 +02:00
|
|
|
HandleClick(analog_map_buttons[analog_id][sub_button_id],
|
2018-12-29 15:31:03 +01:00
|
|
|
[=](const Common::ParamPackage& params) {
|
|
|
|
SetAnalogButton(params, analogs_param[analog_id],
|
|
|
|
analog_sub_buttons[sub_button_id]);
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 15:31:03 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
},
|
|
|
|
InputCommon::Polling::DeviceType::Button);
|
2018-12-29 03:26:50 +01:00
|
|
|
});
|
|
|
|
connect(analog_map_buttons[analog_id][sub_button_id],
|
|
|
|
&QPushButton::customContextMenuRequested, [=](const QPoint& menu_location) {
|
|
|
|
QMenu context_menu;
|
|
|
|
context_menu.addAction(tr("Clear"), [&] {
|
|
|
|
analogs_param[analog_id].Erase(analog_sub_buttons[sub_button_id]);
|
|
|
|
analog_map_buttons[analog_id][sub_button_id]->setText(tr("[not set]"));
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 03:26:50 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
});
|
|
|
|
context_menu.addAction(tr("Restore Default"), [&] {
|
|
|
|
Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
|
|
|
|
Config::default_analogs[analog_id][sub_button_id])};
|
|
|
|
SetAnalogButton(params, analogs_param[analog_id],
|
|
|
|
analog_sub_buttons[sub_button_id]);
|
|
|
|
analog_map_buttons[analog_id][sub_button_id]->setText(AnalogToText(
|
|
|
|
analogs_param[analog_id], analog_sub_buttons[sub_button_id]));
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 03:26:50 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
});
|
|
|
|
context_menu.exec(analog_map_buttons[analog_id][sub_button_id]->mapToGlobal(
|
|
|
|
menu_location));
|
2018-08-26 10:23:12 +02:00
|
|
|
});
|
2018-12-29 03:26:50 +01:00
|
|
|
}
|
2019-07-22 23:28:10 +02:00
|
|
|
connect(analog_map_stick[analog_id], &QPushButton::clicked, [=]() {
|
2019-09-01 02:58:07 +02:00
|
|
|
if (QMessageBox::information(
|
|
|
|
this, tr("Information"),
|
|
|
|
tr("After pressing OK, first move your joystick horizontally, "
|
|
|
|
"and then vertically."),
|
|
|
|
QMessageBox::Ok | QMessageBox::Cancel) == QMessageBox::Ok) {
|
|
|
|
HandleClick(analog_map_stick[analog_id],
|
|
|
|
[=](const Common::ParamPackage& params) {
|
|
|
|
analogs_param[analog_id] = params;
|
|
|
|
ApplyConfiguration();
|
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
},
|
|
|
|
InputCommon::Polling::DeviceType::Analog);
|
|
|
|
}
|
2018-12-29 03:26:50 +01:00
|
|
|
});
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
2016-12-10 00:59:09 +01:00
|
|
|
|
2019-07-22 23:28:10 +02:00
|
|
|
connect(ui->buttonMotionTouch, &QPushButton::clicked, [this] {
|
2018-12-29 03:26:50 +01:00
|
|
|
QDialog* motion_touch_dialog = new ConfigureMotionTouch(this);
|
|
|
|
return motion_touch_dialog->exec();
|
|
|
|
});
|
2018-12-29 14:27:06 +01:00
|
|
|
|
|
|
|
ui->buttonDelete->setEnabled(ui->profile->count() > 1);
|
|
|
|
|
2019-07-22 23:28:10 +02:00
|
|
|
connect(ui->buttonClearAll, &QPushButton::clicked, this, &ConfigureInput::ClearAll);
|
|
|
|
connect(ui->buttonRestoreDefaults, &QPushButton::clicked, this,
|
|
|
|
&ConfigureInput::RestoreDefaults);
|
|
|
|
connect(ui->buttonNew, &QPushButton::clicked, this, &ConfigureInput::NewProfile);
|
|
|
|
connect(ui->buttonDelete, &QPushButton::clicked, this, &ConfigureInput::DeleteProfile);
|
|
|
|
connect(ui->buttonRename, &QPushButton::clicked, this, &ConfigureInput::RenameProfile);
|
2018-12-29 03:13:57 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
connect(ui->profile, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
|
|
|
|
[this](int i) {
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 05:31:55 +01:00
|
|
|
Settings::SaveProfile(Settings::values.current_input_profile_index);
|
2018-12-29 03:26:50 +01:00
|
|
|
Settings::LoadProfile(i);
|
2019-05-26 06:39:23 +02:00
|
|
|
LoadConfiguration();
|
2018-12-29 03:26:50 +01:00
|
|
|
});
|
2017-12-06 05:26:29 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
timeout_timer->setSingleShot(true);
|
2019-05-26 06:39:23 +02:00
|
|
|
connect(timeout_timer.get(), &QTimer::timeout, [this]() { SetPollingResult({}, true); });
|
2018-12-29 03:13:57 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
connect(poll_timer.get(), &QTimer::timeout, [this]() {
|
|
|
|
Common::ParamPackage params;
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
params = poller->GetNextInput();
|
|
|
|
if (params.Has("engine")) {
|
2019-05-26 06:39:23 +02:00
|
|
|
SetPollingResult(params, false);
|
2018-12-29 03:26:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-12-06 05:26:29 +01:00
|
|
|
}
|
2018-12-29 03:26:50 +01:00
|
|
|
});
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
LoadConfiguration();
|
2017-01-22 20:02:29 +01:00
|
|
|
|
2018-12-29 03:26:50 +01:00
|
|
|
// TODO(wwylele): enable this when we actually emulate it
|
|
|
|
ui->buttonHome->setEnabled(false);
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-02 20:06:39 +01:00
|
|
|
ConfigureInput::~ConfigureInput() = default;
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::ApplyConfiguration() {
|
2018-12-29 05:31:55 +01:00
|
|
|
std::transform(buttons_param.begin(), buttons_param.end(),
|
|
|
|
Settings::values.current_input_profile.buttons.begin(),
|
2017-01-22 20:02:29 +01:00
|
|
|
[](const Common::ParamPackage& param) { return param.Serialize(); });
|
2018-12-29 05:31:55 +01:00
|
|
|
std::transform(analogs_param.begin(), analogs_param.end(),
|
|
|
|
Settings::values.current_input_profile.analogs.begin(),
|
2017-01-22 20:02:29 +01:00
|
|
|
[](const Common::ParamPackage& param) { return param.Serialize(); });
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:27:06 +01:00
|
|
|
void ConfigureInput::ApplyProfile() {
|
2018-12-29 05:31:55 +01:00
|
|
|
Settings::values.current_input_profile_index = ui->profile->currentIndex();
|
2018-12-29 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
2018-05-22 21:30:36 +02:00
|
|
|
void ConfigureInput::EmitInputKeysChanged() {
|
|
|
|
emit InputKeysChanged(GetUsedKeyboardKeys());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureInput::OnHotkeysChanged(QList<QKeySequence> new_key_list) {
|
|
|
|
hotkey_list = new_key_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QKeySequence> ConfigureInput::GetUsedKeyboardKeys() {
|
|
|
|
QList<QKeySequence> list;
|
|
|
|
for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
|
2019-04-05 06:16:01 +02:00
|
|
|
// TODO(adityaruplaha): Add home button to list when we finally emulate it
|
|
|
|
if (button == Settings::NativeButton::Home) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-22 21:30:36 +02:00
|
|
|
|
2019-04-05 06:16:01 +02:00
|
|
|
auto button_param = buttons_param[button];
|
2018-05-22 21:30:36 +02:00
|
|
|
if (button_param.Get("engine", "") == "keyboard") {
|
|
|
|
list << QKeySequence(button_param.Get("code", 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::LoadConfiguration() {
|
2018-12-29 05:31:55 +01:00
|
|
|
std::transform(Settings::values.current_input_profile.buttons.begin(),
|
|
|
|
Settings::values.current_input_profile.buttons.end(), buttons_param.begin(),
|
2017-01-22 20:02:29 +01:00
|
|
|
[](const std::string& str) { return Common::ParamPackage(str); });
|
2018-12-29 05:31:55 +01:00
|
|
|
std::transform(Settings::values.current_input_profile.analogs.begin(),
|
|
|
|
Settings::values.current_input_profile.analogs.end(), analogs_param.begin(),
|
2017-01-22 20:02:29 +01:00
|
|
|
[](const std::string& str) { return Common::ParamPackage(str); });
|
2019-05-26 06:39:23 +02:00
|
|
|
UpdateButtonLabels();
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::RestoreDefaults() {
|
2017-01-22 20:02:29 +01:00
|
|
|
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
|
2017-12-06 05:26:29 +01:00
|
|
|
buttons_param[button_id] = Common::ParamPackage{
|
|
|
|
InputCommon::GenerateKeyboardParam(Config::default_buttons[button_id])};
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
|
|
|
|
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
|
2017-12-06 05:26:29 +01:00
|
|
|
Common::ParamPackage params{InputCommon::GenerateKeyboardParam(
|
|
|
|
Config::default_analogs[analog_id][sub_button_id])};
|
|
|
|
SetAnalogButton(params, analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-26 06:39:23 +02:00
|
|
|
UpdateButtonLabels();
|
2018-08-26 10:23:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureInput::ClearAll() {
|
|
|
|
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
|
|
|
|
if (button_map[button_id] && button_map[button_id]->isEnabled())
|
|
|
|
buttons_param[button_id].Clear();
|
|
|
|
}
|
|
|
|
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
|
|
|
|
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
|
|
|
|
if (analog_map_buttons[analog_id][sub_button_id] &&
|
|
|
|
analog_map_buttons[analog_id][sub_button_id]->isEnabled())
|
|
|
|
analogs_param[analog_id].Erase(analog_sub_buttons[sub_button_id]);
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 06:39:23 +02:00
|
|
|
UpdateButtonLabels();
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::UpdateButtonLabels() {
|
2017-01-22 20:02:29 +01:00
|
|
|
for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
|
2018-12-28 19:47:50 +01:00
|
|
|
if (button_map[button])
|
|
|
|
button_map[button]->setText(ButtonToText(buttons_param[button]));
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
|
2017-12-08 13:03:43 +01:00
|
|
|
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
|
|
|
|
if (analog_map_buttons[analog_id][sub_button_id]) {
|
|
|
|
analog_map_buttons[analog_id][sub_button_id]->setText(
|
|
|
|
AnalogToText(analogs_param[analog_id], analog_sub_buttons[sub_button_id]));
|
2017-01-22 20:02:29 +01:00
|
|
|
}
|
|
|
|
}
|
2017-12-09 16:11:17 +01:00
|
|
|
analog_map_stick[analog_id]->setText(tr("Set Analog Stick"));
|
2016-12-10 00:59:09 +01:00
|
|
|
}
|
2018-05-22 21:30:36 +02:00
|
|
|
|
|
|
|
EmitInputKeysChanged();
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::HandleClick(QPushButton* button,
|
2017-12-06 05:26:29 +01:00
|
|
|
std::function<void(const Common::ParamPackage&)> new_input_setter,
|
|
|
|
InputCommon::Polling::DeviceType type) {
|
2018-05-22 21:30:36 +02:00
|
|
|
previous_key_code = QKeySequence(button->text())[0];
|
2016-12-10 00:59:09 +01:00
|
|
|
button->setText(tr("[press key]"));
|
|
|
|
button->setFocus();
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2017-12-06 05:26:29 +01:00
|
|
|
input_setter = new_input_setter;
|
|
|
|
|
|
|
|
device_pollers = InputCommon::Polling::GetPollers(type);
|
|
|
|
|
|
|
|
// Keyboard keys can only be used as button devices
|
|
|
|
want_keyboard_keys = type == InputCommon::Polling::DeviceType::Button;
|
|
|
|
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
poller->Start();
|
|
|
|
}
|
2016-07-29 14:45:49 +02:00
|
|
|
|
2016-12-10 00:59:09 +01:00
|
|
|
grabKeyboard();
|
|
|
|
grabMouse();
|
2017-12-06 05:26:29 +01:00
|
|
|
timeout_timer->start(5000); // Cancel after 5 seconds
|
|
|
|
poll_timer->start(200); // Check for new inputs every 200ms
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::SetPollingResult(const Common::ParamPackage& params, bool abort) {
|
2016-12-10 00:59:09 +01:00
|
|
|
releaseKeyboard();
|
|
|
|
releaseMouse();
|
2017-12-06 05:26:29 +01:00
|
|
|
timeout_timer->stop();
|
|
|
|
poll_timer->stop();
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
poller->Stop();
|
|
|
|
}
|
2016-12-10 00:59:09 +01:00
|
|
|
|
2018-10-05 16:51:33 +02:00
|
|
|
if (!abort && input_setter) {
|
2017-12-06 05:26:29 +01:00
|
|
|
(*input_setter)(params);
|
|
|
|
}
|
2016-12-10 00:59:09 +01:00
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
UpdateButtonLabels();
|
2018-10-05 12:37:55 +02:00
|
|
|
input_setter.reset();
|
2017-12-06 05:26:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureInput::keyPressEvent(QKeyEvent* event) {
|
|
|
|
if (!input_setter || !event)
|
|
|
|
return;
|
|
|
|
|
2018-05-22 21:30:36 +02:00
|
|
|
if (event->key() != Qt::Key_Escape && event->key() != previous_key_code) {
|
2017-12-06 05:26:29 +01:00
|
|
|
if (want_keyboard_keys) {
|
2018-05-22 21:30:36 +02:00
|
|
|
// Check if key is already bound
|
|
|
|
if (hotkey_list.contains(QKeySequence(event->key())) ||
|
|
|
|
GetUsedKeyboardKeys().contains(QKeySequence(event->key()))) {
|
2019-05-26 06:39:23 +02:00
|
|
|
SetPollingResult({}, true);
|
2018-05-22 21:30:36 +02:00
|
|
|
QMessageBox::critical(this, tr("Error!"),
|
|
|
|
tr("You're using a key that's already bound."));
|
|
|
|
return;
|
|
|
|
}
|
2019-05-26 06:39:23 +02:00
|
|
|
SetPollingResult(Common::ParamPackage{InputCommon::GenerateKeyboardParam(event->key())},
|
2017-12-06 05:26:29 +01:00
|
|
|
false);
|
|
|
|
} else {
|
2018-05-22 21:30:36 +02:00
|
|
|
// Escape key wasn't pressed and we don't want any keyboard keys, so don't stop
|
|
|
|
// polling
|
2017-12-06 05:26:29 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 06:39:23 +02:00
|
|
|
SetPollingResult({}, true);
|
2018-05-22 21:30:36 +02:00
|
|
|
previous_key_code = 0;
|
2016-07-29 14:45:49 +02:00
|
|
|
}
|
2017-09-23 15:13:59 +02:00
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
void ConfigureInput::RetranslateUI() {
|
2017-09-23 15:13:59 +02:00
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
2018-12-29 01:24:56 +01:00
|
|
|
|
2018-12-29 14:27:06 +01:00
|
|
|
void ConfigureInput::NewProfile() {
|
2018-12-29 03:00:09 +01:00
|
|
|
const QString name =
|
2018-12-29 01:24:56 +01:00
|
|
|
QInputDialog::getText(this, tr("New Profile"), tr("Enter the name for the new profile."));
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-24 23:28:59 +02:00
|
|
|
if (IsProfileNameDuplicate(name)) {
|
|
|
|
WarnProposedProfileNameIsDuplicate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-26 06:39:23 +02:00
|
|
|
ApplyConfiguration();
|
2018-12-29 01:24:56 +01:00
|
|
|
Settings::SaveProfile(ui->profile->currentIndex());
|
|
|
|
Settings::CreateProfile(name.toStdString());
|
|
|
|
ui->profile->addItem(name);
|
2018-12-29 05:31:55 +01:00
|
|
|
ui->profile->setCurrentIndex(Settings::values.current_input_profile_index);
|
2019-05-26 06:39:23 +02:00
|
|
|
LoadConfiguration();
|
2018-12-29 14:27:06 +01:00
|
|
|
ui->buttonDelete->setEnabled(ui->profile->count() > 1);
|
2018-12-29 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:27:06 +01:00
|
|
|
void ConfigureInput::DeleteProfile() {
|
2018-12-29 03:00:09 +01:00
|
|
|
const auto answer = QMessageBox::question(
|
2018-12-29 01:24:56 +01:00
|
|
|
this, tr("Delete Profile"), tr("Delete profile %1?").arg(ui->profile->currentText()));
|
|
|
|
if (answer != QMessageBox::Yes) {
|
|
|
|
return;
|
|
|
|
}
|
2018-12-29 03:00:09 +01:00
|
|
|
const int index = ui->profile->currentIndex();
|
2018-12-29 01:24:56 +01:00
|
|
|
ui->profile->removeItem(index);
|
|
|
|
ui->profile->setCurrentIndex(0);
|
|
|
|
Settings::DeleteProfile(index);
|
2019-05-26 06:39:23 +02:00
|
|
|
LoadConfiguration();
|
2018-12-29 14:27:06 +01:00
|
|
|
ui->buttonDelete->setEnabled(ui->profile->count() > 1);
|
2018-12-29 01:24:56 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 14:27:06 +01:00
|
|
|
void ConfigureInput::RenameProfile() {
|
2018-12-29 03:00:09 +01:00
|
|
|
const QString new_name = QInputDialog::getText(this, tr("Rename Profile"), tr("New name:"));
|
2018-12-29 01:24:56 +01:00
|
|
|
if (new_name.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-24 23:28:59 +02:00
|
|
|
if (IsProfileNameDuplicate(new_name)) {
|
|
|
|
WarnProposedProfileNameIsDuplicate();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-29 01:24:56 +01:00
|
|
|
ui->profile->setItemText(ui->profile->currentIndex(), new_name);
|
|
|
|
Settings::RenameCurrentProfile(new_name.toStdString());
|
|
|
|
}
|
2019-05-24 23:28:59 +02:00
|
|
|
|
|
|
|
bool ConfigureInput::IsProfileNameDuplicate(const QString& name) const {
|
|
|
|
return ui->profile->findText(name, Qt::MatchFixedString | Qt::MatchCaseSensitive) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureInput::WarnProposedProfileNameIsDuplicate() {
|
|
|
|
QMessageBox::warning(this, tr("Duplicate profile name"),
|
|
|
|
tr("Profile name already exists. Please choose a different name."));
|
|
|
|
}
|