all repos — mgba @ 2b5da04d9c5acf7ac152c903ac73d229ae79d343

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	layout->setSpacing(6);
109
110	m_keyOrder = QList<KeyEditor*>{
111		m_keyDU,
112		m_keyDR,
113		m_keyDD,
114		m_keyDL,
115		m_keyA,
116		m_keyB,
117		m_keySelect,
118		m_keyStart,
119		m_keyL,
120		m_keyR
121	};
122
123	for (auto& key : m_keyOrder) {
124		connect(key, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
125		connect(key, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
126		key->installEventFilter(this);
127	}
128
129	m_currentKey = m_keyOrder.end();
130
131	m_background.load(":/res/keymap.qpic");
132
133	setAll->setFocus();
134}
135
136void GBAKeyEditor::setAll() {
137	m_currentKey = m_keyOrder.begin();
138	(*m_currentKey)->setFocus();
139}
140
141void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
142	setLocation(m_buttons, 0.5, 0.2);
143	setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
144	setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
145	setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
146	setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
147	setLocation(m_keySelect, 0.415, 0.93);
148	setLocation(m_keyStart, 0.585, 0.93);
149	setLocation(m_keyA, 0.826, 0.475);
150	setLocation(m_keyB, 0.667, 0.514);
151	setLocation(m_keyL, 0.1, 0.1);
152	setLocation(m_keyR, 0.9, 0.1);
153
154	if (m_profileSelect) {
155		setLocation(m_profileSelect, 0.5, 0.67);
156	}
157
158	if (m_clear) {
159		setLocation(m_clear, 0.5, 0.77);
160	}
161}
162
163void GBAKeyEditor::paintEvent(QPaintEvent* event) {
164	QPainter painter(this);
165	painter.scale(width() / 480.0, height() / 480.0);
166	painter.drawPicture(0, 0, m_background);
167}
168
169void GBAKeyEditor::closeEvent(QCloseEvent*) {
170	m_controller->releaseFocus(this);
171}
172
173bool GBAKeyEditor::event(QEvent* event) {
174	if (event->type() == QEvent::WindowActivate) {
175		m_controller->stealFocus(this);
176	} else if (event->type() == QEvent::WindowDeactivate) {
177		m_controller->releaseFocus(this);
178	}
179	return QWidget::event(event);
180}
181
182bool GBAKeyEditor::eventFilter(QObject* obj, QEvent* event) {
183	if (event->type() != QEvent::FocusIn) {
184		return false;
185	}
186	findFocus(static_cast<KeyEditor*>(obj));
187	return true;
188}
189
190void GBAKeyEditor::setNext() {
191	if (m_currentKey == m_keyOrder.end()) {
192		return;
193	}
194
195	++m_currentKey;
196	if (m_currentKey != m_keyOrder.end()) {
197		(*m_currentKey)->setFocus();
198	} else {
199		(*(m_currentKey - 1))->clearFocus();
200	}
201}
202
203void GBAKeyEditor::save() {
204#ifdef BUILD_SDL
205	m_controller->unbindAllAxes(m_type);
206#endif
207
208	bindKey(m_keyDU, GBA_KEY_UP);
209	bindKey(m_keyDD, GBA_KEY_DOWN);
210	bindKey(m_keyDL, GBA_KEY_LEFT);
211	bindKey(m_keyDR, GBA_KEY_RIGHT);
212	bindKey(m_keySelect, GBA_KEY_SELECT);
213	bindKey(m_keyStart, GBA_KEY_START);
214	bindKey(m_keyA, GBA_KEY_A);
215	bindKey(m_keyB, GBA_KEY_B);
216	bindKey(m_keyL, GBA_KEY_L);
217	bindKey(m_keyR, GBA_KEY_R);
218	m_controller->saveConfiguration(m_type);
219
220#ifdef BUILD_SDL
221	if (m_profileSelect) {
222		m_controller->setPreferredGamepad(m_type, m_profileSelect->currentText());
223	}
224#endif
225
226	if (!m_profile.isNull()) {
227		m_controller->saveProfile(m_type, m_profile);
228	}
229}
230
231void GBAKeyEditor::refresh() {
232	const GBAInputMap* map = m_controller->map();
233	lookupBinding(map, m_keyDU, GBA_KEY_UP);
234	lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
235	lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
236	lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
237	lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
238	lookupBinding(map, m_keyStart, GBA_KEY_START);
239	lookupBinding(map, m_keyA, GBA_KEY_A);
240	lookupBinding(map, m_keyB, GBA_KEY_B);
241	lookupBinding(map, m_keyL, GBA_KEY_L);
242	lookupBinding(map, m_keyR, GBA_KEY_R);
243}
244
245void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
246#ifdef BUILD_SDL
247	if (m_type == SDL_BINDING_BUTTON) {
248		int value = GBAInputQueryBinding(map, m_type, key);
249		if (value != GBA_KEY_NONE) {
250			keyEditor->setValueButton(value);
251		}
252		return;
253	}
254#endif
255	keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
256}
257
258#ifdef BUILD_SDL
259void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
260	GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
261		GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
262		if (description->highDirection != GBA_KEY_NONE) {
263			KeyEditor* key = self->keyById(description->highDirection);
264			if (key) {
265				key->setValueAxis(axis, description->deadHigh);
266			}
267		}
268		if (description->lowDirection != GBA_KEY_NONE) {
269			KeyEditor* key = self->keyById(description->lowDirection);
270			if (key) {
271				key->setValueAxis(axis, description->deadLow);
272			}
273		}
274	}, this);
275}
276#endif
277
278void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
279#ifdef BUILD_SDL
280	if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) {
281		m_controller->bindAxis(m_type, keyEditor->axis(), keyEditor->direction(), key);
282	}
283#endif
284	m_controller->bindKey(m_type, keyEditor->value(), key);
285}
286
287bool GBAKeyEditor::findFocus(KeyEditor* needle) {
288	if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
289		return true;
290	}
291
292	for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
293		if ((*key)->hasFocus() || needle == *key) {
294			m_currentKey = key;
295			return true;
296		}
297	}
298	return m_currentKey != m_keyOrder.end();
299}
300
301#ifdef BUILD_SDL
302void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
303	if (!findFocus()) {
304		return;
305	}
306	KeyEditor* focused = *m_currentKey;
307	focused->setValueAxis(axis, value);
308}
309#endif
310
311KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
312	switch (key) {
313	case GBA_KEY_UP:
314		return m_keyDU;
315	case GBA_KEY_DOWN:
316		return m_keyDD;
317	case GBA_KEY_LEFT:
318		return m_keyDL;
319	case GBA_KEY_RIGHT:
320		return m_keyDR;
321	case GBA_KEY_A:
322		return m_keyA;
323	case GBA_KEY_B:
324		return m_keyB;
325	case GBA_KEY_L:
326		return m_keyL;
327	case GBA_KEY_R:
328		return m_keyR;
329	case GBA_KEY_SELECT:
330		return m_keySelect;
331	case GBA_KEY_START:
332		return m_keyStart;
333	default:
334		break;
335	}
336	return nullptr;
337}
338
339void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
340	QSize s = size();
341	QSize hint = widget->sizeHint();
342	widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(),
343	                    hint.height());
344}