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