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
GameController.cpp
GamePakView.cpp
GamepadMonitor.cpp
InputController.cpp
KeyEditor.cpp
LoadSaveState.cpp

View File

@ -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* paren
: 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 @@ GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* paren
#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 @@ bool GBAKeyEditor::findFocus() {
}
#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);
return;
}
auto activeButtons = m_controller->activeGamepadButtons();
auto oldButtons = m_activeButtons;
m_activeButtons = activeButtons;
activeButtons.subtract(oldButtons);
if (!activeButtons.empty()) {
focused->setValueButton(*activeButtons.begin());
void GBAKeyEditor::setButton(int button) {
if (!findFocus()) {
return;
}
KeyEditor* focused = *m_currentKey;
focused->setValueButton(button);
}
#endif

View File

@ -20,6 +20,7 @@ class QTimer;
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 @@ private:
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;

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