1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
// Copyright 2016 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <algorithm>
#include <memory>
#include <utility>
#include <QTimer>
#include "citra_qt/config.h"
#include "citra_qt/configure_input.h"
#include "common/param_package.h"
#include "input_common/main.h"
const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
ConfigureInput::analog_sub_buttons{{
"up", "down", "left", "right", "modifier",
}};
static QString getKeyName(int key_code) {
switch (key_code) {
case Qt::Key_Shift:
return QObject::tr("Shift");
case Qt::Key_Control:
return QObject::tr("Ctrl");
case Qt::Key_Alt:
return QObject::tr("Alt");
case Qt::Key_Meta:
return "";
default:
return QKeySequence(key_code).toString();
}
}
static void SetButtonKey(int key, Common::ParamPackage& button_param) {
button_param = Common::ParamPackage{InputCommon::GenerateKeyboardParam(key)};
}
static void SetAnalogKey(int key, Common::ParamPackage& analog_param,
const std::string& button_name) {
if (analog_param.Get("engine", "") != "analog_from_button") {
analog_param = {
{"engine", "analog_from_button"}, {"modifier_scale", "0.5"},
};
}
analog_param.Set(button_name, InputCommon::GenerateKeyboardParam(key));
}
ConfigureInput::ConfigureInput(QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
timer(std::make_unique<QTimer>()) {
ui->setupUi(this);
setFocusPolicy(Qt::ClickFocus);
button_map = {
ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY, ui->buttonDpadUp,
ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight, ui->buttonL, ui->buttonR,
ui->buttonStart, ui->buttonSelect, ui->buttonZL, ui->buttonZR, ui->buttonHome,
};
analog_map = {{
{
ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight,
ui->buttonCircleMod,
},
{
ui->buttonCStickUp, ui->buttonCStickDown, ui->buttonCStickLeft, ui->buttonCStickRight,
nullptr,
},
}};
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
if (button_map[button_id])
connect(button_map[button_id], &QPushButton::released, [=]() {
handleClick(button_map[button_id],
[=](int key) { SetButtonKey(key, buttons_param[button_id]); });
});
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
connect(analog_map[analog_id][sub_button_id], &QPushButton::released, [=]() {
handleClick(analog_map[analog_id][sub_button_id], [=](int key) {
SetAnalogKey(key, analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
});
});
}
}
connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
timer->setSingleShot(true);
connect(timer.get(), &QTimer::timeout, [this]() {
releaseKeyboard();
releaseMouse();
key_setter = boost::none;
updateButtonLabels();
});
this->loadConfiguration();
// TODO(wwylele): enable these when the input emulation for them is implemented
ui->buttonZL->setEnabled(false);
ui->buttonZR->setEnabled(false);
ui->buttonHome->setEnabled(false);
ui->buttonCStickUp->setEnabled(false);
ui->buttonCStickDown->setEnabled(false);
ui->buttonCStickLeft->setEnabled(false);
ui->buttonCStickRight->setEnabled(false);
}
void ConfigureInput::applyConfiguration() {
std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
[](const Common::ParamPackage& param) { return param.Serialize(); });
std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
[](const Common::ParamPackage& param) { return param.Serialize(); });
Settings::Apply();
}
void ConfigureInput::loadConfiguration() {
std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
buttons_param.begin(),
[](const std::string& str) { return Common::ParamPackage(str); });
std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(),
analogs_param.begin(),
[](const std::string& str) { return Common::ParamPackage(str); });
updateButtonLabels();
}
void ConfigureInput::restoreDefaults() {
for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]);
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
SetAnalogKey(Config::default_analogs[analog_id][sub_button_id],
analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
}
}
updateButtonLabels();
applyConfiguration();
}
void ConfigureInput::updateButtonLabels() {
QString non_keyboard(tr("[non-keyboard]"));
auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) {
if (param.Get("engine", "") != "keyboard") {
return non_keyboard;
} else {
return getKeyName(param.Get("code", 0));
}
};
for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
button_map[button]->setText(KeyToText(buttons_param[button]));
}
for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") {
for (QPushButton* button : analog_map[analog_id]) {
if (button)
button->setText(non_keyboard);
}
} else {
for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
Common::ParamPackage param(
analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], ""));
if (analog_map[analog_id][sub_button_id])
analog_map[analog_id][sub_button_id]->setText(KeyToText(param));
}
}
}
}
void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) {
button->setText(tr("[press key]"));
button->setFocus();
key_setter = new_key_setter;
grabKeyboard();
grabMouse();
timer->start(5000); // Cancel after 5 seconds
}
void ConfigureInput::keyPressEvent(QKeyEvent* event) {
releaseKeyboard();
releaseMouse();
if (!key_setter || !event)
return;
if (event->key() != Qt::Key_Escape)
(*key_setter)(event->key());
updateButtonLabels();
key_setter = boost::none;
timer->stop();
}
|