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 <QColorDialog>
9#include <QPushButton>
10
11#include "ConfigController.h"
12#include "GameController.h"
13
14#ifdef M_CORE_GBA
15#include "GBAOverride.h"
16#include <mgba/internal/gba/gba.h>
17#endif
18
19#ifdef M_CORE_GB
20#include "GBOverride.h"
21#include <mgba/internal/gb/gb.h>
22#endif
23
24using namespace QGBA;
25
26#ifdef M_CORE_GB
27QList<enum GBModel> OverrideView::s_gbModelList;
28QList<enum GBMemoryBankControllerType> OverrideView::s_mbcList;
29#endif
30
31OverrideView::OverrideView(GameController* controller, ConfigController* config, QWidget* parent)
32 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
33 , m_controller(controller)
34 , m_config(config)
35{
36#ifdef M_CORE_GB
37 if (s_mbcList.isEmpty()) {
38 // NB: Keep in sync with OverrideView.ui
39 s_mbcList.append(GB_MBC_AUTODETECT);
40 s_mbcList.append(GB_MBC_NONE);
41 s_mbcList.append(GB_MBC1);
42 s_mbcList.append(GB_MBC2);
43 s_mbcList.append(GB_MBC3);
44 s_mbcList.append(GB_MBC3_RTC);
45 s_mbcList.append(GB_MBC5);
46 s_mbcList.append(GB_MBC5_RUMBLE);
47 s_mbcList.append(GB_MBC7);
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_CGB);
55 s_gbModelList.append(GB_MODEL_AGB);
56 }
57#endif
58 m_ui.setupUi(this);
59
60 connect(controller, &GameController::gameStarted, this, &OverrideView::gameStarted);
61 connect(controller, &GameController::gameStopped, this, &OverrideView::gameStopped);
62
63 connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
64 m_ui.hwRTC->setEnabled(!enabled);
65 m_ui.hwGyro->setEnabled(!enabled);
66 m_ui.hwLight->setEnabled(!enabled);
67 m_ui.hwTilt->setEnabled(!enabled);
68 m_ui.hwRumble->setEnabled(!enabled);
69 });
70
71 connect(m_ui.savetype, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
72 connect(m_ui.hwAutodetect, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
73 connect(m_ui.hwRTC, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
74 connect(m_ui.hwGyro, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
75 connect(m_ui.hwLight, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
76 connect(m_ui.hwTilt, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
77 connect(m_ui.hwRumble, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
78 connect(m_ui.hwGBPlayer, &QAbstractButton::clicked, this, &OverrideView::updateOverrides);
79
80 connect(m_ui.gbModel, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
81 connect(m_ui.mbc, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides);
82
83 QPalette palette = m_ui.color0->palette();
84 palette.setColor(backgroundRole(), QColor(0xF8, 0xF8, 0xF8));
85 m_ui.color0->setPalette(palette);
86 palette.setColor(backgroundRole(), QColor(0xA8, 0xA8, 0xA8));
87 m_ui.color1->setPalette(palette);
88 palette.setColor(backgroundRole(), QColor(0x50, 0x50, 0x50));
89 m_ui.color2->setPalette(palette);
90 palette.setColor(backgroundRole(), QColor(0x00, 0x00, 0x00));
91 m_ui.color3->setPalette(palette);
92
93 m_ui.color0->installEventFilter(this);
94 m_ui.color1->installEventFilter(this);
95 m_ui.color2->installEventFilter(this);
96 m_ui.color3->installEventFilter(this);
97
98 connect(m_ui.tabWidget, &QTabWidget::currentChanged, this, &OverrideView::updateOverrides);
99#ifndef M_CORE_GBA
100 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGBA));
101#endif
102#ifndef M_CORE_GB
103 m_ui.tabWidget->removeTab(m_ui.tabWidget->indexOf(m_ui.tabGB));
104#endif
105
106 connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &OverrideView::saveOverride);
107 connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &QWidget::close);
108 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
109
110 if (controller->isLoaded()) {
111 gameStarted(controller->thread());
112 }
113}
114
115bool OverrideView::eventFilter(QObject* obj, QEvent* event) {
116#ifdef M_CORE_GB
117 if (event->type() != QEvent::MouseButtonRelease) {
118 return false;
119 }
120 int colorId;
121 if (obj == m_ui.color0) {
122 colorId = 0;
123 } else if (obj == m_ui.color1) {
124 colorId = 1;
125 } else if (obj == m_ui.color2) {
126 colorId = 2;
127 } else if (obj == m_ui.color3) {
128 colorId = 3;
129 } else {
130 return false;
131 }
132
133 QWidget* swatch = static_cast<QWidget*>(obj);
134
135 QColorDialog* colorPicker = new QColorDialog;
136 colorPicker->setAttribute(Qt::WA_DeleteOnClose);
137 colorPicker->open();
138 connect(colorPicker, &QColorDialog::colorSelected, [this, swatch, colorId](const QColor& color) {
139 QPalette palette = swatch->palette();
140 palette.setColor(backgroundRole(), color);
141 swatch->setPalette(palette);
142 m_gbColors[colorId] = color.rgb();
143 updateOverrides();
144 });
145 return true;
146#else
147 return false;
148#endif
149}
150
151void OverrideView::saveOverride() {
152 if (!m_config) {
153 return;
154 }
155 m_config->saveOverride(*m_controller->override());
156}
157
158void OverrideView::updateOverrides() {
159#ifdef M_CORE_GBA
160 if (m_ui.tabWidget->currentWidget() == m_ui.tabGBA) {
161 GBAOverride* gba = new GBAOverride;
162 memset(gba->override.id, 0, 4);
163 gba->override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
164 gba->override.hardware = HW_NO_OVERRIDE;
165 gba->override.idleLoop = IDLE_LOOP_NONE;
166 gba->override.mirroring = false;
167
168 if (!m_ui.hwAutodetect->isChecked()) {
169 gba->override.hardware = HW_NONE;
170 if (m_ui.hwRTC->isChecked()) {
171 gba->override.hardware |= HW_RTC;
172 }
173 if (m_ui.hwGyro->isChecked()) {
174 gba->override.hardware |= HW_GYRO;
175 }
176 if (m_ui.hwLight->isChecked()) {
177 gba->override.hardware |= HW_LIGHT_SENSOR;
178 }
179 if (m_ui.hwTilt->isChecked()) {
180 gba->override.hardware |= HW_TILT;
181 }
182 if (m_ui.hwRumble->isChecked()) {
183 gba->override.hardware |= HW_RUMBLE;
184 }
185 }
186 if (m_ui.hwGBPlayer->isChecked()) {
187 gba->override.hardware |= HW_GB_PLAYER_DETECTION;
188 }
189
190 bool ok;
191 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
192 if (ok) {
193 gba->override.idleLoop = parsedIdleLoop;
194 }
195
196 if (gba->override.savetype != SAVEDATA_AUTODETECT || gba->override.hardware != HW_NO_OVERRIDE ||
197 gba->override.idleLoop != IDLE_LOOP_NONE) {
198 m_controller->setOverride(gba);
199 } else {
200 m_controller->clearOverride();
201 delete gba;
202 }
203 }
204#endif
205#ifdef M_CORE_GB
206 if (m_ui.tabWidget->currentWidget() == m_ui.tabGB) {
207 GBOverride* gb = new GBOverride;
208 gb->override.mbc = s_mbcList[m_ui.mbc->currentIndex()];
209 gb->override.model = s_gbModelList[m_ui.gbModel->currentIndex()];
210 gb->override.gbColors[0] = m_gbColors[0];
211 gb->override.gbColors[1] = m_gbColors[1];
212 gb->override.gbColors[2] = m_gbColors[2];
213 gb->override.gbColors[3] = m_gbColors[3];
214 bool hasOverride = gb->override.mbc != GB_MBC_AUTODETECT || gb->override.model != GB_MODEL_AUTODETECT;
215 hasOverride = hasOverride || (m_gbColors[0] | m_gbColors[1] | m_gbColors[2] | m_gbColors[3]);
216 if (hasOverride) {
217 m_controller->setOverride(gb);
218 } else {
219 m_controller->clearOverride();
220 delete gb;
221 }
222 }
223#endif
224}
225
226void OverrideView::gameStarted(mCoreThread* thread) {
227 if (!thread->core) {
228 gameStopped();
229 return;
230 }
231
232 m_ui.tabWidget->setEnabled(false);
233 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true);
234
235 switch (thread->core->platform(thread->core)) {
236#ifdef M_CORE_GBA
237 case PLATFORM_GBA: {
238 m_ui.tabWidget->setCurrentWidget(m_ui.tabGBA);
239 GBA* gba = static_cast<GBA*>(thread->core->board);
240 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
241 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
242 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
243 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
244 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
245 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
246 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
247
248 if (gba->idleLoop != IDLE_LOOP_NONE) {
249 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
250 } else {
251 m_ui.idleLoop->clear();
252 }
253 break;
254 }
255#endif
256#ifdef M_CORE_GB
257 case PLATFORM_GB: {
258 m_ui.tabWidget->setCurrentWidget(m_ui.tabGB);
259 GB* gb = static_cast<GB*>(thread->core->board);
260 int mbc = s_mbcList.indexOf(gb->memory.mbcType);
261 if (mbc >= 0) {
262 m_ui.mbc->setCurrentIndex(mbc);
263 } else {
264 m_ui.mbc->setCurrentIndex(0);
265 }
266 int model = s_gbModelList.indexOf(gb->model);
267 if (model >= 0) {
268 m_ui.gbModel->setCurrentIndex(model);
269 } else {
270 m_ui.gbModel->setCurrentIndex(0);
271 }
272 break;
273 }
274#endif
275 case PLATFORM_NONE:
276 break;
277 }
278}
279
280void OverrideView::gameStopped() {
281 m_ui.tabWidget->setEnabled(true);
282 m_ui.savetype->setCurrentIndex(0);
283 m_ui.idleLoop->clear();
284 m_ui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false);
285
286 m_ui.mbc->setCurrentIndex(0);
287 m_ui.gbModel->setCurrentIndex(0);
288
289 updateOverrides();
290}