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, SIGNAL(currentIndexChanged(int)), this, SLOT(selectGamepad(int)));
60
61 updateJoysticks();
62
63 m_clear = new QWidget(this);
64 QHBoxLayout* layout = new QHBoxLayout;
65 m_clear->setLayout(layout);
66 layout->setSpacing(6);
67
68 QPushButton* clearButton = new QPushButton(tr("Clear Button"));
69 layout->addWidget(clearButton);
70 connect(clearButton, &QAbstractButton::pressed, [this]() {
71 if (!findFocus()) {
72 return;
73 }
74 bool signalsBlocked = (*m_currentKey)->blockSignals(true);
75 (*m_currentKey)->clearButton();
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, SIGNAL(pressed()), this, SLOT(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, SIGNAL(pressed()), this, SLOT(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, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
121 connect(key, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
122 key->installEventFilter(this);
123 }
124
125 m_currentKey = m_keyOrder.end();
126
127 m_background.load(":/res/keymap.qpic");
128
129 setAll->setFocus();
130}
131
132GBAKeyEditor::~GBAKeyEditor() {
133 m_controller->releaseFocus(this);
134}
135
136void GBAKeyEditor::setAll() {
137 m_currentKey = m_keyOrder.begin();
138 (*m_currentKey)->setFocus();
139}
140
141void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
142 setLocation(m_buttons, 0.5, 0.2);
143 setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
144 setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
145 setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
146 setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
147 setLocation(m_keySelect, 0.415, 0.93);
148 setLocation(m_keyStart, 0.585, 0.93);
149 setLocation(m_keyA, 0.826, 0.475);
150 setLocation(m_keyB, 0.667, 0.514);
151 setLocation(m_keyL, 0.1, 0.1);
152 setLocation(m_keyR, 0.9, 0.1);
153
154 if (m_profileSelect) {
155 setLocation(m_profileSelect, 0.5, 0.67);
156 }
157
158 if (m_clear) {
159 setLocation(m_clear, 0.5, 0.77);
160 }
161}
162
163void GBAKeyEditor::paintEvent(QPaintEvent* event) {
164 QPainter painter(this);
165 painter.scale(width() / 480.0, height() / 480.0);
166 painter.drawPicture(0, 0, m_background);
167}
168
169void GBAKeyEditor::closeEvent(QCloseEvent*) {
170 m_controller->releaseFocus(this);
171}
172
173bool GBAKeyEditor::event(QEvent* event) {
174 QEvent::Type type = event->type();
175 if (type == QEvent::WindowActivate || type == QEvent::Show) {
176 m_controller->stealFocus(this);
177 } else if (type == QEvent::WindowDeactivate || type == QEvent::Hide) {
178 m_controller->releaseFocus(this);
179 }
180 return QWidget::event(event);
181}
182
183bool GBAKeyEditor::eventFilter(QObject* obj, QEvent* event) {
184 if (event->type() != QEvent::FocusIn) {
185 return false;
186 }
187 findFocus(static_cast<KeyEditor*>(obj));
188 return true;
189}
190
191void GBAKeyEditor::setNext() {
192 if (m_currentKey == m_keyOrder.end()) {
193 return;
194 }
195
196 ++m_currentKey;
197 if (m_currentKey != m_keyOrder.end()) {
198 (*m_currentKey)->setFocus();
199 } else {
200 (*(m_currentKey - 1))->clearFocus();
201 }
202}
203
204void GBAKeyEditor::save() {
205#ifdef BUILD_SDL
206 m_controller->unbindAllAxes(m_type);
207#endif
208
209 bindKey(m_keyDU, GBA_KEY_UP);
210 bindKey(m_keyDD, GBA_KEY_DOWN);
211 bindKey(m_keyDL, GBA_KEY_LEFT);
212 bindKey(m_keyDR, GBA_KEY_RIGHT);
213 bindKey(m_keySelect, GBA_KEY_SELECT);
214 bindKey(m_keyStart, GBA_KEY_START);
215 bindKey(m_keyA, GBA_KEY_A);
216 bindKey(m_keyB, GBA_KEY_B);
217 bindKey(m_keyL, GBA_KEY_L);
218 bindKey(m_keyR, GBA_KEY_R);
219 m_controller->saveConfiguration(m_type);
220
221#ifdef BUILD_SDL
222 if (m_profileSelect) {
223 m_controller->setPreferredGamepad(m_type, m_profileSelect->currentText());
224 }
225#endif
226
227 if (!m_profile.isNull()) {
228 m_controller->saveProfile(m_type, m_profile);
229 }
230}
231
232void GBAKeyEditor::refresh() {
233 const mInputMap* map = m_controller->map();
234 lookupBinding(map, m_keyDU, GBA_KEY_UP);
235 lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
236 lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
237 lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
238 lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
239 lookupBinding(map, m_keyStart, GBA_KEY_START);
240 lookupBinding(map, m_keyA, GBA_KEY_A);
241 lookupBinding(map, m_keyB, GBA_KEY_B);
242 lookupBinding(map, m_keyL, GBA_KEY_L);
243 lookupBinding(map, m_keyR, GBA_KEY_R);
244}
245
246void GBAKeyEditor::lookupBinding(const mInputMap* map, KeyEditor* keyEditor, GBAKey key) {
247#ifdef BUILD_SDL
248 if (m_type == SDL_BINDING_BUTTON) {
249 int value = mInputQueryBinding(map, m_type, key);
250 keyEditor->setValueButton(value);
251 return;
252 }
253#endif
254 keyEditor->setValueKey(mInputQueryBinding(map, m_type, key));
255}
256
257#ifdef BUILD_SDL
258void GBAKeyEditor::lookupAxes(const mInputMap* map) {
259 mInputEnumerateAxes(map, m_type, [](int axis, const mInputAxis* description, void* user) {
260 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
261 if (description->highDirection != GBA_KEY_NONE) {
262 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->highDirection));
263 if (key) {
264 key->setValueAxis(axis, description->deadHigh);
265 }
266 }
267 if (description->lowDirection != GBA_KEY_NONE) {
268 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->lowDirection));
269 if (key) {
270 key->setValueAxis(axis, description->deadLow);
271 }
272 }
273 }, this);
274}
275#endif
276
277void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
278#ifdef BUILD_SDL
279 if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) {
280 m_controller->bindAxis(m_type, keyEditor->axis(), keyEditor->direction(), key);
281 }
282#endif
283 m_controller->bindKey(m_type, keyEditor->value(), key);
284}
285
286bool GBAKeyEditor::findFocus(KeyEditor* needle) {
287 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
288 return true;
289 }
290
291 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
292 if ((*key)->hasFocus() || needle == *key) {
293 m_currentKey = key;
294 return true;
295 }
296 }
297 return m_currentKey != m_keyOrder.end();
298}
299
300#ifdef BUILD_SDL
301void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
302 if (!findFocus()) {
303 return;
304 }
305 KeyEditor* focused = *m_currentKey;
306 focused->setValueAxis(axis, value);
307}
308
309void GBAKeyEditor::selectGamepad(int index) {
310 m_controller->setGamepad(m_type, index);
311 m_profile = m_profileSelect->currentText();
312 m_controller->loadProfile(m_type, m_profile);
313 refresh();
314}
315#endif
316
317KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
318 switch (key) {
319 case GBA_KEY_UP:
320 return m_keyDU;
321 case GBA_KEY_DOWN:
322 return m_keyDD;
323 case GBA_KEY_LEFT:
324 return m_keyDL;
325 case GBA_KEY_RIGHT:
326 return m_keyDR;
327 case GBA_KEY_A:
328 return m_keyA;
329 case GBA_KEY_B:
330 return m_keyB;
331 case GBA_KEY_L:
332 return m_keyL;
333 case GBA_KEY_R:
334 return m_keyR;
335 case GBA_KEY_SELECT:
336 return m_keySelect;
337 case GBA_KEY_START:
338 return m_keyStart;
339 default:
340 break;
341 }
342 return nullptr;
343}
344
345void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
346 QSize s = size();
347 QSize hint = widget->sizeHint();
348 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(),
349 hint.height());
350}
351
352#ifdef BUILD_SDL
353void GBAKeyEditor::updateJoysticks() {
354 m_controller->updateJoysticks();
355 m_controller->recalibrateAxes();
356
357 m_profileSelect->clear();
358 m_profileSelect->addItems(m_controller->connectedGamepads(m_type));
359 int activeGamepad = m_controller->gamepad(m_type);
360 selectGamepad(activeGamepad);
361 if (activeGamepad > 0) {
362 m_profileSelect->setCurrentIndex(activeGamepad);
363 }
364 lookupAxes(m_controller->map());
365}
366#endif