all repos — mgba @ d0a332ffd4bcc238f89d042c770d841ef1166039

mGBA Game Boy Advance Emulator

src/platform/qt/SensorView.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 "SensorView.h"
 7
 8#include "GameController.h"
 9#include "InputController.h"
10
11using namespace QGBA;
12
13SensorView::SensorView(GameController* controller, InputController* input, QWidget* parent)
14	: QWidget(parent)
15	, m_controller(controller)
16	, m_input(input)
17	, m_rotation(input->rotationSource())
18 {
19	m_ui.setupUi(this);
20
21	connect(m_ui.lightSpin, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
22	connect(m_ui.lightSlide, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
23
24	connect(m_ui.timeNoOverride, SIGNAL(clicked()), controller, SLOT(setRealTime()));
25	connect(m_ui.timeFixed, &QRadioButton::clicked, [controller, this] () {
26		controller->setFixedTime(m_ui.time->dateTime());
27	});
28	connect(m_ui.timeFakeEpoch, &QRadioButton::clicked, [controller, this] () {
29		controller->setFakeEpoch(m_ui.time->dateTime());
30	});
31	connect(m_ui.time, &QDateTimeEdit::dateTimeChanged, [controller, this] (const QDateTime&) {
32		m_ui.timeButtons->checkedButton()->clicked();
33	});
34	connect(m_ui.timeNow, &QPushButton::clicked, [controller, this] () {
35		m_ui.time->setDateTime(QDateTime::currentDateTime());
36	});
37
38	connect(m_controller, SIGNAL(luminanceValueChanged(int)), this, SLOT(luminanceValueChanged(int)));
39
40	m_timer.setInterval(2);
41	connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSensors()));
42	if (!m_rotation || !m_rotation->readTiltX || !m_rotation->readTiltY) {
43		m_ui.tilt->hide();
44	} else {
45		m_timer.start();
46	}
47
48	if (!m_rotation || !m_rotation->readGyroZ) {
49		m_ui.gyro->hide();
50	} else {
51		m_timer.start();
52	}
53}
54
55void SensorView::updateSensors() {
56	m_controller->threadInterrupt();
57	if (m_rotation->sample && (!m_controller->isLoaded() || !(m_controller->thread()->gba->memory.hw.devices & (HW_GYRO | HW_TILT)))) {
58		m_rotation->sample(m_rotation);
59		m_rotation->sample(m_rotation);
60		m_rotation->sample(m_rotation);
61		m_rotation->sample(m_rotation);
62		m_rotation->sample(m_rotation);
63		m_rotation->sample(m_rotation);
64		m_rotation->sample(m_rotation);
65		m_rotation->sample(m_rotation);
66	}
67	if (m_rotation->readTiltX && m_rotation->readTiltY) {
68		float x = m_rotation->readTiltX(m_rotation);
69		float y = m_rotation->readTiltY(m_rotation);
70		m_ui.tiltX->setValue(x / 469762048.0f); // TODO: Document this value (0xE0 << 21)
71		m_ui.tiltY->setValue(y / 469762048.0f);
72	}
73	if (m_rotation->readGyroZ) {
74		m_ui.gyroView->setValue(m_rotation->readGyroZ(m_rotation));
75	}
76	m_controller->threadContinue();
77}
78
79void SensorView::setLuminanceValue(int value) {
80	value = std::max(0, std::min(value, 255));
81	m_controller->setLuminanceValue(value);
82}
83
84void SensorView::luminanceValueChanged(int value) {
85	bool oldState;
86	oldState = m_ui.lightSpin->blockSignals(true);
87	m_ui.lightSpin->setValue(value);
88	m_ui.lightSpin->blockSignals(oldState);
89
90	oldState = m_ui.lightSlide->blockSignals(true);
91	m_ui.lightSlide->setValue(value);
92	m_ui.lightSlide->blockSignals(oldState);
93}