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