all repos — mgba @ 1f7616831799b550653b0aca61e6c714cba0383c

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