diff --git a/src/citra_qt/key_bindings.cpp b/src/citra_qt/key_bindings.cpp index 6cd0db86d..6ea62125b 100644 --- a/src/citra_qt/key_bindings.cpp +++ b/src/citra_qt/key_bindings.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include "key_bindings.hxx" @@ -9,7 +10,19 @@ typedef std::map BindingsMap; BindingsMap bindings; -std::map nameMap; + +typedef std::map ReverseBindingsMap; + +ReverseBindingsMap reverseBindings; + + +typedef std::map NameMap; + +NameMap names; + +typedef std::map ReverseNameMap; + +ReverseNameMap reverseNames; void SaveKeyBindings(QSettings& settings) { @@ -47,32 +60,84 @@ HID::PAD GetKeyBinding(QKeyEvent * event) void RegisterKeyBinding(const QKeySequence keySeq, const HID::PAD pad) { bindings[keySeq] = pad; + reverseBindings[pad] = keySeq; } void createNameMap() { for (int i = 0; i < HID::numPadItems; i++){ - nameMap[(HID::PAD)(1 << i)] = QString(HID::PAD_NAMES[i]); + names[(HID::PAD)(1 << i)] = QString(HID::PAD_NAMES[i]); + reverseNames[QString(HID::PAD_NAMES[i])] = (HID::PAD)(1 << i); } } +void setDefaultKeyBindings() { + RegisterKeyBinding(QKeySequence("Y"), HID::PAD::PAD_A); + RegisterKeyBinding(QKeySequence("H"), HID::PAD::PAD_B); + RegisterKeyBinding(QKeySequence("Z"), HID::PAD::PAD_SELECT); + RegisterKeyBinding(QKeySequence("X"), HID::PAD::PAD_START); + + RegisterKeyBinding(QKeySequence("W"), HID::PAD::PAD_UP); + RegisterKeyBinding(QKeySequence("A"), HID::PAD::PAD_LEFT); + RegisterKeyBinding(QKeySequence("S"), HID::PAD::PAD_DOWN); + RegisterKeyBinding(QKeySequence("D"), HID::PAD::PAD_RIGHT); + + RegisterKeyBinding(QKeySequence("6"), HID::PAD::PAD_R); + RegisterKeyBinding(QKeySequence("7"), HID::PAD::PAD_L); + RegisterKeyBinding(QKeySequence("U"), HID::PAD::PAD_X); + RegisterKeyBinding(QKeySequence("J"), HID::PAD::PAD_Y); + +} + +//class makes single columns uneditable +class NoEditDelegate : public QStyledItemDelegate { +public: + NoEditDelegate(QObject* parent = 0) : QStyledItemDelegate(parent) {} + virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const { + return 0; + } +}; + +void GKeyBindingsDialog::updateItem(QTreeWidgetItem * item, int column) { + bindings[item->text(1)] = reverseNames[item->text(0)]; //sanitise + reverseBindings[reverseNames[item->text(0)]] = item->text(1); + return; +} + GKeyBindingsDialog::GKeyBindingsDialog(QWidget* parent) : QDialog(parent) { ui.setupUi(this); createNameMap(); - for (BindingsMap::iterator key = bindings.begin(); key != bindings.end(); ++key) + for (NameMap::iterator key = names.begin(); key != names.end(); ++key) { + QKeySequence keySeq = reverseBindings[key->first]; QStringList columns; - columns << key->first.toString() << nameMap[key->second]; + columns << key->second << keySeq.toString(); QTreeWidgetItem* item = new QTreeWidgetItem(columns); + item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable); ui.treeWidget->addTopLevelItem(item); } // TODO: Make context configurable as well (hiding the column for now) + + ui.treeWidget->setEditTriggers( + QAbstractItemView::EditKeyPressed + | QAbstractItemView::SelectedClicked + | QAbstractItemView::AnyKeyPressed + ); + connect( + this, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), + this, SLOT(OnMouseDoubleClicked(QTreeWidgetItem*, int)) + ); ui.treeWidget->setColumnCount(2); ui.treeWidget->resizeColumnToContents(0); ui.treeWidget->resizeColumnToContents(1); + + connect(ui.treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)), + this, SLOT(updateItem(QTreeWidgetItem *, int))); + ui.treeWidget->setItemDelegateForColumn(0, new NoEditDelegate(this)); + } diff --git a/src/citra_qt/key_bindings.hxx b/src/citra_qt/key_bindings.hxx index 166bca245..690934a47 100644 --- a/src/citra_qt/key_bindings.hxx +++ b/src/citra_qt/key_bindings.hxx @@ -38,6 +38,15 @@ HID::PAD GetKeyBinding(QKeyEvent * event); */ void LoadKeyBindings(QSettings& settings); + +/** +* Sets default key bindings. +* +* +*/ +void setDefaultKeyBindings(); + + class GKeyBindingsDialog : public QDialog { Q_OBJECT @@ -47,4 +56,7 @@ public: private: Ui::key_bindings ui; + +private slots: + void updateItem(QTreeWidgetItem * item, int column); }; diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index 556e9015a..1bdba3443 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -98,10 +98,7 @@ GMainWindow::GMainWindow() connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame())); // Setup key bindings - RegisterKeyBinding(QKeySequence("W"), HID::PAD::PAD_UP); - RegisterKeyBinding(QKeySequence("A"), HID::PAD::PAD_LEFT); - RegisterKeyBinding(QKeySequence("S"), HID::PAD::PAD_DOWN); - RegisterKeyBinding(QKeySequence("D"), HID::PAD::PAD_RIGHT); + setDefaultKeyBindings(); LoadKeyBindings(settings); diff --git a/src/core/core.vcxproj b/src/core/core.vcxproj index ef621b1e8..621aa60b8 100644 --- a/src/core/core.vcxproj +++ b/src/core/core.vcxproj @@ -99,6 +99,9 @@ true + + $(IntDir)%(RelativeDir) + @@ -106,6 +109,9 @@ true + + $(IntDir)%(RelativeDir) + @@ -131,6 +137,9 @@ true + + $(IntDir)%(RelativeDir) +