Qt: Refactor out gamepad monitoring code into a new class

This commit is contained in:
Jeffrey Pfau 2015-01-04 01:14:36 -08:00
parent 0ce8ca36fa
commit d15c4f4bfb
5 changed files with 103 additions and 29 deletions

View File

@ -44,6 +44,7 @@ set(SOURCE_FILES
GIFView.cpp GIFView.cpp
GameController.cpp GameController.cpp
GamePakView.cpp GamePakView.cpp
GamepadMonitor.cpp
InputController.cpp InputController.cpp
KeyEditor.cpp KeyEditor.cpp
LoadSaveState.cpp LoadSaveState.cpp

View File

@ -8,10 +8,10 @@
#include <QPaintEvent> #include <QPaintEvent>
#include <QPainter> #include <QPainter>
#include <QPushButton> #include <QPushButton>
#include <QTimer>
#include <QVBoxLayout> #include <QVBoxLayout>
#include "InputController.h" #include "InputController.h"
#include "GamepadMonitor.h"
#include "KeyEditor.h" #include "KeyEditor.h"
using namespace QGBA; using namespace QGBA;
@ -25,7 +25,7 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren
: QWidget(parent) : QWidget(parent)
, m_type(type) , m_type(type)
, m_controller(controller) , m_controller(controller)
, m_gamepadTimer(nullptr) , m_gamepadMonitor(nullptr)
{ {
setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
setMinimumSize(300, 300); setMinimumSize(300, 300);
@ -114,10 +114,9 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren
#ifdef BUILD_SDL #ifdef BUILD_SDL
if (type == SDL_BINDING_BUTTON) { if (type == SDL_BINDING_BUTTON) {
m_gamepadTimer = new QTimer(this); m_gamepadMonitor = new GamepadMonitor(m_controller, this);
connect(m_gamepadTimer, SIGNAL(timeout()), this, SLOT(testGamepad())); connect(m_gamepadMonitor, SIGNAL(buttonPressed(int)), this, SLOT(setButton(int)));
m_gamepadTimer->setInterval(50); connect(m_gamepadMonitor, SIGNAL(axisChanged(int, int32_t)), this, SLOT(setAxisValue(int, int32_t)));
m_gamepadTimer->start();
} }
#endif #endif
} }
@ -234,30 +233,20 @@ bool GBAKeyEditor::findFocus() {
} }
#ifdef BUILD_SDL #ifdef BUILD_SDL
void GBAKeyEditor::testGamepad() { void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
m_gamepadTimer->setInterval(50);
if (!findFocus()) { if (!findFocus()) {
return; return;
} }
KeyEditor* focused = *m_currentKey; KeyEditor* focused = *m_currentKey;
focused->setValueAxis(axis, value);
}
auto activeAxes = m_controller->activeGamepadAxes(); void GBAKeyEditor::setButton(int button) {
auto oldAxes = m_activeAxes; if (!findFocus()) {
m_activeAxes = activeAxes;
activeAxes.subtract(oldAxes);
if (!activeAxes.empty()) {
focused->setValueAxis(activeAxes.begin()->first, activeAxes.begin()->second);
return;
}
auto activeButtons = m_controller->activeGamepadButtons();
auto oldButtons = m_activeButtons;
m_activeButtons = activeButtons;
activeButtons.subtract(oldButtons);
if (!activeButtons.empty()) {
focused->setValueButton(*activeButtons.begin());
return; return;
} }
KeyEditor* focused = *m_currentKey;
focused->setValueButton(button);
} }
#endif #endif

View File

@ -20,6 +20,7 @@ class QTimer;
namespace QGBA { namespace QGBA {
class InputController; class InputController;
class GamepadMonitor;
class KeyEditor; class KeyEditor;
class GBAKeyEditor : public QWidget { class GBAKeyEditor : public QWidget {
@ -39,7 +40,8 @@ private slots:
void setNext(); void setNext();
void save(); void save();
#ifdef BUILD_SDL #ifdef BUILD_SDL
void testGamepad(); void setAxisValue(int axis, int32_t value);
void setButton(int button);
#endif #endif
private: private:
@ -75,11 +77,7 @@ private:
QList<KeyEditor*> m_keyOrder; QList<KeyEditor*> m_keyOrder;
QList<KeyEditor*>::iterator m_currentKey; QList<KeyEditor*>::iterator m_currentKey;
#ifdef BUILD_SDL GamepadMonitor* m_gamepadMonitor;
QSet<int> m_activeButtons;
QSet<QPair<int, int32_t>> m_activeAxes;
QTimer* m_gamepadTimer;
#endif
uint32_t m_type; uint32_t m_type;
InputController* m_controller; InputController* m_controller;

View File

@ -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
}

View File

@ -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