2019-02-16 15:19:29 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
#include <QMenu>
|
2019-02-16 15:19:29 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QStandardItemModel>
|
2021-11-15 23:57:41 +00:00
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include "core/hid/emulated_controller.h"
|
|
|
|
#include "core/hid/hid_core.h"
|
|
|
|
|
2019-02-16 15:19:29 +00:00
|
|
|
#include "ui_configure_hotkeys.h"
|
2020-04-23 12:31:16 +01:00
|
|
|
#include "yuzu/configuration/config.h"
|
2019-02-16 15:19:29 +00:00
|
|
|
#include "yuzu/configuration/configure_hotkeys.h"
|
|
|
|
#include "yuzu/hotkeys.h"
|
|
|
|
#include "yuzu/util/sequence_dialog/sequence_dialog.h"
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
constexpr int name_column = 0;
|
|
|
|
constexpr int hotkey_column = 1;
|
|
|
|
constexpr int controller_column = 2;
|
|
|
|
|
|
|
|
ConfigureHotkeys::ConfigureHotkeys(Core::HID::HIDCore& hid_core, QWidget* parent)
|
|
|
|
: QWidget(parent), ui(std::make_unique<Ui::ConfigureHotkeys>()),
|
|
|
|
timeout_timer(std::make_unique<QTimer>()), poll_timer(std::make_unique<QTimer>()) {
|
2019-02-16 15:19:29 +00:00
|
|
|
ui->setupUi(this);
|
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
model = new QStandardItemModel(this);
|
|
|
|
model->setColumnCount(3);
|
|
|
|
|
|
|
|
connect(ui->hotkey_list, &QTreeView::doubleClicked, this, &ConfigureHotkeys::Configure);
|
2020-04-23 12:31:16 +01:00
|
|
|
connect(ui->hotkey_list, &QTreeView::customContextMenuRequested, this,
|
|
|
|
&ConfigureHotkeys::PopupContextMenu);
|
|
|
|
ui->hotkey_list->setContextMenuPolicy(Qt::CustomContextMenu);
|
2019-02-16 15:19:29 +00:00
|
|
|
ui->hotkey_list->setModel(model);
|
|
|
|
|
2022-04-02 13:52:03 +01:00
|
|
|
ui->hotkey_list->header()->setStretchLastSection(false);
|
|
|
|
ui->hotkey_list->header()->setSectionResizeMode(name_column, QHeaderView::ResizeMode::Stretch);
|
|
|
|
ui->hotkey_list->header()->setMinimumSectionSize(150);
|
2019-06-05 23:39:46 +01:00
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
connect(ui->button_restore_defaults, &QPushButton::clicked, this,
|
|
|
|
&ConfigureHotkeys::RestoreDefaults);
|
|
|
|
connect(ui->button_clear_all, &QPushButton::clicked, this, &ConfigureHotkeys::ClearAll);
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Player1);
|
|
|
|
|
|
|
|
connect(timeout_timer.get(), &QTimer::timeout, [this] { SetPollingResult({}, true); });
|
|
|
|
|
|
|
|
connect(poll_timer.get(), &QTimer::timeout, [this] {
|
|
|
|
const auto buttons = controller->GetNpadButtons();
|
|
|
|
if (buttons.raw != Core::HID::NpadButton::None) {
|
|
|
|
SetPollingResult(buttons.raw, false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
2019-06-05 23:39:46 +01:00
|
|
|
RetranslateUI();
|
2019-02-16 15:19:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ConfigureHotkeys::~ConfigureHotkeys() = default;
|
|
|
|
|
|
|
|
void ConfigureHotkeys::Populate(const HotkeyRegistry& registry) {
|
|
|
|
for (const auto& group : registry.hotkey_groups) {
|
|
|
|
auto* parent_item = new QStandardItem(group.first);
|
|
|
|
parent_item->setEditable(false);
|
|
|
|
for (const auto& hotkey : group.second) {
|
|
|
|
auto* action = new QStandardItem(hotkey.first);
|
|
|
|
auto* keyseq =
|
|
|
|
new QStandardItem(hotkey.second.keyseq.toString(QKeySequence::NativeText));
|
2021-11-15 23:57:41 +00:00
|
|
|
auto* controller_keyseq = new QStandardItem(hotkey.second.controller_keyseq);
|
2019-02-16 15:19:29 +00:00
|
|
|
action->setEditable(false);
|
|
|
|
keyseq->setEditable(false);
|
2021-11-15 23:57:41 +00:00
|
|
|
controller_keyseq->setEditable(false);
|
|
|
|
parent_item->appendRow({action, keyseq, controller_keyseq});
|
2019-02-16 15:19:29 +00:00
|
|
|
}
|
|
|
|
model->appendRow(parent_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->hotkey_list->expandAll();
|
2021-11-15 23:57:41 +00:00
|
|
|
ui->hotkey_list->resizeColumnToContents(hotkey_column);
|
2022-04-02 13:52:03 +01:00
|
|
|
ui->hotkey_list->resizeColumnToContents(controller_column);
|
2019-02-16 15:19:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 23:39:46 +01:00
|
|
|
void ConfigureHotkeys::changeEvent(QEvent* event) {
|
|
|
|
if (event->type() == QEvent::LanguageChange) {
|
|
|
|
RetranslateUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget::changeEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::RetranslateUI() {
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
model->setHorizontalHeaderLabels({tr("Action"), tr("Hotkey"), tr("Controller Hotkey")});
|
2019-06-05 23:39:46 +01:00
|
|
|
}
|
|
|
|
|
2019-02-16 15:19:29 +00:00
|
|
|
void ConfigureHotkeys::Configure(QModelIndex index) {
|
2019-04-10 00:47:18 +01:00
|
|
|
if (!index.parent().isValid()) {
|
2019-02-16 15:19:29 +00:00
|
|
|
return;
|
2019-04-10 00:47:18 +01:00
|
|
|
}
|
2019-02-16 15:19:29 +00:00
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
// Controller configuration is selected
|
|
|
|
if (index.column() == controller_column) {
|
|
|
|
ConfigureController(index);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap to the hotkey column
|
|
|
|
index = index.sibling(index.row(), hotkey_column);
|
|
|
|
|
2019-04-10 00:50:14 +01:00
|
|
|
const auto previous_key = model->data(index);
|
2019-02-16 15:19:29 +00:00
|
|
|
|
2019-04-10 01:06:45 +01:00
|
|
|
SequenceDialog hotkey_dialog{this};
|
2019-02-16 15:19:29 +00:00
|
|
|
|
2019-04-10 00:50:59 +01:00
|
|
|
const int return_code = hotkey_dialog.exec();
|
|
|
|
const auto key_sequence = hotkey_dialog.GetSequence();
|
2019-04-10 00:50:14 +01:00
|
|
|
if (return_code == QDialog::Rejected || key_sequence.isEmpty()) {
|
2019-02-16 15:19:29 +00:00
|
|
|
return;
|
2019-04-10 00:50:14 +01:00
|
|
|
}
|
2020-04-23 12:31:16 +01:00
|
|
|
const auto [key_sequence_used, used_action] = IsUsedKey(key_sequence);
|
2019-02-16 15:19:29 +00:00
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
if (key_sequence_used && key_sequence != QKeySequence(previous_key.toString())) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Key Sequence"),
|
|
|
|
tr("The entered key sequence is already assigned to: %1").arg(used_action));
|
2019-02-16 15:19:29 +00:00
|
|
|
} else {
|
|
|
|
model->setData(index, key_sequence.toString(QKeySequence::NativeText));
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 23:57:41 +00:00
|
|
|
void ConfigureHotkeys::ConfigureController(QModelIndex index) {
|
|
|
|
if (timeout_timer->isActive()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto previous_key = model->data(index);
|
|
|
|
|
|
|
|
input_setter = [this, index, previous_key](const Core::HID::NpadButton button,
|
|
|
|
const bool cancel) {
|
|
|
|
if (cancel) {
|
|
|
|
model->setData(index, previous_key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QString button_string = tr("Home+%1").arg(GetButtonName(button));
|
|
|
|
|
|
|
|
const auto [key_sequence_used, used_action] = IsUsedControllerKey(button_string);
|
|
|
|
|
|
|
|
if (key_sequence_used) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Key Sequence"),
|
|
|
|
tr("The entered key sequence is already assigned to: %1").arg(used_action));
|
|
|
|
model->setData(index, previous_key);
|
|
|
|
} else {
|
|
|
|
model->setData(index, button_string);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
model->setData(index, tr("[waiting]"));
|
|
|
|
timeout_timer->start(2500); // Cancel after 2.5 seconds
|
|
|
|
poll_timer->start(200); // Check for new inputs every 200ms
|
|
|
|
// We need to disable configuration to be able to read npad buttons
|
|
|
|
controller->DisableConfiguration();
|
|
|
|
controller->DisableSystemButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::SetPollingResult(Core::HID::NpadButton button, const bool cancel) {
|
|
|
|
timeout_timer->stop();
|
|
|
|
poll_timer->stop();
|
|
|
|
// Re-Enable configuration
|
|
|
|
controller->EnableConfiguration();
|
|
|
|
controller->EnableSystemButtons();
|
|
|
|
|
|
|
|
(*input_setter)(button, cancel);
|
|
|
|
|
|
|
|
input_setter = std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ConfigureHotkeys::GetButtonName(Core::HID::NpadButton button) const {
|
|
|
|
Core::HID::NpadButtonState state{button};
|
|
|
|
if (state.a) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("A");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.b) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("B");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.x) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("X");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.y) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Y");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.l || state.right_sl || state.left_sl) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("L");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.r || state.right_sr || state.left_sr) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("R");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.zl) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("ZL");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.zr) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("ZR");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.left) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Dpad_Left");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.right) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Dpad_Right");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.up) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Dpad_Up");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.down) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Dpad_Down");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.stick_l) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Left_Stick");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.stick_r) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Right_Stick");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.minus) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Minus");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
if (state.plus) {
|
2022-01-27 23:32:26 +00:00
|
|
|
return QStringLiteral("Plus");
|
2021-11-15 23:57:41 +00:00
|
|
|
}
|
|
|
|
return tr("Invalid");
|
|
|
|
}
|
2019-02-16 15:19:29 +00:00
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
std::pair<bool, QString> ConfigureHotkeys::IsUsedKey(QKeySequence key_sequence) const {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
2019-05-25 09:03:15 +01:00
|
|
|
const QStandardItem* const parent = model->item(r, 0);
|
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
2021-11-15 23:57:41 +00:00
|
|
|
const QStandardItem* const key_seq_item = parent->child(r2, hotkey_column);
|
2019-05-25 09:03:15 +01:00
|
|
|
const auto key_seq_str = key_seq_item->text();
|
|
|
|
const auto key_seq = QKeySequence::fromString(key_seq_str, QKeySequence::NativeText);
|
|
|
|
|
|
|
|
if (key_sequence == key_seq) {
|
2020-04-23 12:31:16 +01:00
|
|
|
return std::make_pair(true, parent->child(r2, 0)->text());
|
2019-05-25 09:03:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
return std::make_pair(false, QString());
|
2019-02-16 15:19:29 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
std::pair<bool, QString> ConfigureHotkeys::IsUsedControllerKey(const QString& key_sequence) const {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* const parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
|
|
|
const QStandardItem* const key_seq_item = parent->child(r2, controller_column);
|
|
|
|
const auto key_seq_str = key_seq_item->text();
|
|
|
|
|
|
|
|
if (key_sequence == key_seq_str) {
|
|
|
|
return std::make_pair(true, parent->child(r2, 0)->text());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::make_pair(false, QString());
|
|
|
|
}
|
|
|
|
|
2019-05-26 05:39:23 +01:00
|
|
|
void ConfigureHotkeys::ApplyConfiguration(HotkeyRegistry& registry) {
|
2019-02-16 15:19:29 +00:00
|
|
|
for (int key_id = 0; key_id < model->rowCount(); key_id++) {
|
|
|
|
const QStandardItem* parent = model->item(key_id, 0);
|
|
|
|
for (int key_column_id = 0; key_column_id < parent->rowCount(); key_column_id++) {
|
2021-11-15 23:57:41 +00:00
|
|
|
const QStandardItem* action = parent->child(key_column_id, name_column);
|
|
|
|
const QStandardItem* keyseq = parent->child(key_column_id, hotkey_column);
|
|
|
|
const QStandardItem* controller_keyseq =
|
|
|
|
parent->child(key_column_id, controller_column);
|
2019-02-16 15:19:29 +00:00
|
|
|
for (auto& [group, sub_actions] : registry.hotkey_groups) {
|
|
|
|
if (group != parent->text())
|
|
|
|
continue;
|
|
|
|
for (auto& [action_name, hotkey] : sub_actions) {
|
|
|
|
if (action_name != action->text())
|
|
|
|
continue;
|
|
|
|
hotkey.keyseq = QKeySequence(keyseq->text());
|
2021-11-15 23:57:41 +00:00
|
|
|
hotkey.controller_keyseq = controller_keyseq->text();
|
2019-02-16 15:19:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
registry.SaveHotkeys();
|
|
|
|
}
|
2020-04-23 12:31:16 +01:00
|
|
|
|
|
|
|
void ConfigureHotkeys::RestoreDefaults() {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
2021-11-15 23:57:41 +00:00
|
|
|
model->item(r, 0)
|
|
|
|
->child(r2, hotkey_column)
|
|
|
|
->setText(Config::default_hotkeys[r2].shortcut.keyseq);
|
|
|
|
model->item(r, 0)
|
|
|
|
->child(r2, controller_column)
|
|
|
|
->setText(Config::default_hotkeys[r2].shortcut.controller_keyseq);
|
2020-04-23 12:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::ClearAll() {
|
|
|
|
for (int r = 0; r < model->rowCount(); ++r) {
|
|
|
|
const QStandardItem* parent = model->item(r, 0);
|
|
|
|
|
|
|
|
for (int r2 = 0; r2 < parent->rowCount(); ++r2) {
|
2021-11-15 23:57:41 +00:00
|
|
|
model->item(r, 0)->child(r2, hotkey_column)->setText(QString{});
|
|
|
|
model->item(r, 0)->child(r2, controller_column)->setText(QString{});
|
2020-04-23 12:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::PopupContextMenu(const QPoint& menu_location) {
|
|
|
|
QModelIndex index = ui->hotkey_list->indexAt(menu_location);
|
|
|
|
if (!index.parent().isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
// Swap to the hotkey column if the controller hotkey column is not selected
|
|
|
|
if (index.column() != controller_column) {
|
|
|
|
index = index.sibling(index.row(), hotkey_column);
|
|
|
|
}
|
|
|
|
|
2020-04-23 12:31:16 +01:00
|
|
|
QMenu context_menu;
|
|
|
|
|
|
|
|
QAction* restore_default = context_menu.addAction(tr("Restore Default"));
|
|
|
|
QAction* clear = context_menu.addAction(tr("Clear"));
|
|
|
|
|
2021-11-15 23:57:41 +00:00
|
|
|
connect(restore_default, &QAction::triggered, [this, index] {
|
|
|
|
if (index.column() == controller_column) {
|
|
|
|
RestoreControllerHotkey(index);
|
|
|
|
return;
|
2020-04-23 12:31:16 +01:00
|
|
|
}
|
2021-11-15 23:57:41 +00:00
|
|
|
RestoreHotkey(index);
|
2020-04-23 12:31:16 +01:00
|
|
|
});
|
2021-11-15 23:57:41 +00:00
|
|
|
connect(clear, &QAction::triggered, [this, index] { model->setData(index, QString{}); });
|
2020-04-23 12:31:16 +01:00
|
|
|
|
|
|
|
context_menu.exec(ui->hotkey_list->viewport()->mapToGlobal(menu_location));
|
|
|
|
}
|
2021-11-15 23:57:41 +00:00
|
|
|
|
|
|
|
void ConfigureHotkeys::RestoreControllerHotkey(QModelIndex index) {
|
|
|
|
const QString& default_key_sequence =
|
|
|
|
Config::default_hotkeys[index.row()].shortcut.controller_keyseq;
|
|
|
|
const auto [key_sequence_used, used_action] = IsUsedControllerKey(default_key_sequence);
|
|
|
|
|
|
|
|
if (key_sequence_used && default_key_sequence != model->data(index).toString()) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Button Sequence"),
|
|
|
|
tr("The default button sequence is already assigned to: %1").arg(used_action));
|
|
|
|
} else {
|
|
|
|
model->setData(index, default_key_sequence);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigureHotkeys::RestoreHotkey(QModelIndex index) {
|
|
|
|
const QKeySequence& default_key_sequence = QKeySequence::fromString(
|
|
|
|
Config::default_hotkeys[index.row()].shortcut.keyseq, QKeySequence::NativeText);
|
|
|
|
const auto [key_sequence_used, used_action] = IsUsedKey(default_key_sequence);
|
|
|
|
|
|
|
|
if (key_sequence_used && default_key_sequence != QKeySequence(model->data(index).toString())) {
|
|
|
|
QMessageBox::warning(
|
|
|
|
this, tr("Conflicting Key Sequence"),
|
|
|
|
tr("The default key sequence is already assigned to: %1").arg(used_action));
|
|
|
|
} else {
|
|
|
|
model->setData(index, default_key_sequence.toString(QKeySequence::NativeText));
|
|
|
|
}
|
|
|
|
}
|