Qt: Allow rebinding keyboard controls (#779)

* Initial input UI draft

Co-Authored-By: Paris Oplopoios <parisoplop@gmail.com>

* More keybinding work

Co-Authored-By: Paris Oplopoios <parisoplop@gmail.com>

* Nit

Co-Authored-By: Paris Oplopoios <parisoplop@gmail.com>

* More nits

Co-Authored-By: Paris Oplopoios <parisoplop@gmail.com>

---------

Co-authored-by: Paris Oplopoios <parisoplop@gmail.com>
This commit is contained in:
wheremyfoodat
2025-07-18 04:08:08 +03:00
committed by GitHub
parent 3cae1bd256
commit 81f37e1699
14 changed files with 385 additions and 14 deletions

View File

@@ -14,7 +14,7 @@
#include "services/dsp.hpp"
#include "version.hpp"
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings::defaultKeyboardMappings()) {
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), keyboardMappings(InputMappings()) {
emu = new Emulator();
loadTranslation();
@@ -115,6 +115,13 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
[&]() { return this; }, emu->getConfig(), this
);
loadKeybindings();
connect(configWindow->getInputWindow(), &InputWindow::mappingsChanged, this, [&]() {
keybindingsChanged = true;
configWindow->getInputWindow()->applyToMappings(keyboardMappings);
});
auto args = QCoreApplication::arguments();
if (args.size() > 1) {
auto romPath = std::filesystem::current_path() / args.at(1).toStdU16String();
@@ -274,6 +281,10 @@ void MainWindow::closeEvent(QCloseEvent* event) {
// Cleanup when the main window closes
MainWindow::~MainWindow() {
if (keybindingsChanged) {
saveKeybindings();
}
delete emu;
delete menuBar;
delete aboutWindow;
@@ -766,3 +777,23 @@ void MainWindow::setupControllerSensors(SDL_GameController* controller) {
SDL_GameControllerSetSensorEnabled(controller, SDL_SENSOR_ACCEL, SDL_TRUE);
}
}
void MainWindow::loadKeybindings() {
auto mappings = InputMappings::deserialize(emu->getAppDataRoot() / "controls_qt.toml", "Qt", [](const std::string& name) {
return InputMappings::Scancode(QKeySequence(QString::fromStdString(name))[0].key());
});
if (mappings.has_value()) {
keyboardMappings = *mappings;
} else {
keyboardMappings = InputMappings::defaultKeyboardMappings();
}
configWindow->getInputWindow()->loadFromMappings(keyboardMappings);
}
void MainWindow::saveKeybindings() {
keyboardMappings.serialize(emu->getAppDataRoot() / "controls_qt.toml", "Qt", [](InputMappings::Scancode scancode) {
return QKeySequence(scancode).toString().toStdString();
});
}