Qt: Rudimentary gamepad mapper
Jeffrey Pfau jeffrey@endrift.com
Wed, 03 Dec 2014 00:07:56 -0800
8 files changed,
97 insertions(+),
2 deletions(-)
M
src/platform/qt/GBAKeyEditor.cpp
→
src/platform/qt/GBAKeyEditor.cpp
@@ -3,6 +3,7 @@
#include <QPaintEvent> #include <QPainter> #include <QPushButton> +#include <QTimer> #include <QVBoxLayout> #include "InputController.h"@@ -39,6 +40,22 @@ m_keyA = new KeyEditor(this);
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 @@
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() {@@ -149,6 +175,21 @@ m_controller->bindKey(m_type, m_keyL->value(), GBA_KEY_L);
m_controller->bindKey(m_type, m_keyR->value(), GBA_KEY_R); 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();
M
src/platform/qt/GBAKeyEditor.h
→
src/platform/qt/GBAKeyEditor.h
@@ -26,6 +26,9 @@
private slots: void setNext(); void save(); +#ifdef BUILD_SDL + void testGamepad(); +#endif private: static const qreal DPAD_CENTER_X;@@ -34,6 +37,11 @@ static const qreal DPAD_WIDTH;
static const qreal DPAD_HEIGHT; void setLocation(QWidget* widget, qreal x, qreal y); + + +#ifdef BUILD_SDL + QTimer* m_gamepadTimer; +#endif QWidget* m_buttons; KeyEditor* m_keyDU;
M
src/platform/qt/InputController.cpp
→
src/platform/qt/InputController.cpp
@@ -97,4 +97,18 @@ }
} 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
M
src/platform/qt/InputController.h
→
src/platform/qt/InputController.h
@@ -9,6 +9,8 @@ #include "platform/sdl/sdl-events.h"
#endif } +#include <QSet> + namespace QGBA { class ConfigController;@@ -32,6 +34,7 @@ const GBAInputMap* map() const { return &m_inputMap; }
#ifdef BUILD_SDL int testSDLEvents(); + QSet<int> activeGamepadButtons(); #endif private:
M
src/platform/qt/KeyEditor.cpp
→
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 @@ return hint;
} void KeyEditor::keyPressEvent(QKeyEvent* event) { - setValue(event->key()); + if (!m_numeric) { + setValue(event->key()); + } event->accept(); }
M
src/platform/qt/KeyEditor.h
→
src/platform/qt/KeyEditor.h
@@ -14,6 +14,8 @@
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 @@ virtual void keyPressEvent(QKeyEvent* event) override;
private: int m_key; + bool m_numeric; }; }
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -173,6 +173,15 @@ keyEditor->setAttribute(Qt::WA_DeleteOnClose);
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) {@@ -453,6 +462,12 @@ emulationMenu->addSeparator();
QAction* keymap = new QAction(tr("Remap keyboard..."), emulationMenu); 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"));
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -56,6 +56,10 @@ void saveConfig();
void openKeymapWindow(); +#ifdef BUILD_SDL + void openGamepadWindow(); +#endif + #ifdef USE_FFMPEG void openVideoWindow(); #endif