all repos — mgba @ 2f98f542e5cb5b2018eb9961e62b41b96912c6e7

mGBA Game Boy Advance Emulator

src/platform/qt/Window.cpp (view raw)

  1#include "Window.h"
  2
  3#include <QFileDialog>
  4#include <QKeyEvent>
  5#include <QKeySequence>
  6#include <QMenuBar>
  7
  8using namespace QGBA;
  9
 10Window::Window(QWidget* parent) : QMainWindow(parent) {
 11	setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
 12	setMinimumSize(240, 160);
 13
 14	m_controller = new GameController(this);
 15	m_display = new Display(this);
 16	setCentralWidget(m_display);
 17	connect(m_controller, SIGNAL(frameAvailable(const QImage&)), m_display, SLOT(draw(const QImage&)));
 18	connect(m_controller, SIGNAL(audioDeviceAvailable(GBAAudio*)), this, SLOT(setupAudio(GBAAudio*)));
 19	connect(m_controller, SIGNAL(gameStarted()), this, SLOT(gameStarted()));
 20
 21	setupMenu(menuBar());
 22}
 23
 24GBAKey Window::mapKey(int qtKey) {
 25	switch (qtKey) {
 26	case Qt::Key_Z:
 27		return GBA_KEY_A;
 28		break;
 29	case Qt::Key_X:
 30		return GBA_KEY_B;
 31		break;
 32	case Qt::Key_A:
 33		return GBA_KEY_L;
 34		break;
 35	case Qt::Key_S:
 36		return GBA_KEY_R;
 37		break;
 38	case Qt::Key_Return:
 39		return GBA_KEY_START;
 40		break;
 41	case Qt::Key_Backspace:
 42		return GBA_KEY_SELECT;
 43		break;
 44	case Qt::Key_Up:
 45		return GBA_KEY_UP;
 46		break;
 47	case Qt::Key_Down:
 48		return GBA_KEY_DOWN;
 49		break;
 50	case Qt::Key_Left:
 51		return GBA_KEY_LEFT;
 52		break;
 53	case Qt::Key_Right:
 54		return GBA_KEY_RIGHT;
 55		break;
 56	default:
 57		return GBA_KEY_NONE;
 58	}
 59}
 60
 61void Window::selectROM() {
 62	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
 63	if (!filename.isEmpty()) {
 64		m_controller->loadGame(filename);
 65	}
 66}
 67
 68void Window::keyPressEvent(QKeyEvent* event) {
 69	if (event->isAutoRepeat()) {
 70		QWidget::keyPressEvent(event);
 71		return;
 72	}
 73	GBAKey key = mapKey(event->key());
 74	if (key == GBA_KEY_NONE) {
 75		QWidget::keyPressEvent(event);
 76		return;
 77	}
 78	m_controller->keyPressed(key);
 79	event->accept();
 80}
 81
 82void Window::keyReleaseEvent(QKeyEvent* event) {
 83	if (event->isAutoRepeat()) {
 84		QWidget::keyReleaseEvent(event);
 85		return;
 86	}
 87	GBAKey key = mapKey(event->key());
 88	if (key == GBA_KEY_NONE) {
 89		QWidget::keyPressEvent(event);
 90		return;
 91	}
 92	m_controller->keyReleased(key);
 93	event->accept();
 94}
 95
 96void Window::gameStarted() {
 97	foreach (QAction* action,  m_gameActions) {
 98		action->setDisabled(false);
 99	}
100}
101
102void Window::setupAudio(GBAAudio* audio) {
103	AudioDevice::Thread* thread = new AudioDevice::Thread(this);
104	thread->setInput(audio);
105	thread->start(QThread::HighPriority);
106}
107
108void Window::setupMenu(QMenuBar* menubar) {
109	menubar->clear();
110	QMenu* fileMenu = menubar->addMenu(tr("&File"));
111	fileMenu->addAction(tr("Load &ROM"), this, SLOT(selectROM()), QKeySequence::Open);
112
113	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
114	QAction* pause = new QAction(tr("&Pause"), 0);
115	pause->setChecked(false);
116	pause->setCheckable(true);
117	pause->setShortcut(tr("Ctrl+P"));
118	pause->setDisabled(true);
119	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
120	m_gameActions.append(pause);
121	emulationMenu->addAction(pause);
122
123	QAction* frameAdvance = new QAction(tr("&Next frame"), 0);
124	frameAdvance->setShortcut(tr("Ctrl+N"));
125	frameAdvance->setDisabled(true);
126	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
127	m_gameActions.append(frameAdvance);
128	emulationMenu->addAction(frameAdvance);
129}