all repos — mgba @ d957736ed95751328f6913b1954cdeac0bf7e80d

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2015 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 "OverrideView.h"
  7
  8#include "ConfigController.h"
  9#include "GameController.h"
 10
 11extern "C" {
 12#include "gba/supervisor/thread.h"
 13}
 14
 15using namespace QGBA;
 16
 17OverrideView::OverrideView(GameController* controller, ConfigController* config, QWidget* parent)
 18	: QWidget(parent)
 19	, m_controller(controller)
 20	, m_config(config)
 21{
 22	m_ui.setupUi(this);
 23
 24	connect(controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
 25	connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
 26
 27	connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
 28		m_ui.hwRTC->setEnabled(!enabled);
 29		m_ui.hwGyro->setEnabled(!enabled);
 30		m_ui.hwLight->setEnabled(!enabled);
 31		m_ui.hwTilt->setEnabled(!enabled);
 32		m_ui.hwRumble->setEnabled(!enabled);
 33	});
 34
 35	connect(m_ui.savetype, SIGNAL(currentIndexChanged(int)), this, SLOT(updateOverrides()));
 36	connect(m_ui.hwAutodetect, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 37	connect(m_ui.hwRTC, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 38	connect(m_ui.hwGyro, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 39	connect(m_ui.hwLight, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 40	connect(m_ui.hwTilt, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 41	connect(m_ui.hwRumble, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 42
 43	connect(m_ui.save, SIGNAL(clicked()), this, SLOT(saveOverride()));
 44
 45	if (controller->isLoaded()) {
 46		gameStarted(controller->thread());
 47	}
 48}
 49
 50void OverrideView::saveOverride() {
 51	if (!m_config) {
 52		return;
 53	}
 54	m_config->saveOverride(m_override);
 55}
 56
 57void OverrideView::updateOverrides() {
 58	m_override = (GBACartridgeOverride) {
 59		"",
 60		static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1),
 61		HW_NO_OVERRIDE,
 62		IDLE_LOOP_NONE
 63	};
 64
 65	if (!m_ui.hwAutodetect->isChecked()) {
 66		m_override.hardware = HW_NONE;
 67		if (m_ui.hwRTC->isChecked()) {
 68			m_override.hardware |= HW_RTC;
 69		}
 70		if (m_ui.hwGyro->isChecked()) {
 71			m_override.hardware |= HW_GYRO;
 72		}
 73		if (m_ui.hwLight->isChecked()) {
 74			m_override.hardware |= HW_LIGHT_SENSOR;
 75		}
 76		if (m_ui.hwTilt->isChecked()) {
 77			m_override.hardware |= HW_TILT;
 78		}
 79		if (m_ui.hwRumble->isChecked()) {
 80			m_override.hardware |= HW_RUMBLE;
 81		}
 82	}
 83
 84	bool ok;
 85	uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
 86	if (ok) {
 87		m_override.idleLoop = parsedIdleLoop;
 88	}
 89
 90	if (m_override.savetype != SAVEDATA_AUTODETECT || m_override.hardware != HW_NO_OVERRIDE ||
 91	    m_override.idleLoop != IDLE_LOOP_NONE) {
 92		m_controller->setOverride(m_override);
 93	} else {
 94		m_controller->clearOverride();
 95	}
 96}
 97
 98void OverrideView::gameStarted(GBAThread* thread) {
 99	if (!thread->gba) {
100		gameStopped();
101		return;
102	}
103	m_ui.savetype->setCurrentIndex(thread->gba->memory.savedata.type + 1);
104	m_ui.savetype->setEnabled(false);
105
106	m_ui.hwAutodetect->setEnabled(false);
107	m_ui.hwRTC->setEnabled(false);
108	m_ui.hwGyro->setEnabled(false);
109	m_ui.hwLight->setEnabled(false);
110	m_ui.hwTilt->setEnabled(false);
111	m_ui.hwRumble->setEnabled(false);
112
113	m_ui.hwRTC->setChecked(thread->gba->memory.hw.devices & HW_RTC);
114	m_ui.hwGyro->setChecked(thread->gba->memory.hw.devices & HW_GYRO);
115	m_ui.hwLight->setChecked(thread->gba->memory.hw.devices & HW_LIGHT_SENSOR);
116	m_ui.hwTilt->setChecked(thread->gba->memory.hw.devices & HW_TILT);
117	m_ui.hwRumble->setChecked(thread->gba->memory.hw.devices & HW_RUMBLE);
118
119	if (thread->gba->idleLoop != IDLE_LOOP_NONE) {
120		m_ui.idleLoop->setText(QString::number(thread->gba->idleLoop, 16));
121	} else {
122		m_ui.idleLoop->clear();
123	}
124
125	GBAGetGameCode(thread->gba, m_override.id);
126	m_override.hardware = thread->gba->memory.hw.devices;
127	m_override.savetype = thread->gba->memory.savedata.type;
128	m_override.idleLoop = thread->gba->idleLoop;
129
130	m_ui.idleLoop->setEnabled(false);
131
132	m_ui.save->setEnabled(m_config);
133}
134
135void OverrideView::gameStopped() {
136	m_ui.savetype->setCurrentIndex(0);
137	m_ui.savetype->setEnabled(true);
138
139	m_ui.hwAutodetect->setEnabled(true);
140	m_ui.hwRTC->setEnabled(!m_ui.hwAutodetect->isChecked());
141	m_ui.hwGyro->setEnabled(!m_ui.hwAutodetect->isChecked());
142	m_ui.hwLight->setEnabled(!m_ui.hwAutodetect->isChecked());
143	m_ui.hwTilt->setEnabled(!m_ui.hwAutodetect->isChecked());
144	m_ui.hwRumble->setEnabled(!m_ui.hwAutodetect->isChecked());
145
146	m_ui.hwAutodetect->setChecked(true);
147	m_ui.hwRTC->setChecked(false);
148	m_ui.hwGyro->setChecked(false);
149	m_ui.hwLight->setChecked(false);
150	m_ui.hwTilt->setChecked(false);
151	m_ui.hwRumble->setChecked(false);
152
153	m_ui.idleLoop->setEnabled(true);
154	m_ui.idleLoop->clear();
155
156	m_ui.save->setEnabled(false);
157
158	updateOverrides();
159}