all repos — mgba @ 74ac89a584b068b9d9c02e5117eddbe6da22d9c5

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