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 "GamepadAxisEvent.h"
10#include "InputController.h"
11
12using namespace QGBA;
13
14SensorView::SensorView(GameController* controller, InputController* input, QWidget* parent)
15 : QDialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint)
16 , m_controller(controller)
17 , m_input(input)
18 , m_rotation(input->rotationSource())
19 {
20 m_ui.setupUi(this);
21
22 connect(m_ui.lightSpin, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
23 connect(m_ui.lightSlide, SIGNAL(valueChanged(int)), this, SLOT(setLuminanceValue(int)));
24
25 connect(m_ui.timeNoOverride, SIGNAL(clicked()), controller, SLOT(setRealTime()));
26 connect(m_ui.timeFixed, &QRadioButton::clicked, [controller, this] () {
27 controller->setFixedTime(m_ui.time->dateTime());
28 });
29 connect(m_ui.timeFakeEpoch, &QRadioButton::clicked, [controller, this] () {
30 controller->setFakeEpoch(m_ui.time->dateTime());
31 });
32 connect(m_ui.time, &QDateTimeEdit::dateTimeChanged, [controller, this] (const QDateTime&) {
33 m_ui.timeButtons->checkedButton()->clicked();
34 });
35 connect(m_ui.timeNow, &QPushButton::clicked, [controller, this] () {
36 m_ui.time->setDateTime(QDateTime::currentDateTime());
37 });
38
39 connect(m_controller, SIGNAL(luminanceValueChanged(int)), this, SLOT(luminanceValueChanged(int)));
40
41 m_timer.setInterval(2);
42 connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateSensors()));
43 if (!m_rotation || !m_rotation->readTiltX || !m_rotation->readTiltY) {
44 m_ui.tilt->hide();
45 } else {
46 m_timer.start();
47 }
48
49 if (!m_rotation || !m_rotation->readGyroZ) {
50 m_ui.gyro->hide();
51 } else {
52 m_timer.start();
53 }
54
55 jiggerer(m_ui.tiltSetX, &InputController::registerTiltAxisX);
56 jiggerer(m_ui.tiltSetY, &InputController::registerTiltAxisY);
57 jiggerer(m_ui.gyroSetX, &InputController::registerGyroAxisX);
58 jiggerer(m_ui.gyroSetY, &InputController::registerGyroAxisY);
59
60 m_ui.gyroSensitivity->setValue(m_input->gyroSensitivity() / 1e8f);
61 connect(m_ui.gyroSensitivity, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [this](double value) {
62 m_input->setGyroSensitivity(value * 1e8f);
63 });
64 m_input->stealFocus(this);
65}
66
67void SensorView::jiggerer(QAbstractButton* button, void (InputController::*setter)(int)) {
68 connect(button, &QAbstractButton::toggled, [this, button, setter](bool checked) {
69 if (!checked) {
70 m_jiggered = nullptr;
71 } else {
72 button->setFocus();
73 m_jiggered = [this, button, setter](int axis) {
74 (m_input->*setter)(axis);
75 button->setChecked(false);
76 button->clearFocus();
77 };
78 }
79 });
80 button->installEventFilter(this);
81}
82
83bool SensorView::event(QEvent* event) {
84 if (event->type() == QEvent::WindowActivate) {
85 m_input->stealFocus(this);
86 } else if (event->type() == QEvent::WindowDeactivate) {
87 m_input->releaseFocus(this);
88 }
89 return QWidget::event(event);
90}
91
92bool SensorView::eventFilter(QObject*, QEvent* event) {
93 if (event->type() == GamepadAxisEvent::Type()) {
94 GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
95 gae->accept();
96 if (m_jiggered && gae->direction() != GamepadAxisEvent::NEUTRAL && gae->isNew()) {
97 m_jiggered(gae->axis());
98 }
99 return true;
100 }
101 return false;
102}
103
104void SensorView::updateSensors() {
105 m_controller->threadInterrupt();
106 if (m_rotation->sample &&
107 (!m_controller->isLoaded() || !(m_controller->thread()->gba->memory.hw.devices & (HW_GYRO | HW_TILT)))) {
108 m_rotation->sample(m_rotation);
109 m_rotation->sample(m_rotation);
110 m_rotation->sample(m_rotation);
111 m_rotation->sample(m_rotation);
112 m_rotation->sample(m_rotation);
113 m_rotation->sample(m_rotation);
114 m_rotation->sample(m_rotation);
115 m_rotation->sample(m_rotation);
116 }
117 if (m_rotation->readTiltX && m_rotation->readTiltY) {
118 float x = m_rotation->readTiltX(m_rotation);
119 float y = m_rotation->readTiltY(m_rotation);
120 m_ui.tiltX->setValue(x / 469762048.0f); // TODO: Document this value (0xE0 << 21)
121 m_ui.tiltY->setValue(y / 469762048.0f);
122 }
123 if (m_rotation->readGyroZ) {
124 m_ui.gyroView->setValue(m_rotation->readGyroZ(m_rotation));
125 }
126 m_controller->threadContinue();
127}
128
129void SensorView::setLuminanceValue(int value) {
130 value = std::max(0, std::min(value, 255));
131 m_controller->setLuminanceValue(value);
132}
133
134void SensorView::luminanceValueChanged(int value) {
135 bool oldState;
136 oldState = m_ui.lightSpin->blockSignals(true);
137 m_ui.lightSpin->setValue(value);
138 m_ui.lightSpin->blockSignals(oldState);
139
140 oldState = m_ui.lightSlide->blockSignals(true);
141 m_ui.lightSlide->setValue(value);
142 m_ui.lightSlide->blockSignals(oldState);
143}