Added keybind customization system
This commit is contained in:
@@ -16,8 +16,11 @@
|
||||
*/
|
||||
#include "qt_settingsinput.hpp"
|
||||
#include "ui_qt_settingsinput.h"
|
||||
#include "qt_mainwindow.hpp"
|
||||
|
||||
#include <QDebug>
|
||||
#include <QKeySequence>
|
||||
#include <string>
|
||||
|
||||
extern "C" {
|
||||
#include <86box/86box.h>
|
||||
@@ -25,11 +28,18 @@ extern "C" {
|
||||
#include <86box/machine.h>
|
||||
#include <86box/mouse.h>
|
||||
#include <86box/gameport.h>
|
||||
#include <86box/ui.h>
|
||||
}
|
||||
|
||||
#include "qt_models_common.hpp"
|
||||
#include "qt_deviceconfig.hpp"
|
||||
#include "qt_joystickconfiguration.hpp"
|
||||
#include "qt_keybind.hpp"
|
||||
|
||||
extern MainWindow *main_window;
|
||||
|
||||
// Temporary working copy of key list
|
||||
accelKey acc_keys_t[NUM_ACCELS];
|
||||
|
||||
SettingsInput::SettingsInput(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
@@ -37,9 +47,55 @@ SettingsInput::SettingsInput(QWidget *parent)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
QStandardItemModel *model;
|
||||
QStringList horizontalHeader;
|
||||
QStringList verticalHeader;
|
||||
|
||||
horizontalHeader.append("Action");
|
||||
horizontalHeader.append("Keybind");
|
||||
|
||||
QTableWidget *keyTable = ui->tableKeys;
|
||||
keyTable->setRowCount(10);
|
||||
keyTable->setColumnCount(3);
|
||||
keyTable->setColumnHidden(2, true);
|
||||
keyTable->setColumnWidth(0, 200);
|
||||
keyTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||||
QStringList headers;
|
||||
headers << "Action" << "Bound key";
|
||||
keyTable->setHorizontalHeaderLabels(headers);
|
||||
keyTable->verticalHeader()->setVisible(false);
|
||||
keyTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
||||
keyTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||||
keyTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
||||
keyTable->setShowGrid(true);
|
||||
keyTable->setStyleSheet("QTableWidget::item:hover { }");
|
||||
keyTable->setFocusPolicy(Qt::NoFocus);
|
||||
keyTable->setSelectionMode(QAbstractItemView::NoSelection);
|
||||
|
||||
// Make a working copy of acc_keys so we can check for dupes later without getting
|
||||
// confused
|
||||
printf("Instantiating list\n");
|
||||
for(int x=0;x<NUM_ACCELS;x++) {
|
||||
strcpy(acc_keys_t[x].name, acc_keys[x].name);
|
||||
strcpy(acc_keys_t[x].desc, acc_keys[x].desc);
|
||||
strcpy(acc_keys_t[x].seq, acc_keys[x].seq);
|
||||
}
|
||||
|
||||
refreshInputList();
|
||||
|
||||
connect(ui->tableKeys, &QTableWidget::cellDoubleClicked,
|
||||
this, &SettingsInput::on_tableKeys_doubleClicked);
|
||||
|
||||
connect(ui->pushButtonBind, &QPushButton::clicked,
|
||||
this, &SettingsInput::on_pushButtonBind_Clicked);
|
||||
|
||||
connect(ui->pushButtonClearBind, &QPushButton::clicked,
|
||||
this, &SettingsInput::on_pushButtonClearBind_Clicked);
|
||||
|
||||
onCurrentMachineChanged(machine);
|
||||
}
|
||||
|
||||
|
||||
SettingsInput::~SettingsInput()
|
||||
{
|
||||
delete ui;
|
||||
@@ -50,8 +106,15 @@ SettingsInput::save()
|
||||
{
|
||||
mouse_type = ui->comboBoxMouse->currentData().toInt();
|
||||
joystick_type = ui->comboBoxJoystick->currentData().toInt();
|
||||
|
||||
// Copy accelerators from working set to global set
|
||||
for(int x=0;x<NUM_ACCELS;x++) {
|
||||
strcpy(acc_keys[x].name, acc_keys_t[x].name);
|
||||
strcpy(acc_keys[x].desc, acc_keys_t[x].desc);
|
||||
strcpy(acc_keys[x].seq, acc_keys_t[x].seq);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
SettingsInput::onCurrentMachineChanged(int machineId)
|
||||
{
|
||||
@@ -105,6 +168,100 @@ SettingsInput::onCurrentMachineChanged(int machineId)
|
||||
ui->comboBoxJoystick->setCurrentIndex(selectedRow);
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::refreshInputList()
|
||||
{
|
||||
|
||||
for (int x=0;x<NUM_ACCELS;x++) {
|
||||
ui->tableKeys->setItem(x, 0, new QTableWidgetItem(acc_keys_t[x].desc));
|
||||
ui->tableKeys->setItem(x, 1, new QTableWidgetItem(acc_keys_t[x].seq));
|
||||
ui->tableKeys->setItem(x, 2, new QTableWidgetItem(acc_keys_t[x].name));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::on_tableKeys_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn)
|
||||
{
|
||||
// Enable/disable bind/clear buttons if user clicked valid row
|
||||
QTableWidgetItem *cell = ui->tableKeys->item(currentRow,1);
|
||||
if (!cell)
|
||||
{
|
||||
ui->pushButtonBind->setEnabled(false);
|
||||
ui->pushButtonClearBind->setEnabled(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
ui->pushButtonBind->setEnabled(true);
|
||||
ui->pushButtonClearBind->setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::on_tableKeys_doubleClicked(int row, int col)
|
||||
{
|
||||
// Edit bind
|
||||
QTableWidgetItem *cell = ui->tableKeys->item(row,1);
|
||||
if (!cell) return;
|
||||
|
||||
QKeySequence keyseq = KeyBinder::BindKey(cell->text());
|
||||
if (keyseq != false) {
|
||||
// If no change was made, don't change anything.
|
||||
if (keyseq.toString(QKeySequence::NativeText) == cell->text()) return;
|
||||
|
||||
// Otherwise, check for conflicts.
|
||||
// Check against the *working* copy - NOT the one in use by the app,
|
||||
// so we don't test against shortcuts the user already changed.
|
||||
for(int x=0;x<NUM_ACCELS;x++)
|
||||
{
|
||||
if(QString::fromStdString(acc_keys_t[x].seq) == keyseq.toString(QKeySequence::NativeText))
|
||||
{
|
||||
// That key is already in use
|
||||
main_window->showMessage(MBX_ANSI & MBX_INFO, "Bind conflict", "This key combo is already in use", false);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// If we made it here, there were no conflicts.
|
||||
// Go ahead and apply the bind.
|
||||
|
||||
// Find the correct accelerator key entry
|
||||
int accKeyID = FindAccelerator(qPrintable(ui->tableKeys->item(row,2)->text()));
|
||||
if (!accKeyID) return; // this should never happen
|
||||
|
||||
// Make the change
|
||||
cell->setText(keyseq.toString(QKeySequence::NativeText));
|
||||
strcpy(acc_keys_t[accKeyID].seq, qPrintable(keyseq.toString(QKeySequence::NativeText)));
|
||||
|
||||
refreshInputList();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::on_pushButtonBind_Clicked()
|
||||
{
|
||||
// Edit bind
|
||||
QTableWidgetItem *cell = ui->tableKeys->currentItem();
|
||||
if (!cell) return;
|
||||
|
||||
on_tableKeys_doubleClicked(cell->row(), cell->column());
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::on_pushButtonClearBind_Clicked()
|
||||
{
|
||||
// Wipe bind
|
||||
QTableWidgetItem *cell = ui->tableKeys->currentItem();
|
||||
if (!cell) return;
|
||||
|
||||
cell->setText("");
|
||||
// Find the correct accelerator key entry
|
||||
int accKeyID = FindAccelerator(qPrintable(ui->tableKeys->item(cell->row(),2)->text()));
|
||||
if (!accKeyID) return; // this should never happen
|
||||
|
||||
// Make the change
|
||||
cell->setText("");
|
||||
strcpy(acc_keys_t[accKeyID].seq, "");
|
||||
}
|
||||
|
||||
void
|
||||
SettingsInput::on_comboBoxMouse_currentIndexChanged(int index)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user