Key input
Jeffrey Pfau jeffrey@endrift.com
Wed, 29 Jan 2014 22:21:25 -0800
5 files changed,
93 insertions(+),
1 deletions(-)
M
src/gba/gba.h
→
src/gba/gba.h
@@ -54,7 +54,8 @@ GBA_KEY_LEFT = 5,
GBA_KEY_UP = 6, GBA_KEY_DOWN = 7, GBA_KEY_R = 8, - GBA_KEY_L = 9 + GBA_KEY_L = 9, + GBA_KEY_NONE = -1 }; struct GBARotationSource;
M
src/platform/qt/GameController.cpp
→
src/platform/qt/GameController.cpp
@@ -53,3 +53,13 @@ m_threadContext.fname = path.toLocal8Bit().constData();
GBAThreadStart(&m_threadContext); return true; } + +void GameController::keyPressed(int key) { + int mappedKey = 1 << key; + m_threadContext.activeKeys |= mappedKey; +} + +void GameController::keyReleased(int key) { + int mappedKey = 1 << key; + m_threadContext.activeKeys &= ~mappedKey; +}
M
src/platform/qt/GameController.h
→
src/platform/qt/GameController.h
@@ -30,6 +30,8 @@ void audioDeviceAvailable(GBAAudio*);
public slots: bool loadGame(const QString& path); + void keyPressed(int key); + void keyReleased(int key); private: void setupAudio(GBAAudio* audio);
M
src/platform/qt/Window.cpp
→
src/platform/qt/Window.cpp
@@ -1,6 +1,11 @@
#include "Window.h" #include <QFileDialog> +#include <QKeyEvent> + +extern "C" { +#include "gba.h" +} using namespace QGBA;@@ -16,11 +21,76 @@
connect(actionOpen, SIGNAL(triggered()), this, SLOT(selectROM())); } +GBAKey Window::mapKey(int qtKey) { + switch (qtKey) { + case Qt::Key_Z: + return GBA_KEY_A; + break; + case Qt::Key_X: + return GBA_KEY_B; + break; + case Qt::Key_A: + return GBA_KEY_L; + break; + case Qt::Key_S: + return GBA_KEY_R; + break; + case Qt::Key_Return: + return GBA_KEY_START; + break; + case Qt::Key_Backspace: + return GBA_KEY_SELECT; + break; + case Qt::Key_Up: + return GBA_KEY_UP; + break; + case Qt::Key_Down: + return GBA_KEY_DOWN; + break; + case Qt::Key_Left: + return GBA_KEY_LEFT; + break; + case Qt::Key_Right: + return GBA_KEY_RIGHT; + break; + default: + return GBA_KEY_NONE; + } +} + void Window::selectROM() { QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM")); if (!filename.isEmpty()) { m_controller->loadGame(filename); } +} + +void Window::keyPressEvent(QKeyEvent* event) { + if (event->isAutoRepeat()) { + QWidget::keyPressEvent(event); + return; + } + GBAKey key = mapKey(event->key()); + if (key == GBA_KEY_NONE) { + QWidget::keyPressEvent(event); + return; + } + m_controller->keyPressed(key); + event->accept(); +} + +void Window::keyReleaseEvent(QKeyEvent* event) { + if (event->isAutoRepeat()) { + QWidget::keyReleaseEvent(event); + return; + } + GBAKey key = mapKey(event->key()); + if (key == GBA_KEY_NONE) { + QWidget::keyPressEvent(event); + return; + } + m_controller->keyReleased(key); + event->accept(); } void Window::setupAudio(GBAAudio* audio) {
M
src/platform/qt/Window.h
→
src/platform/qt/Window.h
@@ -4,6 +4,10 @@
#include <QAudioOutput> #include <QMainWindow> +extern "C" { +#include "gba.h" +} + #include "GameController.h" #include "Display.h"@@ -16,9 +20,14 @@ Q_OBJECT
public: Window(QWidget* parent = 0); + static GBAKey mapKey(int qtKey); public slots: void selectROM(); + +protected: + virtual void keyPressEvent(QKeyEvent* event); + virtual void keyReleaseEvent(QKeyEvent* event); private slots: void setupAudio(GBAAudio*);