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 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 mInputMap* 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 mInputMap* map, KeyEditor* keyEditor, GBAKey key) {
254#ifdef BUILD_SDL
255 if (m_type == SDL_BINDING_BUTTON) {
256 int value = mInputQueryBinding(map, m_type, key);
257 keyEditor->setValueButton(value);
258 return;
259 }
260#endif
261 keyEditor->setValueKey(mInputQueryBinding(map, m_type, key));
262}
263
264#ifdef BUILD_SDL
265void GBAKeyEditor::lookupAxes(const mInputMap* map) {
266 mInputEnumerateAxes(map, m_type, [](int axis, const mInputAxis* description, void* user) {
267 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
268 if (description->highDirection != GBA_KEY_NONE) {
269 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->highDirection));
270 if (key) {
271 key->setValueAxis(axis, description->deadHigh);
272 }
273 }
274 if (description->lowDirection != GBA_KEY_NONE) {
275 KeyEditor* key = self->keyById(static_cast<enum GBAKey>(description->lowDirection));
276 if (key) {
277 key->setValueAxis(axis, description->deadLow);
278 }
279 }
280 }, this);
281}
282#endif
283
284void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
285#ifdef BUILD_SDL
286 if (m_type == SDL_BINDING_BUTTON && keyEditor->axis() >= 0) {
287 m_controller->bindAxis(m_type, keyEditor->axis(), keyEditor->direction(), key);
288 }
289#endif
290 m_controller->bindKey(m_type, keyEditor->value(), key);
291}
292
293bool GBAKeyEditor::findFocus(KeyEditor* needle) {
294 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
295 return true;
296 }
297
298 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
299 if ((*key)->hasFocus() || needle == *key) {
300 m_currentKey = key;
301 return true;
302 }
303 }
304 return m_currentKey != m_keyOrder.end();
305}
306
307#ifdef BUILD_SDL
308void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
309 if (!findFocus()) {
310 return;
311 }
312 KeyEditor* focused = *m_currentKey;
313 focused->setValueAxis(axis, value);
314}
315
316void GBAKeyEditor::selectGamepad(int index) {
317 m_controller->setGamepad(m_type, index);
318 m_profile = m_profileSelect->currentText();
319 m_controller->loadProfile(m_type, m_profile);
320 refresh();
321}
322#endif
323
324KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
325 switch (key) {
326 case GBA_KEY_UP:
327 return m_keyDU;
328 case GBA_KEY_DOWN:
329 return m_keyDD;
330 case GBA_KEY_LEFT:
331 return m_keyDL;
332 case GBA_KEY_RIGHT:
333 return m_keyDR;
334 case GBA_KEY_A:
335 return m_keyA;
336 case GBA_KEY_B:
337 return m_keyB;
338 case GBA_KEY_L:
339 return m_keyL;
340 case GBA_KEY_R:
341 return m_keyR;
342 case GBA_KEY_SELECT:
343 return m_keySelect;
344 case GBA_KEY_START:
345 return m_keyStart;
346 default:
347 break;
348 }
349 return nullptr;
350}
351
352void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
353 QSize s = size();
354 QSize hint = widget->sizeHint();
355 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(),
356 hint.height());
357}