2018-11-01 21:07:46 -05:00
|
|
|
// Copyright 2016 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2018-11-27 04:14:59 -06:00
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QTimer>
|
2018-11-27 04:14:59 -06:00
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/param_package.h"
|
|
|
|
#include "input_common/main.h"
|
|
|
|
#include "ui_configure_mouse_advanced.h"
|
|
|
|
#include "yuzu/configuration/config.h"
|
|
|
|
#include "yuzu/configuration/configure_mouse_advanced.h"
|
|
|
|
|
2018-11-03 11:55:39 -05:00
|
|
|
static QString GetKeyName(int key_code) {
|
2018-11-01 21:07:46 -05:00
|
|
|
switch (key_code) {
|
2020-08-23 07:04:26 -05:00
|
|
|
case Qt::LeftButton:
|
|
|
|
return QObject::tr("Click 0");
|
|
|
|
case Qt::RightButton:
|
|
|
|
return QObject::tr("Click 1");
|
|
|
|
case Qt::MiddleButton:
|
|
|
|
return QObject::tr("Click 2");
|
|
|
|
case Qt::BackButton:
|
|
|
|
return QObject::tr("Click 3");
|
|
|
|
case Qt::ForwardButton:
|
|
|
|
return QObject::tr("Click 4");
|
2018-11-01 21:07:46 -05:00
|
|
|
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-05-19 11:24:13 -05:00
|
|
|
return {};
|
2018-11-01 21:07:46 -05:00
|
|
|
default:
|
|
|
|
return QKeySequence(key_code).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString ButtonToText(const Common::ParamPackage& param) {
|
|
|
|
if (!param.Has("engine")) {
|
|
|
|
return QObject::tr("[not set]");
|
2019-05-19 11:24:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "keyboard") {
|
2018-11-03 11:55:39 -05:00
|
|
|
return GetKeyName(param.Get("code", 0));
|
2019-05-19 11:24:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (param.Get("engine", "") == "sdl") {
|
2018-11-01 21:07:46 -05:00
|
|
|
if (param.Has("hat")) {
|
2019-05-19 11:24:13 -05: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);
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
2019-05-19 11:24:13 -05:00
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
if (param.Has("axis")) {
|
2019-05-19 11:24:13 -05: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);
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
2019-05-19 11:24:13 -05:00
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
if (param.Has("button")) {
|
2019-05-19 11:24:13 -05:00
|
|
|
const QString button_str = QString::fromStdString(param.Get("button", ""));
|
|
|
|
|
|
|
|
return QObject::tr("Button %1").arg(button_str);
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
2019-05-19 11:24:13 -05:00
|
|
|
return {};
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
2019-05-19 11:24:13 -05:00
|
|
|
|
|
|
|
return QObject::tr("[unknown]");
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigureMouseAdvanced::ConfigureMouseAdvanced(QWidget* parent)
|
|
|
|
: QDialog(parent), ui(std::make_unique<Ui::ConfigureMouseAdvanced>()),
|
|
|
|
timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()) {
|
|
|
|
ui->setupUi(this);
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
button_map = {
|
|
|
|
ui->left_button, ui->right_button, ui->middle_button, ui->forward_button, ui->back_button,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int button_id = 0; button_id < Settings::NativeMouseButton::NumMouseButtons; button_id++) {
|
2019-05-19 11:31:47 -05:00
|
|
|
auto* const button = button_map[button_id];
|
|
|
|
if (button == nullptr) {
|
2018-11-01 21:07:46 -05:00
|
|
|
continue;
|
2019-05-19 11:31:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
button->setContextMenuPolicy(Qt::CustomContextMenu);
|
2020-08-03 10:28:18 -05:00
|
|
|
connect(button, &QPushButton::clicked, [=, this] {
|
2020-08-11 10:08:10 -05:00
|
|
|
HandleClick(
|
|
|
|
button_map[button_id],
|
|
|
|
[=, this](const Common::ParamPackage& params) {
|
|
|
|
buttons_param[button_id] = params;
|
|
|
|
},
|
|
|
|
InputCommon::Polling::DeviceType::Button);
|
2019-05-19 11:31:47 -05:00
|
|
|
});
|
2020-08-03 10:28:18 -05:00
|
|
|
connect(button, &QPushButton::customContextMenuRequested,
|
|
|
|
[=, this](const QPoint& menu_location) {
|
|
|
|
QMenu context_menu;
|
|
|
|
context_menu.addAction(tr("Clear"), [&] {
|
|
|
|
buttons_param[button_id].Clear();
|
|
|
|
button_map[button_id]->setText(tr("[not set]"));
|
|
|
|
});
|
|
|
|
context_menu.addAction(tr("Restore Default"), [&] {
|
|
|
|
buttons_param[button_id] =
|
|
|
|
Common::ParamPackage{InputCommon::GenerateKeyboardParam(
|
|
|
|
Config::default_mouse_buttons[button_id])};
|
|
|
|
button_map[button_id]->setText(ButtonToText(buttons_param[button_id]));
|
|
|
|
});
|
|
|
|
context_menu.exec(button_map[button_id]->mapToGlobal(menu_location));
|
|
|
|
});
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2019-07-22 16:28:10 -05:00
|
|
|
connect(ui->buttonClearAll, &QPushButton::clicked, [this] { ClearAll(); });
|
|
|
|
connect(ui->buttonRestoreDefaults, &QPushButton::clicked, [this] { RestoreDefaults(); });
|
2018-11-01 21:07:46 -05:00
|
|
|
|
|
|
|
timeout_timer->setSingleShot(true);
|
2019-05-25 23:39:23 -05:00
|
|
|
connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
|
2018-11-01 21:07:46 -05:00
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
connect(poll_timer.get(), &QTimer::timeout, [this] {
|
2018-11-01 21:07:46 -05:00
|
|
|
Common::ParamPackage params;
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
params = poller->GetNextInput();
|
|
|
|
if (params.Has("engine")) {
|
2019-05-25 23:39:23 -05:00
|
|
|
SetPollingResult(params, false);
|
2018-11-01 21:07:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
LoadConfiguration();
|
2018-11-01 21:07:46 -05:00
|
|
|
resize(0, 0);
|
|
|
|
}
|
|
|
|
|
2018-11-04 09:18:59 -06:00
|
|
|
ConfigureMouseAdvanced::~ConfigureMouseAdvanced() = default;
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::ApplyConfiguration() {
|
2018-11-01 21:07:46 -05:00
|
|
|
std::transform(buttons_param.begin(), buttons_param.end(),
|
|
|
|
Settings::values.mouse_buttons.begin(),
|
|
|
|
[](const Common::ParamPackage& param) { return param.Serialize(); });
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::LoadConfiguration() {
|
2018-11-01 21:07:46 -05:00
|
|
|
std::transform(Settings::values.mouse_buttons.begin(), Settings::values.mouse_buttons.end(),
|
|
|
|
buttons_param.begin(),
|
|
|
|
[](const std::string& str) { return Common::ParamPackage(str); });
|
2019-05-25 23:39:23 -05:00
|
|
|
UpdateButtonLabels();
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2019-06-05 17:39:46 -05:00
|
|
|
void ConfigureMouseAdvanced::changeEvent(QEvent* event) {
|
|
|
|
if (event->type() == QEvent::LanguageChange) {
|
|
|
|
RetranslateUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMouseAdvanced::RetranslateUI() {
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::RestoreDefaults() {
|
2018-11-01 21:07:46 -05:00
|
|
|
for (int button_id = 0; button_id < Settings::NativeMouseButton::NumMouseButtons; button_id++) {
|
|
|
|
buttons_param[button_id] = Common::ParamPackage{
|
|
|
|
InputCommon::GenerateKeyboardParam(Config::default_mouse_buttons[button_id])};
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
UpdateButtonLabels();
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureMouseAdvanced::ClearAll() {
|
|
|
|
for (int i = 0; i < Settings::NativeMouseButton::NumMouseButtons; ++i) {
|
2019-05-19 11:31:47 -05:00
|
|
|
const auto* const button = button_map[i];
|
|
|
|
if (button != nullptr && button->isEnabled()) {
|
2018-11-01 21:07:46 -05:00
|
|
|
buttons_param[i].Clear();
|
2019-05-19 11:31:47 -05:00
|
|
|
}
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
UpdateButtonLabels();
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::UpdateButtonLabels() {
|
2018-11-01 21:07:46 -05:00
|
|
|
for (int button = 0; button < Settings::NativeMouseButton::NumMouseButtons; button++) {
|
|
|
|
button_map[button]->setText(ButtonToText(buttons_param[button]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::HandleClick(
|
2018-11-01 21:07:46 -05:00
|
|
|
QPushButton* button, std::function<void(const Common::ParamPackage&)> new_input_setter,
|
|
|
|
InputCommon::Polling::DeviceType type) {
|
|
|
|
button->setText(tr("[press key]"));
|
|
|
|
button->setFocus();
|
|
|
|
|
2020-08-23 07:04:26 -05:00
|
|
|
// Keyboard keys or mouse buttons can only be used as button devices
|
|
|
|
want_keyboard_mouse = type == InputCommon::Polling::DeviceType::Button;
|
|
|
|
if (want_keyboard_mouse) {
|
2020-04-05 00:16:09 -05:00
|
|
|
const auto iter = std::find(button_map.begin(), button_map.end(), button);
|
|
|
|
ASSERT(iter != button_map.end());
|
|
|
|
const auto index = std::distance(button_map.begin(), iter);
|
|
|
|
ASSERT(index < Settings::NativeButton::NumButtons && index >= 0);
|
|
|
|
}
|
2018-11-01 21:07:46 -05:00
|
|
|
|
|
|
|
input_setter = new_input_setter;
|
|
|
|
|
|
|
|
device_pollers = InputCommon::Polling::GetPollers(type);
|
|
|
|
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
poller->Start();
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:04:26 -05:00
|
|
|
QWidget::grabMouse();
|
|
|
|
QWidget::grabKeyboard();
|
|
|
|
|
2020-07-22 09:39:53 -05:00
|
|
|
timeout_timer->start(2500); // Cancel after 2.5 seconds
|
|
|
|
poll_timer->start(50); // Check for new inputs every 50ms
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
void ConfigureMouseAdvanced::SetPollingResult(const Common::ParamPackage& params, bool abort) {
|
2018-11-01 21:07:46 -05:00
|
|
|
timeout_timer->stop();
|
|
|
|
poll_timer->stop();
|
|
|
|
for (auto& poller : device_pollers) {
|
|
|
|
poller->Stop();
|
|
|
|
}
|
|
|
|
|
2020-08-23 07:04:26 -05:00
|
|
|
QWidget::releaseMouse();
|
|
|
|
QWidget::releaseKeyboard();
|
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
if (!abort) {
|
|
|
|
(*input_setter)(params);
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:39:23 -05:00
|
|
|
UpdateButtonLabels();
|
2018-11-03 11:55:39 -05:00
|
|
|
input_setter = std::nullopt;
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|
|
|
|
|
2020-08-23 07:04:26 -05:00
|
|
|
void ConfigureMouseAdvanced::mousePressEvent(QMouseEvent* event) {
|
|
|
|
if (!input_setter || !event) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want_keyboard_mouse) {
|
|
|
|
SetPollingResult(Common::ParamPackage{InputCommon::GenerateKeyboardParam(event->button())},
|
|
|
|
false);
|
|
|
|
} else {
|
|
|
|
// We don't want any mouse buttons, so don't stop polling
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetPollingResult({}, true);
|
|
|
|
}
|
|
|
|
|
2018-11-01 21:07:46 -05:00
|
|
|
void ConfigureMouseAdvanced::keyPressEvent(QKeyEvent* event) {
|
2019-05-25 23:39:23 -05:00
|
|
|
if (!input_setter || !event) {
|
2018-11-01 21:07:46 -05:00
|
|
|
return;
|
2019-05-25 23:39:23 -05:00
|
|
|
}
|
2018-11-01 21:07:46 -05:00
|
|
|
|
|
|
|
if (event->key() != Qt::Key_Escape) {
|
2020-08-23 07:04:26 -05:00
|
|
|
if (want_keyboard_mouse) {
|
2019-05-25 23:39:23 -05:00
|
|
|
SetPollingResult(Common::ParamPackage{InputCommon::GenerateKeyboardParam(event->key())},
|
2018-11-01 21:07:46 -05:00
|
|
|
false);
|
|
|
|
} else {
|
|
|
|
// Escape key wasn't pressed and we don't want any keyboard keys, so don't stop polling
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-05-25 23:39:23 -05:00
|
|
|
SetPollingResult({}, true);
|
2018-11-01 21:07:46 -05:00
|
|
|
}
|