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