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 <QPaintEvent>
9#include <QPainter>
10#include <QPushButton>
11#include <QTimer>
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, QWidget* parent)
25 : QWidget(parent)
26 , m_type(type)
27 , m_controller(controller)
28 , m_gamepadTimer(nullptr)
29{
30 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
31 setMinimumSize(300, 300);
32
33 const GBAInputMap* map = controller->map();
34
35 m_keyDU = new KeyEditor(this);
36 m_keyDD = new KeyEditor(this);
37 m_keyDL = new KeyEditor(this);
38 m_keyDR = new KeyEditor(this);
39 m_keySelect = new KeyEditor(this);
40 m_keyStart = new KeyEditor(this);
41 m_keyA = new KeyEditor(this);
42 m_keyB = new KeyEditor(this);
43 m_keyL = new KeyEditor(this);
44 m_keyR = new KeyEditor(this);
45
46 lookupBinding(map, m_keyDU, GBA_KEY_UP);
47 lookupBinding(map, m_keyDD, GBA_KEY_DOWN);
48 lookupBinding(map, m_keyDL, GBA_KEY_LEFT);
49 lookupBinding(map, m_keyDR, GBA_KEY_RIGHT);
50 lookupBinding(map, m_keySelect, GBA_KEY_SELECT);
51 lookupBinding(map, m_keyStart, GBA_KEY_START);
52 lookupBinding(map, m_keyA, GBA_KEY_A);
53 lookupBinding(map, m_keyB, GBA_KEY_B);
54 lookupBinding(map, m_keyL, GBA_KEY_L);
55 lookupBinding(map, m_keyR, GBA_KEY_R);
56
57#ifdef BUILD_SDL
58 lookupAxes(map);
59#endif
60
61 connect(m_keyDU, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
62 connect(m_keyDD, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
63 connect(m_keyDL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
64 connect(m_keyDR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
65 connect(m_keySelect, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
66 connect(m_keyStart, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
67 connect(m_keyA, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
68 connect(m_keyB, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
69 connect(m_keyL, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
70 connect(m_keyR, SIGNAL(valueChanged(int)), this, SLOT(setNext()));
71
72 connect(m_keyDU, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
73 connect(m_keyDD, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
74 connect(m_keyDL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
75 connect(m_keyDR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
76 connect(m_keySelect, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
77 connect(m_keyStart, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
78 connect(m_keyA, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
79 connect(m_keyB, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
80 connect(m_keyL, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
81 connect(m_keyR, SIGNAL(axisChanged(int, int)), this, SLOT(setNext()));
82
83 m_buttons = new QWidget(this);
84 QVBoxLayout* layout = new QVBoxLayout;
85 m_buttons->setLayout(layout);
86
87 QPushButton* setAll = new QPushButton(tr("Set all"));
88 connect(setAll, SIGNAL(pressed()), this, SLOT(setAll()));
89 layout->addWidget(setAll);
90
91 QPushButton* save = new QPushButton(tr("Save"));
92 connect(save, SIGNAL(pressed()), this, SLOT(save()));
93 layout->addWidget(save);
94 layout->setSpacing(6);
95
96 m_keyOrder = QList<KeyEditor*>{
97 m_keyDU,
98 m_keyDR,
99 m_keyDD,
100 m_keyDL,
101 m_keyA,
102 m_keyB,
103 m_keySelect,
104 m_keyStart,
105 m_keyL,
106 m_keyR
107 };
108
109 m_currentKey = m_keyOrder.end();
110
111 m_background.load(":/res/keymap.qpic");
112
113 setAll->setFocus();
114
115#ifdef BUILD_SDL
116 if (type == SDL_BINDING_BUTTON) {
117 m_gamepadTimer = new QTimer(this);
118 connect(m_gamepadTimer, SIGNAL(timeout()), this, SLOT(testGamepad()));
119 m_gamepadTimer->setInterval(50);
120 m_gamepadTimer->start();
121 }
122#endif
123}
124
125void GBAKeyEditor::setAll() {
126 m_currentKey = m_keyOrder.begin();
127 (*m_currentKey)->setFocus();
128}
129
130void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
131 setLocation(m_buttons, 0.5, 0.2);
132 setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
133 setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
134 setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
135 setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
136 setLocation(m_keySelect, 0.415, 0.93);
137 setLocation(m_keyStart, 0.585, 0.93);
138 setLocation(m_keyA, 0.826, 0.451);
139 setLocation(m_keyB, 0.667, 0.490);
140 setLocation(m_keyL, 0.1, 0.1);
141 setLocation(m_keyR, 0.9, 0.1);
142}
143
144void GBAKeyEditor::paintEvent(QPaintEvent* event) {
145 QPainter painter(this);
146 painter.scale(width() / 480.0, height() / 480.0);
147 painter.drawPicture(0, 0, m_background);
148}
149
150void GBAKeyEditor::setNext() {
151 if (m_currentKey == m_keyOrder.end()) {
152 return;
153 }
154
155 if (!(*m_currentKey)->hasFocus()) {
156 m_currentKey = m_keyOrder.end();
157 }
158
159 ++m_currentKey;
160 if (m_currentKey != m_keyOrder.end()) {
161 (*m_currentKey)->setFocus();
162 } else {
163 (*(m_currentKey - 1))->clearFocus();
164 }
165}
166
167void GBAKeyEditor::save() {
168 bindKey(m_keyDU, GBA_KEY_UP);
169 bindKey(m_keyDD, GBA_KEY_DOWN);
170 bindKey(m_keyDL, GBA_KEY_LEFT);
171 bindKey(m_keyDR, GBA_KEY_RIGHT);
172 bindKey(m_keySelect, GBA_KEY_SELECT);
173 bindKey(m_keyStart, GBA_KEY_START);
174 bindKey(m_keyA, GBA_KEY_A);
175 bindKey(m_keyB, GBA_KEY_B);
176 bindKey(m_keyL, GBA_KEY_L);
177 bindKey(m_keyR, GBA_KEY_R);
178 m_controller->saveConfiguration(m_type);
179}
180
181void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
182 #ifdef BUILD_SDL
183 if (m_type == SDL_BINDING_BUTTON) {
184 int value = GBAInputQueryBinding(map, m_type, key);
185 if (value != GBA_NO_MAPPING) {
186 keyEditor->setValueButton(value);
187 }
188 return;
189 }
190 #endif
191 keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
192}
193
194#ifdef BUILD_SDL
195void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
196 GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
197 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
198 if (description->highDirection != GBA_KEY_NONE) {
199 KeyEditor* key = self->keyById(description->highDirection);
200 if (key) {
201 key->setValueAxis(axis, description->deadHigh);
202 }
203 }
204 if (description->lowDirection != GBA_KEY_NONE) {
205 KeyEditor* key = self->keyById(description->lowDirection);
206 if (key) {
207 key->setValueAxis(axis, description->deadLow);
208 }
209 }
210 }, this);
211}
212#endif
213
214void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
215 if (keyEditor->direction() != InputController::NEUTRAL) {
216 m_controller->bindAxis(m_type, keyEditor->value(), keyEditor->direction(), key);
217 } else {
218 m_controller->bindKey(m_type, keyEditor->value(), key);
219 }
220}
221
222bool GBAKeyEditor::findFocus() {
223 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
224 return true;
225 }
226
227 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
228 if ((*key)->hasFocus()) {
229 m_currentKey = key;
230 return true;
231 }
232 }
233 return false;
234}
235
236#ifdef BUILD_SDL
237void GBAKeyEditor::testGamepad() {
238 m_gamepadTimer->setInterval(50);
239 if (!findFocus()) {
240 return;
241 }
242 KeyEditor* focused = *m_currentKey;
243
244 auto activeAxes = m_controller->activeGamepadAxes();
245 auto oldAxes = m_activeAxes;
246 m_activeAxes = activeAxes;
247 activeAxes.subtract(oldAxes);
248 if (!activeAxes.empty()) {
249 focused->setValueAxis(activeAxes.begin()->first, activeAxes.begin()->second);
250 return;
251 }
252
253 auto activeButtons = m_controller->activeGamepadButtons();
254 auto oldButtons = m_activeButtons;
255 m_activeButtons = activeButtons;
256 activeButtons.subtract(oldButtons);
257 if (!activeButtons.empty()) {
258 focused->setValueButton(*activeButtons.begin());
259 return;
260 }
261}
262#endif
263
264KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
265 switch (key) {
266 case GBA_KEY_UP:
267 return m_keyDU;
268 case GBA_KEY_DOWN:
269 return m_keyDD;
270 case GBA_KEY_LEFT:
271 return m_keyDL;
272 case GBA_KEY_RIGHT:
273 return m_keyDR;
274 case GBA_KEY_A:
275 return m_keyA;
276 case GBA_KEY_B:
277 return m_keyB;
278 case GBA_KEY_L:
279 return m_keyL;
280 case GBA_KEY_R:
281 return m_keyR;
282 case GBA_KEY_SELECT:
283 return m_keySelect;
284 case GBA_KEY_START:
285 return m_keyStart;
286 default:
287 break;
288 }
289 return nullptr;
290}
291
292void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
293 QSize s = size();
294 QSize hint = widget->sizeHint();
295 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
296}