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