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