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 <QPaintEvent>
10#include <QPainter>
11#include <QPushButton>
12#include <QVBoxLayout>
13
14#include "InputController.h"
15#include "KeyEditor.h"
16
17using namespace QGBA;
18
19const qreal GBAKeyEditor::DPAD_CENTER_X = 0.247;
20const qreal GBAKeyEditor::DPAD_CENTER_Y = 0.431;
21const qreal GBAKeyEditor::DPAD_WIDTH = 0.1;
22const qreal GBAKeyEditor::DPAD_HEIGHT = 0.1;
23
24GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, const QString& profile, QWidget* parent)
25 : QWidget(parent)
26 , m_profileSelect(nullptr)
27 , m_type(type)
28 , m_profile(profile)
29 , m_controller(controller)
30{
31 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
32 setMinimumSize(300, 300);
33
34 const GBAInputMap* map = controller->map();
35 controller->stealFocus(this);
36
37 m_keyDU = new KeyEditor(this);
38 m_keyDD = new KeyEditor(this);
39 m_keyDL = new KeyEditor(this);
40 m_keyDR = new KeyEditor(this);
41 m_keySelect = new KeyEditor(this);
42 m_keyStart = new KeyEditor(this);
43 m_keyA = new KeyEditor(this);
44 m_keyB = new KeyEditor(this);
45 m_keyL = new KeyEditor(this);
46 m_keyR = new KeyEditor(this);
47
48 refresh();
49
50#ifdef BUILD_SDL
51 if (type == SDL_BINDING_BUTTON) {
52 controller->recalibrateAxes();
53 lookupAxes(map);
54
55 m_profileSelect = new QComboBox(this);
56 m_profileSelect->addItems(controller->connectedGamepads(type));
57 int activeGamepad = controller->gamepad(type);
58 if (activeGamepad > 0) {
59 m_profileSelect->setCurrentIndex(activeGamepad);
60 }
61
62 connect(m_profileSelect, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this] (int i) {
63 m_controller->setGamepad(m_type, i);
64 m_profile = m_profileSelect->currentText();
65 m_controller->loadProfile(m_type, m_profile);
66 refresh();
67 });
68 }
69#endif
70
71 connect(m_keyDU, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
72 connect(m_keyDD, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
73 connect(m_keyDL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
74 connect(m_keyDR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
75 connect(m_keySelect, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
76 connect(m_keyStart, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
77 connect(m_keyA, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
78 connect(m_keyB, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
79 connect(m_keyL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
80 connect(m_keyR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
81
82 connect(m_keyDU, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
83 connect(m_keyDD, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
84 connect(m_keyDL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
85 connect(m_keyDR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
86 connect(m_keySelect, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
87 connect(m_keyStart, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
88 connect(m_keyA, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
89 connect(m_keyB, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
90 connect(m_keyL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
91 connect(m_keyR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
92
93 m_buttons = new QWidget(this);
94 QVBoxLayout* layout = new QVBoxLayout;
95 m_buttons->setLayout(layout);
96
97 QPushButton* setAll = new QPushButton(tr("Set all"));
98 connect(setAll, SIGNAL(pressed()), this, SLOT(setAll()));
99 layout->addWidget(setAll);
100
101 QPushButton* save = new QPushButton(tr("Save"));
102 connect(save, SIGNAL(pressed()), this, SLOT(save()));
103 layout->addWidget(save);
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 m_currentKey = m_keyOrder.end();
120
121 m_background.load(":/res/keymap.qpic");
122
123 setAll->setFocus();
124}
125
126void GBAKeyEditor::setAll() {
127 m_currentKey = m_keyOrder.begin();
128 (*m_currentKey)->setFocus();
129}
130
131void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
132 setLocation(m_buttons, 0.5, 0.2);
133 setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
134 setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
135 setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
136 setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
137 setLocation(m_keySelect, 0.415, 0.93);
138 setLocation(m_keyStart, 0.585, 0.93);
139 setLocation(m_keyA, 0.826, 0.451);
140 setLocation(m_keyB, 0.667, 0.490);
141 setLocation(m_keyL, 0.1, 0.1);
142 setLocation(m_keyR, 0.9, 0.1);
143
144 if (m_profileSelect) {
145 setLocation(m_profileSelect, 0.5, 0.7);
146 }
147}
148
149void GBAKeyEditor::paintEvent(QPaintEvent* event) {
150 QPainter painter(this);
151 painter.scale(width() / 480.0, height() / 480.0);
152 painter.drawPicture(0, 0, m_background);
153}
154
155void GBAKeyEditor::closeEvent(QCloseEvent*) {
156 m_controller->releaseFocus(this);
157}
158
159bool GBAKeyEditor::event(QEvent* event) {
160 if (event->type() == QEvent::WindowActivate) {
161 m_controller->stealFocus(this);
162 } else if (event->type() == QEvent::WindowDeactivate) {
163 m_controller->releaseFocus(this);
164 }
165 return QWidget::event(event);
166}
167
168void GBAKeyEditor::setNext() {
169 findFocus();
170
171 if (m_currentKey == m_keyOrder.end()) {
172 return;
173 }
174
175 ++m_currentKey;
176 if (m_currentKey != m_keyOrder.end()) {
177 (*m_currentKey)->setFocus();
178 } else {
179 (*(m_currentKey - 1))->clearFocus();
180 }
181}
182
183void GBAKeyEditor::save() {
184 bindKey(m_keyDU, GBA_KEY_UP);
185 bindKey(m_keyDD, GBA_KEY_DOWN);
186 bindKey(m_keyDL, GBA_KEY_LEFT);
187 bindKey(m_keyDR, GBA_KEY_RIGHT);
188 bindKey(m_keySelect, GBA_KEY_SELECT);
189 bindKey(m_keyStart, GBA_KEY_START);
190 bindKey(m_keyA, GBA_KEY_A);
191 bindKey(m_keyB, GBA_KEY_B);
192 bindKey(m_keyL, GBA_KEY_L);
193 bindKey(m_keyR, GBA_KEY_R);
194 m_controller->saveConfiguration(m_type);
195
196#ifdef BUILD_SDL
197 if (m_profileSelect) {
198 m_controller->setPreferredGamepad(m_type, m_profileSelect->currentText());
199 }
200#endif
201
202 if (!m_profile.isNull()) {
203 m_controller->saveProfile(m_type, m_profile);
204 }
205}
206
207void GBAKeyEditor::refresh() {
208 const GBAInputMap* map = m_controller->map();
209 lookupBinding(map, m_keyDU, GBA_KEY_UP);
210 lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
211 lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
212 lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
213 lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
214 lookupBinding(map, m_keyStart, GBA_KEY_START);
215 lookupBinding(map, m_keyA, GBA_KEY_A);
216 lookupBinding(map, m_keyB, GBA_KEY_B);
217 lookupBinding(map, m_keyL, GBA_KEY_L);
218 lookupBinding(map, m_keyR, GBA_KEY_R);
219}
220
221void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
222#ifdef BUILD_SDL
223 if (m_type == SDL_BINDING_BUTTON) {
224 int value = GBAInputQueryBinding(map, m_type, key);
225 if (value != GBA_NO_MAPPING) {
226 keyEditor->setValueButton(value);
227 }
228 return;
229 }
230#endif
231 keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
232}
233
234#ifdef BUILD_SDL
235void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
236 GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
237 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
238 if (description->highDirection != GBA_KEY_NONE) {
239 KeyEditor* key = self->keyById(description->highDirection);
240 if (key) {
241 key->setValueAxis(axis, description->deadHigh);
242 }
243 }
244 if (description->lowDirection != GBA_KEY_NONE) {
245 KeyEditor* key = self->keyById(description->lowDirection);
246 if (key) {
247 key->setValueAxis(axis, description->deadLow);
248 }
249 }
250 }, this);
251}
252#endif
253
254void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
255#ifdef BUILD_SDL
256 if (keyEditor->direction() != GamepadAxisEvent::NEUTRAL) {
257 m_controller->bindAxis(m_type, keyEditor->value(), keyEditor->direction(), key);
258 } else {
259#endif
260 m_controller->bindKey(m_type, keyEditor->value(), key);
261#ifdef BUILD_SDL
262 }
263#endif
264}
265
266bool GBAKeyEditor::findFocus() {
267 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
268 return true;
269 }
270
271 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
272 if ((*key)->hasFocus()) {
273 m_currentKey = key;
274 return true;
275 }
276 }
277 return false;
278}
279
280#ifdef BUILD_SDL
281void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
282 if (!findFocus()) {
283 return;
284 }
285 KeyEditor* focused = *m_currentKey;
286 focused->setValueAxis(axis, value);
287}
288#endif
289
290KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
291 switch (key) {
292 case GBA_KEY_UP:
293 return m_keyDU;
294 case GBA_KEY_DOWN:
295 return m_keyDD;
296 case GBA_KEY_LEFT:
297 return m_keyDL;
298 case GBA_KEY_RIGHT:
299 return m_keyDR;
300 case GBA_KEY_A:
301 return m_keyA;
302 case GBA_KEY_B:
303 return m_keyB;
304 case GBA_KEY_L:
305 return m_keyL;
306 case GBA_KEY_R:
307 return m_keyR;
308 case GBA_KEY_SELECT:
309 return m_keySelect;
310 case GBA_KEY_START:
311 return m_keyStart;
312 default:
313 break;
314 }
315 return nullptr;
316}
317
318void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
319 QSize s = size();
320 QSize hint = widget->sizeHint();
321 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(),
322 hint.height());
323}