all repos — mgba @ c4aedfa69aff7c8d26e5199b006196f47fe517c3

mGBA Game Boy Advance Emulator

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

   1/* Copyright (c) 2013-2014 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include "Window.h"
   7
   8#include <QFileDialog>
   9#include <QFileInfo>
  10#include <QKeyEvent>
  11#include <QKeySequence>
  12#include <QMenuBar>
  13#include <QMessageBox>
  14#include <QMimeData>
  15#include <QPainter>
  16#include <QStackedLayout>
  17
  18#include "CheatsView.h"
  19#include "ConfigController.h"
  20#include "DisplayGL.h"
  21#include "GameController.h"
  22#include "GBAKeyEditor.h"
  23#include "GDBController.h"
  24#include "GDBWindow.h"
  25#include "GIFView.h"
  26#include "LoadSaveState.h"
  27#include "LogView.h"
  28#include "MultiplayerController.h"
  29#include "OverrideView.h"
  30#include "PaletteView.h"
  31#include "SensorView.h"
  32#include "SettingsView.h"
  33#include "ShortcutController.h"
  34#include "ShortcutView.h"
  35#include "VideoView.h"
  36
  37extern "C" {
  38#include "platform/commandline.h"
  39}
  40
  41using namespace QGBA;
  42
  43Window::Window(ConfigController* config, int playerId, QWidget* parent)
  44	: QMainWindow(parent)
  45	, m_logView(new LogView())
  46	, m_stateWindow(nullptr)
  47	, m_screenWidget(new WindowBackground())
  48	, m_logo(":/res/mgba-1024.png")
  49	, m_config(config)
  50	, m_inputController(playerId)
  51#ifdef USE_FFMPEG
  52	, m_videoView(nullptr)
  53#endif
  54#ifdef USE_MAGICK
  55	, m_gifView(nullptr)
  56#endif
  57#ifdef USE_GDB_STUB
  58	, m_gdbController(nullptr)
  59#endif
  60	, m_mruMenu(nullptr)
  61	, m_shortcutController(new ShortcutController(this))
  62	, m_playerId(playerId)
  63{
  64	setWindowTitle(PROJECT_NAME);
  65	setFocusPolicy(Qt::StrongFocus);
  66	setAcceptDrops(true);
  67	m_controller = new GameController(this);
  68	m_controller->setInputController(&m_inputController);
  69	m_controller->setOverrides(m_config->overrides());
  70
  71	QGLFormat format(QGLFormat(QGL::Rgba | QGL::DoubleBuffer));
  72	format.setSwapInterval(1);
  73	m_display = new DisplayGL(format, this);
  74
  75	m_logo.setDevicePixelRatio(m_screenWidget->devicePixelRatio());
  76	m_logo = m_logo; // Free memory left over in old pixmap
  77
  78	m_screenWidget->setMinimumSize(m_display->minimumSize());
  79	m_screenWidget->setSizePolicy(m_display->sizePolicy());
  80	m_screenWidget->setSizeHint(m_display->minimumSize() * 2);
  81	m_screenWidget->setPixmap(m_logo);
  82	m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
  83	setCentralWidget(m_screenWidget);
  84
  85	QVariant windowPos = m_config->getQtOption("windowPos");
  86	if (!windowPos.isNull()) {
  87		move(windowPos.toPoint());
  88	}
  89
  90	connect(m_controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
  91	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_display, SLOT(stopDrawing()));
  92	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
  93	connect(m_controller, SIGNAL(stateLoaded(GBAThread*)), m_display, SLOT(forceDraw()));
  94	connect(m_controller, SIGNAL(rewound(GBAThread*)), m_display, SLOT(forceDraw()));
  95	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), m_display, SLOT(pauseDrawing()));
  96#ifndef Q_OS_MAC
  97	connect(m_controller, SIGNAL(gamePaused(GBAThread*)), menuBar(), SLOT(show()));
  98	connect(m_controller, &GameController::gameUnpaused, [this]() {
  99		if(isFullScreen()) {
 100			menuBar()->hide();
 101		}
 102	});
 103#endif
 104	connect(m_controller, SIGNAL(gameUnpaused(GBAThread*)), m_display, SLOT(unpauseDrawing()));
 105	connect(m_controller, SIGNAL(postLog(int, const QString&)), m_logView, SLOT(postLog(int, const QString&)));
 106	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), this, SLOT(recordFrame()));
 107	connect(m_controller, SIGNAL(frameAvailable(const uint32_t*)), m_display, SLOT(framePosted(const uint32_t*)));
 108	connect(m_controller, SIGNAL(gameCrashed(const QString&)), this, SLOT(gameCrashed(const QString&)));
 109	connect(m_controller, SIGNAL(gameFailed()), this, SLOT(gameFailed()));
 110	connect(m_controller, SIGNAL(unimplementedBiosCall(int)), this, SLOT(unimplementedBiosCall(int)));
 111	connect(m_logView, SIGNAL(levelsSet(int)), m_controller, SLOT(setLogLevel(int)));
 112	connect(m_logView, SIGNAL(levelsEnabled(int)), m_controller, SLOT(enableLogLevel(int)));
 113	connect(m_logView, SIGNAL(levelsDisabled(int)), m_controller, SLOT(disableLogLevel(int)));
 114	connect(this, SIGNAL(startDrawing(GBAThread*)), m_display, SLOT(startDrawing(GBAThread*)), Qt::QueuedConnection);
 115	connect(this, SIGNAL(shutdown()), m_display, SLOT(stopDrawing()));
 116	connect(this, SIGNAL(shutdown()), m_controller, SLOT(closeGame()));
 117	connect(this, SIGNAL(shutdown()), m_logView, SLOT(hide()));
 118	connect(this, SIGNAL(audioBufferSamplesChanged(int)), m_controller, SLOT(setAudioBufferSamples(int)));
 119	connect(this, SIGNAL(fpsTargetChanged(float)), m_controller, SLOT(setFPSTarget(float)));
 120	connect(&m_fpsTimer, SIGNAL(timeout()), this, SLOT(showFPS()));
 121
 122	m_logView->setLevels(GBA_LOG_WARN | GBA_LOG_ERROR | GBA_LOG_FATAL);
 123	m_fpsTimer.setInterval(FPS_TIMER_INTERVAL);
 124
 125	m_shortcutController->setConfigController(m_config);
 126	setupMenu(menuBar());
 127}
 128
 129Window::~Window() {
 130	delete m_logView;
 131
 132#ifdef USE_FFMPEG
 133	delete m_videoView;
 134#endif
 135
 136#ifdef USE_MAGICK
 137	delete m_gifView;
 138#endif
 139}
 140
 141void Window::argumentsPassed(GBAArguments* args) {
 142	loadConfig();
 143
 144	if (args->patch) {
 145		m_controller->loadPatch(args->patch);
 146	}
 147
 148	if (args->fname) {
 149		m_controller->loadGame(args->fname, args->dirmode);
 150	}
 151}
 152
 153void Window::resizeFrame(int width, int height) {
 154	QSize newSize(width, height);
 155	newSize -= m_screenWidget->size();
 156	newSize += size();
 157	resize(newSize);
 158}
 159
 160void Window::setConfig(ConfigController* config) {
 161	m_config = config;
 162}
 163
 164void Window::loadConfig() {
 165	const GBAOptions* opts = m_config->options();
 166
 167	m_logView->setLevels(opts->logLevel);
 168
 169	m_controller->setOptions(opts);
 170	m_display->lockAspectRatio(opts->lockAspectRatio);
 171	m_display->filter(opts->resampleVideo);
 172
 173	if (opts->bios) {
 174		m_controller->loadBIOS(opts->bios);
 175	}
 176
 177	if (opts->fpsTarget) {
 178		emit fpsTargetChanged(opts->fpsTarget);
 179	}
 180
 181	if (opts->audioBuffers) {
 182		emit audioBufferSamplesChanged(opts->audioBuffers);
 183	}
 184
 185	if (opts->width && opts->height) {
 186		resizeFrame(opts->width, opts->height);
 187	}
 188
 189	if (opts->fullscreen) {
 190		enterFullScreen();
 191	}
 192
 193	m_mruFiles = m_config->getMRU();
 194	updateMRU();
 195
 196	m_inputController.setConfiguration(m_config);
 197}
 198
 199void Window::saveConfig() {
 200	m_inputController.saveConfiguration();
 201	m_config->write();
 202}
 203
 204void Window::selectROM() {
 205	bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
 206	if (doPause) {
 207		m_controller->setPaused(true);
 208	}
 209	QString filename = QFileDialog::getOpenFileName(this, tr("Select ROM"), m_config->getQtOption("lastDirectory").toString(), tr("Game Boy Advance ROMs (*.gba *.zip *.rom *.bin)"));
 210	if (doPause) {
 211		m_controller->setPaused(false);
 212	}
 213	if (!filename.isEmpty()) {
 214		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
 215		m_controller->loadGame(filename);
 216	}
 217}
 218
 219void Window::selectBIOS() {
 220	bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
 221	if (doPause) {
 222		m_controller->setPaused(true);
 223	}
 224	QString filename = QFileDialog::getOpenFileName(this, tr("Select BIOS"), m_config->getQtOption("lastDirectory").toString());
 225	if (doPause) {
 226		m_controller->setPaused(false);
 227	}
 228	if (!filename.isEmpty()) {
 229		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
 230		m_config->setOption("bios", filename);
 231		m_config->updateOption("bios");
 232		m_config->setOption("useBios", true);
 233		m_config->updateOption("useBios");
 234		m_controller->loadBIOS(filename);
 235	}
 236}
 237
 238void Window::selectPatch() {
 239	bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
 240	if (doPause) {
 241		m_controller->setPaused(true);
 242	}
 243	QString filename = QFileDialog::getOpenFileName(this, tr("Select patch"), m_config->getQtOption("lastDirectory").toString(), tr("Patches (*.ips *.ups *.bps)"));
 244	if (doPause) {
 245		m_controller->setPaused(false);
 246	}
 247	if (!filename.isEmpty()) {
 248		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
 249		m_controller->loadPatch(filename);
 250	}
 251}
 252
 253void Window::openView(QWidget* widget) {
 254	connect(this, SIGNAL(shutdown()), widget, SLOT(close()));
 255	widget->setAttribute(Qt::WA_DeleteOnClose);
 256	widget->show();
 257}
 258
 259void Window::importSharkport() {
 260	bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
 261	if (doPause) {
 262		m_controller->setPaused(true);
 263	}
 264	QString filename = QFileDialog::getOpenFileName(this, tr("Select save"), m_config->getQtOption("lastDirectory").toString(), tr("GameShark saves (*.sps *.xps)"));
 265	if (doPause) {
 266		m_controller->setPaused(false);
 267	}
 268	if (!filename.isEmpty()) {
 269		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
 270		m_controller->importSharkport(filename);
 271	}
 272}
 273
 274void Window::exportSharkport() {
 275	bool doPause = m_controller->isLoaded() && !m_controller->isPaused();
 276	if (doPause) {
 277		m_controller->setPaused(true);
 278	}
 279	QString filename = QFileDialog::getSaveFileName(this, tr("Select save"), m_config->getQtOption("lastDirectory").toString(), tr("GameShark saves (*.sps *.xps)"));
 280	if (doPause) {
 281		m_controller->setPaused(false);
 282	}
 283	if (!filename.isEmpty()) {
 284		m_config->setQtOption("lastDirectory", QFileInfo(filename).dir().path());
 285		m_controller->exportSharkport(filename);
 286	}
 287}
 288
 289void Window::openKeymapWindow() {
 290	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, InputController::KEYBOARD);
 291	openView(keyEditor);
 292}
 293
 294void Window::openSettingsWindow() {
 295	SettingsView* settingsWindow = new SettingsView(m_config);
 296	connect(settingsWindow, SIGNAL(biosLoaded(const QString&)), m_controller, SLOT(loadBIOS(const QString&)));
 297	connect(settingsWindow, SIGNAL(audioDriverChanged()), m_controller, SLOT(reloadAudioDriver()));
 298	openView(settingsWindow);
 299}
 300
 301void Window::openShortcutWindow() {
 302#ifdef BUILD_SDL
 303	m_inputController.recalibrateAxes();
 304#endif
 305	ShortcutView* shortcutView = new ShortcutView();
 306	shortcutView->setController(m_shortcutController);
 307	openView(shortcutView);
 308}
 309
 310void Window::openOverrideWindow() {
 311	OverrideView* overrideWindow = new OverrideView(m_controller, m_config);
 312	openView(overrideWindow);
 313}
 314
 315void Window::openSensorWindow() {
 316	SensorView* sensorWindow = new SensorView(m_controller, &m_inputController);
 317	openView(sensorWindow);
 318}
 319
 320void Window::openCheatsWindow() {
 321	CheatsView* cheatsWindow = new CheatsView(m_controller);
 322	openView(cheatsWindow);
 323}
 324
 325void Window::openPaletteWindow() {
 326	PaletteView* paletteWindow = new PaletteView(m_controller);
 327	openView(paletteWindow);
 328}
 329
 330#ifdef BUILD_SDL
 331void Window::openGamepadWindow() {
 332	const char* profile = m_inputController.profileForType(SDL_BINDING_BUTTON);
 333	GBAKeyEditor* keyEditor = new GBAKeyEditor(&m_inputController, SDL_BINDING_BUTTON, profile);
 334	openView(keyEditor);
 335}
 336#endif
 337
 338#ifdef USE_FFMPEG
 339void Window::openVideoWindow() {
 340	if (!m_videoView) {
 341		m_videoView = new VideoView();
 342		connect(m_videoView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
 343		connect(m_videoView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
 344		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(stopRecording()));
 345		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_videoView, SLOT(close()));
 346		connect(this, SIGNAL(shutdown()), m_videoView, SLOT(close()));
 347	}
 348	m_videoView->show();
 349}
 350#endif
 351
 352#ifdef USE_MAGICK
 353void Window::openGIFWindow() {
 354	if (!m_gifView) {
 355		m_gifView = new GIFView();
 356		connect(m_gifView, SIGNAL(recordingStarted(GBAAVStream*)), m_controller, SLOT(setAVStream(GBAAVStream*)));
 357		connect(m_gifView, SIGNAL(recordingStopped()), m_controller, SLOT(clearAVStream()), Qt::DirectConnection);
 358		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(stopRecording()));
 359		connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_gifView, SLOT(close()));
 360		connect(this, SIGNAL(shutdown()), m_gifView, SLOT(close()));
 361	}
 362	m_gifView->show();
 363}
 364#endif
 365
 366#ifdef USE_GDB_STUB
 367void Window::gdbOpen() {
 368	if (!m_gdbController) {
 369		m_gdbController = new GDBController(m_controller, this);
 370	}
 371	GDBWindow* window = new GDBWindow(m_gdbController);
 372	connect(this, SIGNAL(shutdown()), window, SLOT(close()));
 373	window->setAttribute(Qt::WA_DeleteOnClose);
 374	window->show();
 375}
 376#endif
 377
 378void Window::keyPressEvent(QKeyEvent* event) {
 379	if (event->isAutoRepeat()) {
 380		QWidget::keyPressEvent(event);
 381		return;
 382	}
 383	GBAKey key = m_inputController.mapKeyboard(event->key());
 384	if (key == GBA_KEY_NONE) {
 385		QWidget::keyPressEvent(event);
 386		return;
 387	}
 388	m_controller->keyPressed(key);
 389	event->accept();
 390}
 391
 392void Window::keyReleaseEvent(QKeyEvent* event) {
 393	if (event->isAutoRepeat()) {
 394		QWidget::keyReleaseEvent(event);
 395		return;
 396	}
 397	GBAKey key = m_inputController.mapKeyboard(event->key());
 398	if (key == GBA_KEY_NONE) {
 399		QWidget::keyPressEvent(event);
 400		return;
 401	}
 402	m_controller->keyReleased(key);
 403	event->accept();
 404}
 405
 406void Window::resizeEvent(QResizeEvent*) {
 407	m_config->setOption("height", m_screenWidget->height());
 408	m_config->setOption("width", m_screenWidget->width());
 409	m_config->setOption("fullscreen", isFullScreen());
 410}
 411
 412void Window::closeEvent(QCloseEvent* event) {
 413	emit shutdown();
 414	m_config->setQtOption("windowPos", pos());
 415	saveConfig();
 416	QMainWindow::closeEvent(event);
 417}
 418
 419void Window::focusOutEvent(QFocusEvent*) {
 420	m_controller->setTurbo(false, false);
 421	m_controller->clearKeys();
 422}
 423
 424void Window::dragEnterEvent(QDragEnterEvent* event) {
 425	if (event->mimeData()->hasFormat("text/uri-list")) {
 426		event->acceptProposedAction();
 427	}
 428}
 429
 430void Window::dropEvent(QDropEvent* event) {
 431	QString uris = event->mimeData()->data("text/uri-list");
 432	uris = uris.trimmed();
 433	if (uris.contains("\n")) {
 434		// Only one file please
 435		return;
 436	}
 437	QUrl url(uris);
 438	if (!url.isLocalFile()) {
 439		// No remote loading
 440		return;
 441	}
 442	event->accept();
 443	m_controller->loadGame(url.path());
 444}
 445
 446void Window::mouseDoubleClickEvent(QMouseEvent* event) {
 447	if (event->button() != Qt::LeftButton) {
 448		return;
 449	}
 450	toggleFullScreen();
 451}
 452
 453void Window::enterFullScreen() {
 454	if (isFullScreen()) {
 455		return;
 456	}
 457	showFullScreen();
 458#ifndef Q_OS_MAC
 459	if (m_controller->isLoaded() && !m_controller->isPaused()) {
 460		menuBar()->hide();
 461	}
 462#endif
 463}
 464
 465void Window::exitFullScreen() {
 466	if (!isFullScreen()) {
 467		return;
 468	}
 469	showNormal();
 470	menuBar()->show();
 471}
 472
 473void Window::toggleFullScreen() {
 474	if (isFullScreen()) {
 475		exitFullScreen();
 476	} else {
 477		enterFullScreen();
 478	}
 479}
 480
 481void Window::gameStarted(GBAThread* context) {
 482	char title[13] = { '\0' };
 483	MutexLock(&context->stateMutex);
 484	if (context->state < THREAD_EXITING) {
 485		emit startDrawing(context);
 486		GBAGetGameTitle(context->gba, title);
 487	} else {
 488		MutexUnlock(&context->stateMutex);
 489		return;
 490	}
 491	MutexUnlock(&context->stateMutex);
 492	foreach (QAction* action, m_gameActions) {
 493		action->setDisabled(false);
 494	}
 495	appendMRU(context->fname);
 496	setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
 497	attachWidget(m_display);
 498
 499#ifndef Q_OS_MAC
 500	if(isFullScreen()) {
 501		menuBar()->hide();
 502	}
 503#endif
 504
 505	m_hitUnimplementedBiosCall = false;
 506	m_fpsTimer.start();
 507}
 508
 509void Window::gameStopped() {
 510	foreach (QAction* action, m_gameActions) {
 511		action->setDisabled(true);
 512	}
 513	setWindowTitle(tr(PROJECT_NAME));
 514	detachWidget(m_display);
 515	m_screenWidget->setLockAspectRatio(m_logo.width(), m_logo.height());
 516	m_screenWidget->setPixmap(m_logo);
 517
 518	m_fpsTimer.stop();
 519}
 520
 521void Window::gameCrashed(const QString& errorMessage) {
 522	QMessageBox* crash = new QMessageBox(QMessageBox::Critical, tr("Crash"),
 523		tr("The game has crashed with the following error:\n\n%1").arg(errorMessage),
 524		QMessageBox::Ok, this,  Qt::Sheet);
 525	crash->setAttribute(Qt::WA_DeleteOnClose);
 526	crash->show();
 527}
 528
 529void Window::gameFailed() {
 530	QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Couldn't Load"),
 531		tr("Could not load game. Are you sure it's in the correct format?"),
 532		QMessageBox::Ok, this,  Qt::Sheet);
 533	fail->setAttribute(Qt::WA_DeleteOnClose);
 534	fail->show();
 535}
 536
 537void Window::unimplementedBiosCall(int call) {
 538	if (m_hitUnimplementedBiosCall) {
 539		return;
 540	}
 541	m_hitUnimplementedBiosCall = true;
 542
 543	QMessageBox* fail = new QMessageBox(QMessageBox::Warning, tr("Unimplemented BIOS call"),
 544		tr("This game uses a BIOS call that is not implemented. Please use the official BIOS for best experience."),
 545		QMessageBox::Ok, this,  Qt::Sheet);
 546	fail->setAttribute(Qt::WA_DeleteOnClose);
 547	fail->show();
 548}
 549
 550void Window::recordFrame() {
 551	m_frameList.append(QDateTime::currentDateTime());
 552	while (m_frameList.count() > FRAME_LIST_SIZE) {
 553		m_frameList.removeFirst();
 554	}
 555}
 556
 557void Window::showFPS() {
 558	char gameTitle[13] = { '\0' };
 559	GBAGetGameTitle(m_controller->thread()->gba, gameTitle);
 560
 561	QString title(gameTitle);
 562	std::shared_ptr<MultiplayerController> multiplayer = m_controller->multiplayerController();
 563	if (multiplayer && multiplayer->attached() > 1) {
 564		title += tr(" -  Player %1 of %2").arg(m_playerId + 1).arg(multiplayer->attached());
 565	}
 566	if (m_frameList.isEmpty()) {
 567		setWindowTitle(tr(PROJECT_NAME " - %1").arg(title));
 568		return;
 569	}
 570	qint64 interval = m_frameList.first().msecsTo(m_frameList.last());
 571	float fps = (m_frameList.count() - 1) * 10000.f / interval;
 572	fps = round(fps) / 10.f;
 573	setWindowTitle(tr(PROJECT_NAME " - %1 (%2 fps)").arg(title).arg(fps));
 574}
 575
 576void Window::openStateWindow(LoadSave ls) {
 577	if (m_stateWindow) {
 578		return;
 579	}
 580	bool wasPaused = m_controller->isPaused();
 581	m_stateWindow = new LoadSaveState(m_controller);
 582	connect(this, SIGNAL(shutdown()), m_stateWindow, SLOT(close()));
 583	connect(m_controller, SIGNAL(gameStopped(GBAThread*)), m_stateWindow, SLOT(close()));
 584	connect(m_stateWindow, &LoadSaveState::closed, [this]() {
 585		m_screenWidget->layout()->removeWidget(m_stateWindow);
 586		m_stateWindow = nullptr;
 587		QMetaObject::invokeMethod(this, "setFocus", Qt::QueuedConnection);
 588	});
 589	if (!wasPaused) {
 590		m_controller->setPaused(true);
 591		connect(m_stateWindow, &LoadSaveState::closed, [this]() { m_controller->setPaused(false); });
 592	}
 593	m_stateWindow->setAttribute(Qt::WA_DeleteOnClose);
 594	m_stateWindow->setMode(ls);
 595	attachWidget(m_stateWindow);
 596}
 597
 598void Window::setupMenu(QMenuBar* menubar) {
 599	menubar->clear();
 600	QMenu* fileMenu = menubar->addMenu(tr("&File"));
 601	m_shortcutController->addMenu(fileMenu);
 602	installEventFilter(m_shortcutController);
 603	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &ROM..."), this, SLOT(selectROM()), QKeySequence::Open), "loadROM");
 604	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &BIOS..."), this, SLOT(selectBIOS())), "loadBIOS");
 605	addControlledAction(fileMenu, fileMenu->addAction(tr("Load &patch..."), this, SLOT(selectPatch())), "loadPatch");
 606
 607	m_mruMenu = fileMenu->addMenu(tr("Recent"));
 608
 609	fileMenu->addSeparator();
 610
 611	QAction* loadState = new QAction(tr("&Load state"), fileMenu);
 612	loadState->setShortcut(tr("F10"));
 613	connect(loadState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::LOAD); });
 614	m_gameActions.append(loadState);
 615	addControlledAction(fileMenu, loadState, "loadState");
 616
 617	QAction* saveState = new QAction(tr("&Save state"), fileMenu);
 618	saveState->setShortcut(tr("Shift+F10"));
 619	connect(saveState, &QAction::triggered, [this]() { this->openStateWindow(LoadSave::SAVE); });
 620	m_gameActions.append(saveState);
 621	addControlledAction(fileMenu, saveState, "saveState");
 622
 623	QMenu* quickLoadMenu = fileMenu->addMenu(tr("Quick load"));
 624	QMenu* quickSaveMenu = fileMenu->addMenu(tr("Quick save"));
 625	m_shortcutController->addMenu(quickLoadMenu);
 626	m_shortcutController->addMenu(quickSaveMenu);
 627	int i;
 628	for (i = 1; i < 10; ++i) {
 629		QAction* quickLoad = new QAction(tr("State &%1").arg(i), quickLoadMenu);
 630		quickLoad->setShortcut(tr("F%1").arg(i));
 631		connect(quickLoad, &QAction::triggered, [this, i]() { m_controller->loadState(i); });
 632		m_gameActions.append(quickLoad);
 633		addControlledAction(quickLoadMenu, quickLoad, QString("quickLoad.%1").arg(i));
 634
 635		QAction* quickSave = new QAction(tr("State &%1").arg(i), quickSaveMenu);
 636		quickSave->setShortcut(tr("Shift+F%1").arg(i));
 637		connect(quickSave, &QAction::triggered, [this, i]() { m_controller->saveState(i); });
 638		m_gameActions.append(quickSave);
 639		addControlledAction(quickSaveMenu, quickSave, QString("quickSave.%1").arg(i));
 640	}
 641
 642	fileMenu->addSeparator();
 643	QAction* importShark = new QAction(tr("Import GameShark Save"), fileMenu);
 644	connect(importShark, SIGNAL(triggered()), this, SLOT(importSharkport()));
 645	m_gameActions.append(importShark);
 646	addControlledAction(fileMenu, importShark, "importShark");
 647
 648	QAction* exportShark = new QAction(tr("Export GameShark Save"), fileMenu);
 649	connect(exportShark, SIGNAL(triggered()), this, SLOT(exportSharkport()));
 650	m_gameActions.append(exportShark);
 651	addControlledAction(fileMenu, exportShark, "exportShark");
 652
 653	fileMenu->addSeparator();
 654	QAction* multiWindow = new QAction(tr("New multiplayer window"), fileMenu);
 655	connect(multiWindow, &QAction::triggered, [this]() {
 656		std::shared_ptr<MultiplayerController> multiplayer = m_controller->multiplayerController();
 657		if (!multiplayer) {
 658			multiplayer = std::make_shared<MultiplayerController>();
 659			m_controller->setMultiplayerController(multiplayer);
 660		}
 661		Window* w2 = new Window(m_config, multiplayer->attached());
 662		w2->setAttribute(Qt::WA_DeleteOnClose);
 663#ifndef Q_OS_MAC
 664		w2->show();
 665#endif
 666		w2->loadConfig();
 667		w2->controller()->setMultiplayerController(multiplayer);
 668#ifdef Q_OS_MAC
 669		w2->show();
 670#endif
 671	});
 672	addControlledAction(fileMenu, multiWindow, "multiWindow");
 673
 674#ifndef Q_OS_MAC
 675	addControlledAction(fileMenu, fileMenu->addAction(tr("E&xit"), this, SLOT(close()), QKeySequence::Quit), "quit");
 676#endif
 677
 678	QMenu* emulationMenu = menubar->addMenu(tr("&Emulation"));
 679	m_shortcutController->addMenu(emulationMenu);
 680	QAction* reset = new QAction(tr("&Reset"), emulationMenu);
 681	reset->setShortcut(tr("Ctrl+R"));
 682	connect(reset, SIGNAL(triggered()), m_controller, SLOT(reset()));
 683	m_gameActions.append(reset);
 684	addControlledAction(emulationMenu, reset, "reset");
 685
 686	QAction* shutdown = new QAction(tr("Sh&utdown"), emulationMenu);
 687	connect(shutdown, SIGNAL(triggered()), m_controller, SLOT(closeGame()));
 688	m_gameActions.append(shutdown);
 689	addControlledAction(emulationMenu, shutdown, "shutdown");
 690	emulationMenu->addSeparator();
 691
 692	QAction* pause = new QAction(tr("&Pause"), emulationMenu);
 693	pause->setChecked(false);
 694	pause->setCheckable(true);
 695	pause->setShortcut(tr("Ctrl+P"));
 696	connect(pause, SIGNAL(triggered(bool)), m_controller, SLOT(setPaused(bool)));
 697	connect(m_controller, &GameController::gamePaused, [this, pause]() {
 698		pause->setChecked(true);
 699
 700		QImage currentImage(reinterpret_cast<const uchar*>(m_controller->drawContext()), VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, 1024, QImage::Format_RGB32);
 701		QPixmap pixmap;
 702		pixmap.convertFromImage(currentImage.rgbSwapped());
 703		m_screenWidget->setPixmap(pixmap);
 704		m_screenWidget->setLockAspectRatio(3, 2);
 705	});
 706	connect(m_controller, &GameController::gameUnpaused, [pause]() { pause->setChecked(false); });
 707	m_gameActions.append(pause);
 708	addControlledAction(emulationMenu, pause, "pause");
 709
 710	QAction* frameAdvance = new QAction(tr("&Next frame"), emulationMenu);
 711	frameAdvance->setShortcut(tr("Ctrl+N"));
 712	connect(frameAdvance, SIGNAL(triggered()), m_controller, SLOT(frameAdvance()));
 713	m_gameActions.append(frameAdvance);
 714	addControlledAction(emulationMenu, frameAdvance, "frameAdvance");
 715
 716	emulationMenu->addSeparator();
 717
 718	QAction* turbo = new QAction(tr("&Fast forward"), emulationMenu);
 719	turbo->setCheckable(true);
 720	turbo->setChecked(false);
 721	turbo->setShortcut(tr("Shift+Tab"));
 722	connect(turbo, SIGNAL(triggered(bool)), m_controller, SLOT(setTurbo(bool)));
 723	addControlledAction(emulationMenu, turbo, "fastForward");
 724
 725	QAction* rewind = new QAction(tr("Re&wind"), emulationMenu);
 726	rewind->setShortcut(tr("`"));
 727	connect(rewind, SIGNAL(triggered()), m_controller, SLOT(rewind()));
 728	m_gameActions.append(rewind);
 729	addControlledAction(emulationMenu, rewind, "rewind");
 730
 731	QAction* frameRewind = new QAction(tr("Step backwards"), emulationMenu);
 732	frameRewind->setShortcut(tr("Ctrl+B"));
 733	connect(frameRewind, &QAction::triggered, [this] () {
 734		m_controller->rewind(1);
 735	});
 736	m_gameActions.append(frameRewind);
 737	addControlledAction(emulationMenu, frameRewind, "frameRewind");
 738
 739	ConfigOption* videoSync = m_config->addOption("videoSync");
 740	videoSync->addBoolean(tr("Sync to &video"), emulationMenu);
 741	videoSync->connect([this](const QVariant& value) {
 742		m_controller->setVideoSync(value.toBool());
 743	}, this);
 744	m_config->updateOption("videoSync");
 745
 746	ConfigOption* audioSync = m_config->addOption("audioSync");
 747	audioSync->addBoolean(tr("Sync to &audio"), emulationMenu);
 748	audioSync->connect([this](const QVariant& value) {
 749		m_controller->setAudioSync(value.toBool());
 750	}, this);
 751	m_config->updateOption("audioSync");
 752
 753	emulationMenu->addSeparator();
 754
 755	QMenu* solarMenu = emulationMenu->addMenu(tr("Solar sensor"));
 756	m_shortcutController->addMenu(solarMenu);
 757	QAction* solarIncrease = new QAction(tr("Increase solar level"), solarMenu);
 758	connect(solarIncrease, SIGNAL(triggered()), m_controller, SLOT(increaseLuminanceLevel()));
 759	addControlledAction(solarMenu, solarIncrease, "increaseLuminanceLevel");
 760
 761	QAction* solarDecrease = new QAction(tr("Decrease solar level"), solarMenu);
 762	connect(solarDecrease, SIGNAL(triggered()), m_controller, SLOT(decreaseLuminanceLevel()));
 763	addControlledAction(solarMenu, solarDecrease, "decreaseLuminanceLevel");
 764
 765	QAction* maxSolar = new QAction(tr("Brightest solar level"), solarMenu);
 766	connect(maxSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(10); });
 767	addControlledAction(solarMenu, maxSolar, "maxLuminanceLevel");
 768
 769	QAction* minSolar = new QAction(tr("Darkest solar level"), solarMenu);
 770	connect(minSolar, &QAction::triggered, [this]() { m_controller->setLuminanceLevel(0); });
 771	addControlledAction(solarMenu, minSolar, "minLuminanceLevel");
 772
 773	solarMenu->addSeparator();
 774	for (int i = 0; i <= 10; ++i) {
 775		QAction* setSolar = new QAction(tr("Brightness %1").arg(QString::number(i)), solarMenu);
 776		connect(setSolar, &QAction::triggered, [this, i]() {
 777			m_controller->setLuminanceLevel(i);
 778		});
 779		addControlledAction(solarMenu, setSolar, QString("luminanceLevel.%1").arg(QString::number(i)));
 780	}
 781
 782	QMenu* avMenu = menubar->addMenu(tr("Audio/&Video"));
 783	m_shortcutController->addMenu(avMenu);
 784	QMenu* frameMenu = avMenu->addMenu(tr("Frame size"));
 785	m_shortcutController->addMenu(frameMenu, avMenu);
 786	for (int i = 1; i <= 6; ++i) {
 787		QAction* setSize = new QAction(tr("%1x").arg(QString::number(i)), avMenu);
 788		connect(setSize, &QAction::triggered, [this, i]() {
 789			showNormal();
 790			resizeFrame(VIDEO_HORIZONTAL_PIXELS * i, VIDEO_VERTICAL_PIXELS * i);
 791		});
 792		addControlledAction(frameMenu, setSize, QString("frame%1x").arg(QString::number(i)));
 793	}
 794	addControlledAction(frameMenu, frameMenu->addAction(tr("Fullscreen"), this, SLOT(toggleFullScreen()), QKeySequence("Ctrl+F")), "fullscreen");
 795
 796	ConfigOption* lockAspectRatio = m_config->addOption("lockAspectRatio");
 797	lockAspectRatio->addBoolean(tr("Lock aspect ratio"), avMenu);
 798	lockAspectRatio->connect([this](const QVariant& value) {
 799		m_display->lockAspectRatio(value.toBool());
 800	}, this);
 801	m_config->updateOption("lockAspectRatio");
 802
 803	ConfigOption* resampleVideo = m_config->addOption("resampleVideo");
 804	resampleVideo->addBoolean(tr("Resample video"), avMenu);
 805	resampleVideo->connect([this](const QVariant& value) {
 806		m_display->filter(value.toBool());
 807	}, this);
 808	m_config->updateOption("resampleVideo");
 809
 810	QMenu* skipMenu = avMenu->addMenu(tr("Frame&skip"));
 811	ConfigOption* skip = m_config->addOption("frameskip");
 812	skip->connect([this](const QVariant& value) {
 813		m_controller->setFrameskip(value.toInt());
 814	}, this);
 815	for (int i = 0; i <= 10; ++i) {
 816		skip->addValue(QString::number(i), i, skipMenu);
 817	}
 818	m_config->updateOption("frameskip");
 819
 820	avMenu->addSeparator();
 821
 822	QMenu* buffersMenu = avMenu->addMenu(tr("Audio buffer &size"));
 823	ConfigOption* buffers = m_config->addOption("audioBuffers");
 824	buffers->connect([this](const QVariant& value) {
 825		emit audioBufferSamplesChanged(value.toInt());
 826	}, this);
 827	buffers->addValue(tr("512"), 512, buffersMenu);
 828	buffers->addValue(tr("768"), 768, buffersMenu);
 829	buffers->addValue(tr("1024"), 1024, buffersMenu);
 830	buffers->addValue(tr("2048"), 2048, buffersMenu);
 831	buffers->addValue(tr("4096"), 4096, buffersMenu);
 832	m_config->updateOption("audioBuffers");
 833
 834	avMenu->addSeparator();
 835
 836	QMenu* target = avMenu->addMenu(tr("FPS target"));
 837	ConfigOption* fpsTargetOption = m_config->addOption("fpsTarget");
 838	fpsTargetOption->connect([this](const QVariant& value) {
 839		emit fpsTargetChanged(value.toFloat());
 840	}, this);
 841	fpsTargetOption->addValue(tr("15"), 15, target);
 842	fpsTargetOption->addValue(tr("30"), 30, target);
 843	fpsTargetOption->addValue(tr("45"), 45, target);
 844	fpsTargetOption->addValue(tr("Native (59.7)"), float(GBA_ARM7TDMI_FREQUENCY) / float(VIDEO_TOTAL_LENGTH), target);
 845	fpsTargetOption->addValue(tr("60"), 60, target);
 846	fpsTargetOption->addValue(tr("90"), 90, target);
 847	fpsTargetOption->addValue(tr("120"), 120, target);
 848	fpsTargetOption->addValue(tr("240"), 240, target);
 849	m_config->updateOption("fpsTarget");
 850
 851#if defined(USE_PNG) || defined(USE_FFMPEG) || defined(USE_MAGICK)
 852	avMenu->addSeparator();
 853#endif
 854
 855#ifdef USE_PNG
 856	QAction* screenshot = new QAction(tr("Take &screenshot"), avMenu);
 857	screenshot->setShortcut(tr("F12"));
 858	connect(screenshot, SIGNAL(triggered()), m_controller, SLOT(screenshot()));
 859	m_gameActions.append(screenshot);
 860	addControlledAction(avMenu, screenshot, "screenshot");
 861#endif
 862
 863#ifdef USE_FFMPEG
 864	QAction* recordOutput = new QAction(tr("Record output..."), avMenu);
 865	recordOutput->setShortcut(tr("F11"));
 866	connect(recordOutput, SIGNAL(triggered()), this, SLOT(openVideoWindow()));
 867	addControlledAction(avMenu, recordOutput, "recordOutput");
 868#endif
 869
 870#ifdef USE_MAGICK
 871	QAction* recordGIF = new QAction(tr("Record GIF..."), avMenu);
 872	recordGIF->setShortcut(tr("Shift+F11"));
 873	connect(recordGIF, SIGNAL(triggered()), this, SLOT(openGIFWindow()));
 874	addControlledAction(avMenu, recordGIF, "recordGIF");
 875#endif
 876
 877	avMenu->addSeparator();
 878	QMenu* videoLayers = avMenu->addMenu(tr("Video layers"));
 879
 880	for (int i = 0; i < 4; ++i) {
 881		QAction* enableBg = new QAction(tr("Background %0").arg(i), videoLayers);
 882		enableBg->setCheckable(true);
 883		enableBg->setChecked(true);
 884		connect(enableBg, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->video.renderer->disableBG[i] = !enable; });
 885		m_gameActions.append(enableBg);
 886		addControlledAction(videoLayers, enableBg, QString("enableBG%0").arg(i));
 887	}
 888
 889	QAction* enableObj = new QAction(tr("OBJ (sprites)"), videoLayers);
 890	enableObj->setCheckable(true);
 891	enableObj->setChecked(true);
 892	connect(enableObj, &QAction::triggered, [this](bool enable) { m_controller->thread()->gba->video.renderer->disableOBJ = !enable; });
 893	m_gameActions.append(enableObj);
 894	addControlledAction(videoLayers, enableObj, "enableOBJ");
 895
 896	QMenu* audioChannels = avMenu->addMenu(tr("Audio channels"));
 897
 898	for (int i = 0; i < 4; ++i) {
 899		QAction* enableCh = new QAction(tr("Channel %0").arg(i + 1), audioChannels);
 900		enableCh->setCheckable(true);
 901		enableCh->setChecked(true);
 902		connect(enableCh, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableCh[i] = !enable; });
 903		m_gameActions.append(enableCh);
 904		addControlledAction(audioChannels, enableCh, QString("enableCh%0").arg(i + 1));
 905	}
 906
 907	QAction* enableChA = new QAction(tr("Channel A"), audioChannels);
 908	enableChA->setCheckable(true);
 909	enableChA->setChecked(true);
 910	connect(enableChA, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChA = !enable; });
 911	m_gameActions.append(enableChA);
 912	addControlledAction(audioChannels, enableChA, QString("enableChA"));
 913
 914	QAction* enableChB = new QAction(tr("Channel B"), audioChannels);
 915	enableChB->setCheckable(true);
 916	enableChB->setChecked(true);
 917	connect(enableChB, &QAction::triggered, [this, i](bool enable) { m_controller->thread()->gba->audio.forceDisableChB = !enable; });
 918	m_gameActions.append(enableChB);
 919	addControlledAction(audioChannels, enableChB, QString("enableChB"));
 920
 921	QMenu* toolsMenu = menubar->addMenu(tr("&Tools"));
 922	m_shortcutController->addMenu(toolsMenu);
 923	QAction* viewLogs = new QAction(tr("View &logs..."), toolsMenu);
 924	connect(viewLogs, SIGNAL(triggered()), m_logView, SLOT(show()));
 925	addControlledAction(toolsMenu, viewLogs, "viewLogs");
 926
 927	QAction* overrides = new QAction(tr("Game &overrides..."), toolsMenu);
 928	connect(overrides, SIGNAL(triggered()), this, SLOT(openOverrideWindow()));
 929	addControlledAction(toolsMenu, overrides, "overrideWindow");
 930
 931	QAction* sensors = new QAction(tr("Game &Pak sensors..."), toolsMenu);
 932	connect(sensors, SIGNAL(triggered()), this, SLOT(openSensorWindow()));
 933	addControlledAction(toolsMenu, sensors, "sensorWindow");
 934
 935	QAction* cheats = new QAction(tr("&Cheats..."), toolsMenu);
 936	connect(cheats, SIGNAL(triggered()), this, SLOT(openCheatsWindow()));
 937	addControlledAction(toolsMenu, cheats, "cheatsWindow");
 938
 939#ifdef USE_GDB_STUB
 940	QAction* gdbWindow = new QAction(tr("Start &GDB server..."), toolsMenu);
 941	connect(gdbWindow, SIGNAL(triggered()), this, SLOT(gdbOpen()));
 942	addControlledAction(toolsMenu, gdbWindow, "gdbWindow");
 943#endif
 944
 945	toolsMenu->addSeparator();
 946	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Settings..."), this, SLOT(openSettingsWindow())), "settings");
 947	addControlledAction(toolsMenu, toolsMenu->addAction(tr("Edit shortcuts..."), this, SLOT(openShortcutWindow())), "shortcuts");
 948
 949	QAction* keymap = new QAction(tr("Remap keyboard..."), toolsMenu);
 950	connect(keymap, SIGNAL(triggered()), this, SLOT(openKeymapWindow()));
 951	addControlledAction(toolsMenu, keymap, "remapKeyboard");
 952
 953#ifdef BUILD_SDL
 954	QAction* gamepad = new QAction(tr("Remap gamepad..."), toolsMenu);
 955	connect(gamepad, SIGNAL(triggered()), this, SLOT(openGamepadWindow()));
 956	addControlledAction(toolsMenu, gamepad, "remapGamepad");
 957#endif
 958
 959	toolsMenu->addSeparator();
 960
 961	QAction* paletteView = new QAction(tr("View &palette..."), toolsMenu);
 962	connect(paletteView, SIGNAL(triggered()), this, SLOT(openPaletteWindow()));
 963	m_gameActions.append(paletteView);
 964	addControlledAction(toolsMenu, paletteView, "paletteWindow");
 965
 966	ConfigOption* skipBios = m_config->addOption("skipBios");
 967	skipBios->connect([this](const QVariant& value) {
 968		m_controller->setSkipBIOS(value.toBool());
 969	}, this);
 970
 971	ConfigOption* volume = m_config->addOption("volume");
 972	volume->connect([this](const QVariant& value) {
 973		m_controller->setVolume(value.toInt());
 974	}, this);
 975
 976	ConfigOption* mute = m_config->addOption("mute");
 977	mute->connect([this](const QVariant& value) {
 978		m_controller->setMute(value.toBool());
 979	}, this);
 980
 981	ConfigOption* rewindEnable = m_config->addOption("rewindEnable");
 982	rewindEnable->connect([this](const QVariant& value) {
 983		m_controller->setRewind(value.toBool(), m_config->getOption("rewindBufferCapacity").toInt(), m_config->getOption("rewindBufferInterval").toInt());
 984	}, this);
 985
 986	ConfigOption* rewindBufferCapacity = m_config->addOption("rewindBufferCapacity");
 987	rewindBufferCapacity->connect([this](const QVariant& value) {
 988		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), value.toInt(), m_config->getOption("rewindBufferInterval").toInt());
 989	}, this);
 990
 991	ConfigOption* rewindBufferInterval = m_config->addOption("rewindBufferInterval");
 992	rewindBufferInterval->connect([this](const QVariant& value) {
 993		m_controller->setRewind(m_config->getOption("rewindEnable").toInt(), m_config->getOption("rewindBufferCapacity").toInt(), value.toInt());
 994	}, this);
 995
 996	ConfigOption* allowOpposingDirections = m_config->addOption("allowOpposingDirections");
 997	allowOpposingDirections->connect([this](const QVariant& value) {
 998		m_inputController.setAllowOpposing(value.toBool());
 999	}, this);
1000
1001	QMenu* other = new QMenu(tr("Other"), this);
1002	m_shortcutController->addMenu(other);
1003	m_shortcutController->addFunctions(other, [this]() {
1004		m_controller->setTurbo(true, false);
1005	}, [this]() {
1006		m_controller->setTurbo(false, false);
1007	}, QKeySequence(Qt::Key_Tab), tr("Fast Forward (held)"), "holdFastForward");
1008
1009	addControlledAction(other, other->addAction(tr("Exit fullscreen"), this, SLOT(exitFullScreen()), QKeySequence("Esc")), "exitFullScreen");
1010
1011	foreach (QAction* action, m_gameActions) {
1012		action->setDisabled(true);
1013	}
1014}
1015
1016void Window::attachWidget(QWidget* widget) {
1017	m_screenWidget->layout()->addWidget(widget);
1018	static_cast<QStackedLayout*>(m_screenWidget->layout())->setCurrentWidget(widget);
1019}
1020
1021void Window::detachWidget(QWidget* widget) {
1022	m_screenWidget->layout()->removeWidget(widget);
1023}
1024
1025void Window::appendMRU(const QString& fname) {
1026	int index = m_mruFiles.indexOf(fname);
1027	if (index >= 0) {
1028		m_mruFiles.removeAt(index);
1029	}
1030	m_mruFiles.prepend(fname);
1031	while (m_mruFiles.size() > ConfigController::MRU_LIST_SIZE) {
1032		m_mruFiles.removeLast();
1033	}
1034	updateMRU();
1035}
1036
1037void Window::updateMRU() {
1038	if (!m_mruMenu) {
1039		return;
1040	}
1041	m_mruMenu->clear();
1042	int i = 0;
1043	for (const QString& file : m_mruFiles) {
1044		QAction* item = new QAction(file, m_mruMenu);
1045		item->setShortcut(QString("Ctrl+%1").arg(i));
1046		connect(item, &QAction::triggered, [this, file]() { m_controller->loadGame(file); });
1047		m_mruMenu->addAction(item);
1048		++i;
1049	}
1050	m_config->setMRU(m_mruFiles);
1051	m_config->write();
1052	m_mruMenu->setEnabled(i > 0);
1053}
1054
1055QAction* Window::addControlledAction(QMenu* menu, QAction* action, const QString& name) {
1056	m_shortcutController->addAction(menu, action, name);
1057	menu->addAction(action);
1058	action->setShortcutContext(Qt::WidgetShortcut);
1059	addAction(action);
1060	return action;
1061}
1062
1063WindowBackground::WindowBackground(QWidget* parent)
1064	: QLabel(parent)
1065{
1066	setLayout(new QStackedLayout());
1067	layout()->setContentsMargins(0, 0, 0, 0);
1068	setAlignment(Qt::AlignCenter);
1069}
1070
1071void WindowBackground::setSizeHint(const QSize& hint) {
1072	m_sizeHint = hint;
1073}
1074
1075QSize WindowBackground::sizeHint() const {
1076	return m_sizeHint;
1077}
1078
1079void WindowBackground::setLockAspectRatio(int width, int height) {
1080	m_aspectWidth = width;
1081	m_aspectHeight = height;
1082}
1083
1084void WindowBackground::paintEvent(QPaintEvent*) {
1085	const QPixmap* logo = pixmap();
1086	if (!logo) {
1087		return;
1088	}
1089	QPainter painter(this);
1090	painter.setRenderHint(QPainter::SmoothPixmapTransform);
1091	painter.fillRect(QRect(QPoint(), size()), Qt::black);
1092	QSize s = size();
1093	QSize ds = s;
1094	if (s.width() * m_aspectHeight > s.height() * m_aspectWidth) {
1095		ds.setWidth(s.height() * m_aspectWidth / m_aspectHeight);
1096	} else if (s.width() * m_aspectHeight < s.height() * m_aspectWidth) {
1097		ds.setHeight(s.width() * m_aspectHeight / m_aspectWidth);
1098	}
1099	QPoint origin = QPoint((s.width() - ds.width()) / 2, (s.height() - ds.height()) / 2);
1100	QRect full(origin, ds);
1101	painter.drawPixmap(full, *logo);
1102}