all repos — mgba @ 02ecfa684360a458de8477665162fcd394ef2c09

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