all repos — mgba @ d3d7b9606c28b285898547feb8a6ff232f60a611

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	for (int colorId = 0; colorId < 4; ++colorId) {
 74		connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
 75			m_gbColors[colorId] = color.rgb();
 76		});
 77	}
 78
 79#ifndef M_CORE_GBA
 80	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
 81#endif
 82#ifndef M_CORE_GB
 83	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
 84#endif
 85
 86	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
 87	connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
 88}
 89
 90void OverrideView::setController(std::shared_ptr<CoreController> controller) {
 91	m_controller = controller;
 92	connect(controller.get(), &CoreController::started, this, &OverrideView::gameStarted);
 93	connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
 94	updateOverrides();
 95}
 96
 97void OverrideView::saveOverride() {
 98	if (!m_controller) {
 99		m_savePending = true;
100		return;
101	}
102
103	Override* override = m_controller->override();
104	if (!override) {
105		return;
106	}
107	m_config->saveOverride(*override);
108}
109
110void OverrideView::updateOverrides() {
111	if (!m_controller) {
112		return;
113	}
114#ifdef M_CORE_GBA
115	if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
116		std::unique_ptr<GBAOverride> gba(new GBAOverride);
117		memset(gba->override.id, 0, 4);
118		gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
119		gba->override.hardware = HW_NO_OVERRIDE;
120		gba->override.idleLoop = IDLE_LOOP_NONE;
121		gba->override.mirroring = false;
122
123		if (!m_ui.hwAutodetect->isChecked()) {
124			gba->override.hardware = HW_NONE;
125			if (m_ui.hwRTC->isChecked()) {
126				gba->override.hardware |= HW_RTC;
127			}
128			if (m_ui.hwGyro->isChecked()) {
129				gba->override.hardware |= HW_GYRO;
130			}
131			if (m_ui.hwLight->isChecked()) {
132				gba->override.hardware |= HW_LIGHT_SENSOR;
133			}
134			if (m_ui.hwTilt->isChecked()) {
135				gba->override.hardware |= HW_TILT;
136			}
137			if (m_ui.hwRumble->isChecked()) {
138				gba->override.hardware |= HW_RUMBLE;
139			}
140		}
141		if (m_ui.hwGBPlayer->isChecked()) {
142			gba->override.hardware |= HW_GB_PLAYER_DETECTION;
143		}
144
145		bool ok;
146		uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
147		if (ok) {
148			gba->override.idleLoop = parsedIdleLoop;
149		}
150
151		if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
152		    gba->override.idleLoop != IDLE_LOOP_NONE) {
153			m_controller->setOverride(std::move(gba));
154		} else {
155			m_controller->clearOverride();
156		}
157	}
158#endif
159#ifdef M_CORE_GB
160	if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
161		std::unique_ptr<GBOverride> gb(new GBOverride);
162		gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
163		gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
164		gb->override.gbColors[0] = m_gbColors[0];
165		gb->override.gbColors[1] = m_gbColors[1];
166		gb->override.gbColors[2] = m_gbColors[2];
167		gb->override.gbColors[3] = m_gbColors[3];
168		bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
169		hasOverride = hasOverride || (m_gbColors[0] | m_gbColors[1] | m_gbColors[2] | m_gbColors[3]);
170		if (hasOverride) {
171			m_controller->setOverride(std::move(gb));
172		} else {
173			m_controller->clearOverride();
174		}
175	}
176#endif
177}
178
179void OverrideView::gameStarted() {
180	CoreController::Interrupter interrupter(m_controller);
181	mCoreThread* thread = m_controller->thread();
182
183	m_ui.tabWidget->setEnabled(false);
184
185	switch (thread->core->platform(thread->core)) {
186#ifdef M_CORE_GBA
187	case PLATFORM_GBA: {
188		m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
189		GBA* gba = static_cast<GBA*>(thread->core->board);
190		m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
191		m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
192		m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
193		m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
194		m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
195		m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
196		m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
197
198		if (gba->idleLoop != IDLE_LOOP_NONE) {
199			m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
200		} else {
201			m_ui.idleLoop->clear();
202		}
203		break;
204	}
205#endif
206#ifdef M_CORE_GB
207	case PLATFORM_GB: {
208		m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
209		GB* gb = static_cast<GB*>(thread->core->board);
210		int mbc = s_mbcList.indexOf(gb->memory.mbcType);
211		if (mbc >= 0) {
212			m_ui.mbc->setCurrentIndex(mbc);
213		} else {
214			m_ui.mbc->setCurrentIndex(0);
215		}
216		int model = s_gbModelList.indexOf(gb->model);
217		if (model >= 0) {
218			m_ui.gbModel->setCurrentIndex(model);
219		} else {
220			m_ui.gbModel->setCurrentIndex(0);
221		}
222		break;
223	}
224#endif
225	case PLATFORM_NONE:
226		break;
227	}
228
229	if (m_savePending) {
230		m_savePending = false;
231		saveOverride();
232	}
233}
234
235void OverrideView::gameStopped() {
236	m_controller.reset();
237	m_ui.tabWidget->setEnabled(true);
238	m_ui.savetype->setCurrentIndex(0);
239	m_ui.idleLoop->clear();
240
241	m_ui.mbc->setCurrentIndex(0);
242	m_ui.gbModel->setCurrentIndex(0);
243}