all repos — mgba @ a7fb4460298027da9d48aa91450e5268d913dc27

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());
 67	});
 68	connect(m_ui.timeFakeEpoch, &QRadioButton::clicked, [controller, this] () {
 69		controller->setFakeEpoch(m_ui.time->dateTime());
 70	});
 71
 72	connect(controller.get(), &CoreController::stopping, [this]() {
 73		m_controller.reset();
 74	});
 75}
 76
 77void SensorView::jiggerer(QAbstractButton* button, void (InputController::*setter)(int)) {
 78	connect(button, &QAbstractButton::toggled, [this, button, setter](bool checked) {
 79		if (!checked) {
 80			m_jiggered = nullptr;
 81		} else {
 82			button->setFocus();
 83			m_jiggered = [this, button, setter](int axis) {
 84				(m_input->*setter)(axis);
 85				button->setChecked(false);
 86				button->clearFocus();
 87			};
 88		}
 89	});
 90	button->installEventFilter(this);
 91}
 92
 93bool SensorView::event(QEvent* event) {
 94	QEvent::Type type = event->type();
 95	if (type == QEvent::WindowActivate || type == QEvent::Show) {
 96		m_input->stealFocus(this);
 97	} else if (type == QEvent::WindowDeactivate || type == QEvent::Hide) {
 98		m_input->releaseFocus(this);
 99	}
100	return QWidget::event(event);
101}
102
103bool SensorView::eventFilter(QObject*, QEvent* event) {
104	if (event->type() == GamepadAxisEvent::Type()) {
105		GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
106		gae->accept();
107		if (m_jiggered && gae->direction() != GamepadAxisEvent::NEUTRAL && gae->isNew()) {
108			m_jiggered(gae->axis());
109		}
110		return true;
111	}
112	return false;
113}
114
115void SensorView::updateSensors() {
116	if (m_rotation->sample && (!m_controller || m_controller->isPaused())) {
117		m_rotation->sample(m_rotation);
118	}
119	if (m_rotation->readTiltX && m_rotation->readTiltY) {
120		float x = m_rotation->readTiltX(m_rotation);
121		float y = m_rotation->readTiltY(m_rotation);
122		m_ui.tiltX->setValue(x / 469762048.0f); // TODO: Document this value (0xE0 << 21)
123		m_ui.tiltY->setValue(y / 469762048.0f);
124	}
125	if (m_rotation->readGyroZ) {
126		m_ui.gyroView->setValue(m_rotation->readGyroZ(m_rotation));
127	}
128}
129
130void SensorView::setLuminanceValue(int value) {
131	value = std::max(0, std::min(value, 255));
132	if (m_input) {
133		m_input->setLuminanceValue(value);
134	}
135}
136
137void SensorView::luminanceValueChanged(int value) {
138	bool oldState;
139	oldState = m_ui.lightSpin->blockSignals(true);
140	m_ui.lightSpin->setValue(value);
141	m_ui.lightSpin->blockSignals(oldState);
142
143	oldState = m_ui.lightSlide->blockSignals(true);
144	m_ui.lightSlide->setValue(value);
145	m_ui.lightSlide->blockSignals(oldState);
146}