Qt: Rudimentary gamepad mapper

This commit is contained in:
Jeffrey Pfau 2014-12-03 00:07:56 -08:00
parent 091e717133
commit e6ea94d229
8 changed files with 97 additions and 2 deletions

View File

@ -3,6 +3,7 @@
#include <QPaintEvent>
#include <QPainter>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#include "InputController.h"
@ -39,6 +40,22 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren
m_keyB = new KeyEditor(this);
m_keyL = new KeyEditor(this);
m_keyR = new KeyEditor(this);
#ifdef BUILD_SDL
if (type == SDL_BINDING_BUTTON) {
m_keyDU->setNumeric(true);
m_keyDD->setNumeric(true);
m_keyDL->setNumeric(true);
m_keyDR->setNumeric(true);
m_keySelect->setNumeric(true);
m_keyStart->setNumeric(true);
m_keyA->setNumeric(true);
m_keyB->setNumeric(true);
m_keyL->setNumeric(true);
m_keyR->setNumeric(true);
}
#endif
m_keyDU->setValue(GBAInputQueryBinding(map, type, GBA_KEY_UP));
m_keyDD->setValue(GBAInputQueryBinding(map, type, GBA_KEY_DOWN));
m_keyDL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_LEFT));
@ -92,6 +109,15 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren
m_background.load(":/res/keymap.qpic");
setAll->setFocus();
#ifdef BUILD_SDL
if (type == SDL_BINDING_BUTTON) {
m_gamepadTimer = new QTimer(this);
connect(m_gamepadTimer, SIGNAL(timeout()), this, SLOT(testGamepad()));
m_gamepadTimer->setInterval(100);
m_gamepadTimer->start();
}
#endif
}
void GBAKeyEditor::setAll() {
@ -150,6 +176,21 @@ void GBAKeyEditor::save() {
m_controller->saveConfiguration(m_type);
}
#ifdef BUILD_SDL
void GBAKeyEditor::testGamepad() {
QSet<int> activeKeys = m_controller->activeGamepadButtons();
if (activeKeys.empty()) {
return;
}
for (KeyEditor* key : m_keyOrder) {
if (!key->hasFocus()) {
continue;
}
key->setValue(*activeKeys.begin());
}
}
#endif
void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
QSize s = size();
QSize hint = widget->sizeHint();

View File

@ -26,6 +26,9 @@ protected:
private slots:
void setNext();
void save();
#ifdef BUILD_SDL
void testGamepad();
#endif
private:
static const qreal DPAD_CENTER_X;
@ -35,6 +38,11 @@ private:
void setLocation(QWidget* widget, qreal x, qreal y);
#ifdef BUILD_SDL
QTimer* m_gamepadTimer;
#endif
QWidget* m_buttons;
KeyEditor* m_keyDU;
KeyEditor* m_keyDD;

View File

@ -97,4 +97,18 @@ int InputController::testSDLEvents() {
}
return activeButtons;
}
QSet<int> InputController::activeGamepadButtons() {
SDL_Joystick* joystick = m_sdlEvents.joystick;
SDL_JoystickUpdate();
int numButtons = SDL_JoystickNumButtons(joystick);
QSet<int> activeButtons;
int i;
for (i = 0; i < numButtons; ++i) {
if (SDL_JoystickGetButton(joystick, i)) {
activeButtons.insert(i);
}
}
return activeButtons;
}
#endif

View File

@ -9,6 +9,8 @@ extern "C" {
#endif
}
#include <QSet>
namespace QGBA {
class ConfigController;
@ -32,6 +34,7 @@ public:
#ifdef BUILD_SDL
int testSDLEvents();
QSet<int> activeGamepadButtons();
#endif
private:

View File

@ -6,12 +6,17 @@ using namespace QGBA;
KeyEditor::KeyEditor(QWidget* parent)
: QLineEdit(parent)
, m_numeric(false)
{
setAlignment(Qt::AlignCenter);
}
void KeyEditor::setValue(int key) {
if (m_numeric) {
setText(QString::number(key));
} else {
setText(QKeySequence(key).toString(QKeySequence::NativeText));
}
m_key = key;
emit valueChanged(key);
}
@ -23,6 +28,8 @@ QSize KeyEditor::sizeHint() const {
}
void KeyEditor::keyPressEvent(QKeyEvent* event) {
if (!m_numeric) {
setValue(event->key());
}
event->accept();
}

View File

@ -14,6 +14,8 @@ public:
void setValue(int key);
int value() const { return m_key; }
void setNumeric(bool numeric) { m_numeric = numeric; }
virtual QSize sizeHint() const override;
signals:
@ -24,6 +26,7 @@ protected:
private:
int m_key;
bool m_numeric;
};
}

View File

@ -173,6 +173,15 @@ void Window::openKeymapWindow() {
keyEditor->show();
}
#ifdef BUILD_SDL
void Window::openGamepadWindow() {
GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON);
connect(this, SIGNAL(shutdown()), keyEditor, SLOT(close()));
keyEditor->setAttribute(Qt::WA_DeleteOnClose);
keyEditor->show();
}
#endif
#ifdef USE_FFMPEG
void Window::openVideoWindow() {
if (!m_videoView) {
@ -454,6 +463,12 @@ void Window::setupMenu(QMenuBar* menubar) {
connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
emulationMenu->addAction(keymap);
#ifdef BUILD_SDL
QAction* gamepad = new QAction(tr("Remap gamepad..."), emulationMenu);
connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
emulationMenu->addAction(gamepad);
#endif
QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
QAction* setSize = new QAction(tr("1x"), avMenu);

View File

@ -56,6 +56,10 @@ public slots:
void openKeymapWindow();
#ifdef BUILD_SDL
void openGamepadWindow();
#endif
#ifdef USE_FFMPEG
void openVideoWindow();
#endif