all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

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