all repos — mgba @ 12fce1fba0adf92d61d95a279324e31e27fef03b

mGBA Game Boy Advance Emulator

src/platform/qt/KeyEditor.cpp (view raw)

 1/* Copyright (c) 2013-2014 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 "KeyEditor.h"
 7
 8#include "GamepadAxisEvent.h"
 9#include "GamepadButtonEvent.h"
10
11#include <QKeyEvent>
12
13using namespace QGBA;
14
15KeyEditor::KeyEditor(QWidget* parent)
16	: QLineEdit(parent)
17	, m_direction(GamepadAxisEvent::NEUTRAL)
18{
19	setAlignment(Qt::AlignCenter);
20}
21
22void KeyEditor::setValue(int key) {
23	if (m_button) {
24		if (key < 0) {
25			clear();
26		} else {
27			setText(QString::number(key));
28		}
29	} else {
30		setText(QKeySequence(key).toString(QKeySequence::NativeText));
31	}
32	m_key = key;
33	emit valueChanged(key);
34}
35
36void KeyEditor::setValueKey(int key) {
37	m_button = false;
38	setValue(key);
39}
40
41void KeyEditor::setValueButton(int button) {
42	m_button = true;
43	m_direction = GamepadAxisEvent::NEUTRAL;
44	setValue(button);
45}
46
47void KeyEditor::setValueAxis(int axis, int32_t value) {
48	m_button = true;
49	m_key = axis;
50	m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
51	setText((value < 0 ? "-" : "+") + QString::number(axis));
52	emit axisChanged(axis, m_direction);
53}
54
55QSize KeyEditor::sizeHint() const {
56	QSize hint = QLineEdit::sizeHint();
57	hint.setWidth(40);
58	return hint;
59}
60
61void KeyEditor::keyPressEvent(QKeyEvent* event) {
62	if (!m_button) {
63		setValue(event->key());
64	}
65	event->accept();
66}
67
68bool KeyEditor::event(QEvent* event) {
69	if (!m_button) {
70		return QWidget::event(event);
71	}
72	if (event->type() == GamepadButtonEvent::Down()) {
73		setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
74		event->accept();
75		return true;
76	}
77	if (event->type() == GamepadAxisEvent::Type()) {
78		GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
79		if (gae->isNew()) {
80			setValueAxis(gae->axis(), gae->direction());
81		}
82		event->accept();
83		return true;
84	}
85	return QWidget::event(event);
86}