all repos — mgba @ 3d339b13279bea84a6fe86b731933c1466396f83

mGBA Game Boy Advance Emulator

Key input
Jeffrey Pfau jeffrey@endrift.com
Wed, 29 Jan 2014 22:21:25 -0800
commit

3d339b13279bea84a6fe86b731933c1466396f83

parent

98c9121ac2eaac864041c43ca1e4355a6041ef4a

M src/gba/gba.hsrc/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.cppsrc/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.hsrc/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.cppsrc/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.hsrc/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*);