all repos — mgba @ aaf96b43cd02bfb878e04011ffb1dafe418fcd33

mGBA Game Boy Advance Emulator

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 "GameController.h"
 9
10extern "C" {
11#include "gba-thread.h"
12}
13
14using namespace QGBA;
15
16GamePakView::GamePakView(GameController* controller, QWidget* parent)
17	: QWidget(parent)
18	, m_controller(controller)
19{
20	m_ui.setupUi(this);
21
22	connect(controller, SIGNAL(gameStarted(GBAThread*)), this, SLOT(gameStarted(GBAThread*)));
23	connect(controller, SIGNAL(gameStopped(GBAThread*)), this, SLOT(gameStopped()));
24	connect(m_ui.lightSpin, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
25	connect(m_ui.lightSlide, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
26
27	if (controller->isLoaded()) {
28		gameStarted(controller->thread());
29	}
30}
31
32void GamePakView::gameStarted(GBAThread* thread) {
33	if (!thread->gba) {
34		gameStopped();
35		return;
36	}
37	SavedataType savetype = thread->gba->memory.savedata.type;
38	if (m_ui.savetype->currentIndex() > 0) {
39		if (savetype > SAVEDATA_NONE) {
40			VFile* vf = thread->gba->memory.savedata.vf;
41			GBASavedataDeinit(&thread->gba->memory.savedata);
42			GBASavedataInit(&thread->gba->memory.savedata, vf);
43		}
44		savetype = static_cast<SavedataType>(m_ui.savetype->currentIndex() - 1);
45		GBASavedataForceType(&thread->gba->memory.savedata, savetype);
46	}
47
48	if (savetype > SAVEDATA_NONE) {
49		m_ui.savetype->setCurrentIndex(savetype + 1);
50	}
51	m_ui.savetype->setEnabled(false);
52
53	m_ui.sensorRTC->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_RTC);
54	m_ui.sensorGyro->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_GYRO);
55	m_ui.sensorLight->setChecked(thread->gba->memory.gpio.gpioDevices & GPIO_LIGHT_SENSOR);
56}
57
58void GamePakView::gameStopped() {	
59	m_ui.savetype->setCurrentIndex(0);
60	m_ui.savetype->setEnabled(true);
61
62	m_ui.sensorRTC->setChecked(false);
63	m_ui.sensorGyro->setChecked(false);
64	m_ui.sensorLight->setChecked(false);
65}
66
67void GamePakView::setLuminanceValue(int value) {
68	bool oldState;
69	value = std::max(0, std::min(value, 255));
70
71	oldState = m_ui.lightSpin->blockSignals(true);
72	m_ui.lightSpin->setValue(value);
73	m_ui.lightSpin->blockSignals(oldState);
74
75	oldState = m_ui.lightSlide->blockSignals(true);
76	m_ui.lightSlide->setValue(value);
77	m_ui.lightSlide->blockSignals(oldState);
78
79	m_controller->setLuminanceValue(value);
80}