From e6ea94d2296eae963a48a18d009217a38d92bf9b Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 3 Dec 2014 00:07:56 -0800 Subject: [PATCH] Qt: Rudimentary gamepad mapper --- src/platform/qt/GBAKeyEditor.cpp | 41 +++++++++++++++++++++++++++++ src/platform/qt/GBAKeyEditor.h | 8 ++++++ src/platform/qt/InputController.cpp | 14 ++++++++++ src/platform/qt/InputController.h | 3 +++ src/platform/qt/KeyEditor.cpp | 11 ++++++-- src/platform/qt/KeyEditor.h | 3 +++ src/platform/qt/Window.cpp | 15 +++++++++++ src/platform/qt/Window.h | 4 +++ 8 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/platform/qt/GBAKeyEditor.cpp b/src/platform/qt/GBAKeyEditor.cpp index 759b731c8..9bac73778 100644 --- a/src/platform/qt/GBAKeyEditor.cpp +++ b/src/platform/qt/GBAKeyEditor.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #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 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(); diff --git a/src/platform/qt/GBAKeyEditor.h b/src/platform/qt/GBAKeyEditor.h index 1430d379d..ee9738e32 100644 --- a/src/platform/qt/GBAKeyEditor.h +++ b/src/platform/qt/GBAKeyEditor.h @@ -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; diff --git a/src/platform/qt/InputController.cpp b/src/platform/qt/InputController.cpp index 576408d82..7108018f1 100644 --- a/src/platform/qt/InputController.cpp +++ b/src/platform/qt/InputController.cpp @@ -97,4 +97,18 @@ int InputController::testSDLEvents() { } return activeButtons; } + +QSet InputController::activeGamepadButtons() { + SDL_Joystick* joystick = m_sdlEvents.joystick; + SDL_JoystickUpdate(); + int numButtons = SDL_JoystickNumButtons(joystick); + QSet activeButtons; + int i; + for (i = 0; i < numButtons; ++i) { + if (SDL_JoystickGetButton(joystick, i)) { + activeButtons.insert(i); + } + } + return activeButtons; +} #endif diff --git a/src/platform/qt/InputController.h b/src/platform/qt/InputController.h index c092f463b..12bfd672e 100644 --- a/src/platform/qt/InputController.h +++ b/src/platform/qt/InputController.h @@ -9,6 +9,8 @@ extern "C" { #endif } +#include + namespace QGBA { class ConfigController; @@ -32,6 +34,7 @@ public: #ifdef BUILD_SDL int testSDLEvents(); + QSet activeGamepadButtons(); #endif private: diff --git a/src/platform/qt/KeyEditor.cpp b/src/platform/qt/KeyEditor.cpp index 958ce69e1..be37729ad 100644 --- a/src/platform/qt/KeyEditor.cpp +++ b/src/platform/qt/KeyEditor.cpp @@ -6,12 +6,17 @@ using namespace QGBA; KeyEditor::KeyEditor(QWidget* parent) : QLineEdit(parent) + , m_numeric(false) { setAlignment(Qt::AlignCenter); } void KeyEditor::setValue(int key) { - setText(QKeySequence(key).toString(QKeySequence::NativeText)); + 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) { - setValue(event->key()); + if (!m_numeric) { + setValue(event->key()); + } event->accept(); } diff --git a/src/platform/qt/KeyEditor.h b/src/platform/qt/KeyEditor.h index 362bcd6ab..8132d5139 100644 --- a/src/platform/qt/KeyEditor.h +++ b/src/platform/qt/KeyEditor.h @@ -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; }; } diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index de0572f40..5c8e946ee 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -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); diff --git a/src/platform/qt/Window.h b/src/platform/qt/Window.h index 40608064d..100a87538 100644 --- a/src/platform/qt/Window.h +++ b/src/platform/qt/Window.h @@ -56,6 +56,10 @@ public slots: void openKeymapWindow(); +#ifdef BUILD_SDL + void openGamepadWindow(); +#endif + #ifdef USE_FFMPEG void openVideoWindow(); #endif