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 QEvent::Type type = event->type();
85 if (type == QEvent::WindowActivate || type == QEvent::Show) {
86 m_input->stealFocus(this);
87 } else if (type == QEvent::WindowDeactivate || type == QEvent::Hide) {
88 m_input->releaseFocus(this);
89 }
90 return QWidget::event(event);
91}
92
93bool SensorView::eventFilter(QObject*, QEvent* event) {
94 if (event->type() == GamepadAxisEvent::Type()) {
95 GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
96 gae->accept();
97 if (m_jiggered && gae->direction() != GamepadAxisEvent::NEUTRAL && gae->isNew()) {
98 m_jiggered(gae->axis());
99 }
100 return true;
101 }
102 return false;
103}
104
105void SensorView::updateSensors() {
106 m_controller->threadInterrupt();
107 if (m_rotation->sample &&
108 (!m_controller->isLoaded() || !(m_controller->thread()->gba->memory.hw.devices & (HW_GYRO | HW_TILT)))) {
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 m_rotation->sample(m_rotation);
117 }
118 if (m_rotation->readTiltX && m_rotation->readTiltY) {
119 float x = m_rotation->readTiltX(m_rotation);
120 float y = m_rotation->readTiltY(m_rotation);
121 m_ui.tiltX->setValue(x / 469762048.0f); // TODO: Document this value (0xE0 << 21)
122 m_ui.tiltY->setValue(y / 469762048.0f);
123 }
124 if (m_rotation->readGyroZ) {
125 m_ui.gyroView->setValue(m_rotation->readGyroZ(m_rotation));
126 }
127 m_controller->threadContinue();
128}
129
130void SensorView::setLuminanceValue(int value) {
131 value = std::max(0, std::min(value, 255));
132 m_controller->setLuminanceValue(value);
133}
134
135void SensorView::luminanceValueChanged(int value) {
136 bool oldState;
137 oldState = m_ui.lightSpin->blockSignals(true);
138 m_ui.lightSpin->setValue(value);
139 m_ui.lightSpin->blockSignals(oldState);
140
141 oldState = m_ui.lightSlide->blockSignals(true);
142 m_ui.lightSlide->setValue(value);
143 m_ui.lightSlide->blockSignals(oldState);
144}