all repos — mgba @ 53c586044d76775bb0226a6c92ca61f0c1844467

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(buttonPressed(int)), this, SLOT(setButton(int)));
116		connect(m_controller, SIGNAL(axisChanged(int, int32_t)), this, SLOT(setAxisValue(int, int32_t)));
117	}
118#endif
119}
120
121void GBAKeyEditor::setAll() {
122	m_currentKey = m_keyOrder.begin();
123	(*m_currentKey)->setFocus();
124}
125
126void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
127	setLocation(m_buttons, 0.5, 0.2);
128	setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
129	setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
130	setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
131	setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
132	setLocation(m_keySelect, 0.415, 0.93);
133	setLocation(m_keyStart, 0.585, 0.93);
134	setLocation(m_keyA, 0.826, 0.451);
135	setLocation(m_keyB, 0.667, 0.490);
136	setLocation(m_keyL, 0.1, 0.1);
137	setLocation(m_keyR, 0.9, 0.1);
138}
139
140void GBAKeyEditor::paintEvent(QPaintEvent* event) {
141	QPainter painter(this);
142	painter.scale(width() / 480.0, height() / 480.0);
143	painter.drawPicture(0, 0, m_background);
144}
145
146void GBAKeyEditor::setNext() {
147	if (m_currentKey == m_keyOrder.end()) {
148		return;
149	}
150
151	if (!(*m_currentKey)->hasFocus()) {
152		m_currentKey = m_keyOrder.end();
153	}
154
155	++m_currentKey;
156	if (m_currentKey != m_keyOrder.end()) {
157		(*m_currentKey)->setFocus();
158	} else {
159		(*(m_currentKey - 1))->clearFocus();
160	}
161}
162
163void GBAKeyEditor::save() {
164	bindKey(m_keyDU, GBA_KEY_UP);
165	bindKey(m_keyDD, GBA_KEY_DOWN);
166	bindKey(m_keyDL, GBA_KEY_LEFT);
167	bindKey(m_keyDR, GBA_KEY_RIGHT);
168	bindKey(m_keySelect, GBA_KEY_SELECT);
169	bindKey(m_keyStart, GBA_KEY_START);
170	bindKey(m_keyA, GBA_KEY_A);
171	bindKey(m_keyB, GBA_KEY_B);
172	bindKey(m_keyL, GBA_KEY_L);
173	bindKey(m_keyR, GBA_KEY_R);
174	m_controller->saveConfiguration(m_type);
175}
176
177void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
178	#ifdef BUILD_SDL
179	if (m_type == SDL_BINDING_BUTTON) {
180		int value = GBAInputQueryBinding(map, m_type, key);
181		if (value != GBA_NO_MAPPING) {
182			keyEditor->setValueButton(value);
183		}
184		return;
185	}
186	#endif
187	keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
188}
189
190#ifdef BUILD_SDL
191void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
192	GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
193		GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
194		if (description->highDirection != GBA_KEY_NONE) {
195			KeyEditor* key = self->keyById(description->highDirection);
196			if (key) {
197				key->setValueAxis(axis, description->deadHigh);
198			}
199		}
200		if (description->lowDirection != GBA_KEY_NONE) {
201			KeyEditor* key = self->keyById(description->lowDirection);
202			if (key) {
203				key->setValueAxis(axis, description->deadLow);
204			}
205		}
206	}, this);
207}
208#endif
209
210void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
211	if (keyEditor->direction() != InputController::NEUTRAL) {
212		m_controller->bindAxis(m_type, keyEditor->value(), keyEditor->direction(), key);
213	} else {
214		m_controller->bindKey(m_type, keyEditor->value(), key);
215	}
216}
217
218bool GBAKeyEditor::findFocus() {
219	if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
220		return true;
221	}
222
223	for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
224		if ((*key)->hasFocus()) {
225			m_currentKey = key;
226			return true;
227		}
228	}
229	return false;
230}
231
232#ifdef BUILD_SDL
233void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
234	if (!findFocus()) {
235		return;
236	}
237	KeyEditor* focused = *m_currentKey;
238	focused->setValueAxis(axis, value);
239}
240
241void GBAKeyEditor::setButton(int button) {
242	if (!findFocus()) {
243		return;
244	}
245	KeyEditor* focused = *m_currentKey;
246	focused->setValueButton(button);
247}
248#endif
249
250KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
251	switch (key) {
252	case GBA_KEY_UP:
253		return m_keyDU;
254	case GBA_KEY_DOWN:
255		return m_keyDD;
256	case GBA_KEY_LEFT:
257		return m_keyDL;
258	case GBA_KEY_RIGHT:
259		return m_keyDR;
260	case GBA_KEY_A:
261		return m_keyA;
262	case GBA_KEY_B:
263		return m_keyB;
264	case GBA_KEY_L:
265		return m_keyL;
266	case GBA_KEY_R:
267		return m_keyR;
268	case GBA_KEY_SELECT:
269		return m_keySelect;
270	case GBA_KEY_START:
271		return m_keyStart;
272	default:
273		break;
274	}
275	return nullptr;
276}
277
278void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
279	QSize s = size();
280	QSize hint = widget->sizeHint();
281	widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
282}