Key input

This commit is contained in:
Jeffrey Pfau 2014-01-29 22:21:25 -08:00
parent 98c9121ac2
commit 3d339b1327
5 changed files with 93 additions and 1 deletions

View File

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

View File

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

View File

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

View File

@ -1,6 +1,11 @@
#include "Window.h"
#include <QFileDialog>
#include <QKeyEvent>
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);

View File

@ -4,6 +4,10 @@
#include <QAudioOutput>
#include <QMainWindow>
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*);