Added a hot key selection dialog for advanced gamepad function bindings. Makes it more intuitive to setup gamepad function bindings.

This commit is contained in:
mjbudd77 2021-06-13 14:19:30 -04:00
parent 2b0e4a8876
commit f6f868671e
4 changed files with 219 additions and 4 deletions

View File

@ -1937,12 +1937,42 @@ void GamePadFuncConfigDialog::changeButton1(void)
//----------------------------------------------------
void GamePadFuncConfigDialog::changeKeySeq0(void)
{
int ret;
HotKeySelectDialog_t hkd;
ret = hkd.exec();
if ( ret == QDialog::Accepted )
{
printf("Accepted Hot Key: %i\n", hkd.getSelHotKey() );
k->hk[0] = hkd.getSelHotKey();
}
else
{
printf("Rejected Hot Key\n");
}
hk[0]->setCaptureState(true);
hk[0]->setStyleSheet("background-color: green; color: white;");
}
//----------------------------------------------------
void GamePadFuncConfigDialog::changeKeySeq1(void)
{
int ret;
HotKeySelectDialog_t hkd;
ret = hkd.exec();
if ( ret == QDialog::Accepted )
{
printf("Accepted Hot Key: %i\n", hkd.getSelHotKey() );
k->hk[1] = hkd.getSelHotKey();
}
else
{
printf("Rejected Hot Key\n");
}
hk[1]->setCaptureState(true);
hk[1]->setStyleSheet("background-color: green; color: white;");
}
@ -1972,6 +2002,9 @@ void GamePadFuncConfigDialog::clearButton2(void)
k->keySeq[0].key = 0;
k->keySeq[0].modifier = 0;
k->keySeq[0].name.clear();
k->hk[0] = -1;
keySeqLbl[0]->clear();
}
//----------------------------------------------------
void GamePadFuncConfigDialog::clearButton3(void)
@ -1979,6 +2012,9 @@ void GamePadFuncConfigDialog::clearButton3(void)
k->keySeq[1].key = 0;
k->keySeq[1].modifier = 0;
k->keySeq[1].name.clear();
k->hk[1] = -1;
keySeqLbl[1]->clear();
}
//----------------------------------------------------
GamePadConfigHotKey_t::GamePadConfigHotKey_t(int idxIn, gamepad_function_key_t *fk)
@ -2030,3 +2066,123 @@ void GamePadConfigHotKey_t::keyReleaseEvent(QKeyEvent *event)
//printf("GamePad Hot Key Release: 0x%x \n", event->key() );
}
//----------------------------------------------------
// Hot Key Selection Dialog
//----------------------------------------------------
HotKeySelectDialog_t::HotKeySelectDialog_t( QWidget *parent )
: QDialog(parent)
{
QVBoxLayout *mainLayout;
QHBoxLayout *hbox;
QTreeWidgetItem *item;
std::string prefix = "SDL.Hotkeys.";
hotKeyIdx = -1;
setWindowTitle("Hotkey Select");
resize(512, 512);
mainLayout = new QVBoxLayout();
tree = new QTreeWidget(this);
tree->setColumnCount(2);
tree->setSelectionMode( QAbstractItemView::SingleSelection );
item = new QTreeWidgetItem();
item->setText(0, QString::fromStdString("Command"));
item->setText(1, QString::fromStdString("Key"));
item->setTextAlignment(0, Qt::AlignLeft);
item->setTextAlignment(1, Qt::AlignCenter);
tree->setHeaderItem(item);
tree->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
for (int i = 0; i < HK_MAX; i++)
{
char keyName[128];
std::string optionName = prefix + Hotkeys[i].getConfigName();
//g_config->getOption (optionName.c_str (), &keycode);
Hotkeys[i].getString(keyName);
item = new QTreeWidgetItem();
tree->addTopLevelItem(item);
//item->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemNeverHasChildren );
//item->setCheckState( 0, Qt::Checked );
item->setText(0, QString::fromStdString(optionName));
item->setText(1, QString::fromStdString(keyName));
item->setTextAlignment(0, Qt::AlignLeft);
item->setTextAlignment(1, Qt::AlignCenter);
}
connect( tree, SIGNAL(itemClicked(QTreeWidgetItem *, int)),
this, SLOT(hotkeyItemClicked(QTreeWidgetItem *, int)));
mainLayout->addWidget(tree);
okButton = new QPushButton( tr("Ok") );
okButton->setIcon(style()->standardIcon(QStyle::SP_DialogOkButton));
okButton->setEnabled(false);
connect(okButton, SIGNAL(clicked(void)), this, SLOT(acceptCB(void)));
cancelButton = new QPushButton( tr("Cancel") );
cancelButton->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton));
connect(cancelButton, SIGNAL(clicked(void)), this, SLOT(rejectCB(void)));
hbox = new QHBoxLayout();
hbox->addWidget( cancelButton, 1 );
hbox->addStretch(5);
hbox->addWidget( okButton, 1 );
mainLayout->addLayout( hbox );
setLayout(mainLayout);
}
//----------------------------------------------------
HotKeySelectDialog_t::~HotKeySelectDialog_t(void)
{
}
//----------------------------------------------------
void HotKeySelectDialog_t::hotkeyItemClicked(QTreeWidgetItem *item, int column)
{
int row = tree->indexOfTopLevelItem(item);
if ( (row >= 0) && (row < HK_MAX) )
{
hotKeyIdx = row;
okButton->setEnabled(true);
}
}
//----------------------------------------------------
void HotKeySelectDialog_t::closeEvent(QCloseEvent *event)
{
done( result() );
deleteLater();
event->accept();
}
//----------------------------------------------------
void HotKeySelectDialog_t::acceptCB(void)
{
done( QDialog::Accepted );
deleteLater();
}
//----------------------------------------------------
void HotKeySelectDialog_t::rejectCB(void)
{
done( QDialog::Rejected );
deleteLater();
}
//----------------------------------------------------
void HotKeySelectDialog_t::closeWindow(void)
{
done( result() );
deleteLater();
}
//----------------------------------------------------

View File

@ -52,6 +52,32 @@ protected:
bool captureState;
};
class HotKeySelectDialog_t : public QDialog
{
Q_OBJECT
public:
HotKeySelectDialog_t( QWidget *parent = 0);
~HotKeySelectDialog_t(void);
int getSelHotKey(void){ return hotKeyIdx; };
protected:
void closeEvent(QCloseEvent *bar);
QTreeWidget *tree;
QPushButton *okButton;
QPushButton *cancelButton;
int hotKeyIdx;
public slots:
void closeWindow(void);
private slots:
void acceptCB(void);
void rejectCB(void);
void hotkeyItemClicked(QTreeWidgetItem *item, int column);
};
class GamePadFuncConfigDialog : public QDialog
{
Q_OBJECT

View File

@ -450,6 +450,11 @@ gamepad_function_key_t::gamepad_function_key_t(void)
{
keySeq[i].key = 0;
keySeq[i].modifier = Qt::NoModifier;
keyRelReq[i] = 0;
}
for (int i = 0; i < 2; i++)
{
hk[i] = -1;
}
for (int i = 0; i < 2; i++)
{
@ -464,16 +469,41 @@ gamepad_function_key_t::~gamepad_function_key_t(void)
void gamepad_function_key_t::sendKeyPressEvent(int idx)
{
QKeyEvent *k = new QKeyEvent(QEvent::KeyPress, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier);
bool hasShortcut = false;
qApp->postEvent((QObject *)consoleWindow, (QEvent *)k);
// If the hot key has a shortcut associated with it,
// activate shortcut directly instead of attempting to send key sequence events.
if ( (hk[idx] >= 0) && (hk[idx] < HK_MAX) )
{
QShortcut *s = Hotkeys[ hk[idx] ].getShortcut();
if ( s )
{
emit s->activated();
hasShortcut = true;
}
}
if ( !hasShortcut && (keySeq[idx].key > 0) )
{
QKeyEvent *k = new QKeyEvent(QEvent::KeyPress, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier);
qApp->postEvent((QObject *)consoleWindow, (QEvent *)k);
keyRelReq[idx] = 1;
}
}
void gamepad_function_key_t::sendKeyReleaseEvent(int idx)
{
QKeyEvent *k = new QKeyEvent(QEvent::KeyRelease, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier);
if ( keyRelReq[idx] )
{
QKeyEvent *k = new QKeyEvent(QEvent::KeyRelease, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier);
qApp->postEvent((QObject *)consoleWindow, (QEvent *)k);
qApp->postEvent((QObject *)consoleWindow, (QEvent *)k);
keyRelReq[idx] = 0;
}
}
void gamepad_function_key_t::updateStatus(void)

View File

@ -90,6 +90,9 @@ struct gamepad_function_key_t
} keySeq[2];
int hk[2];
char keyRelReq[2];
struct ButtConfig bmap[2];
gamepad_function_key_t(void);