all repos — mgba @ 74ac89a584b068b9d9c02e5117eddbe6da22d9c5

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	connect(m_ui.hwGBPlayer, SIGNAL(clicked()), this, SLOT(updateOverrides()));
 43
 44	connect(m_ui.save, SIGNAL(clicked()), this, SLOT(saveOverride()));
 45
 46	if (controller->isLoaded()) {
 47		gameStarted(controller->thread());
 48	}
 49}
 50
 51void OverrideView::saveOverride() {
 52	if (!m_config) {
 53		return;
 54	}
 55	m_config->saveOverride(m_override);
 56}
 57
 58void OverrideView::updateOverrides() {
 59	m_override = (GBACartridgeOverride) {
 60		"",
 61		static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1),
 62		HW_NO_OVERRIDE,
 63		IDLE_LOOP_NONE
 64	};
 65
 66	if (!m_ui.hwAutodetect->isChecked()) {
 67		m_override.hardware = HW_NONE;
 68		if (m_ui.hwRTC->isChecked()) {
 69			m_override.hardware |= HW_RTC;
 70		}
 71		if (m_ui.hwGyro->isChecked()) {
 72			m_override.hardware |= HW_GYRO;
 73		}
 74		if (m_ui.hwLight->isChecked()) {
 75			m_override.hardware |= HW_LIGHT_SENSOR;
 76		}
 77		if (m_ui.hwTilt->isChecked()) {
 78			m_override.hardware |= HW_TILT;
 79		}
 80		if (m_ui.hwRumble->isChecked()) {
 81			m_override.hardware |= HW_RUMBLE;
 82		}
 83	}
 84	if (m_ui.hwGBPlayer->isChecked()) {
 85		m_override.hardware |= HW_GB_PLAYER_DETECTION;
 86	}
 87
 88	bool ok;
 89	uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
 90	if (ok) {
 91		m_override.idleLoop = parsedIdleLoop;
 92	}
 93
 94	if (m_override.savetype != SAVEDATA_AUTODETECT || m_override.hardware != HW_NO_OVERRIDE ||
 95	    m_override.idleLoop != IDLE_LOOP_NONE) {
 96		m_controller->setOverride(m_override);
 97	} else {
 98		m_controller->clearOverride();
 99	}
100}
101
102void OverrideView::gameStarted(GBAThread* thread) {
103	if (!thread->gba) {
104		gameStopped();
105		return;
106	}
107	m_ui.savetype->setCurrentIndex(thread->gba->memory.savedata.type + 1);
108	m_ui.savetype->setEnabled(false);
109
110	m_ui.hwAutodetect->setEnabled(false);
111	m_ui.hwRTC->setEnabled(false);
112	m_ui.hwGyro->setEnabled(false);
113	m_ui.hwLight->setEnabled(false);
114	m_ui.hwTilt->setEnabled(false);
115	m_ui.hwRumble->setEnabled(false);
116
117	m_ui.hwRTC->setChecked(thread->gba->memory.hw.devices & HW_RTC);
118	m_ui.hwGyro->setChecked(thread->gba->memory.hw.devices & HW_GYRO);
119	m_ui.hwLight->setChecked(thread->gba->memory.hw.devices & HW_LIGHT_SENSOR);
120	m_ui.hwTilt->setChecked(thread->gba->memory.hw.devices & HW_TILT);
121	m_ui.hwRumble->setChecked(thread->gba->memory.hw.devices & HW_RUMBLE);
122	m_ui.hwGBPlayer->setChecked(thread->gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
123
124	if (thread->gba->idleLoop != IDLE_LOOP_NONE) {
125		m_ui.idleLoop->setText(QString::number(thread->gba->idleLoop, 16));
126	} else {
127		m_ui.idleLoop->clear();
128	}
129
130	GBAGetGameCode(thread->gba, m_override.id);
131	m_override.hardware = thread->gba->memory.hw.devices;
132	m_override.savetype = thread->gba->memory.savedata.type;
133	m_override.idleLoop = thread->gba->idleLoop;
134
135	m_ui.idleLoop->setEnabled(false);
136
137	m_ui.save->setEnabled(m_config);
138}
139
140void OverrideView::gameStopped() {
141	m_ui.savetype->setCurrentIndex(0);
142	m_ui.savetype->setEnabled(true);
143
144	m_ui.hwAutodetect->setEnabled(true);
145	m_ui.hwRTC->setEnabled(!m_ui.hwAutodetect->isChecked());
146	m_ui.hwGyro->setEnabled(!m_ui.hwAutodetect->isChecked());
147	m_ui.hwLight->setEnabled(!m_ui.hwAutodetect->isChecked());
148	m_ui.hwTilt->setEnabled(!m_ui.hwAutodetect->isChecked());
149	m_ui.hwRumble->setEnabled(!m_ui.hwAutodetect->isChecked());
150
151	m_ui.hwAutodetect->setChecked(true);
152	m_ui.hwRTC->setChecked(false);
153	m_ui.hwGyro->setChecked(false);
154	m_ui.hwLight->setChecked(false);
155	m_ui.hwTilt->setChecked(false);
156	m_ui.hwRumble->setChecked(false);
157
158	m_ui.idleLoop->setEnabled(true);
159	m_ui.idleLoop->clear();
160
161	m_ui.save->setEnabled(false);
162
163	updateOverrides();
164}