all repos — mgba @ a9f0fabdd278ea847af78822023cd8651aa34af8

mGBA Game Boy Advance Emulator

src/platform/qt/GBAKeyEditor.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 "GBAKeyEditor.h"
  7
  8#include <QPaintEvent>
  9#include <QPainter>
 10#include <QPushButton>
 11#include <QVBoxLayout>
 12
 13#include "InputController.h"
 14#include "KeyEditor.h"
 15
 16using namespace QGBA;
 17
 18const qreal GBAKeyEditor::DPAD_CENTER_X = 0.247;
 19const qreal GBAKeyEditor::DPAD_CENTER_Y = 0.431;
 20const qreal GBAKeyEditor::DPAD_WIDTH = 0.1;
 21const qreal GBAKeyEditor::DPAD_HEIGHT = 0.1;
 22
 23GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* parent)
 24	: QWidget(parent)
 25	, m_type(type)
 26	, m_controller(controller)
 27{
 28	setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
 29	setMinimumSize(300, 300);
 30
 31	const GBAInputMap* map = controller->map();
 32
 33	m_keyDU = new KeyEditor(this);
 34	m_keyDD = new KeyEditor(this);
 35	m_keyDL = new KeyEditor(this);
 36	m_keyDR = new KeyEditor(this);
 37	m_keySelect = new KeyEditor(this);
 38	m_keyStart = new KeyEditor(this);
 39	m_keyA = new KeyEditor(this);
 40	m_keyB = new KeyEditor(this);
 41	m_keyL = new KeyEditor(this);
 42	m_keyR = new KeyEditor(this);
 43
 44	lookupBinding(map, m_keyDU, GBA_KEY_UP);
 45	lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
 46	lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
 47	lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
 48	lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
 49	lookupBinding(map, m_keyStart, GBA_KEY_START);
 50	lookupBinding(map, m_keyA, GBA_KEY_A);
 51	lookupBinding(map, m_keyB, GBA_KEY_B);
 52	lookupBinding(map, m_keyL, GBA_KEY_L);
 53	lookupBinding(map, m_keyR, GBA_KEY_R);
 54
 55#ifdef BUILD_SDL
 56	lookupAxes(map);
 57#endif
 58
 59	connect(m_keyDU, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 60	connect(m_keyDD, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 61	connect(m_keyDL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 62	connect(m_keyDR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 63	connect(m_keySelect, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 64	connect(m_keyStart, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 65	connect(m_keyA, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 66	connect(m_keyB, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 67	connect(m_keyL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 68	connect(m_keyR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
 69
 70	connect(m_keyDU, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 71	connect(m_keyDD, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 72	connect(m_keyDL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 73	connect(m_keyDR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 74	connect(m_keySelect, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 75	connect(m_keyStart, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 76	connect(m_keyA, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 77	connect(m_keyB, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 78	connect(m_keyL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 79	connect(m_keyR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
 80
 81	m_buttons = new QWidget(this);
 82	QVBoxLayout* layout = new QVBoxLayout;
 83	m_buttons->setLayout(layout);
 84
 85	QPushButton* setAll = new QPushButton(tr("Set all"));
 86	connect(setAll, SIGNAL(pressed()), this, SLOT(setAll()));
 87	layout->addWidget(setAll);
 88
 89	QPushButton* save = new QPushButton(tr("Save"));
 90	connect(save, SIGNAL(pressed()), this, SLOT(save()));
 91	layout->addWidget(save);
 92	layout->setSpacing(6);
 93
 94	m_keyOrder = QList<KeyEditor*>{
 95		m_keyDU,
 96		m_keyDR,
 97		m_keyDD,
 98		m_keyDL,
 99		m_keyA,
100		m_keyB,
101		m_keySelect,
102		m_keyStart,
103		m_keyL,
104		m_keyR
105	};
106
107	m_currentKey = m_keyOrder.end();
108
109	m_background.load(":/res/keymap.qpic");
110
111	setAll->setFocus();
112
113#ifdef BUILD_SDL
114	if (type == SDL_BINDING_BUTTON) {
115		connect(m_controller, SIGNAL(axisChanged(int, int32_t)), this, SLOT(setAxisValue(int, int32_t)));
116	}
117#endif
118}
119
120void GBAKeyEditor::setAll() {
121	m_currentKey = m_keyOrder.begin();
122	(*m_currentKey)->setFocus();
123}
124
125void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
126	setLocation(m_buttons, 0.5, 0.2);
127	setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
128	setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
129	setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
130	setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
131	setLocation(m_keySelect, 0.415, 0.93);
132	setLocation(m_keyStart, 0.585, 0.93);
133	setLocation(m_keyA, 0.826, 0.451);
134	setLocation(m_keyB, 0.667, 0.490);
135	setLocation(m_keyL, 0.1, 0.1);
136	setLocation(m_keyR, 0.9, 0.1);
137}
138
139void GBAKeyEditor::paintEvent(QPaintEvent* event) {
140	QPainter painter(this);
141	painter.scale(width() / 480.0, height() / 480.0);
142	painter.drawPicture(0, 0, m_background);
143}
144
145void GBAKeyEditor::setNext() {
146	findFocus();
147
148	if (m_currentKey == m_keyOrder.end()) {
149		return;
150	}
151
152	++m_currentKey;
153	if (m_currentKey != m_keyOrder.end()) {
154		(*m_currentKey)->setFocus();
155	} else {
156		(*(m_currentKey - 1))->clearFocus();
157	}
158}
159
160void GBAKeyEditor::save() {
161	bindKey(m_keyDU, GBA_KEY_UP);
162	bindKey(m_keyDD, GBA_KEY_DOWN);
163	bindKey(m_keyDL, GBA_KEY_LEFT);
164	bindKey(m_keyDR, GBA_KEY_RIGHT);
165	bindKey(m_keySelect, GBA_KEY_SELECT);
166	bindKey(m_keyStart, GBA_KEY_START);
167	bindKey(m_keyA, GBA_KEY_A);
168	bindKey(m_keyB, GBA_KEY_B);
169	bindKey(m_keyL, GBA_KEY_L);
170	bindKey(m_keyR, GBA_KEY_R);
171	m_controller->saveConfiguration(m_type);
172}
173
174void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
175	#ifdef BUILD_SDL
176	if (m_type == SDL_BINDING_BUTTON) {
177		int value = GBAInputQueryBinding(map, m_type, key);
178		if (value != GBA_NO_MAPPING) {
179			keyEditor->setValueButton(value);
180		}
181		return;
182	}
183	#endif
184	keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
185}
186
187#ifdef BUILD_SDL
188void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
189	GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
190		GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
191		if (description->highDirection != GBA_KEY_NONE) {
192			KeyEditor* key = self->keyById(description->highDirection);
193			if (key) {
194				key->setValueAxis(axis, description->deadHigh);
195			}
196		}
197		if (description->lowDirection != GBA_KEY_NONE) {
198			KeyEditor* key = self->keyById(description->lowDirection);
199			if (key) {
200				key->setValueAxis(axis, description->deadLow);
201			}
202		}
203	}, this);
204}
205#endif
206
207void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
208	if (keyEditor->direction() != InputController::NEUTRAL) {
209		m_controller->bindAxis(m_type, keyEditor->value(), keyEditor->direction(), key);
210	} else {
211		m_controller->bindKey(m_type, keyEditor->value(), key);
212	}
213}
214
215bool GBAKeyEditor::findFocus() {
216	if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
217		return true;
218	}
219
220	for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
221		if ((*key)->hasFocus()) {
222			m_currentKey = key;
223			return true;
224		}
225	}
226	return false;
227}
228
229#ifdef BUILD_SDL
230void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
231	if (!findFocus()) {
232		return;
233	}
234	KeyEditor* focused = *m_currentKey;
235	focused->setValueAxis(axis, value);
236}
237#endif
238
239KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
240	switch (key) {
241	case GBA_KEY_UP:
242		return m_keyDU;
243	case GBA_KEY_DOWN:
244		return m_keyDD;
245	case GBA_KEY_LEFT:
246		return m_keyDL;
247	case GBA_KEY_RIGHT:
248		return m_keyDR;
249	case GBA_KEY_A:
250		return m_keyA;
251	case GBA_KEY_B:
252		return m_keyB;
253	case GBA_KEY_L:
254		return m_keyL;
255	case GBA_KEY_R:
256		return m_keyR;
257	case GBA_KEY_SELECT:
258		return m_keySelect;
259	case GBA_KEY_START:
260		return m_keyStart;
261	default:
262		break;
263	}
264	return nullptr;
265}
266
267void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
268	QSize s = size();
269	QSize hint = widget->sizeHint();
270	widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
271}