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