From 3d339b13279bea84a6fe86b731933c1466396f83 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 29 Jan 2014 22:21:25 -0800 Subject: [PATCH] Key input --- src/gba/gba.h | 3 +- src/platform/qt/GameController.cpp | 10 +++++ src/platform/qt/GameController.h | 2 + src/platform/qt/Window.cpp | 70 ++++++++++++++++++++++++++++++ src/platform/qt/Window.h | 9 ++++ 5 files changed, 93 insertions(+), 1 deletion(-) diff --git a/src/gba/gba.h b/src/gba/gba.h index 3fa6bfe45..0ddf4ad33 100644 --- a/src/gba/gba.h +++ b/src/gba/gba.h @@ -54,7 +54,8 @@ enum GBAKey { 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; diff --git a/src/platform/qt/GameController.cpp b/src/platform/qt/GameController.cpp index 3f5c637f3..12178d98a 100644 --- a/src/platform/qt/GameController.cpp +++ b/src/platform/qt/GameController.cpp @@ -53,3 +53,13 @@ bool GameController::loadGame(const QString& path) { 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; +} diff --git a/src/platform/qt/GameController.h b/src/platform/qt/GameController.h index dfd953f87..73d675c37 100644 --- a/src/platform/qt/GameController.h +++ b/src/platform/qt/GameController.h @@ -30,6 +30,8 @@ signals: public slots: bool loadGame(const QString& path); + void keyPressed(int key); + void keyReleased(int key); private: void setupAudio(GBAAudio* audio); diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index e12533966..5bf0b12a9 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -1,6 +1,11 @@ #include "Window.h" #include +#include + +extern "C" { +#include "gba.h" +} using namespace QGBA; @@ -16,6 +21,43 @@ Window::Window(QWidget* parent) : QMainWindow(parent) { 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()) { @@ -23,6 +65,34 @@ void Window::selectROM() { } } +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) { AudioDevice::Thread* thread = new AudioDevice::Thread(this); thread->setInput(audio); diff --git a/src/platform/qt/Window.h b/src/platform/qt/Window.h index ea641cfbb..c0cc195cb 100644 --- a/src/platform/qt/Window.h +++ b/src/platform/qt/Window.h @@ -4,6 +4,10 @@ #include #include +extern "C" { +#include "gba.h" +} + #include "GameController.h" #include "Display.h" @@ -16,10 +20,15 @@ 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*);