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
8#include "GameController.h"
9#include "GDBWindow.h"
10#include "GDBController.h"
11#include "LoadSaveState.h"
12#include "LogView.h"
13
14using namespace QGBA;
15
16Window::Window(QWidget* parent)
17 : QMainWindow(parent)
18#ifdef USE_GDB_STUB
19 , m_gdbController(nullptr)
20#endif
21{
22 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
23 setMinimumSize(240, 160);
24
25 m_controller = new GameController(this);
26 m_logView = new LogView();
27
28 QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
29 format.setSwapInterval(1);
30 m_display = new Display(format);
31 setCentralWidget(m_display);
32 connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
33 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
34 connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
35 connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
36 connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
37 connect(this, SIGNAL(startDrawing(const uint32_t*, GBAThread*)), m_display, SLOT(startDrawing(const uint32_t*, GBAThread*)), Qt::QueuedConnection);
38 connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
39 connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
40 connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
41 connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
42 connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
43
44 setupMenu(menuBar());
45}
46
47Window::~Window() {
48 delete m_logView;
49}
50
51GBAKey Window::mapKey(int qtKey) {
52 switch (qtKey) {
53 case Qt::Key_Z:
54 return GBA_KEY_A;
55 break;
56 case Qt::Key_X:
57 return GBA_KEY_B;
58 break;
59 case Qt::Key_A:
60 return GBA_KEY_L;
61 break;
62 case Qt::Key_S:
63 return GBA_KEY_R;
64 break;
65 case Qt::Key_Return:
66 return GBA_KEY_START;
67 break;
68 case Qt::Key_Backspace:
69 return GBA_KEY_SELECT;
70 break;
71 case Qt::Key_Up:
72 return GBA_KEY_UP;
73 break;
74 case Qt::Key_Down:
75 return GBA_KEY_DOWN;
76 break;
77 case Qt::Key_Left:
78 return GBA_KEY_LEFT;
79 break;
80 case Qt::Key_Right:
81 return GBA_KEY_RIGHT;
82 break;
83 default:
84 return GBA_KEY_NONE;
85 }
86}
87
88void Window::selectROM() {
89 QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"));
90 if (!filename.isEmpty()) {
91 m_controller->loadGame(filename);
92 }
93}
94
95#ifdef USE_GDB_STUB
96void Window::gdbOpen() {
97 if (!m_gdbController) {
98 m_gdbController = new GDBController(m_controller, this);
99 }
100 GDBWindow* window = new GDBWindow(m_gdbController);
101 window->show();
102}
103#endif
104
105void Window::keyPressEvent(QKeyEvent* event) {
106 if (event->isAutoRepeat()) {
107 QWidget::keyPressEvent(event);
108 return;
109 }
110 GBAKey key = mapKey(event->key());
111 if (key == GBA_KEY_NONE) {
112 QWidget::keyPressEvent(event);
113 return;
114 }
115 m_controller->keyPressed(key);
116 event->accept();
117}
118
119void Window::keyReleaseEvent(QKeyEvent* event) {
120 if (event->isAutoRepeat()) {
121 QWidget::keyReleaseEvent(event);
122 return;
123 }
124 GBAKey key = mapKey(event->key());
125 if (key == GBA_KEY_NONE) {
126 QWidget::keyPressEvent(event);
127 return;
128 }
129 m_controller->keyReleased(key);
130 event->accept();
131}
132
133void Window::closeEvent(QCloseEvent* event) {
134 emit shutdown();
135 QMainWindow::closeEvent(event);
136}
137
138void Window::gameStarted(GBAThread* context) {
139 emit startDrawing(m_controller->drawContext(), context);
140 foreach (QAction* action, m_gameActions) {
141 action->setDisabled(false);
142 }
143}
144
145void Window::gameStopped() {
146 foreach (QAction* action, m_gameActions) {
147 action->setDisabled(true);
148 }
149}
150
151void Window::openStateWindow(LoadSave ls) {
152 bool wasPaused = m_controller->isPaused();
153 LoadSaveState* window = new LoadSaveState(m_controller);
154 connect(this, SIGNAL(shutdown()), window, SLOT(hide()));
155 if (!wasPaused) {
156 m_controller->setPaused(true);
157 connect(window, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
158 }
159 window->setAttribute(Qt::WA_DeleteOnClose);
160 window->setMode(ls);
161 window->show();
162}
163
164void Window::setupMenu(QMenuBar* menubar) {
165 menubar->clear();
166 QMenu* fileMenu = menubar->addMenu(tr("&File"));
167 fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open);
168
169 QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
170 QAction* reset = new QAction(tr("&Reset"), emulationMenu);
171 reset->setShortcut(tr("Ctrl+R"));
172 connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
173 m_gameActions.append(reset);
174 emulationMenu->addAction(reset);
175
176 QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
177 connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
178 m_gameActions.append(shutdown);
179 emulationMenu->addAction(shutdown);
180 emulationMenu->addSeparator();
181
182 QAction* loadState = new QAction(tr("&Load state"), emulationMenu);
183 loadState->setShortcut(tr("Ctrl+L"));
184 connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
185 m_gameActions.append(loadState);
186 emulationMenu->addAction(loadState);
187
188 QAction* saveState = new QAction(tr("&Save state"), emulationMenu);
189 saveState->setShortcut(tr("Ctrl+S"));
190 connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
191 m_gameActions.append(saveState);
192 emulationMenu->addAction(saveState);
193 emulationMenu->addSeparator();
194
195 QAction* pause = new QAction(tr("&Pause"), emulationMenu);
196 pause->setChecked(false);
197 pause->setCheckable(true);
198 pause->setShortcut(tr("Ctrl+P"));
199 connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
200 connect(m_controller, &GameController::gamePaused, [pause]() { pause->setChecked(true); });
201 connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
202 m_gameActions.append(pause);
203 emulationMenu->addAction(pause);
204
205 QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
206 frameAdvance->setShortcut(tr("Ctrl+N"));
207 connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
208 m_gameActions.append(frameAdvance);
209 emulationMenu->addAction(frameAdvance);
210
211 QMenu* target = emulationMenu->addMenu("FPS target");
212 QAction* setTarget = new QAction(tr("15"), emulationMenu);
213 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(15); });
214 target->addAction(setTarget);
215 setTarget = new QAction(tr("30"), emulationMenu);
216 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(30); });
217 target->addAction(setTarget);
218 setTarget = new QAction(tr("45"), emulationMenu);
219 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(45); });
220 target->addAction(setTarget);
221 setTarget = new QAction(tr("60"), emulationMenu);
222 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(60); });
223 target->addAction(setTarget);
224 setTarget = new QAction(tr("90"), emulationMenu);
225 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(90); });
226 target->addAction(setTarget);
227 setTarget = new QAction(tr("120"), emulationMenu);
228 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(120); });
229 target->addAction(setTarget);
230 setTarget = new QAction(tr("240"), emulationMenu);
231 connect(setTarget, &QAction::triggered, [this]() { emit fpsTargetChanged(240); });
232 target->addAction(setTarget);
233
234 QMenu* videoMenu = menubar->addMenu(tr("&Video"));
235 QMenu* frameMenu = videoMenu->addMenu(tr("Frame &size"));
236 QAction* setSize = new QAction(tr("1x"), videoMenu);
237 connect(setSize, &QAction::triggered, [this]() {
238 showNormal();
239 resize(VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
240 });
241 frameMenu->addAction(setSize);
242 setSize = new QAction(tr("2x"), videoMenu);
243 connect(setSize, &QAction::triggered, [this]() {
244 showNormal();
245 resize(VIDEO_HORIZONTAL_PIXELS * 2, VIDEO_VERTICAL_PIXELS * 2);
246 });
247 frameMenu->addAction(setSize);
248 setSize = new QAction(tr("3x"), videoMenu);
249 connect(setSize, &QAction::triggered, [this]() {
250 showNormal();
251 resize(VIDEO_HORIZONTAL_PIXELS * 3, VIDEO_VERTICAL_PIXELS * 3);
252 });
253 frameMenu->addAction(setSize);
254 setSize = new QAction(tr("4x"), videoMenu);
255 connect(setSize, &QAction::triggered, [this]() {
256 showNormal();
257 resize(VIDEO_HORIZONTAL_PIXELS * 4, VIDEO_VERTICAL_PIXELS * 4);
258 });
259 frameMenu->addAction(setSize);
260 frameMenu->addAction(tr("Fullscreen"), this, SLOT(showFullScreen()), QKeySequence("Ctrl+F"));
261
262 QMenu* soundMenu = menubar->addMenu(tr("&Sound"));
263 QMenu* buffersMenu = soundMenu->addMenu(tr("Buffer &size"));
264 QAction* setBuffer = new QAction(tr("512"), buffersMenu);
265 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(512); });
266 buffersMenu->addAction(setBuffer);
267 setBuffer = new QAction(tr("1024"), buffersMenu);
268 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(1024); });
269 buffersMenu->addAction(setBuffer);
270 setBuffer = new QAction(tr("2048"), buffersMenu);
271 connect(setBuffer, &QAction::triggered, [this]() { emit audioBufferSamplesChanged(2048); });
272 buffersMenu->addAction(setBuffer);
273
274 QMenu* debuggingMenu = menubar->addMenu(tr("&Debugging"));
275 QAction* viewLogs = new QAction(tr("View &logs..."), debuggingMenu);
276 connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
277 debuggingMenu->addAction(viewLogs);
278#ifdef USE_GDB_STUB
279 QAction* gdbWindow = new QAction(tr("Start &GDB server..."), debuggingMenu);
280 connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
281 debuggingMenu->addAction(gdbWindow);
282#endif
283
284 foreach (QAction* action, m_gameActions) {
285 action->setDisabled(true);
286 }
287}