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/gba.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 memset(m_override.id, 0, 4);
60 m_override.savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
61 m_override.hardware = HW_NO_OVERRIDE;
62 m_override.idleLoop = IDLE_LOOP_NONE;
63 m_override.mirroring = false;
64
65 if (!m_ui.hwAutodetect->isChecked()) {
66 m_override.hardware = HW_NONE;
67 if (m_ui.hwRTC->isChecked()) {
68 m_override.hardware |= HW_RTC;
69 }
70 if (m_ui.hwGyro->isChecked()) {
71 m_override.hardware |= HW_GYRO;
72 }
73 if (m_ui.hwLight->isChecked()) {
74 m_override.hardware |= HW_LIGHT_SENSOR;
75 }
76 if (m_ui.hwTilt->isChecked()) {
77 m_override.hardware |= HW_TILT;
78 }
79 if (m_ui.hwRumble->isChecked()) {
80 m_override.hardware |= HW_RUMBLE;
81 }
82 }
83 if (m_ui.hwGBPlayer->isChecked()) {
84 m_override.hardware |= HW_GB_PLAYER_DETECTION;
85 }
86
87 bool ok;
88 uint32_t parsedIdleLoop = m_ui.idleLoop->text().toInt(&ok, 16);
89 if (ok) {
90 m_override.idleLoop = parsedIdleLoop;
91 }
92
93 if (m_override.savetype != SAVEDATA_AUTODETECT || m_override.hardware != HW_NO_OVERRIDE ||
94 m_override.idleLoop != IDLE_LOOP_NONE) {
95 m_controller->setOverride(m_override);
96 } else {
97 m_controller->clearOverride();
98 }
99}
100
101void OverrideView::gameStarted(mCoreThread* thread) {
102 if (!thread->core) {
103 gameStopped();
104 return;
105 }
106 if (thread->core->platform(thread->core) != PLATFORM_GBA) {
107 close();
108 return;
109 }
110 GBA* gba = static_cast<GBA*>(thread->core->board);
111 m_ui.savetype->setCurrentIndex(gba->memory.savedata.type + 1);
112 m_ui.savetype->setEnabled(false);
113
114 m_ui.hwAutodetect->setEnabled(false);
115 m_ui.hwRTC->setEnabled(false);
116 m_ui.hwGyro->setEnabled(false);
117 m_ui.hwLight->setEnabled(false);
118 m_ui.hwTilt->setEnabled(false);
119 m_ui.hwRumble->setEnabled(false);
120
121 m_ui.hwRTC->setChecked(gba->memory.hw.devices & HW_RTC);
122 m_ui.hwGyro->setChecked(gba->memory.hw.devices & HW_GYRO);
123 m_ui.hwLight->setChecked(gba->memory.hw.devices & HW_LIGHT_SENSOR);
124 m_ui.hwTilt->setChecked(gba->memory.hw.devices & HW_TILT);
125 m_ui.hwRumble->setChecked(gba->memory.hw.devices & HW_RUMBLE);
126 m_ui.hwGBPlayer->setChecked(gba->memory.hw.devices & HW_GB_PLAYER_DETECTION);
127
128 if (gba->idleLoop != IDLE_LOOP_NONE) {
129 m_ui.idleLoop->setText(QString::number(gba->idleLoop, 16));
130 } else {
131 m_ui.idleLoop->clear();
132 }
133
134 GBAGetGameCode(gba, m_override.id);
135 m_override.hardware = gba->memory.hw.devices;
136 m_override.savetype = gba->memory.savedata.type;
137 m_override.idleLoop = gba->idleLoop;
138
139 m_ui.idleLoop->setEnabled(false);
140
141 m_ui.save->setEnabled(m_config);
142}
143
144void OverrideView::gameStopped() {
145 m_ui.savetype->setCurrentIndex(0);
146 m_ui.savetype->setEnabled(true);
147
148 m_ui.hwAutodetect->setEnabled(true);
149 m_ui.hwRTC->setEnabled(!m_ui.hwAutodetect->isChecked());
150 m_ui.hwGyro->setEnabled(!m_ui.hwAutodetect->isChecked());
151 m_ui.hwLight->setEnabled(!m_ui.hwAutodetect->isChecked());
152 m_ui.hwTilt->setEnabled(!m_ui.hwAutodetect->isChecked());
153 m_ui.hwRumble->setEnabled(!m_ui.hwAutodetect->isChecked());
154
155 m_ui.hwAutodetect->setChecked(true);
156 m_ui.hwRTC->setChecked(false);
157 m_ui.hwGyro->setChecked(false);
158 m_ui.hwLight->setChecked(false);
159 m_ui.hwTilt->setChecked(false);
160 m_ui.hwRumble->setChecked(false);
161
162 m_ui.idleLoop->setEnabled(true);
163 m_ui.idleLoop->clear();
164
165 m_ui.save->setEnabled(false);
166
167 updateOverrides();
168}