src/platform/qt/GamePakView.cpp (view raw)
1/* Copyright (c) 2013-2014 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 "GamePakView.h"
7
8#include "ConfigController.h"
9#include "GameController.h"
10
11extern "C" {
12#include "gba-thread.h"
13}
14
15using namespace QGBA;
16
17GamePakView::GamePakView(GameController* controller, ConfigController* config, QWidget* parent)
18 : QWidget(parent)
19 , m_controller(controller)
20 , m_config(config)
21{
22 m_ui.setupUi(this);
23
24 connect(controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
25 connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
26 connect(m_ui.lightSpin, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
27 connect(m_ui.lightSlide, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
28
29 connect(m_ui.timeNoOverride, SIGNAL(clicked()), controller, SLOT(setRealTime()));
30 connect(m_ui.timeFixed, &QRadioButton::clicked, [controller, this] () {
31 controller->setFixedTime(m_ui.time->dateTime());
32 });
33 connect(m_ui.timeFakeEpoch, &QRadioButton::clicked, [controller, this] () {
34 controller->setFakeEpoch(m_ui.time->dateTime());
35 });
36 connect(m_ui.time, &QDateTimeEdit::dateTimeChanged, [controller, this] (const QDateTime&) {
37 m_ui.timeButtons->checkedButton()->clicked();
38 });
39 connect(m_ui.timeNow, &QPushButton::clicked, [controller, this] () {
40 m_ui.time->setDateTime(QDateTime::currentDateTime());
41 });
42
43 connect(m_ui.hwAutodetect, &QAbstractButton::toggled, [this] (bool enabled) {
44 m_ui.hwRTC->setEnabled(!enabled);
45 m_ui.hwGyro->setEnabled(!enabled);
46 m_ui.hwLight->setEnabled(!enabled);
47 m_ui.hwTilt->setEnabled(!enabled);
48 m_ui.hwRumble->setEnabled(!enabled);
49 });
50
51 connect(m_ui.savetype, SIGNAL(currentIndexChanged(int)), this, SLOT(updateOverrides()));
52 connect(m_ui.hwAutodetect, SIGNAL(clicked()), this, SLOT(updateOverrides()));
53 connect(m_ui.hwRTC, SIGNAL(clicked()), this, SLOT(updateOverrides()));
54 connect(m_ui.hwGyro, SIGNAL(clicked()), this, SLOT(updateOverrides()));
55 connect(m_ui.hwLight, SIGNAL(clicked()), this, SLOT(updateOverrides()));
56 connect(m_ui.hwTilt, SIGNAL(clicked()), this, SLOT(updateOverrides()));
57 connect(m_ui.hwRumble, SIGNAL(clicked()), this, SLOT(updateOverrides()));
58
59 connect(m_ui.save, SIGNAL(clicked()), this, SLOT(saveOverride()));
60
61 if (controller->isLoaded()) {
62 gameStarted(controller->thread());
63 }
64}
65
66void GamePakView::saveOverride() {
67 if (!m_config) {
68 return;
69 }
70 m_config->saveOverride(m_override);
71}
72
73void GamePakView::updateOverrides() {
74 m_override = (GBACartridgeOverride) {
75 "",
76 static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1),
77 GPIO_NO_OVERRIDE,
78 0xFFFFFFFF
79 };
80
81 if (!m_ui.hwAutodetect->isChecked()) {
82 m_override.hardware = GPIO_NONE;
83 if (m_ui.hwRTC->isChecked()) {
84 m_override.hardware |= GPIO_RTC;
85 }
86 if (m_ui.hwGyro->isChecked()) {
87 m_override.hardware |= GPIO_GYRO;
88 }
89 if (m_ui.hwLight->isChecked()) {
90 m_override.hardware |= GPIO_LIGHT_SENSOR;
91 }
92 if (m_ui.hwTilt->isChecked()) {
93 m_override.hardware |= GPIO_TILT;
94 }
95 if (m_ui.hwRumble->isChecked()) {
96 m_override.hardware |= GPIO_RUMBLE;
97 }
98 }
99
100 if (m_override.savetype != SAVEDATA_AUTODETECT || m_override.hardware != GPIO_NO_OVERRIDE) {
101 m_controller->setOverride(m_override);
102 } else {
103 m_controller->clearOverride();
104 }
105}
106
107void GamePakView::gameStarted(GBAThread* thread) {
108 if (!thread->gba) {
109 gameStopped();
110 return;
111 }
112 m_ui.savetype->setCurrentIndex(thread->gba->memory.savedata.type + 1);
113 m_ui.savetype->setEnabled(false);
114
115 m_ui.hwAutodetect->setEnabled(false);
116 m_ui.hwRTC->setEnabled(false);
117 m_ui.hwGyro->setEnabled(false);
118 m_ui.hwLight->setEnabled(false);
119 m_ui.hwTilt->setEnabled(false);
120 m_ui.hwRumble->setEnabled(false);
121
122 m_ui.hwRTC->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_RTC);
123 m_ui.hwGyro->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_GYRO);
124 m_ui.hwLight->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_LIGHT_SENSOR);
125 m_ui.hwTilt->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_TILT);
126 m_ui.hwRumble->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_RUMBLE);
127
128 GBAGetGameCode(thread->gba, m_override.id);
129 m_override.hardware = thread->gba->memory.gpio.gpioDevices;
130 m_override.savetype = thread->gba->memory.savedata.type;
131
132 m_ui.save->setEnabled(m_config);
133}
134
135void GamePakView::gameStopped() {
136 m_ui.savetype->setCurrentIndex(0);
137 m_ui.savetype->setEnabled(true);
138
139 m_ui.hwAutodetect->setEnabled(true);
140 m_ui.hwRTC->setEnabled(!m_ui.hwAutodetect->isChecked());
141 m_ui.hwGyro->setEnabled(!m_ui.hwAutodetect->isChecked());
142 m_ui.hwLight->setEnabled(!m_ui.hwAutodetect->isChecked());
143 m_ui.hwTilt->setEnabled(!m_ui.hwAutodetect->isChecked());
144 m_ui.hwRumble->setEnabled(!m_ui.hwAutodetect->isChecked());
145
146 m_ui.hwRTC->setChecked(false);
147 m_ui.hwGyro->setChecked(false);
148 m_ui.hwLight->setChecked(false);
149 m_ui.hwTilt->setChecked(false);
150 m_ui.hwRumble->setChecked(false);
151
152 m_ui.save->setEnabled(false);
153}
154
155void GamePakView::setLuminanceValue(int value) {
156 bool oldState;
157 value = std::max(0, std::min(value, 255));
158
159 oldState = m_ui.lightSpin->blockSignals(true);
160 m_ui.lightSpin->setValue(value);
161 m_ui.lightSpin->blockSignals(oldState);
162
163 oldState = m_ui.lightSlide->blockSignals(true);
164 m_ui.lightSlide->setValue(value);
165 m_ui.lightSlide->blockSignals(oldState);
166
167 m_controller->setLuminanceValue(value);
168}