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 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
19 , m_controller(controller)
20 , m_config(config)
21{
22 m_ui.setupUi(this);
23
24 connect(controller, SIGNAL(gameStarted(mCoreThread*, const QString&)), this, SLOT(gameStarted(mCoreThread*)));
25 connect(controller, SIGNAL(gameStopped(mCoreThread*)), 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(mCoreThread* thread) {
103 if (!thread->core) {
104 gameStopped();
105 return;
106 }
107 GBA* gba = static_cast<GBA*>(thread->core->board);
108 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
109 m_ui.savetype->setEnabled(false);
110
111 m_ui.hwAutodetect->setEnabled(false);
112 m_ui.hwRTC->setEnabled(false);
113 m_ui.hwGyro->setEnabled(false);
114 m_ui.hwLight->setEnabled(false);
115 m_ui.hwTilt->setEnabled(false);
116 m_ui.hwRumble->setEnabled(false);
117
118 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
119 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
120 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
121 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
122 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
123 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
124
125 if (gba->idleLoop != IDLE_LOOP_NONE) {
126 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
127 } else {
128 m_ui.idleLoop->clear();
129 }
130
131 GBAGetGameCode(gba, m_override.id);
132 m_override.hardware = gba->memory.hw.devices;
133 m_override.savetype = gba->memory.savedata.type;
134 m_override.idleLoop = gba->idleLoop;
135
136 m_ui.idleLoop->setEnabled(false);
137
138 m_ui.save->setEnabled(m_config);
139}
140
141void OverrideView::gameStopped() {
142 m_ui.savetype->setCurrentIndex(0);
143 m_ui.savetype->setEnabled(true);
144
145 m_ui.hwAutodetect->setEnabled(true);
146 m_ui.hwRTC->setEnabled(!m_ui.hwAutodetect->isChecked());
147 m_ui.hwGyro->setEnabled(!m_ui.hwAutodetect->isChecked());
148 m_ui.hwLight->setEnabled(!m_ui.hwAutodetect->isChecked());
149 m_ui.hwTilt->setEnabled(!m_ui.hwAutodetect->isChecked());
150 m_ui.hwRumble->setEnabled(!m_ui.hwAutodetect->isChecked());
151
152 m_ui.hwAutodetect->setChecked(true);
153 m_ui.hwRTC->setChecked(false);
154 m_ui.hwGyro->setChecked(false);
155 m_ui.hwLight->setChecked(false);
156 m_ui.hwTilt->setChecked(false);
157 m_ui.hwRumble->setChecked(false);
158
159 m_ui.idleLoop->setEnabled(true);
160 m_ui.idleLoop->clear();
161
162 m_ui.save->setEnabled(false);
163
164 updateOverrides();
165}