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 <QApplication>
9#include <QComboBox>
10#include <QHBoxLayout>
11#include <QPaintEvent>
12#include <QPainter>
13#include <QPushButton>
14#include <QVBoxLayout>
15
16#include "InputController.h"
17#include "KeyEditor.h"
18
19#ifdef BUILD_SDL
20#include "platform/sdl/sdl-events.h"
21#endif
22
23using namespace QGBA;
24
25const qreal GBAKeyEditor::DPAD_CENTER_X = 0.247;
26const qreal GBAKeyEditor::DPAD_CENTER_Y = 0.432;
27const qreal GBAKeyEditor::DPAD_WIDTH = 0.12;
28const qreal GBAKeyEditor::DPAD_HEIGHT = 0.12;
29
30GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString& profile, QWidget* parent)
31 : QWidget(parent)
32 , m_type(type)
33 , m_profile(profile)
34 , m_controller(controller)
35{
36 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
37 setMinimumSize(300, 300);
38
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 KeyEditor* keyEditor = static_cast<KeyEditor*>(obj);
186 if (event->type() == QEvent::FocusOut) {
187 keyEditor->setPalette(QApplication::palette(keyEditor));
188 }
189 if (event->type() != QEvent::FocusIn) {
190 return false;
191 }
192
193 QPalette palette = keyEditor->palette();
194 palette.setBrush(keyEditor->backgroundRole(), palette.highlight());
195 palette.setBrush(keyEditor->foregroundRole(), palette.highlightedText());
196 keyEditor->setPalette(palette);
197
198 findFocus(keyEditor);
199 return true;
200}
201
202void GBAKeyEditor::setNext() {
203 if (m_currentKey == m_keyOrder.end()) {
204 return;
205 }
206
207 ++m_currentKey;
208 if (m_currentKey != m_keyOrder.end()) {
209 (*m_currentKey)->setFocus();
210 } else {
211 (*(m_currentKey - 1))->clearFocus();
212 }
213}
214
215void GBAKeyEditor::save() {
216#ifdef BUILD_SDL
217 m_controller->unbindAllAxes(m_type);
218#endif
219
220 bindKey(m_keyDU, GBA_KEY_UP);
221 bindKey(m_keyDD, GBA_KEY_DOWN);
222 bindKey(m_keyDL, GBA_KEY_LEFT);
223 bindKey(m_keyDR, GBA_KEY_RIGHT);
224 bindKey(m_keySelect, GBA_KEY_SELECT);
225 bindKey(m_keyStart, GBA_KEY_START);
226 bindKey(m_keyA, GBA_KEY_A);
227 bindKey(m_keyB, GBA_KEY_B);
228 bindKey(m_keyL, GBA_KEY_L);
229 bindKey(m_keyR, GBA_KEY_R);
230 m_controller->saveConfiguration(m_type);
231
232#ifdef BUILD_SDL
233 if (m_profileSelect && m_profileSelect->count()) {
234 m_controller->setPreferredGamepad(m_type, m_profileSelect->currentIndex());
235 }
236#endif
237
238 if (!m_profile.isNull()) {
239 m_controller->saveProfile(m_type, m_profile);
240 }
241}
242
243void GBAKeyEditor::refresh() {
244 const mInputMap* map = m_controller->map();
245 lookupBinding(map, m_keyDU, GBA_KEY_UP);
246 lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
247 lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
248 lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
249 lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
250 lookupBinding(map, m_keyStart, GBA_KEY_START);
251 lookupBinding(map, m_keyA, GBA_KEY_A);
252 lookupBinding(map, m_keyB, GBA_KEY_B);
253 lookupBinding(map, m_keyL, GBA_KEY_L);
254 lookupBinding(map, m_keyR, GBA_KEY_R);
255
256#ifdef BUILD_SDL
257 lookupAxes(map);
258 lookupHats(map);
259#endif
260}
261
262void GBAKeyEditor::lookupBinding(const mInputMap* map, KeyEditor* keyEditor, GBAKey key) {
263#ifdef BUILD_SDL
264 if (m_type == SDL_BINDING_BUTTON) {
265 int value = mInputQueryBinding(map, m_type, key);
266 keyEditor->setValueButton(value);
267 return;
268 }
269#endif
270 keyEditor->setValueKey(mInputQueryBinding(map, m_type, key));
271}
272
273#ifdef BUILD_SDL
274void GBAKeyEditor::lookupAxes(const mInputMap* map) {
275 mInputEnumerateAxes(map, m_type, [](int axis, const mInputAxis* description, void* user) {
276 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
277 if (description->highDirection != GBA_KEY_NONE) {
278 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->highDirection));
279 if (key) {
280 key->setValueAxis(axis, GamepadAxisEvent::POSITIVE);
281 }
282 }
283 if (description->lowDirection != GBA_KEY_NONE) {
284 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->lowDirection));
285 if (key) {
286 key->setValueAxis(axis, GamepadAxisEvent::NEGATIVE);
287 }
288 }
289 }, this);
290}
291
292void GBAKeyEditor::lookupHats(const mInputMap* map) {
293 struct mInputHatBindings bindings;
294 int i = 0;
295 while (mInputQueryHat(map, m_type, i, &bindings)) {
296 if (bindings.up >= 0) {
297 KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.up));
298 if (key) {
299 key->setValueHat(i, GamepadHatEvent::UP);
300 }
301 }
302 if (bindings.right >= 0) {
303 KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.right));
304 if (key) {
305 key->setValueHat(i, GamepadHatEvent::RIGHT);
306 }
307 }
308 if (bindings.down >= 0) {
309 KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.down));
310 if (key) {
311 key->setValueHat(i, GamepadHatEvent::DOWN);
312 }
313 }
314 if (bindings.left >= 0) {
315 KeyEditor* key = keyById(static_cast<enum GBAKey>(bindings.left));
316 if (key) {
317 key->setValueHat(i, GamepadHatEvent::LEFT);
318 }
319 }
320 ++i;
321 }
322}
323#endif
324
325void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
326#ifdef BUILD_SDL
327 if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) {
328 m_controller->bindAxis(m_type, keyEditor->axis(), keyEditor->direction(), key);
329 }
330 if (m_type == SDL_BINDING_BUTTON && keyEditor->hat() >= 0) {
331 m_controller->bindHat(m_type, keyEditor->hat(), keyEditor->hatDirection(), key);
332 }
333#endif
334 m_controller->bindKey(m_type, keyEditor->value(), key);
335}
336
337bool GBAKeyEditor::findFocus(KeyEditor* needle) {
338 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
339 return true;
340 }
341
342 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
343 if ((*key)->hasFocus() || needle == *key) {
344 m_currentKey = key;
345 return true;
346 }
347 }
348 return m_currentKey != m_keyOrder.end();
349}
350
351#ifdef BUILD_SDL
352void GBAKeyEditor::selectGamepad(int index) {
353 m_controller->setGamepad(m_type, index);
354 m_profile = m_profileSelect->currentText();
355 m_controller->loadProfile(m_type, m_profile);
356 refresh();
357}
358#endif
359
360KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
361 switch (key) {
362 case GBA_KEY_UP:
363 return m_keyDU;
364 case GBA_KEY_DOWN:
365 return m_keyDD;
366 case GBA_KEY_LEFT:
367 return m_keyDL;
368 case GBA_KEY_RIGHT:
369 return m_keyDR;
370 case GBA_KEY_A:
371 return m_keyA;
372 case GBA_KEY_B:
373 return m_keyB;
374 case GBA_KEY_L:
375 return m_keyL;
376 case GBA_KEY_R:
377 return m_keyR;
378 case GBA_KEY_SELECT:
379 return m_keySelect;
380 case GBA_KEY_START:
381 return m_keyStart;
382 default:
383 break;
384 }
385 return nullptr;
386}
387
388void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
389 QSize s = size();
390 QSize hint = widget->sizeHint();
391 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(),
392 hint.height());
393}
394
395#ifdef BUILD_SDL
396void GBAKeyEditor::updateJoysticks() {
397 m_controller->updateJoysticks();
398 m_controller->recalibrateAxes();
399
400 // Block the currentIndexChanged signal while rearranging the combo box
401 auto wasBlocked = m_profileSelect->blockSignals(true);
402 m_profileSelect->clear();
403 m_profileSelect->addItems(m_controller->connectedGamepads(m_type));
404 int activeGamepad = m_controller->gamepad(m_type);
405 m_profileSelect->setCurrentIndex(activeGamepad);
406 m_profileSelect->blockSignals(wasBlocked);
407
408 selectGamepad(activeGamepad);
409}
410#endif