all repos — mgba @ 908e61f4153702bce5a28616229f848168203b84

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