From b8a727da081744a47c5deea284067327e8bc1dcb Mon Sep 17 00:00:00 2001 From: Chin Date: Sun, 1 Feb 2015 21:56:20 -0500 Subject: [PATCH] Settings: Handle key configuration changes. Implement reset and ok buttons logics. --- src/citra_qt/KeyPressEater.cpp | 59 ++++++++++++++ src/citra_qt/KeyPressEater.h | 4 +- src/citra_qt/inputs.cpp | 139 +++++++++++++++++++++++++++------ src/citra_qt/inputs.h | 30 ++++++- 4 files changed, 205 insertions(+), 27 deletions(-) diff --git a/src/citra_qt/KeyPressEater.cpp b/src/citra_qt/KeyPressEater.cpp index 6510f22d5..a8242b0e7 100644 --- a/src/citra_qt/KeyPressEater.cpp +++ b/src/citra_qt/KeyPressEater.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -10,6 +11,64 @@ bool KeyPressEater::eventFilter(QObject *obj, QEvent *event) { auto keyName = QKeySequence(keyEvent->key()).toString(); QLineEdit* lineEdit = static_cast(obj); + Button button_modified = GInputsDialog::edit_input_map[lineEdit->objectName()]; + + switch (button_modified) { + case Button::A: + GInputsDialog::temp_settings.pad_a_key = keyEvent->key(); + break; + case Button::B: + GInputsDialog::temp_settings.pad_b_key = keyEvent->key(); + break; + case Button::X: + GInputsDialog::temp_settings.pad_x_key = keyEvent->key(); + break; + case Button::Y: + GInputsDialog::temp_settings.pad_y_key = keyEvent->key(); + break; + case Button::L: + GInputsDialog::temp_settings.pad_l_key = keyEvent->key(); + break; + case Button::R: + GInputsDialog::temp_settings.pad_r_key = keyEvent->key(); + break; + case Button::Start: + GInputsDialog::temp_settings.pad_start_key = keyEvent->key(); + break; + case Button::Select: + GInputsDialog::temp_settings.pad_select_key = keyEvent->key(); + break; + case Button::Home: + GInputsDialog::temp_settings.pad_home_key = keyEvent->key(); + break; + case Button::DUp: + GInputsDialog::temp_settings.pad_dup_key = keyEvent->key(); + break; + case Button::DDown: + GInputsDialog::temp_settings.pad_ddown_key = keyEvent->key(); + break; + case Button::DLeft: + GInputsDialog::temp_settings.pad_dleft_key = keyEvent->key(); + break; + case Button::DRight: + GInputsDialog::temp_settings.pad_dright_key = keyEvent->key(); + break; + case Button::SUp: + GInputsDialog::temp_settings.pad_sup_key = keyEvent->key(); + break; + case Button::SDown: + GInputsDialog::temp_settings.pad_sdown_key = keyEvent->key(); + break; + case Button::SLeft: + GInputsDialog::temp_settings.pad_sleft_key = keyEvent->key(); + break; + case Button::SRight: + GInputsDialog::temp_settings.pad_sright_key = keyEvent->key(); + break; + default: + break; + } + lineEdit->setText(keyName); return true; } diff --git a/src/citra_qt/KeyPressEater.h b/src/citra_qt/KeyPressEater.h index 9c5021574..493bc7376 100644 --- a/src/citra_qt/KeyPressEater.h +++ b/src/citra_qt/KeyPressEater.h @@ -1,8 +1,8 @@ #include #include -class KeyPressEater : public QObject -{ +/// The event filter used for LineEdits to handle key presses +class KeyPressEater : public QObject { Q_OBJECT public: KeyPressEater(QObject* parent); diff --git a/src/citra_qt/inputs.cpp b/src/citra_qt/inputs.cpp index 32b848bdb..621ecab14 100644 --- a/src/citra_qt/inputs.cpp +++ b/src/citra_qt/inputs.cpp @@ -4,36 +4,58 @@ #include #include -#include "core/settings.h" +#include #include "inputs.h" #include "KeyPressEater.h" -#include +#include "config.h" +#include "common/log.h" -GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) -{ +std::map GInputsDialog::edit_input_map; +bool GInputsDialog::edit_input_map_initialized = false; +Settings::Values GInputsDialog::temp_settings; + +GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) { ui.setupUi(this); - KeyPressEater *keyPressEater = new KeyPressEater(this); + + // initialize the edit_input_map + if (!edit_input_map_initialized) { + GInputsDialog::edit_input_map = std::map { + { this->ui.lineEdit_A->objectName(), Button::A}, + { this->ui.lineEdit_B->objectName(), Button::B }, + { this->ui.lineEdit_X->objectName(), Button::X }, + { this->ui.lineEdit_Y->objectName(), Button::Y }, + { this->ui.lineEdit_L->objectName(), Button::L }, + { this->ui.lineEdit_R->objectName(), Button::R }, + { this->ui.lineEdit_Start->objectName(), Button::Start }, + { this->ui.lineEdit_Select->objectName(), Button::Select }, + { this->ui.lineEdit_Home->objectName(), Button::Home }, + { this->ui.lineEdit_dUp->objectName(), Button::DUp }, + { this->ui.lineEdit_dDown->objectName(), Button::DDown }, + { this->ui.lineEdit_dLeft->objectName(), Button::DLeft }, + { this->ui.lineEdit_dRight->objectName(), Button::DRight }, + { this->ui.lineEdit_sUp->objectName(), Button::SUp }, + { this->ui.lineEdit_sDown->objectName(), Button::SDown }, + { this->ui.lineEdit_sLeft->objectName(), Button::SLeft }, + { this->ui.lineEdit_sRight->objectName(), Button::SRight }, + }; + edit_input_map_initialized = true; + } + + // set up event handlers for the buttons + QPushButton *resetButton = this->ui.buttonBox->button(QDialogButtonBox::Reset); + connect(resetButton, SIGNAL(clicked()), this, SLOT(OnResetClicked())); + + QPushButton *okButton = this->ui.buttonBox->button(QDialogButtonBox::Ok); + connect(okButton, SIGNAL(clicked()), this, SLOT(OnOkClicked())); + + // create a copy of the current settings + GInputsDialog::temp_settings = Settings::Values(Settings::values); // display current key settings - this->ui.lineEdit_A->setText(QKeySequence(Settings::values.pad_a_key).toString()); - this->ui.lineEdit_B->setText(QKeySequence(Settings::values.pad_b_key).toString()); - this->ui.lineEdit_X->setText(QKeySequence(Settings::values.pad_x_key).toString()); - this->ui.lineEdit_Y->setText(QKeySequence(Settings::values.pad_y_key).toString()); - this->ui.lineEdit_L->setText(QKeySequence(Settings::values.pad_l_key).toString()); - this->ui.lineEdit_R->setText(QKeySequence(Settings::values.pad_r_key).toString()); - this->ui.lineEdit_Start->setText(QKeySequence(Settings::values.pad_start_key).toString()); - this->ui.lineEdit_Select->setText(QKeySequence(Settings::values.pad_select_key).toString()); - this->ui.lineEdit_dUp->setText(QKeySequence(Settings::values.pad_dup_key).toString()); - this->ui.lineEdit_dDown->setText(QKeySequence(Settings::values.pad_ddown_key).toString()); - this->ui.lineEdit_dLeft->setText(QKeySequence(Settings::values.pad_dleft_key).toString()); - this->ui.lineEdit_dRight->setText(QKeySequence(Settings::values.pad_dright_key).toString()); - this->ui.lineEdit_sUp->setText(QKeySequence(Settings::values.pad_sup_key).toString()); - this->ui.lineEdit_sDown->setText(QKeySequence(Settings::values.pad_sdown_key).toString()); - this->ui.lineEdit_sLeft->setText(QKeySequence(Settings::values.pad_sleft_key).toString()); - this->ui.lineEdit_sRight->setText(QKeySequence(Settings::values.pad_sright_key).toString()); - this->ui.lineEdit_Home->setText(QKeySequence(Settings::values.pad_home_key).toString()); + this->displayButtonSettings(Settings::values); // setup event filters for the LineEdits + KeyPressEater* keyPressEater = new KeyPressEater(this); this->ui.lineEdit_A->installEventFilter(keyPressEater); this->ui.lineEdit_B->installEventFilter(keyPressEater); this->ui.lineEdit_X->installEventFilter(keyPressEater); @@ -42,6 +64,7 @@ GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) this->ui.lineEdit_R->installEventFilter(keyPressEater); this->ui.lineEdit_Start->installEventFilter(keyPressEater); this->ui.lineEdit_Select->installEventFilter(keyPressEater); + this->ui.lineEdit_Home->installEventFilter(keyPressEater); this->ui.lineEdit_dUp->installEventFilter(keyPressEater); this->ui.lineEdit_dDown->installEventFilter(keyPressEater); this->ui.lineEdit_dLeft->installEventFilter(keyPressEater); @@ -50,5 +73,75 @@ GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) this->ui.lineEdit_sDown->installEventFilter(keyPressEater); this->ui.lineEdit_sLeft->installEventFilter(keyPressEater); this->ui.lineEdit_sRight->installEventFilter(keyPressEater); - this->ui.lineEdit_Home->installEventFilter(keyPressEater); +} + +void GInputsDialog::displayButtonSettings(Settings::Values values) { + this->ui.lineEdit_A->setText(QKeySequence(values.pad_a_key).toString()); + this->ui.lineEdit_B->setText(QKeySequence(values.pad_b_key).toString()); + this->ui.lineEdit_X->setText(QKeySequence(values.pad_x_key).toString()); + this->ui.lineEdit_Y->setText(QKeySequence(values.pad_y_key).toString()); + this->ui.lineEdit_L->setText(QKeySequence(values.pad_l_key).toString()); + this->ui.lineEdit_R->setText(QKeySequence(values.pad_r_key).toString()); + this->ui.lineEdit_Start->setText(QKeySequence(values.pad_start_key).toString()); + this->ui.lineEdit_Select->setText(QKeySequence(values.pad_select_key).toString()); + this->ui.lineEdit_Home->setText(QKeySequence(values.pad_home_key).toString()); + this->ui.lineEdit_dUp->setText(QKeySequence(values.pad_dup_key).toString()); + this->ui.lineEdit_dDown->setText(QKeySequence(values.pad_ddown_key).toString()); + this->ui.lineEdit_dLeft->setText(QKeySequence(values.pad_dleft_key).toString()); + this->ui.lineEdit_dRight->setText(QKeySequence(values.pad_dright_key).toString()); + this->ui.lineEdit_sUp->setText(QKeySequence(values.pad_sup_key).toString()); + this->ui.lineEdit_sDown->setText(QKeySequence(values.pad_sdown_key).toString()); + this->ui.lineEdit_sLeft->setText(QKeySequence(values.pad_sleft_key).toString()); + this->ui.lineEdit_sRight->setText(QKeySequence(values.pad_sright_key).toString()); +} + +void GInputsDialog::OnResetClicked() { + // load the default button settings into temp_settings + GInputsDialog::temp_settings.pad_a_key = Qt::Key_A; + GInputsDialog::temp_settings.pad_b_key = Qt::Key_S; + GInputsDialog::temp_settings.pad_x_key = Qt::Key_Z; + GInputsDialog::temp_settings.pad_y_key = Qt::Key_X; + GInputsDialog::temp_settings.pad_l_key = Qt::Key_Q; + GInputsDialog::temp_settings.pad_r_key = Qt::Key_W; + GInputsDialog::temp_settings.pad_start_key = Qt::Key_M; + GInputsDialog::temp_settings.pad_select_key = Qt::Key_N; + GInputsDialog::temp_settings.pad_home_key = Qt::Key_B; + GInputsDialog::temp_settings.pad_dup_key = Qt::Key_T; + GInputsDialog::temp_settings.pad_ddown_key = Qt::Key_G; + GInputsDialog::temp_settings.pad_dleft_key = Qt::Key_F; + GInputsDialog::temp_settings.pad_dright_key = Qt::Key_H; + GInputsDialog::temp_settings.pad_sup_key = Qt::Key_Up; + GInputsDialog::temp_settings.pad_sdown_key = Qt::Key_Down; + GInputsDialog::temp_settings.pad_sleft_key = Qt::Key_Left; + GInputsDialog::temp_settings.pad_sright_key = Qt::Key_Right; + + // then display it + this->displayButtonSettings(GInputsDialog::temp_settings); +} + +void GInputsDialog::OnOkClicked() { + Config config; + + // load the temporary settings into our real settings + Settings::values.pad_a_key = GInputsDialog::temp_settings.pad_a_key; + Settings::values.pad_b_key = GInputsDialog::temp_settings.pad_b_key; + Settings::values.pad_x_key = GInputsDialog::temp_settings.pad_x_key; + Settings::values.pad_y_key = GInputsDialog::temp_settings.pad_y_key; + Settings::values.pad_l_key = GInputsDialog::temp_settings.pad_l_key; + Settings::values.pad_r_key = GInputsDialog::temp_settings.pad_r_key; + Settings::values.pad_start_key = GInputsDialog::temp_settings.pad_start_key; + Settings::values.pad_select_key = GInputsDialog::temp_settings.pad_select_key; + Settings::values.pad_home_key = GInputsDialog::temp_settings.pad_home_key; + Settings::values.pad_dup_key = GInputsDialog::temp_settings.pad_dup_key; + Settings::values.pad_ddown_key = GInputsDialog::temp_settings.pad_ddown_key; + Settings::values.pad_dleft_key = GInputsDialog::temp_settings.pad_dleft_key; + Settings::values.pad_dright_key = GInputsDialog::temp_settings.pad_dright_key; + Settings::values.pad_sup_key = GInputsDialog::temp_settings.pad_sup_key; + Settings::values.pad_sdown_key = GInputsDialog::temp_settings.pad_sdown_key; + Settings::values.pad_sleft_key = GInputsDialog::temp_settings.pad_sleft_key; + Settings::values.pad_sright_key = GInputsDialog::temp_settings.pad_sright_key; + + // then save it + config.Save(); + LOG_INFO(Config, "Input settings changed."); } diff --git a/src/citra_qt/inputs.h b/src/citra_qt/inputs.h index 1e64c23ac..957cb6a84 100644 --- a/src/citra_qt/inputs.h +++ b/src/citra_qt/inputs.h @@ -2,16 +2,42 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#pragma once #include +#include +#include "core/settings.h" #include "ui_inputs.h" -class GInputsDialog : public QDialog -{ +/// An enum for the buttons on the 3DS +enum Button { + A, B, X, Y, L, R, Start, Select, Home, + DUp, DDown, DLeft, DRight, + SUp, SDown, SLeft, SRight +}; + +/// The input configuration dialog +class GInputsDialog : public QDialog { Q_OBJECT public: GInputsDialog(QWidget* parent = NULL); + /// A map between the name of a LineEdit and the button that it modifies + static std::map edit_input_map; + + /// true if edit_input_map is initialized + static bool edit_input_map_initialized; + + /// Temporary settings used when configuration is changed but not saved yet + static Settings::Values temp_settings; + private: Ui::inputs ui; + + /// Display the button settings on the LineEdits from the given values + void displayButtonSettings(Settings::Values values); + +private slots: + void OnResetClicked(); + void OnOkClicked(); };