all repos — mgba @ 12cf61f9fc0f3a38104d69bd61964d0099e136b3

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