Bindings are now editable

This commit is contained in:
inspuration 2014-05-29 13:26:33 -04:00
parent 5dc4223a62
commit 9b6c4ac77d
4 changed files with 91 additions and 8 deletions

View File

@ -1,4 +1,5 @@
#include <QKeySequence> #include <QKeySequence>
#include <QStyledItemDelegate>
#include <QSettings> #include <QSettings>
#include <QKeyEvent> #include <QKeyEvent>
#include "key_bindings.hxx" #include "key_bindings.hxx"
@ -9,7 +10,19 @@ typedef std::map<QKeySequence, HID::PAD> BindingsMap;
BindingsMap bindings; BindingsMap bindings;
std::map<HID::PAD, QString> nameMap;
typedef std::map<HID::PAD, QKeySequence> ReverseBindingsMap;
ReverseBindingsMap reverseBindings;
typedef std::map<HID::PAD, QString> NameMap;
NameMap names;
typedef std::map<QString, HID::PAD> ReverseNameMap;
ReverseNameMap reverseNames;
void SaveKeyBindings(QSettings& settings) void SaveKeyBindings(QSettings& settings)
{ {
@ -47,32 +60,84 @@ HID::PAD GetKeyBinding(QKeyEvent * event)
void RegisterKeyBinding(const QKeySequence keySeq, const HID::PAD pad) void RegisterKeyBinding(const QKeySequence keySeq, const HID::PAD pad)
{ {
bindings[keySeq] = pad; bindings[keySeq] = pad;
reverseBindings[pad] = keySeq;
} }
void createNameMap() { void createNameMap() {
for (int i = 0; i < HID::numPadItems; i++){ 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) GKeyBindingsDialog::GKeyBindingsDialog(QWidget* parent) : QDialog(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
createNameMap(); 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; QStringList columns;
columns << key->first.toString() << nameMap[key->second]; columns << key->second << keySeq.toString();
QTreeWidgetItem* item = new QTreeWidgetItem(columns); QTreeWidgetItem* item = new QTreeWidgetItem(columns);
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
ui.treeWidget->addTopLevelItem(item); ui.treeWidget->addTopLevelItem(item);
} }
// TODO: Make context configurable as well (hiding the column for now) // 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->setColumnCount(2);
ui.treeWidget->resizeColumnToContents(0); ui.treeWidget->resizeColumnToContents(0);
ui.treeWidget->resizeColumnToContents(1); ui.treeWidget->resizeColumnToContents(1);
connect(ui.treeWidget, SIGNAL(itemChanged(QTreeWidgetItem *, int)),
this, SLOT(updateItem(QTreeWidgetItem *, int)));
ui.treeWidget->setItemDelegateForColumn(0, new NoEditDelegate(this));
} }

View File

@ -38,6 +38,15 @@ HID::PAD GetKeyBinding(QKeyEvent * event);
*/ */
void LoadKeyBindings(QSettings& settings); void LoadKeyBindings(QSettings& settings);
/**
* Sets default key bindings.
*
*
*/
void setDefaultKeyBindings();
class GKeyBindingsDialog : public QDialog class GKeyBindingsDialog : public QDialog
{ {
Q_OBJECT Q_OBJECT
@ -47,4 +56,7 @@ public:
private: private:
Ui::key_bindings ui; Ui::key_bindings ui;
private slots:
void updateItem(QTreeWidgetItem * item, int column);
}; };

View File

@ -98,10 +98,7 @@ GMainWindow::GMainWindow()
connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame())); connect(GetHotkey("Main Window", "Start Emulation", this), SIGNAL(activated()), this, SLOT(OnStartGame()));
// Setup key bindings // Setup key bindings
RegisterKeyBinding(QKeySequence("W"), HID::PAD::PAD_UP); setDefaultKeyBindings();
RegisterKeyBinding(QKeySequence("A"), HID::PAD::PAD_LEFT);
RegisterKeyBinding(QKeySequence("S"), HID::PAD::PAD_DOWN);
RegisterKeyBinding(QKeySequence("D"), HID::PAD::PAD_RIGHT);
LoadKeyBindings(settings); LoadKeyBindings(settings);

View File

@ -99,6 +99,9 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<ClCompile /> <ClCompile />
<ClCompile>
<ObjectFileName>$(IntDir)%(RelativeDir)</ObjectFileName>
</ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile /> <ClCompile />
@ -106,6 +109,9 @@
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<ClCompile /> <ClCompile />
<ClCompile>
<ObjectFileName>$(IntDir)%(RelativeDir)</ObjectFileName>
</ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile /> <ClCompile />
@ -131,6 +137,9 @@
<OptimizeReferences>true</OptimizeReferences> <OptimizeReferences>true</OptimizeReferences>
</Link> </Link>
<ClCompile /> <ClCompile />
<ClCompile>
<ObjectFileName>$(IntDir)%(RelativeDir)</ObjectFileName>
</ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\common\common.vcxproj"> <ProjectReference Include="..\common\common.vcxproj">