all repos — mgba @ 22807c6274057e4bac42de986c182db864542ffb

mGBA Game Boy Advance Emulator

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

  1/* Copyright (c) 2013-2016 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 <QPushButton>
  9
 10#include "ConfigController.h"
 11#include "CoreController.h"
 12
 13#ifdef M_CORE_GBA
 14#include "GBAOverride.h"
 15#include <mgba/internal/gba/gba.h>
 16#endif
 17
 18#ifdef M_CORE_GB
 19#include "GBOverride.h"
 20#include <mgba/internal/gb/gb.h>
 21#endif
 22
 23using namespace QGBA;
 24
 25#ifdef M_CORE_GB
 26QList<enum GBModel> OverrideView::s_gbModelList;
 27QList<enum GBMemoryBankControllerType> OverrideView::s_mbcList;
 28#endif
 29
 30OverrideView::OverrideView(ConfigController* config, QWidget* parent)
 31	: QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
 32	, m_config(config)
 33{
 34#ifdef M_CORE_GB
 35	if (s_mbcList.isEmpty()) {
 36		// NB: Keep in sync with OverrideView.ui
 37		s_mbcList.append(GB_MBC_AUTODETECT);
 38		s_mbcList.append(GB_MBC_NONE);
 39		s_mbcList.append(GB_MBC1);
 40		s_mbcList.append(GB_MBC2);
 41		s_mbcList.append(GB_MBC3);
 42		s_mbcList.append(GB_MBC3_RTC);
 43		s_mbcList.append(GB_MBC5);
 44		s_mbcList.append(GB_MBC5_RUMBLE);
 45		s_mbcList.append(GB_MBC7);
 46		s_mbcList.append(GB_POCKETCAM);
 47		s_mbcList.append(GB_TAMA5);
 48		s_mbcList.append(GB_HuC3);
 49	}
 50	if (s_gbModelList.isEmpty()) {
 51		// NB: Keep in sync with OverrideView.ui
 52		s_gbModelList.append(GB_MODEL_AUTODETECT);
 53		s_gbModelList.append(GB_MODEL_DMG);
 54		s_gbModelList.append(GB_MODEL_SGB);
 55		s_gbModelList.append(GB_MODEL_CGB);
 56		s_gbModelList.append(GB_MODEL_AGB);
 57	}
 58#endif
 59	m_ui.setupUi(this);
 60
 61	connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
 62		m_ui.hwRTC->setEnabled(!enabled);
 63		m_ui.hwGyro->setEnabled(!enabled);
 64		m_ui.hwLight->setEnabled(!enabled);
 65		m_ui.hwTilt->setEnabled(!enabled);
 66		m_ui.hwRumble->setEnabled(!enabled);
 67	});
 68
 69	m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8));
 70	m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8));
 71	m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50));
 72	m_colorPickers[3] = ColorPicker(m_ui.color3, QColor(0x00, 0x00, 0x00));
 73	m_colorPickers[4] = ColorPicker(m_ui.color4, QColor(0xF8, 0xF8, 0xF8));
 74	m_colorPickers[5] = ColorPicker(m_ui.color5, QColor(0xA8, 0xA8, 0xA8));
 75	m_colorPickers[6] = ColorPicker(m_ui.color6, QColor(0x50, 0x50, 0x50));
 76	m_colorPickers[7] = ColorPicker(m_ui.color7, QColor(0x00, 0x00, 0x00));
 77	m_colorPickers[8] = ColorPicker(m_ui.color8, QColor(0xF8, 0xF8, 0xF8));
 78	m_colorPickers[9] = ColorPicker(m_ui.color9, QColor(0xA8, 0xA8, 0xA8));
 79	m_colorPickers[10] = ColorPicker(m_ui.color10, QColor(0x50, 0x50, 0x50));
 80	m_colorPickers[11] = ColorPicker(m_ui.color11, QColor(0x00, 0x00, 0x00));
 81	for (int colorId = 0; colorId < 12; ++colorId) {
 82		connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
 83			m_gbColors[colorId] = color.rgb() | 0xFF000000;
 84		});
 85	}
 86
 87#ifndef M_CORE_GBA
 88	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
 89#endif
 90#ifndef M_CORE_GB
 91	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
 92#endif
 93
 94	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
 95	connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
 96}
 97
 98void OverrideView::setController(std::shared_ptr<CoreController> controller) {
 99	m_controller = controller;
100	connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
101	connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
102	updateOverrides();
103}
104
105void OverrideView::saveOverride() {
106	if (!m_controller) {
107		m_savePending = true;
108		return;
109	}
110
111	Override* override = m_controller->override();
112	if (!override) {
113		return;
114	}
115	m_config->saveOverride(*override);
116}
117
118void OverrideView::updateOverrides() {
119	if (!m_controller) {
120		return;
121	}
122#ifdef M_CORE_GBA
123	if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
124		std::unique_ptr<GBAOverride> gba(new GBAOverride);
125		memset(gba->override.id, 0, 4);
126		gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
127		gba->override.hardware = HW_NO_OVERRIDE;
128		gba->override.idleLoop = IDLE_LOOP_NONE;
129		gba->override.mirroring = false;
130
131		if (!m_ui.hwAutodetect->isChecked()) {
132			gba->override.hardware = HW_NONE;
133			if (m_ui.hwRTC->isChecked()) {
134				gba->override.hardware |= HW_RTC;
135			}
136			if (m_ui.hwGyro->isChecked()) {
137				gba->override.hardware |= HW_GYRO;
138			}
139			if (m_ui.hwLight->isChecked()) {
140				gba->override.hardware |= HW_LIGHT_SENSOR;
141			}
142			if (m_ui.hwTilt->isChecked()) {
143				gba->override.hardware |= HW_TILT;
144			}
145			if (m_ui.hwRumble->isChecked()) {
146				gba->override.hardware |= HW_RUMBLE;
147			}
148		}
149		if (m_ui.hwGBPlayer->isChecked()) {
150			gba->override.hardware |= HW_GB_PLAYER_DETECTION;
151		}
152
153		bool ok;
154		uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
155		if (ok) {
156			gba->override.idleLoop = parsedIdleLoop;
157		}
158
159		if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
160		    gba->override.idleLoop != IDLE_LOOP_NONE) {
161			m_controller->setOverride(std::move(gba));
162		} else {
163			m_controller->clearOverride();
164		}
165	}
166#endif
167#ifdef M_CORE_GB
168	if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
169		std::unique_ptr<GBOverride> gb(new GBOverride);
170		gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
171		gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
172		bool hasColor = false;
173		for (int i = 0; i < 12; ++i) {
174			gb->override.gbColors[i] = m_gbColors[i];
175			hasColor = hasColor || (m_gbColors[i] & 0xFF000000);
176		}
177		bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
178		hasOverride = hasOverride || hasColor;
179		if (hasOverride) {
180			m_controller->setOverride(std::move(gb));
181		} else {
182			m_controller->clearOverride();
183		}
184	}
185#endif
186}
187
188void OverrideView::gameStarted() {
189	CoreController::Interrupter interrupter(m_controller);
190	mCoreThread* thread = m_controller->thread();
191
192	m_ui.tabWidget->setEnabled(false);
193
194	switch (thread->core->platform(thread->core)) {
195#ifdef M_CORE_GBA
196	case PLATFORM_GBA: {
197		m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
198		GBA* gba = static_cast<GBA*>(thread->core->board);
199		m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
200		m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
201		m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
202		m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
203		m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
204		m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
205		m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
206
207		if (gba->idleLoop != IDLE_LOOP_NONE) {
208			m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
209		} else {
210			m_ui.idleLoop->clear();
211		}
212		break;
213	}
214#endif
215#ifdef M_CORE_GB
216	case PLATFORM_GB: {
217		m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
218		GB* gb = static_cast<GB*>(thread->core->board);
219		int mbc = s_mbcList.indexOf(gb->memory.mbcType);
220		if (mbc >= 0) {
221			m_ui.mbc->setCurrentIndex(mbc);
222		} else {
223			m_ui.mbc->setCurrentIndex(0);
224		}
225		int model = s_gbModelList.indexOf(gb->model);
226		if (model >= 0) {
227			m_ui.gbModel->setCurrentIndex(model);
228		} else {
229			m_ui.gbModel->setCurrentIndex(0);
230		}
231		break;
232	}
233#endif
234	case PLATFORM_NONE:
235		break;
236	}
237
238	if (m_savePending) {
239		m_savePending = false;
240		saveOverride();
241	}
242}
243
244void OverrideView::gameStopped() {
245	m_controller.reset();
246	m_ui.tabWidget->setEnabled(true);
247	m_ui.savetype->setCurrentIndex(0);
248	m_ui.idleLoop->clear();
249
250	m_ui.mbc->setCurrentIndex(0);
251	m_ui.gbModel->setCurrentIndex(0);
252}