2016-07-29 08:45:49 -04:00
|
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
#include <algorithm>
|
2016-07-29 08:45:49 -04:00
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
2016-09-20 11:21:23 -04:00
|
|
|
|
#include <QTimer>
|
2016-12-21 23:49:36 -05:00
|
|
|
|
#include "citra_qt/configuration/config.h"
|
|
|
|
|
#include "citra_qt/configuration/configure_input.h"
|
2017-01-22 14:02:29 -05:00
|
|
|
|
#include "common/param_package.h"
|
|
|
|
|
#include "input_common/main.h"
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
|
|
|
|
|
ConfigureInput::analog_sub_buttons{{
|
|
|
|
|
"up", "down", "left", "right", "modifier",
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
static QString getKeyName(int key_code) {
|
2016-12-09 18:59:09 -05: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:
|
|
|
|
|
return "";
|
|
|
|
|
default:
|
|
|
|
|
return QKeySequence(key_code).toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
static void SetButtonKey(int key, Common::ParamPackage& button_param) {
|
|
|
|
|
button_param = Common::ParamPackage{InputCommon::GenerateKeyboardParam(key)};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void SetAnalogKey(int key, Common::ParamPackage& analog_param,
|
|
|
|
|
const std::string& button_name) {
|
|
|
|
|
if (analog_param.Get("engine", "") != "analog_from_button") {
|
|
|
|
|
analog_param = {
|
|
|
|
|
{"engine", "analog_from_button"}, {"modifier_scale", "0.5"},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
analog_param.Set(button_name, InputCommon::GenerateKeyboardParam(key));
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 20:38:01 -04:00
|
|
|
|
ConfigureInput::ConfigureInput(QWidget* parent)
|
2016-12-09 18:59:09 -05:00
|
|
|
|
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
|
|
|
|
|
timer(std::make_unique<QTimer>()) {
|
2016-09-18 21:01:46 -04:00
|
|
|
|
|
2016-07-29 08:45:49 -04:00
|
|
|
|
ui->setupUi(this);
|
2016-12-09 18:59:09 -05:00
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
button_map = {
|
2017-01-22 14:02:29 -05: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, ui->buttonZL, ui->buttonZR, ui->buttonHome,
|
2016-07-29 08:45:49 -04:00
|
|
|
|
};
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
analog_map = {{
|
|
|
|
|
{
|
|
|
|
|
ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight,
|
|
|
|
|
ui->buttonCircleMod,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
ui->buttonCStickUp, ui->buttonCStickDown, ui->buttonCStickLeft, ui->buttonCStickRight,
|
|
|
|
|
nullptr,
|
|
|
|
|
},
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
|
|
|
|
|
if (button_map[button_id])
|
|
|
|
|
connect(button_map[button_id], &QPushButton::released, [=]() {
|
|
|
|
|
handleClick(button_map[button_id],
|
|
|
|
|
[=](int key) { SetButtonKey(key, buttons_param[button_id]); });
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-03-02 06:29:28 -05:00
|
|
|
|
if (analog_map[analog_id][sub_button_id] != nullptr) {
|
|
|
|
|
connect(analog_map[analog_id][sub_button_id], &QPushButton::released, [=]() {
|
|
|
|
|
handleClick(analog_map[analog_id][sub_button_id], [=](int key) {
|
|
|
|
|
SetAnalogKey(key, analogs_param[analog_id],
|
|
|
|
|
analog_sub_buttons[sub_button_id]);
|
|
|
|
|
});
|
2017-01-22 14:02:29 -05:00
|
|
|
|
});
|
2017-03-02 06:29:28 -05:00
|
|
|
|
}
|
2017-01-22 14:02:29 -05:00
|
|
|
|
}
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|
2016-12-09 18:59:09 -05:00
|
|
|
|
|
|
|
|
|
connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
|
|
|
|
|
|
2016-07-29 08:45:49 -04:00
|
|
|
|
timer->setSingleShot(true);
|
2016-12-09 18:59:09 -05:00
|
|
|
|
connect(timer.get(), &QTimer::timeout, [this]() {
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
releaseMouse();
|
2017-01-22 14:02:29 -05:00
|
|
|
|
key_setter = boost::none;
|
2016-12-09 18:59:09 -05:00
|
|
|
|
updateButtonLabels();
|
2016-09-17 20:38:01 -04:00
|
|
|
|
});
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
this->loadConfiguration();
|
2017-01-22 14:02:29 -05:00
|
|
|
|
|
2017-02-25 13:56:43 -05:00
|
|
|
|
// TODO(wwylele): enable this when we actually emulate it
|
2017-01-22 14:02:29 -05:00
|
|
|
|
ui->buttonHome->setEnabled(false);
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureInput::applyConfiguration() {
|
2017-01-22 14:02:29 -05:00
|
|
|
|
std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
|
|
|
|
|
[](const Common::ParamPackage& param) { return param.Serialize(); });
|
|
|
|
|
std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
|
|
|
|
|
[](const Common::ParamPackage& param) { return param.Serialize(); });
|
|
|
|
|
|
2016-07-29 08:45:49 -04:00
|
|
|
|
Settings::Apply();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
void ConfigureInput::loadConfiguration() {
|
2017-01-22 14:02:29 -05:00
|
|
|
|
std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
|
|
|
|
|
buttons_param.begin(),
|
|
|
|
|
[](const std::string& str) { return Common::ParamPackage(str); });
|
|
|
|
|
std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(),
|
|
|
|
|
analogs_param.begin(),
|
|
|
|
|
[](const std::string& str) { return Common::ParamPackage(str); });
|
2016-12-09 18:59:09 -05:00
|
|
|
|
updateButtonLabels();
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
void ConfigureInput::restoreDefaults() {
|
|
|
|
|
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
|
|
|
|
|
SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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++) {
|
|
|
|
|
SetAnalogKey(Config::default_analogs[analog_id][sub_button_id],
|
|
|
|
|
analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
updateButtonLabels();
|
|
|
|
|
applyConfiguration();
|
|
|
|
|
}
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
void ConfigureInput::updateButtonLabels() {
|
2017-01-22 14:02:29 -05:00
|
|
|
|
QString non_keyboard(tr("[non-keyboard]"));
|
|
|
|
|
|
|
|
|
|
auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) {
|
|
|
|
|
if (param.Get("engine", "") != "keyboard") {
|
|
|
|
|
return non_keyboard;
|
|
|
|
|
} else {
|
|
|
|
|
return getKeyName(param.Get("code", 0));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
|
|
|
|
|
button_map[button]->setText(KeyToText(buttons_param[button]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
|
|
|
|
|
if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") {
|
|
|
|
|
for (QPushButton* button : analog_map[analog_id]) {
|
|
|
|
|
if (button)
|
|
|
|
|
button->setText(non_keyboard);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
|
|
|
|
|
Common::ParamPackage param(
|
|
|
|
|
analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], ""));
|
|
|
|
|
if (analog_map[analog_id][sub_button_id])
|
|
|
|
|
analog_map[analog_id][sub_button_id]->setText(KeyToText(param));
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-09 18:59:09 -05:00
|
|
|
|
}
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) {
|
2016-12-09 18:59:09 -05:00
|
|
|
|
button->setText(tr("[press key]"));
|
|
|
|
|
button->setFocus();
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
key_setter = new_key_setter;
|
2016-07-29 08:45:49 -04:00
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
grabKeyboard();
|
|
|
|
|
grabMouse();
|
|
|
|
|
timer->start(5000); // Cancel after 5 seconds
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 18:59:09 -05:00
|
|
|
|
void ConfigureInput::keyPressEvent(QKeyEvent* event) {
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
releaseMouse();
|
|
|
|
|
|
2017-01-22 14:02:29 -05:00
|
|
|
|
if (!key_setter || !event)
|
2016-12-09 18:59:09 -05:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (event->key() != Qt::Key_Escape)
|
2017-01-22 14:02:29 -05:00
|
|
|
|
(*key_setter)(event->key());
|
2016-12-09 18:59:09 -05:00
|
|
|
|
|
|
|
|
|
updateButtonLabels();
|
2017-01-22 14:02:29 -05:00
|
|
|
|
key_setter = boost::none;
|
2016-12-09 18:59:09 -05:00
|
|
|
|
timer->stop();
|
2016-07-29 08:45:49 -04:00
|
|
|
|
}
|