all repos — mgba @ e9436e43dbc19b8f0e7e3a2a7e7c3d73cd6fcf14

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