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
114void GBAKeyEditor::setAll() {
115 m_currentKey = m_keyOrder.begin();
116 (*m_currentKey)->setFocus();
117}
118
119void GBAKeyEditor::resizeEvent(QResizeEvent* event) {
120 setLocation(m_buttons, 0.5, 0.2);
121 setLocation(m_keyDU, DPAD_CENTER_X, DPAD_CENTER_Y - DPAD_HEIGHT);
122 setLocation(m_keyDD, DPAD_CENTER_X, DPAD_CENTER_Y + DPAD_HEIGHT);
123 setLocation(m_keyDL, DPAD_CENTER_X - DPAD_WIDTH, DPAD_CENTER_Y);
124 setLocation(m_keyDR, DPAD_CENTER_X + DPAD_WIDTH, DPAD_CENTER_Y);
125 setLocation(m_keySelect, 0.415, 0.93);
126 setLocation(m_keyStart, 0.585, 0.93);
127 setLocation(m_keyA, 0.826, 0.451);
128 setLocation(m_keyB, 0.667, 0.490);
129 setLocation(m_keyL, 0.1, 0.1);
130 setLocation(m_keyR, 0.9, 0.1);
131}
132
133void GBAKeyEditor::paintEvent(QPaintEvent* event) {
134 QPainter painter(this);
135 painter.scale(width() / 480.0, height() / 480.0);
136 painter.drawPicture(0, 0, m_background);
137}
138
139void GBAKeyEditor::setNext() {
140 findFocus();
141
142 if (m_currentKey == m_keyOrder.end()) {
143 return;
144 }
145
146 ++m_currentKey;
147 if (m_currentKey != m_keyOrder.end()) {
148 (*m_currentKey)->setFocus();
149 } else {
150 (*(m_currentKey - 1))->clearFocus();
151 }
152}
153
154void GBAKeyEditor::save() {
155 bindKey(m_keyDU, GBA_KEY_UP);
156 bindKey(m_keyDD, GBA_KEY_DOWN);
157 bindKey(m_keyDL, GBA_KEY_LEFT);
158 bindKey(m_keyDR, GBA_KEY_RIGHT);
159 bindKey(m_keySelect, GBA_KEY_SELECT);
160 bindKey(m_keyStart, GBA_KEY_START);
161 bindKey(m_keyA, GBA_KEY_A);
162 bindKey(m_keyB, GBA_KEY_B);
163 bindKey(m_keyL, GBA_KEY_L);
164 bindKey(m_keyR, GBA_KEY_R);
165 m_controller->saveConfiguration(m_type);
166}
167
168void GBAKeyEditor::lookupBinding(const GBAInputMap* map, KeyEditor* keyEditor, GBAKey key) {
169 #ifdef BUILD_SDL
170 if (m_type == SDL_BINDING_BUTTON) {
171 int value = GBAInputQueryBinding(map, m_type, key);
172 if (value != GBA_NO_MAPPING) {
173 keyEditor->setValueButton(value);
174 }
175 return;
176 }
177 #endif
178 keyEditor->setValueKey(GBAInputQueryBinding(map, m_type, key));
179}
180
181#ifdef BUILD_SDL
182void GBAKeyEditor::lookupAxes(const GBAInputMap* map) {
183 GBAInputEnumerateAxes(map, m_type, [](int axis, const GBAAxis* description, void* user) {
184 GBAKeyEditor* self = static_cast<GBAKeyEditor*>(user);
185 if (description->highDirection != GBA_KEY_NONE) {
186 KeyEditor* key = self->keyById(description->highDirection);
187 if (key) {
188 key->setValueAxis(axis, description->deadHigh);
189 }
190 }
191 if (description->lowDirection != GBA_KEY_NONE) {
192 KeyEditor* key = self->keyById(description->lowDirection);
193 if (key) {
194 key->setValueAxis(axis, description->deadLow);
195 }
196 }
197 }, this);
198}
199#endif
200
201void GBAKeyEditor::bindKey(const KeyEditor* keyEditor, GBAKey key) {
202 if (keyEditor->direction() != GamepadAxisEvent::NEUTRAL) {
203 m_controller->bindAxis(m_type, keyEditor->value(), keyEditor->direction(), key);
204 } else {
205 m_controller->bindKey(m_type, keyEditor->value(), key);
206 }
207}
208
209bool GBAKeyEditor::findFocus() {
210 if (m_currentKey != m_keyOrder.end() && (*m_currentKey)->hasFocus()) {
211 return true;
212 }
213
214 for (auto key = m_keyOrder.begin(); key != m_keyOrder.end(); ++key) {
215 if ((*key)->hasFocus()) {
216 m_currentKey = key;
217 return true;
218 }
219 }
220 return false;
221}
222
223#ifdef BUILD_SDL
224void GBAKeyEditor::setAxisValue(int axis, int32_t value) {
225 if (!findFocus()) {
226 return;
227 }
228 KeyEditor* focused = *m_currentKey;
229 focused->setValueAxis(axis, value);
230}
231#endif
232
233KeyEditor* GBAKeyEditor::keyById(GBAKey key) {
234 switch (key) {
235 case GBA_KEY_UP:
236 return m_keyDU;
237 case GBA_KEY_DOWN:
238 return m_keyDD;
239 case GBA_KEY_LEFT:
240 return m_keyDL;
241 case GBA_KEY_RIGHT:
242 return m_keyDR;
243 case GBA_KEY_A:
244 return m_keyA;
245 case GBA_KEY_B:
246 return m_keyB;
247 case GBA_KEY_L:
248 return m_keyL;
249 case GBA_KEY_R:
250 return m_keyR;
251 case GBA_KEY_SELECT:
252 return m_keySelect;
253 case GBA_KEY_START:
254 return m_keyStart;
255 default:
256 break;
257 }
258 return nullptr;
259}
260
261void GBAKeyEditor::setLocation(QWidget* widget, qreal x, qreal y) {
262 QSize s = size();
263 QSize hint = widget->sizeHint();
264 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
265}