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