src/platform/qt/GBAKeyEditor.cpp (view raw)
1#include "GBAKeyEditor.h"
2
3#include <QPaintEvent>
4#include <QPainter>
5#include <QPushButton>
6
7#include "InputController.h"
8#include "KeyEditor.h"
9
10extern "C" {
11#include "gba-input.h"
12}
13
14using namespace QGBA;
15
16const qreal GBAKeyEditor::DPAD_CENTER_X = 0.247;
17const qreal GBAKeyEditor::DPAD_CENTER_Y = 0.431;
18const qreal GBAKeyEditor::DPAD_WIDTH = 0.1;
19const qreal GBAKeyEditor::DPAD_HEIGHT = 0.1;
20
21GBAKeyEditor::GBAKeyEditor(InputController* controller, int type, QWidget* parent)
22 : QWidget(parent)
23{
24 setWindowFlags(windowFlags() & ~Qt::WindowFullscreenButtonHint);
25 setMinimumSize(300, 300);
26
27 const GBAInputMap* map = controller->map();
28
29 m_keyDU = new KeyEditor(this);
30 m_keyDD = new KeyEditor(this);
31 m_keyDL = new KeyEditor(this);
32 m_keyDR = new KeyEditor(this);
33 m_keySelect = new KeyEditor(this);
34 m_keyStart = new KeyEditor(this);
35 m_keyA = new KeyEditor(this);
36 m_keyB = new KeyEditor(this);
37 m_keyL = new KeyEditor(this);
38 m_keyR = new KeyEditor(this);
39 m_keyDU->setValue(GBAInputQueryBinding(map, type, GBA_KEY_UP));
40 m_keyDD->setValue(GBAInputQueryBinding(map, type, GBA_KEY_DOWN));
41 m_keyDL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_LEFT));
42 m_keyDR->setValue(GBAInputQueryBinding(map, type, GBA_KEY_RIGHT));
43 m_keySelect->setValue(GBAInputQueryBinding(map, type, GBA_KEY_SELECT));
44 m_keyStart->setValue(GBAInputQueryBinding(map, type, GBA_KEY_START));
45 m_keyA->setValue(GBAInputQueryBinding(map, type, GBA_KEY_A));
46 m_keyB->setValue(GBAInputQueryBinding(map, type, GBA_KEY_B));
47 m_keyL->setValue(GBAInputQueryBinding(map, type, GBA_KEY_L));
48 m_keyR->setValue(GBAInputQueryBinding(map, type, GBA_KEY_R));
49
50 connect(m_keyDU, &KeyEditor::valueChanged, [this, type, controller](int key) {
51 controller->bindKey(type, key, GBA_KEY_UP);
52 setNext();
53 });
54
55 connect(m_keyDD, &KeyEditor::valueChanged, [this, type, controller](int key) {
56 controller->bindKey(type, key, GBA_KEY_DOWN);
57 setNext();
58 });
59
60 connect(m_keyDL, &KeyEditor::valueChanged, [this, type, controller](int key) {
61 controller->bindKey(type, key, GBA_KEY_LEFT);
62 setNext();
63 });
64
65 connect(m_keyDR, &KeyEditor::valueChanged, [this, type, controller](int key) {
66 controller->bindKey(type, key, GBA_KEY_RIGHT);
67 setNext();
68 });
69
70 connect(m_keySelect, &KeyEditor::valueChanged, [this, type, controller](int key) {
71 controller->bindKey(type, key, GBA_KEY_SELECT);
72 setNext();
73 });
74
75 connect(m_keyStart, &KeyEditor::valueChanged, [this, type, controller](int key) {
76 controller->bindKey(type, key, GBA_KEY_START);
77 setNext();
78 });
79
80 connect(m_keyA, &KeyEditor::valueChanged, [this, type, controller](int key) {
81 controller->bindKey(type, key, GBA_KEY_A);
82 setNext();
83 });
84
85 connect(m_keyB, &KeyEditor::valueChanged, [this, type, controller](int key) {
86 controller->bindKey(type, key, GBA_KEY_B);
87 setNext();
88 });
89
90 connect(m_keyL, &KeyEditor::valueChanged, [this, type, controller](int key) {
91 controller->bindKey(type, key, GBA_KEY_L);
92 setNext();
93 });
94
95 connect(m_keyR, &KeyEditor::valueChanged, [this, type, controller](int key) {
96 controller->bindKey(type, key, GBA_KEY_R);
97 setNext();
98 });
99
100 m_setAll = new QPushButton(tr("Set all"), this);
101 connect(m_setAll, SIGNAL(pressed()), this, SLOT(setAll()));
102
103 m_keyOrder = QList<KeyEditor*>{
104 m_keyDU,
105 m_keyDR,
106 m_keyDD,
107 m_keyDL,
108 m_keyA,
109 m_keyB,
110 m_keySelect,
111 m_keyStart,
112 m_keyL,
113 m_keyR
114 };
115
116 m_currentKey = m_keyOrder.end();
117
118 m_background.load(":/res/keymap.qpic");
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_setAll, 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::setLocation(QWidget* widget, qreal x, qreal y) {
164 QSize s = size();
165 QSize hint = widget->sizeHint();
166 widget->setGeometry(s.width() * x - hint.width() / 2.0, s.height() * y - hint.height() / 2.0, hint.width(), hint.height());
167}