Successful test of basic gamepad button remapping to keyboard sequences.

This commit is contained in:
mjbudd77 2021-03-23 21:38:13 -04:00
parent 16fc02daaf
commit c753820d32
4 changed files with 219 additions and 48 deletions

View File

@ -623,7 +623,7 @@ void GamePadConfDialog_t::refreshKeyBindTree( bool reset )
}
item->setText(0, tr(btnSeq));
//item->setText(1, QString::fromStdString(keyName));
item->setText(1, QString::fromStdString(binding->keySeq[0].name));
item->setTextAlignment(0, Qt::AlignLeft);
item->setTextAlignment(1, Qt::AlignLeft);
@ -1528,8 +1528,9 @@ GamePadFuncConfigDialog::GamePadFuncConfigDialog( gamepad_function_key_t *fk, QW
QVBoxLayout *mainLayout, *vbox;
QGridLayout *grid;
QLabel *lbl;
QGroupBox *frame;
QPushButton *okButton, *cancelButton;
QPushButton *clearButton[3];
QPushButton *clearButton[4];
if ( fk == NULL )
{
@ -1544,35 +1545,47 @@ GamePadFuncConfigDialog::GamePadFuncConfigDialog( gamepad_function_key_t *fk, QW
if ( editMode )
{
setWindowTitle( tr("Edit Function") );
setWindowTitle( tr("Edit Gamepad Key Mapping") );
}
else
{
setWindowTitle( tr("Add Function") );
setWindowTitle( tr("Add Gamepad Key Mapping") );
}
btnLbl[0] = new QLineEdit();
btnLbl[1] = new QLineEdit();
keySeqLbl = new QLineEdit();
btnLbl[0] = new QLineEdit();
btnLbl[1] = new QLineEdit();
keySeqLbl[0] = new QLineEdit();
keySeqLbl[1] = new QLineEdit();
btnLbl[0]->setReadOnly(true);
btnLbl[1]->setReadOnly(true);
keySeqLbl->setReadOnly(true);
keySeqLbl[0]->setReadOnly(true);
keySeqLbl[1]->setReadOnly(true);
b[0] = new GamePadConfigButton_t(0);
b[1] = new GamePadConfigButton_t(1);
hk = new GamePadConfigHotKey_t(k);
btnLbl[0]->setAlignment( Qt::AlignCenter );
btnLbl[1]->setAlignment( Qt::AlignCenter );
keySeqLbl[0]->setAlignment( Qt::AlignCenter );
keySeqLbl[1]->setAlignment( Qt::AlignCenter );
b[0] = new GamePadConfigButton_t(0);
b[1] = new GamePadConfigButton_t(1);
hk[0] = new GamePadConfigHotKey_t(0,k);
hk[1] = new GamePadConfigHotKey_t(1,k);
clearButton[0] = new QPushButton( tr("Clear") );
clearButton[1] = new QPushButton( tr("Clear") );
clearButton[2] = new QPushButton( tr("Clear") );
clearButton[3] = new QPushButton( tr("Clear") );
mainLayout = new QVBoxLayout();
setLayout(mainLayout);
frame = new QGroupBox( tr("Game Pad Button Sequence:") );
mainLayout->addWidget( frame );
grid = new QGridLayout();
mainLayout->addLayout( grid );
frame->setLayout( grid );
//grid->setColumnMinimumWidth( 1, 20 );
@ -1586,18 +1599,33 @@ GamePadFuncConfigDialog::GamePadFuncConfigDialog( gamepad_function_key_t *fk, QW
grid->addWidget( b[1], 1, 2 );
grid->addWidget( clearButton[1], 1, 3 );
grid->addWidget( new QLabel( tr("Key Sequence:") ), 2, 0 );
grid->addWidget( keySeqLbl, 2, 1 );
grid->addWidget( hk, 2, 2 );
grid->addWidget( clearButton[2], 2, 3 );
frame = new QGroupBox( tr("Maps to Key Sequence:") );
mainLayout->addWidget( frame );
grid = new QGridLayout();
frame->setLayout( grid );
grid->addWidget( new QLabel( tr("On Press:") ), 0, 0 );
grid->addWidget( keySeqLbl[0], 0, 1 );
grid->addWidget( hk[0], 0, 2 );
grid->addWidget( clearButton[2], 0, 3 );
grid->addWidget( new QLabel( tr("On Release:") ), 1, 0 );
grid->addWidget( keySeqLbl[1], 1, 1 );
grid->addWidget( hk[1], 1, 2 );
grid->addWidget( clearButton[3], 1, 3 );
hbox = new QHBoxLayout();
okButton = new QPushButton( tr("OK") );
cancelButton = new QPushButton( tr("Cancel") );
okButton->setIcon(style()->standardIcon(QStyle::SP_DialogApplyButton));
cancelButton->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton));
mainLayout->addLayout( hbox );
hbox->addWidget( cancelButton );
hbox->addWidget( okButton );
hbox->addWidget( cancelButton, 1 );
hbox->addStretch( 5 );
hbox->addWidget( okButton, 1 );
connect( okButton, SIGNAL(clicked(void)), this, SLOT(acceptCB(void)) );
connect( cancelButton, SIGNAL(clicked(void)), this, SLOT(rejectCB(void)) );
@ -1607,9 +1635,18 @@ GamePadFuncConfigDialog::GamePadFuncConfigDialog( gamepad_function_key_t *fk, QW
//ret = dialog.exec();
buttonConfigStatus = 0;
connect( b[0], SIGNAL(clicked(void)), this, SLOT(changeButton0(void)) );
connect( b[1], SIGNAL(clicked(void)), this, SLOT(changeButton1(void)) );
connect( hk, SIGNAL(clicked(void)), this, SLOT(changeKeySeq(void) ) );
connect( b[0] , SIGNAL(clicked(void)), this, SLOT(changeButton0(void)) );
connect( b[1] , SIGNAL(clicked(void)), this, SLOT(changeButton1(void)) );
connect( hk[0], SIGNAL(clicked(void)), this, SLOT(changeKeySeq0(void) ) );
connect( hk[1], SIGNAL(clicked(void)), this, SLOT(changeKeySeq1(void) ) );
connect( clearButton[0], SIGNAL(clicked(void)), this, SLOT(clearButton0(void)) );
connect( clearButton[1], SIGNAL(clicked(void)), this, SLOT(clearButton1(void)) );
connect( clearButton[2], SIGNAL(clicked(void)), this, SLOT(clearButton2(void)) );
connect( clearButton[3], SIGNAL(clicked(void)), this, SLOT(clearButton3(void)) );
hk[0]->setKeyNameLbl( keySeqLbl[0] );
hk[1]->setKeyNameLbl( keySeqLbl[1] );
}
//----------------------------------------------------
GamePadFuncConfigDialog::~GamePadFuncConfigDialog(void)
@ -1707,36 +1744,90 @@ void GamePadFuncConfigDialog::changeButton1(void)
changeButton(1);
}
//----------------------------------------------------
void GamePadFuncConfigDialog::changeKeySeq(void)
void GamePadFuncConfigDialog::changeKeySeq0(void)
{
hk->setCaptureState(true);
hk->setStyleSheet("background-color: green; color: white;");
hk[0]->setCaptureState(true);
hk[0]->setStyleSheet("background-color: green; color: white;");
}
//----------------------------------------------------
GamePadConfigHotKey_t::GamePadConfigHotKey_t(gamepad_function_key_t *fk)
void GamePadFuncConfigDialog::changeKeySeq1(void)
{
hk[1]->setCaptureState(true);
hk[1]->setStyleSheet("background-color: green; color: white;");
}
//----------------------------------------------------
void GamePadFuncConfigDialog::clearButton0(void)
{
k->bmap[0].ButtType = -1;
k->bmap[0].DeviceNum = -1;
k->bmap[0].ButtonNum = -1;
k->bmap[0].state = 0;
btnLbl[0]->clear();
}
//----------------------------------------------------
void GamePadFuncConfigDialog::clearButton1(void)
{
k->bmap[1].ButtType = -1;
k->bmap[1].DeviceNum = -1;
k->bmap[1].ButtonNum = -1;
k->bmap[1].state = 0;
btnLbl[1]->clear();
}
//----------------------------------------------------
void GamePadFuncConfigDialog::clearButton2(void)
{
k->keySeq[0].key = 0;
k->keySeq[0].modifier = 0;
k->keySeq[0].name.clear();
}
//----------------------------------------------------
void GamePadFuncConfigDialog::clearButton3(void)
{
k->keySeq[1].key = 0;
k->keySeq[1].modifier = 0;
k->keySeq[1].name.clear();
}
//----------------------------------------------------
GamePadConfigHotKey_t::GamePadConfigHotKey_t(int idxIn, gamepad_function_key_t *fk)
{
setText("Change");
k = fk;
keySeqLbl = NULL;
captureState = false;
idx = idxIn;
}
//----------------------------------------------------
void GamePadConfigHotKey_t::setKeyNameLbl( QLineEdit *lbl )
{
keySeqLbl = lbl;
}
//----------------------------------------------------
void GamePadConfigHotKey_t::keyPressEvent(QKeyEvent *event)
{
printf("GamePad Hot Key Press: 0x%x '%s'\n", event->key(), event->text().toStdString().c_str() );
//printf("GamePad Hot Key Press: 0x%x '%s'\n", event->key(), event->text().toStdString().c_str() );
//pushKeyEvent(event, 1);
if ( captureState )
{
k->qKey = event->key();
k->qModifier = event->modifiers();
QKeySequence ks( event->modifiers() + event->key() );
k->keySeq[idx].key = event->key();
k->keySeq[idx].modifier = event->modifiers();
k->keySeq[idx].name = ks.toString().toStdString();
if ( keySeqLbl )
{
keySeqLbl->setText( ks.toString() );
}
}
}
void GamePadConfigHotKey_t::keyReleaseEvent(QKeyEvent *event)
{
printf("GamePad Hot Key Release: 0x%x \n", event->key() );
//printf("GamePad Hot Key Release: 0x%x \n", event->key() );
//pushKeyEvent(event, 0);
captureState = false;

View File

@ -36,14 +36,17 @@ protected:
class GamePadConfigHotKey_t : public QPushButton
{
public:
GamePadConfigHotKey_t( gamepad_function_key_t *k );
GamePadConfigHotKey_t( int idx, gamepad_function_key_t *k );
void setCaptureState(bool s){ captureState = s; };
void setKeyNameLbl( QLineEdit *lbl );
protected:
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
int idx;
QLineEdit *keySeqLbl;
gamepad_function_key_t *k;
bool captureState;
};
@ -62,10 +65,10 @@ protected:
void changeButton(int x);
QLineEdit *btnLbl[2];
QLineEdit *keySeqLbl;
QLineEdit *keySeqLbl[2];
GamePadConfigButton_t *b[2];
GamePadConfigHotKey_t *hk;
GamePadConfigHotKey_t *hk[2];
gamepad_function_key_t *k;
int buttonConfigStatus;
@ -78,7 +81,12 @@ private slots:
void rejectCB(void);
void changeButton0(void);
void changeButton1(void);
void changeKeySeq(void);
void changeKeySeq0(void);
void changeKeySeq1(void);
void clearButton0(void);
void clearButton1(void);
void clearButton2(void);
void clearButton3(void);
};
class GamePadView_t : public QWidget

View File

@ -64,6 +64,7 @@ static int cspec = 0;
static int buttonConfigInProgress = 0;
extern int gametype;
static int DTestButton (ButtConfig * bc);
std::list <gamepad_function_key_t*> gpKeySeqList;
@ -412,8 +413,16 @@ setHotKeys (void)
gamepad_function_key_t::gamepad_function_key_t(void)
{
qKey = 0;
qModifier = Qt::NoModifier;
for (int i=0; i<2; i++)
{
keySeq[i].key = 0;
keySeq[i].modifier = Qt::NoModifier;
}
for (int i=0; i<2; i++)
{
bmap[i].ButtonNum = -1;
bmap[i].state = 0;
}
}
gamepad_function_key_t::~gamepad_function_key_t(void)
@ -421,20 +430,64 @@ gamepad_function_key_t::~gamepad_function_key_t(void)
}
void gamepad_function_key_t::sendKeyPressEvent(void)
void gamepad_function_key_t::sendKeyPressEvent(int idx)
{
QKeyEvent *k = new QKeyEvent (QEvent::KeyPress, qKey, (Qt::KeyboardModifiers)qModifier );
QKeyEvent *k = new QKeyEvent (QEvent::KeyPress, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier );
qApp->postEvent((QObject*)consoleWindow,(QEvent *)k);
}
void gamepad_function_key_t::sendKeyReleaseEvent(void)
void gamepad_function_key_t::sendKeyReleaseEvent(int idx)
{
QKeyEvent *k = new QKeyEvent (QEvent::KeyRelease, qKey, (Qt::KeyboardModifiers)qModifier );
QKeyEvent *k = new QKeyEvent (QEvent::KeyRelease, keySeq[idx].key, (Qt::KeyboardModifiers)keySeq[idx].modifier );
qApp->postEvent((QObject*)consoleWindow,(QEvent *)k);
}
void gamepad_function_key_t::updateStatus(void)
{
int state_lp[2], state[2];
state_lp[0] = bmap[0].state;
state_lp[1] = bmap[1].state;
state[0] = DTestButton( &bmap[0] );
state[1] = DTestButton( &bmap[1] );
if ( (bmap[0].ButtonNum >= 0) && (bmap[1].ButtonNum >= 0) )
{
int s,lp;
s = state[0] && state[1];
lp = state_lp[0] && state_lp[1];
if ( s && !lp )
{
sendKeyPressEvent(0);
}
else if ( !s && lp )
{
sendKeyReleaseEvent(0);
sendKeyPressEvent(1);
sendKeyReleaseEvent(1);
}
}
else if ( bmap[1].ButtonNum >= 0 )
{
if ( state[1] && !state_lp[1] )
{
sendKeyPressEvent(0);
}
else if ( !state[1] && state_lp[1] )
{
sendKeyReleaseEvent(0);
sendKeyPressEvent(1);
sendKeyReleaseEvent(1);
}
}
}
/***
* This function is a wrapper for FCEUI_ToggleEmulationPause that handles
* releasing/capturing mouse pointer during pause toggles
@ -1393,6 +1446,16 @@ static int32 MouseRelative[3] = { 0, 0, 0 };
static uint8 fkbkeys[0x48];
static void updateGamePadKeyMappings(void)
{
std::list <gamepad_function_key_t*>::iterator it;
for (it=gpKeySeqList.begin(); it!=gpKeySeqList.end(); it++)
{
(*it)->updateStatus();
}
}
/**
* Update all of the input devices required for the active game.
*/
@ -1401,10 +1464,13 @@ void FCEUD_UpdateInput(void)
int x;
int t = 0;
if ( buttonConfigInProgress )
{
return;
}
if ( buttonConfigInProgress )
{
return;
}
updateGamePadKeyMappings();
UpdatePhysicalInput ();
KeyboardCommands ();

View File

@ -54,16 +54,22 @@ extern struct hotkey_t Hotkeys[];
struct gamepad_function_key_t
{
int qKey;
unsigned int qModifier;
struct {
int key;
unsigned int modifier;
std::string name;
} keySeq[2];
struct ButtConfig bmap[2];
gamepad_function_key_t(void);
~gamepad_function_key_t(void);
void sendKeyPressEvent(void);
void sendKeyReleaseEvent(void);
void sendKeyPressEvent(int idx);
void sendKeyReleaseEvent(int idx);
void updateStatus(void);
};
extern std::list <gamepad_function_key_t*> gpKeySeqList;