all repos — mgba @ d15c4f4bfb2ee7fcb920a391812b72e746353208

mGBA Game Boy Advance Emulator

Qt: Refactor out gamepad monitoring code into a new class
Jeffrey Pfau jeffrey@endrift.com
Sun, 04 Jan 2015 01:14:36 -0800
commit

d15c4f4bfb2ee7fcb920a391812b72e746353208

parent

0ce8ca36fa2032db2b2f8bb3f9550960ab198c27

M src/platform/qt/CMakeLists.txtsrc/platform/qt/CMakeLists.txt

@@ -44,6 +44,7 @@ GBAKeyEditor.cpp

GIFView.cpp GameController.cpp GamePakView.cpp + GamepadMonitor.cpp InputController.cpp KeyEditor.cpp LoadSaveState.cpp
M src/platform/qt/GBAKeyEditor.cppsrc/platform/qt/GBAKeyEditor.cpp

@@ -8,10 +8,10 @@

#include <QPaintEvent> #include <QPainter> #include <QPushButton> -#include <QTimer> #include <QVBoxLayout> #include "InputController.h" +#include "GamepadMonitor.h" #include "KeyEditor.h" using namespace QGBA;

@@ -25,7 +25,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* parent)

: QWidget(parent) , m_type(type) , m_controller(controller) - , m_gamepadTimer(nullptr) + , m_gamepadMonitor(nullptr) { setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint); setMinimumSize(300, 300);

@@ -114,10 +114,9 @@ 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(50); - m_gamepadTimer->start(); + m_gamepadMonitor = new GamepadMonitor(m_controller, this); + connect(m_gamepadMonitor, SIGNAL(buttonPressed(int)), this, SLOT(setButton(int))); + connect(m_gamepadMonitor, SIGNAL(axisChanged(int, int32_t)), this, SLOT(setAxisValue(int, int32_t))); } #endif }

@@ -234,30 +233,20 @@ return false;

} #ifdef BUILD_SDL -void GBAKeyEditor::testGamepad() { - m_gamepadTimer->setInterval(50); +void GBAKeyEditor::setAxisValue(int axis, int32_t value) { if (!findFocus()) { return; } KeyEditor* focused = *m_currentKey; + focused->setValueAxis(axis, value); +} - auto activeAxes = m_controller->activeGamepadAxes(); - auto oldAxes = m_activeAxes; - m_activeAxes = activeAxes; - activeAxes.subtract(oldAxes); - if (!activeAxes.empty()) { - focused->setValueAxis(activeAxes.begin()->first, activeAxes.begin()->second); +void GBAKeyEditor::setButton(int button) { + if (!findFocus()) { return; } - - auto activeButtons = m_controller->activeGamepadButtons(); - auto oldButtons = m_activeButtons; - m_activeButtons = activeButtons; - activeButtons.subtract(oldButtons); - if (!activeButtons.empty()) { - focused->setValueButton(*activeButtons.begin()); - return; - } + KeyEditor* focused = *m_currentKey; + focused->setValueButton(button); } #endif
M src/platform/qt/GBAKeyEditor.hsrc/platform/qt/GBAKeyEditor.h

@@ -20,6 +20,7 @@

namespace QGBA { class InputController; +class GamepadMonitor; class KeyEditor; class GBAKeyEditor : public QWidget {

@@ -39,7 +40,8 @@ private slots:

void setNext(); void save(); #ifdef BUILD_SDL - void testGamepad(); + void setAxisValue(int axis, int32_t value); + void setButton(int button); #endif private:

@@ -75,11 +77,7 @@ KeyEditor* m_keyR;

QList<KeyEditor*> m_keyOrder; QList<KeyEditor*>::iterator m_currentKey; -#ifdef BUILD_SDL - QSet<int> m_activeButtons; - QSet<QPair<int, int32_t>> m_activeAxes; - QTimer* m_gamepadTimer; -#endif + GamepadMonitor* m_gamepadMonitor; uint32_t m_type; InputController* m_controller;
A src/platform/qt/GamepadMonitor.cpp

@@ -0,0 +1,46 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "GamepadMonitor.h" + +#include "InputController.h" + +#include <QTimer> + +using namespace QGBA; + +GamepadMonitor::GamepadMonitor(InputController* controller, QObject* parent) + : QObject(parent) + , m_controller(controller) +{ +#ifdef BUILD_SDL + m_gamepadTimer = new QTimer(this); + connect(m_gamepadTimer, SIGNAL(timeout()), this, SLOT(testGamepad())); + m_gamepadTimer->setInterval(50); + m_gamepadTimer->start(); +#endif +} + +void GamepadMonitor::testGamepad() { +#ifdef BUILD_SDL + m_gamepadTimer->setInterval(50); + + auto activeAxes = m_controller->activeGamepadAxes(); + auto oldAxes = m_activeAxes; + m_activeAxes = activeAxes; + activeAxes.subtract(oldAxes); + if (!activeAxes.empty()) { + emit axisChanged(activeAxes.begin()->first, activeAxes.begin()->second); + } + + auto activeButtons = m_controller->activeGamepadButtons(); + auto oldButtons = m_activeButtons; + m_activeButtons = activeButtons; + activeButtons.subtract(oldButtons); + if (!activeButtons.empty()) { + emit buttonPressed(*activeButtons.begin()); + } +#endif +}
A src/platform/qt/GamepadMonitor.h

@@ -0,0 +1,40 @@

+/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef QGBA_GAMEPAD_MONITOR +#define QGBA_GAMEPAD_MONITOR + +#include <QObject> +#include <QSet> + +class QTimer; + +namespace QGBA { + +class InputController; + +class GamepadMonitor : public QObject { +Q_OBJECT + +public: + GamepadMonitor(InputController* controller, QObject* parent = nullptr); + +signals: + void axisChanged(int axis, int32_t value); + void buttonPressed(int button); + +public slots: + void testGamepad(); + +private: + InputController* m_controller; + QSet<int> m_activeButtons; + QSet<QPair<int, int32_t>> m_activeAxes; + QTimer* m_gamepadTimer; +}; + +} + +#endif