all repos — mgba @ d1db97cf0c5a5861446aae0d88d934bcd4441b04

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_HuC3);
 47	}
 48	if (s_gbModelList.isEmpty()) {
 49		// NB: Keep in sync with OverrideView.ui
 50		s_gbModelList.append(GB_MODEL_AUTODETECT);
 51		s_gbModelList.append(GB_MODEL_DMG);
 52		s_gbModelList.append(GB_MODEL_CGB);
 53		s_gbModelList.append(GB_MODEL_AGB);
 54	}
 55#endif
 56	m_ui.setupUi(this);
 57
 58	connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
 59		m_ui.hwRTC->setEnabled(!enabled);
 60		m_ui.hwGyro->setEnabled(!enabled);
 61		m_ui.hwLight->setEnabled(!enabled);
 62		m_ui.hwTilt->setEnabled(!enabled);
 63		m_ui.hwRumble->setEnabled(!enabled);
 64	});
 65
 66	connect(m_ui.savetype, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
 67	connect(m_ui.hwAutodetect, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 68	connect(m_ui.hwRTC, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 69	connect(m_ui.hwGyro, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 70	connect(m_ui.hwLight, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 71	connect(m_ui.hwTilt, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 72	connect(m_ui.hwRumble, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 73	connect(m_ui.hwGBPlayer, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
 74
 75	connect(m_ui.gbModel, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
 76	connect(m_ui.mbc, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
 77
 78	m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8));
 79	m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8));
 80	m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50));
 81	m_colorPickers[3] = ColorPicker(m_ui.color3, QColor(0x00, 0x00, 0x00));
 82	for (int colorId = 0; colorId < 4; ++colorId) {
 83		connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) {
 84			m_gbColors[colorId] = color.rgb();
 85			updateOverrides();
 86		});
 87	}
 88
 89	connect(m_ui.tabWidget, &QTabWidget::currentChanged, this, &OverrideView::updateOverrides);
 90#ifndef M_CORE_GBA
 91	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
 92#endif
 93#ifndef M_CORE_GB
 94	m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
 95#endif
 96
 97	connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
 98	connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
 99	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
100}
101
102void OverrideView::setController(std::shared_ptr<CoreController> controller) {
103	m_controller = controller;
104	gameStarted();
105	connect(controller.get(), &CoreController::stopping, this, &OverrideView::gameStopped);
106	if (m_override) {
107		m_controller->setOverride(std::move(m_override));
108	} else {
109		m_controller->clearOverride();
110	}
111}
112
113void OverrideView::saveOverride() {
114	if (!m_config || !m_controller) {
115		return;
116	}
117	m_config->saveOverride(*m_controller->override());
118}
119
120void OverrideView::updateOverrides() {
121#ifdef M_CORE_GBA
122	if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
123		std::unique_ptr<GBAOverride> gba(new GBAOverride);
124		memset(gba->override.id, 0, 4);
125		gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
126		gba->override.hardware = HW_NO_OVERRIDE;
127		gba->override.idleLoop = IDLE_LOOP_NONE;
128		gba->override.mirroring = false;
129
130		if (!m_ui.hwAutodetect->isChecked()) {
131			gba->override.hardware = HW_NONE;
132			if (m_ui.hwRTC->isChecked()) {
133				gba->override.hardware |= HW_RTC;
134			}
135			if (m_ui.hwGyro->isChecked()) {
136				gba->override.hardware |= HW_GYRO;
137			}
138			if (m_ui.hwLight->isChecked()) {
139				gba->override.hardware |= HW_LIGHT_SENSOR;
140			}
141			if (m_ui.hwTilt->isChecked()) {
142				gba->override.hardware |= HW_TILT;
143			}
144			if (m_ui.hwRumble->isChecked()) {
145				gba->override.hardware |= HW_RUMBLE;
146			}
147		}
148		if (m_ui.hwGBPlayer->isChecked()) {
149			gba->override.hardware |= HW_GB_PLAYER_DETECTION;
150		}
151
152		bool ok;
153		uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
154		if (ok) {
155			gba->override.idleLoop = parsedIdleLoop;
156		}
157
158
159		if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
160		    gba->override.idleLoop != IDLE_LOOP_NONE) {
161			m_override = std::move(gba);
162		} else {
163			m_override.reset();
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		gb->override.gbColors[0] = m_gbColors[0];
173		gb->override.gbColors[1] = m_gbColors[1];
174		gb->override.gbColors[2] = m_gbColors[2];
175		gb->override.gbColors[3] = m_gbColors[3];
176		bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
177		hasOverride = hasOverride || (m_gbColors[0] | m_gbColors[1] | m_gbColors[2] | m_gbColors[3]);
178		if (hasOverride) {
179			m_override = std::move(gb);
180		} else {
181			m_override.reset();
182		}
183	}
184#endif
185}
186
187void OverrideView::gameStarted() {
188	CoreController::Interrupter interrupter(m_controller);
189	mCoreThread* thread = m_controller->thread();
190
191	m_ui.tabWidget->setEnabled(false);
192	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
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
239void OverrideView::gameStopped() {
240	m_controller.reset();
241	m_ui.tabWidget->setEnabled(true);
242	m_ui.savetype->setCurrentIndex(0);
243	m_ui.idleLoop->clear();
244	m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
245
246	m_ui.mbc->setCurrentIndex(0);
247	m_ui.gbModel->setCurrentIndex(0);
248
249	updateOverrides();
250}