2016-07-29 18:15:49 +05:30
|
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <utility>
|
2016-09-20 20:51:23 +05:30
|
|
|
|
#include <QTimer>
|
2016-12-10 05:29:09 +05:30
|
|
|
|
#include "citra_qt/config.h"
|
2016-09-21 12:22:38 +05:30
|
|
|
|
#include "citra_qt/configure_input.h"
|
2016-07-29 18:15:49 +05:30
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
static QString getKeyName(Qt::Key key_code) {
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-18 06:08:01 +05:30
|
|
|
|
ConfigureInput::ConfigureInput(QWidget* parent)
|
2016-12-10 05:29:09 +05:30
|
|
|
|
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
|
|
|
|
|
timer(std::make_unique<QTimer>()) {
|
2016-09-19 06:31:46 +05:30
|
|
|
|
|
2016-07-29 18:15:49 +05:30
|
|
|
|
ui->setupUi(this);
|
2016-12-10 05:29:09 +05:30
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
2016-07-29 18:15:49 +05:30
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
button_map = {
|
2016-09-19 06:31:46 +05:30
|
|
|
|
{Settings::NativeInput::Values::A, ui->buttonA},
|
|
|
|
|
{Settings::NativeInput::Values::B, ui->buttonB},
|
|
|
|
|
{Settings::NativeInput::Values::X, ui->buttonX},
|
|
|
|
|
{Settings::NativeInput::Values::Y, ui->buttonY},
|
|
|
|
|
{Settings::NativeInput::Values::L, ui->buttonL},
|
|
|
|
|
{Settings::NativeInput::Values::R, ui->buttonR},
|
|
|
|
|
{Settings::NativeInput::Values::ZL, ui->buttonZL},
|
|
|
|
|
{Settings::NativeInput::Values::ZR, ui->buttonZR},
|
|
|
|
|
{Settings::NativeInput::Values::START, ui->buttonStart},
|
|
|
|
|
{Settings::NativeInput::Values::SELECT, ui->buttonSelect},
|
|
|
|
|
{Settings::NativeInput::Values::HOME, ui->buttonHome},
|
|
|
|
|
{Settings::NativeInput::Values::DUP, ui->buttonDpadUp},
|
|
|
|
|
{Settings::NativeInput::Values::DDOWN, ui->buttonDpadDown},
|
|
|
|
|
{Settings::NativeInput::Values::DLEFT, ui->buttonDpadLeft},
|
|
|
|
|
{Settings::NativeInput::Values::DRIGHT, ui->buttonDpadRight},
|
|
|
|
|
{Settings::NativeInput::Values::CUP, ui->buttonCStickUp},
|
|
|
|
|
{Settings::NativeInput::Values::CDOWN, ui->buttonCStickDown},
|
|
|
|
|
{Settings::NativeInput::Values::CLEFT, ui->buttonCStickLeft},
|
|
|
|
|
{Settings::NativeInput::Values::CRIGHT, ui->buttonCStickRight},
|
|
|
|
|
{Settings::NativeInput::Values::CIRCLE_UP, ui->buttonCircleUp},
|
|
|
|
|
{Settings::NativeInput::Values::CIRCLE_DOWN, ui->buttonCircleDown},
|
|
|
|
|
{Settings::NativeInput::Values::CIRCLE_LEFT, ui->buttonCircleLeft},
|
|
|
|
|
{Settings::NativeInput::Values::CIRCLE_RIGHT, ui->buttonCircleRight},
|
|
|
|
|
{Settings::NativeInput::Values::CIRCLE_MODIFIER, ui->buttonCircleMod},
|
2016-07-29 18:15:49 +05:30
|
|
|
|
};
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
for (const auto& entry : button_map) {
|
|
|
|
|
const Settings::NativeInput::Values input_id = entry.first;
|
|
|
|
|
connect(entry.second, &QPushButton::released,
|
|
|
|
|
[this, input_id]() { handleClick(input_id); });
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
2016-12-10 05:29:09 +05:30
|
|
|
|
|
|
|
|
|
connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
|
|
|
|
|
|
2016-07-29 18:15:49 +05:30
|
|
|
|
timer->setSingleShot(true);
|
2016-12-10 05:29:09 +05:30
|
|
|
|
connect(timer.get(), &QTimer::timeout, [this]() {
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
releaseMouse();
|
|
|
|
|
current_input_id = boost::none;
|
|
|
|
|
updateButtonLabels();
|
2016-09-18 06:08:01 +05:30
|
|
|
|
});
|
2016-07-29 18:15:49 +05:30
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
this->loadConfiguration();
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ConfigureInput::applyConfiguration() {
|
2016-12-10 05:29:09 +05:30
|
|
|
|
for (const auto& input_id : Settings::NativeInput::All) {
|
|
|
|
|
const size_t index = static_cast<size_t>(input_id);
|
|
|
|
|
Settings::values.input_mappings[index] = static_cast<int>(key_map[input_id]);
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
Settings::Apply();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::loadConfiguration() {
|
|
|
|
|
for (const auto& input_id : Settings::NativeInput::All) {
|
|
|
|
|
const size_t index = static_cast<size_t>(input_id);
|
|
|
|
|
key_map[input_id] = static_cast<Qt::Key>(Settings::values.input_mappings[index]);
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
2016-12-10 05:29:09 +05:30
|
|
|
|
updateButtonLabels();
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::restoreDefaults() {
|
|
|
|
|
for (const auto& input_id : Settings::NativeInput::All) {
|
|
|
|
|
const size_t index = static_cast<size_t>(input_id);
|
|
|
|
|
key_map[input_id] = static_cast<Qt::Key>(Config::defaults[index].toInt());
|
|
|
|
|
}
|
|
|
|
|
updateButtonLabels();
|
|
|
|
|
applyConfiguration();
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::updateButtonLabels() {
|
|
|
|
|
for (const auto& input_id : Settings::NativeInput::All) {
|
|
|
|
|
button_map[input_id]->setText(getKeyName(key_map[input_id]));
|
|
|
|
|
}
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::handleClick(Settings::NativeInput::Values input_id) {
|
|
|
|
|
QPushButton* button = button_map[input_id];
|
|
|
|
|
button->setText(tr("[press key]"));
|
|
|
|
|
button->setFocus();
|
2016-07-29 18:15:49 +05:30
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
current_input_id = input_id;
|
2016-07-29 18:15:49 +05:30
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
grabKeyboard();
|
|
|
|
|
grabMouse();
|
|
|
|
|
timer->start(5000); // Cancel after 5 seconds
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::keyPressEvent(QKeyEvent* event) {
|
|
|
|
|
releaseKeyboard();
|
|
|
|
|
releaseMouse();
|
|
|
|
|
|
|
|
|
|
if (!current_input_id || !event)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (event->key() != Qt::Key_Escape)
|
|
|
|
|
setInput(*current_input_id, static_cast<Qt::Key>(event->key()));
|
|
|
|
|
|
|
|
|
|
updateButtonLabels();
|
|
|
|
|
current_input_id = boost::none;
|
|
|
|
|
timer->stop();
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 05:29:09 +05:30
|
|
|
|
void ConfigureInput::setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed) {
|
|
|
|
|
// Remove duplicates
|
|
|
|
|
for (auto& pair : key_map) {
|
|
|
|
|
if (pair.second == key_pressed)
|
|
|
|
|
pair.second = Qt::Key_unknown;
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|
2016-12-10 05:29:09 +05:30
|
|
|
|
|
|
|
|
|
key_map[input_id] = key_pressed;
|
2016-07-29 18:15:49 +05:30
|
|
|
|
}
|