all repos — mgba @ 0b38dd1289427bd77f0cadaa52aeeaf6d2c2fd94

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