Settings: Move operations into loops
This commit is contained in:
parent
1d213e1656
commit
27dc705f7e
6 changed files with 51 additions and 240 deletions
|
@ -6,13 +6,11 @@
|
|||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
#include "core/settings.h"
|
||||
#include "common/file_util.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
Config::Config() {
|
||||
|
||||
// TODO: Don't hardcode the path; let the frontend decide where to put the config files.
|
||||
qt_config_loc = FileUtil::GetUserPath(D_CONFIG_IDX) + "qt-config.ini";
|
||||
FileUtil::CreateFullPath(qt_config_loc);
|
||||
|
@ -21,7 +19,7 @@ Config::Config() {
|
|||
Reload();
|
||||
}
|
||||
|
||||
static const std::array<QVariant, Settings::NativeInput::NUM_INPUTS> defaults = {
|
||||
const std::array<QVariant, Settings::NativeInput::NUM_INPUTS> Config::defaults = {
|
||||
Qt::Key_A, Qt::Key_S, Qt::Key_Z, Qt::Key_X,
|
||||
Qt::Key_Q, Qt::Key_W, Qt::Key_1, Qt::Key_2,
|
||||
Qt::Key_M, Qt::Key_N, Qt::Key_B,
|
||||
|
|
|
@ -4,8 +4,11 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <string>
|
||||
|
||||
#include "core/settings.h"
|
||||
|
||||
class QSettings;
|
||||
|
||||
class Config {
|
||||
|
@ -20,4 +23,5 @@ public:
|
|||
|
||||
void Reload();
|
||||
void Save();
|
||||
static const std::array<QVariant, Settings::NativeInput::NUM_INPUTS> defaults;
|
||||
};
|
||||
|
|
|
@ -7,21 +7,23 @@
|
|||
#include "QLineEditKeyConfig.h"
|
||||
#include "inputs.h"
|
||||
|
||||
std::map<Button, std::string> ButtonNameMap({
|
||||
{ Button::A, "A" }, { Button::B, "B" }, { Button::X, "X" }, { Button::Y, "Y" },
|
||||
{ Button::L, "L" }, { Button::R, "R" }, { Button::ZL, "ZL" }, { Button::ZR, "ZR" },
|
||||
{ Button::DLeft, "D-Left" }, { Button::DRight, "D-Right" }, { Button::DUp, "D-Up" }, { Button::DDown, "D-Down" },
|
||||
{ Button::SLeft, "S-Left" }, { Button::SRight, "S-Right" }, { Button::SUp, "S-Up" }, { Button::SDown, "S-Down" },
|
||||
{ Button::CLeft, "C-Left" }, { Button::CRight, "C-Right" }, { Button::CUp, "C-Up" }, { Button::CDown, "C-Down" },
|
||||
{ Button::Start, "Start" }, { Button::Select, "Select" }, { Button::Home, "Home" } }
|
||||
using namespace Settings::NativeInput;
|
||||
|
||||
std::map<Values, std::string> ButtonNameMap({
|
||||
{ A, "A" }, { B, "B" }, { X, "X" }, { Y, "Y" },
|
||||
{ L, "L" }, { R, "R" }, { ZL, "ZL" }, { ZR, "ZR" },
|
||||
{ DLEFT, "D-Left" }, { DRIGHT, "D-Right" }, { DUP, "D-Up" }, { DDOWN, "D-Down" },
|
||||
{ SLEFT, "S-Left" }, { SRIGHT, "S-Right" }, { SUP, "S-Up" }, { SDOWN, "S-Down" },
|
||||
{ CLEFT, "C-Left" }, { CRIGHT, "C-Right" }, { CUP, "C-Up" }, { CDOWN, "C-Down" },
|
||||
{ START, "Start" }, { SELECT, "Select" }, { HOME, "Home" } }
|
||||
);
|
||||
|
||||
QLineEditKeyConfig::QLineEditKeyConfig(Button button, QWidget* parent)
|
||||
: button(button), QLineEdit(parent) {}
|
||||
QLineEditKeyConfig::QLineEditKeyConfig(Values button, QWidget* parent)
|
||||
: QLineEdit(parent), button(button) {}
|
||||
|
||||
void QLineEditKeyConfig::keyPressEvent(QKeyEvent* event) {
|
||||
QString key_name = GInputsDialog::GetKeyName(event->key());
|
||||
setText(key_name);
|
||||
|
||||
ValueChanged(button, event->key());
|
||||
emit ValueChanged(button, event->key());
|
||||
}
|
||||
|
|
|
@ -7,30 +7,22 @@
|
|||
#include <map>
|
||||
|
||||
#include <QLineEdit>
|
||||
#include <core/settings.h>
|
||||
|
||||
class QKeyEvent;
|
||||
|
||||
/// An enum for the buttons on the 3DS
|
||||
enum Button {
|
||||
A, B, X, Y, L, R, ZL, ZR,
|
||||
DUp, DDown, DLeft, DRight,
|
||||
SUp, SDown, SLeft, SRight,
|
||||
CUp, CDown, CLeft, CRight,
|
||||
Start, Select, Home
|
||||
};
|
||||
|
||||
/// Map a button to its name
|
||||
extern std::map<Button, std::string> ButtonNameMap;
|
||||
extern std::map<Settings::NativeInput::Values, std::string> ButtonNameMap;
|
||||
|
||||
/// The LineEdits used for button configuration
|
||||
class QLineEditKeyConfig : public QLineEdit {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Button button;
|
||||
QLineEditKeyConfig(Button button, QWidget* parent = nullptr);
|
||||
Settings::NativeInput::Values button;
|
||||
QLineEditKeyConfig(Settings::NativeInput::Values button, QWidget* parent = nullptr);
|
||||
void keyPressEvent(QKeyEvent* event) override;
|
||||
|
||||
signals:
|
||||
void ValueChanged(Button, int);
|
||||
void ValueChanged(Settings::NativeInput::Values, int);
|
||||
};
|
||||
|
|
|
@ -12,62 +12,35 @@
|
|||
#include "common/logging/log.h"
|
||||
#include "config.h"
|
||||
#include "inputs.h"
|
||||
#include "QLineEditKeyConfig.h"
|
||||
|
||||
GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) {
|
||||
// create a copy of the current settings
|
||||
temp_settings = Settings::Values(Settings::values);
|
||||
|
||||
// create the lineEdits
|
||||
lineEdit_A = new QLineEditKeyConfig(Button::A);
|
||||
lineEdit_B = new QLineEditKeyConfig(Button::B);
|
||||
lineEdit_X = new QLineEditKeyConfig(Button::X);
|
||||
lineEdit_Y = new QLineEditKeyConfig(Button::Y);
|
||||
lineEdit_L = new QLineEditKeyConfig(Button::L);
|
||||
lineEdit_R = new QLineEditKeyConfig(Button::R);
|
||||
lineEdit_ZL = new QLineEditKeyConfig(Button::ZL);
|
||||
lineEdit_ZR = new QLineEditKeyConfig(Button::ZR);
|
||||
lineEdit_dUp = new QLineEditKeyConfig(Button::DUp);
|
||||
lineEdit_dDown = new QLineEditKeyConfig(Button::DDown);
|
||||
lineEdit_dLeft = new QLineEditKeyConfig(Button::DLeft);
|
||||
lineEdit_dRight = new QLineEditKeyConfig(Button::DRight);
|
||||
lineEdit_sUp = new QLineEditKeyConfig(Button::SUp);
|
||||
lineEdit_sDown = new QLineEditKeyConfig(Button::SDown);
|
||||
lineEdit_sLeft = new QLineEditKeyConfig(Button::SLeft);
|
||||
lineEdit_sRight = new QLineEditKeyConfig(Button::SRight);
|
||||
lineEdit_cUp = new QLineEditKeyConfig(Button::CUp);
|
||||
lineEdit_cDown = new QLineEditKeyConfig(Button::CDown);
|
||||
lineEdit_cLeft = new QLineEditKeyConfig(Button::CLeft);
|
||||
lineEdit_cRight = new QLineEditKeyConfig(Button::CRight);
|
||||
lineEdit_Home = new QLineEditKeyConfig(Button::Home);
|
||||
lineEdit_Start = new QLineEditKeyConfig(Button::Start);
|
||||
lineEdit_Select = new QLineEditKeyConfig(Button::Select);
|
||||
|
||||
lineEdits = {{
|
||||
lineEdit_A, lineEdit_B, lineEdit_X, lineEdit_Y, lineEdit_L, lineEdit_R, lineEdit_ZL, lineEdit_ZR,
|
||||
lineEdit_dUp, lineEdit_dDown, lineEdit_dLeft, lineEdit_dRight,
|
||||
lineEdit_sUp, lineEdit_sDown, lineEdit_sLeft, lineEdit_sRight,
|
||||
lineEdit_cUp, lineEdit_cDown, lineEdit_cLeft, lineEdit_cRight,
|
||||
lineEdit_Home, lineEdit_Start, lineEdit_Select
|
||||
}};
|
||||
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
|
||||
lineEdits[i] = new QLineEditKeyConfig(Settings::NativeInput::All[i]);
|
||||
}
|
||||
|
||||
// put the lineEdits and their labels into a grid
|
||||
QGridLayout *grid = new QGridLayout();
|
||||
QGridLayout* grid = new QGridLayout();
|
||||
size_t size = lineEdits.size();
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
const int numRow = 8;
|
||||
int row = i % numRow;
|
||||
int labelColumn = i / numRow * 2;
|
||||
QLineEditKeyConfig *lineEdit = lineEdits[i];
|
||||
QLabel *label = new QLabel(tr(ButtonNameMap[lineEdit->button].c_str()));
|
||||
const int NUM_ROW = 8;
|
||||
int row = i % NUM_ROW;
|
||||
int labelColumn = (int)i / NUM_ROW * 2;
|
||||
QLineEditKeyConfig* lineEdit = lineEdits[i];
|
||||
QLabel* label = new QLabel(tr(ButtonNameMap[lineEdit->button].c_str()));
|
||||
grid->addWidget(label, row, labelColumn);
|
||||
grid->addWidget(lineEdit, row, labelColumn + 1);
|
||||
|
||||
QObject::connect(lineEdit, SIGNAL(ValueChanged(Button, int)), this, SLOT(UpdateValue(Button, int)));
|
||||
connect(lineEdit, SIGNAL(ValueChanged(Settings::NativeInput::Values, int)), this, SLOT(UpdateValue(Settings::NativeInput::Values, int)));
|
||||
}
|
||||
|
||||
// the button box that contains the restore default/ok/cancel buttons
|
||||
QVBoxLayout *verticalLayout = new QVBoxLayout(this);
|
||||
QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
|
||||
QVBoxLayout* verticalLayout = new QVBoxLayout();
|
||||
QDialogButtonBox* buttonBox = new QDialogButtonBox();
|
||||
buttonBox->setOrientation(Qt::Horizontal);
|
||||
buttonBox->setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok | QDialogButtonBox::RestoreDefaults);
|
||||
|
||||
|
@ -92,106 +65,14 @@ GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) {
|
|||
UpdateKeyLabels();
|
||||
}
|
||||
|
||||
void GInputsDialog::UpdateValue(Button button, int key) {
|
||||
switch (button) {
|
||||
case Button::A:
|
||||
temp_settings.pad_a_key = key;
|
||||
break;
|
||||
case Button::B:
|
||||
temp_settings.pad_b_key = key;
|
||||
break;
|
||||
case Button::X:
|
||||
temp_settings.pad_x_key = key;
|
||||
break;
|
||||
case Button::Y:
|
||||
temp_settings.pad_y_key = key;
|
||||
break;
|
||||
case Button::L:
|
||||
temp_settings.pad_l_key = key;
|
||||
break;
|
||||
case Button::R:
|
||||
temp_settings.pad_r_key = key;
|
||||
break;
|
||||
case Button::ZL:
|
||||
temp_settings.pad_zl_key = key;
|
||||
break;
|
||||
case Button::ZR:
|
||||
temp_settings.pad_zr_key = key;
|
||||
break;
|
||||
case Button::DUp:
|
||||
temp_settings.pad_dup_key = key;
|
||||
break;
|
||||
case Button::DDown:
|
||||
temp_settings.pad_ddown_key = key;
|
||||
break;
|
||||
case Button::DLeft:
|
||||
temp_settings.pad_dleft_key = key;
|
||||
break;
|
||||
case Button::DRight:
|
||||
temp_settings.pad_dright_key = key;
|
||||
break;
|
||||
case Button::SUp:
|
||||
temp_settings.pad_sup_key = key;
|
||||
break;
|
||||
case Button::SDown:
|
||||
temp_settings.pad_sdown_key = key;
|
||||
break;
|
||||
case Button::SLeft:
|
||||
temp_settings.pad_sleft_key = key;
|
||||
break;
|
||||
case Button::SRight:
|
||||
temp_settings.pad_sright_key = key;
|
||||
break;
|
||||
case Button::CUp:
|
||||
temp_settings.pad_cup_key = key;
|
||||
break;
|
||||
case Button::CDown:
|
||||
temp_settings.pad_cdown_key = key;
|
||||
break;
|
||||
case Button::CLeft:
|
||||
temp_settings.pad_cleft_key = key;
|
||||
break;
|
||||
case Button::CRight:
|
||||
temp_settings.pad_cright_key = key;
|
||||
break;
|
||||
case Button::Home:
|
||||
temp_settings.pad_home_key = key;
|
||||
break;
|
||||
case Button::Start:
|
||||
temp_settings.pad_start_key = key;
|
||||
break;
|
||||
case Button::Select:
|
||||
temp_settings.pad_select_key = key;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
void GInputsDialog::UpdateValue(Settings::NativeInput::Values button, int key) {
|
||||
temp_settings.input_mappings[Settings::NativeInput::All[button]] = key;
|
||||
}
|
||||
|
||||
void GInputsDialog::UpdateKeyLabels() {
|
||||
lineEdit_A->setText(GetKeyName(temp_settings.pad_a_key));
|
||||
lineEdit_B->setText(GetKeyName(temp_settings.pad_b_key));
|
||||
lineEdit_X->setText(GetKeyName(temp_settings.pad_x_key));
|
||||
lineEdit_Y->setText(GetKeyName(temp_settings.pad_y_key));
|
||||
lineEdit_L->setText(GetKeyName(temp_settings.pad_l_key));
|
||||
lineEdit_R->setText(GetKeyName(temp_settings.pad_r_key));
|
||||
lineEdit_ZL->setText(GetKeyName(temp_settings.pad_zl_key));
|
||||
lineEdit_ZR->setText(GetKeyName(temp_settings.pad_zr_key));
|
||||
lineEdit_dUp->setText(GetKeyName(temp_settings.pad_dup_key));
|
||||
lineEdit_dDown->setText(GetKeyName(temp_settings.pad_ddown_key));
|
||||
lineEdit_dLeft->setText(GetKeyName(temp_settings.pad_dleft_key));
|
||||
lineEdit_dRight->setText(GetKeyName(temp_settings.pad_dright_key));
|
||||
lineEdit_sUp->setText(GetKeyName(temp_settings.pad_sup_key));
|
||||
lineEdit_sDown->setText(GetKeyName(temp_settings.pad_sdown_key));
|
||||
lineEdit_sLeft->setText(GetKeyName(temp_settings.pad_sleft_key));
|
||||
lineEdit_sRight->setText(GetKeyName(temp_settings.pad_sright_key));
|
||||
lineEdit_cUp->setText(GetKeyName(temp_settings.pad_cup_key));
|
||||
lineEdit_cDown->setText(GetKeyName(temp_settings.pad_cdown_key));
|
||||
lineEdit_cLeft->setText(GetKeyName(temp_settings.pad_cleft_key));
|
||||
lineEdit_cRight->setText(GetKeyName(temp_settings.pad_cright_key));
|
||||
lineEdit_Home->setText(GetKeyName(temp_settings.pad_home_key));
|
||||
lineEdit_Start->setText(GetKeyName(temp_settings.pad_start_key));
|
||||
lineEdit_Select->setText(GetKeyName(temp_settings.pad_select_key));
|
||||
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
|
||||
lineEdits[i]->setText(GetKeyName(temp_settings.input_mappings[Settings::NativeInput::All[i]]));
|
||||
}
|
||||
}
|
||||
|
||||
QString GInputsDialog::GetKeyName(int key_code) {
|
||||
|
@ -212,31 +93,9 @@ QString GInputsDialog::GetKeyName(int key_code) {
|
|||
|
||||
void GInputsDialog::RestoreDefaultSettings() {
|
||||
// load the default button settings into temp_settings
|
||||
temp_settings.pad_a_key = Qt::Key_A;
|
||||
temp_settings.pad_b_key = Qt::Key_S;
|
||||
temp_settings.pad_x_key = Qt::Key_Z;
|
||||
temp_settings.pad_y_key = Qt::Key_X;
|
||||
temp_settings.pad_l_key = Qt::Key_Q;
|
||||
temp_settings.pad_r_key = Qt::Key_W;
|
||||
temp_settings.pad_zl_key = Qt::Key_1;
|
||||
temp_settings.pad_zr_key = Qt::Key_2;
|
||||
|
||||
temp_settings.pad_dup_key = Qt::Key_T;
|
||||
temp_settings.pad_ddown_key = Qt::Key_G;
|
||||
temp_settings.pad_dleft_key = Qt::Key_F;
|
||||
temp_settings.pad_dright_key = Qt::Key_H;
|
||||
temp_settings.pad_sup_key = Qt::Key_Up;
|
||||
temp_settings.pad_sdown_key = Qt::Key_Down;
|
||||
temp_settings.pad_sleft_key = Qt::Key_Left;
|
||||
temp_settings.pad_sright_key = Qt::Key_Right;
|
||||
temp_settings.pad_cup_key = Qt::Key_I;
|
||||
temp_settings.pad_cdown_key = Qt::Key_K;
|
||||
temp_settings.pad_cleft_key = Qt::Key_J;
|
||||
temp_settings.pad_cright_key = Qt::Key_L;
|
||||
|
||||
temp_settings.pad_home_key = Qt::Key_B;
|
||||
temp_settings.pad_start_key = Qt::Key_M;
|
||||
temp_settings.pad_select_key = Qt::Key_N;
|
||||
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
|
||||
temp_settings.input_mappings[Settings::NativeInput::All[i]] = Config::defaults[i].toInt();
|
||||
}
|
||||
|
||||
// then display it
|
||||
UpdateKeyLabels();
|
||||
|
@ -246,31 +105,10 @@ void GInputsDialog::SaveSettings() {
|
|||
Config config;
|
||||
|
||||
// load the temporary settings into our real settings
|
||||
Settings::values.pad_a_key = temp_settings.pad_a_key;
|
||||
Settings::values.pad_b_key = temp_settings.pad_b_key;
|
||||
Settings::values.pad_x_key = temp_settings.pad_x_key;
|
||||
Settings::values.pad_y_key = temp_settings.pad_y_key;
|
||||
Settings::values.pad_l_key = temp_settings.pad_l_key;
|
||||
Settings::values.pad_r_key = temp_settings.pad_r_key;
|
||||
Settings::values.pad_zl_key = temp_settings.pad_zl_key;
|
||||
Settings::values.pad_zr_key = temp_settings.pad_zr_key;
|
||||
|
||||
Settings::values.pad_dup_key = temp_settings.pad_dup_key;
|
||||
Settings::values.pad_ddown_key = temp_settings.pad_ddown_key;
|
||||
Settings::values.pad_dleft_key = temp_settings.pad_dleft_key;
|
||||
Settings::values.pad_dright_key = temp_settings.pad_dright_key;
|
||||
Settings::values.pad_sup_key = temp_settings.pad_sup_key;
|
||||
Settings::values.pad_sdown_key = temp_settings.pad_sdown_key;
|
||||
Settings::values.pad_sleft_key = temp_settings.pad_sleft_key;
|
||||
Settings::values.pad_sright_key = temp_settings.pad_sright_key;
|
||||
Settings::values.pad_cup_key = temp_settings.pad_cup_key;
|
||||
Settings::values.pad_cdown_key = temp_settings.pad_cdown_key;
|
||||
Settings::values.pad_cleft_key = temp_settings.pad_cleft_key;
|
||||
Settings::values.pad_cright_key = temp_settings.pad_cright_key;
|
||||
|
||||
Settings::values.pad_home_key = temp_settings.pad_home_key;
|
||||
Settings::values.pad_start_key = temp_settings.pad_start_key;
|
||||
Settings::values.pad_select_key = temp_settings.pad_select_key;
|
||||
for (int i = 0; i < Settings::NativeInput::NUM_INPUTS; ++i) {
|
||||
Settings::values.input_mappings[Settings::NativeInput::All[i]] =
|
||||
temp_settings.input_mappings[Settings::NativeInput::All[i]];
|
||||
}
|
||||
|
||||
// then save it
|
||||
config.Save();
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
#include <QDialog>
|
||||
|
||||
#include "../core/settings.h"
|
||||
#include "QLineEditKeyConfig.h"
|
||||
|
||||
class QLineEditKeyConfig;
|
||||
|
||||
/// The input configuration dialog
|
||||
class GInputsDialog : public QDialog {
|
||||
|
@ -22,31 +23,7 @@ public:
|
|||
static QString GetKeyName(int key_code);
|
||||
|
||||
private:
|
||||
QLineEditKeyConfig *lineEdit_A;
|
||||
QLineEditKeyConfig *lineEdit_B;
|
||||
QLineEditKeyConfig *lineEdit_X;
|
||||
QLineEditKeyConfig *lineEdit_Y;
|
||||
QLineEditKeyConfig *lineEdit_L;
|
||||
QLineEditKeyConfig *lineEdit_R;
|
||||
QLineEditKeyConfig *lineEdit_ZL;
|
||||
QLineEditKeyConfig *lineEdit_ZR;
|
||||
QLineEditKeyConfig *lineEdit_dUp;
|
||||
QLineEditKeyConfig *lineEdit_dDown;
|
||||
QLineEditKeyConfig *lineEdit_dLeft;
|
||||
QLineEditKeyConfig *lineEdit_dRight;
|
||||
QLineEditKeyConfig *lineEdit_sUp;
|
||||
QLineEditKeyConfig *lineEdit_sDown;
|
||||
QLineEditKeyConfig *lineEdit_sLeft;
|
||||
QLineEditKeyConfig *lineEdit_sRight;
|
||||
QLineEditKeyConfig *lineEdit_cUp;
|
||||
QLineEditKeyConfig *lineEdit_cDown;
|
||||
QLineEditKeyConfig *lineEdit_cLeft;
|
||||
QLineEditKeyConfig *lineEdit_cRight;
|
||||
QLineEditKeyConfig *lineEdit_Start;
|
||||
QLineEditKeyConfig *lineEdit_Select;
|
||||
QLineEditKeyConfig *lineEdit_Home;
|
||||
|
||||
/// An array of all the lineEdits above for easy iterating though them
|
||||
/// An array of all the lineEdits for easy iterating though them
|
||||
std::array<QLineEditKeyConfig*, 23> lineEdits;
|
||||
|
||||
/// Display the button settings on the LineEdits from the given values
|
||||
|
@ -58,5 +35,5 @@ private:
|
|||
private slots:
|
||||
void SaveSettings();
|
||||
void RestoreDefaultSettings();
|
||||
void UpdateValue(Button button, int key);
|
||||
void UpdateValue(Settings::NativeInput::Values button, int key);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue