summaryrefslogtreecommitdiff
path: root/src/citra_qt/configure_input.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2017-03-17 14:59:39 -0400
committerGravatar GitHub2017-03-17 14:59:39 -0400
commit423ab5e2bcf5a522e5f412447c05f648df57a14c (patch)
tree1e60eaeffa59229254a47f885d2fe2cbbdc1a5c0 /src/citra_qt/configure_input.cpp
parentMerge pull request #2618 from wwylele/log-less-filename (diff)
parentqt/config_input: don't connect for null button (diff)
downloadyuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.gz
yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.xz
yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.zip
Merge pull request #2497 from wwylele/input-2
Refactor input emulation & add SDL gamepad support
Diffstat (limited to 'src/citra_qt/configure_input.cpp')
-rw-r--r--src/citra_qt/configure_input.cpp178
1 files changed, 119 insertions, 59 deletions
diff --git a/src/citra_qt/configure_input.cpp b/src/citra_qt/configure_input.cpp
index c29652f32..b59713e2c 100644
--- a/src/citra_qt/configure_input.cpp
+++ b/src/citra_qt/configure_input.cpp
@@ -2,13 +2,21 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <algorithm>
5#include <memory> 6#include <memory>
6#include <utility> 7#include <utility>
7#include <QTimer> 8#include <QTimer>
8#include "citra_qt/config.h" 9#include "citra_qt/config.h"
9#include "citra_qt/configure_input.h" 10#include "citra_qt/configure_input.h"
11#include "common/param_package.h"
12#include "input_common/main.h"
10 13
11static QString getKeyName(Qt::Key key_code) { 14const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM>
15 ConfigureInput::analog_sub_buttons{{
16 "up", "down", "left", "right", "modifier",
17 }};
18
19static QString getKeyName(int key_code) {
12 switch (key_code) { 20 switch (key_code) {
13 case Qt::Key_Shift: 21 case Qt::Key_Shift:
14 return QObject::tr("Shift"); 22 return QObject::tr("Shift");
@@ -23,6 +31,20 @@ static QString getKeyName(Qt::Key key_code) {
23 } 31 }
24} 32}
25 33
34static void SetButtonKey(int key, Common::ParamPackage& button_param) {
35 button_param = Common::ParamPackage{InputCommon::GenerateKeyboardParam(key)};
36}
37
38static void SetAnalogKey(int key, Common::ParamPackage& analog_param,
39 const std::string& button_name) {
40 if (analog_param.Get("engine", "") != "analog_from_button") {
41 analog_param = {
42 {"engine", "analog_from_button"}, {"modifier_scale", "0.5"},
43 };
44 }
45 analog_param.Set(button_name, InputCommon::GenerateKeyboardParam(key));
46}
47
26ConfigureInput::ConfigureInput(QWidget* parent) 48ConfigureInput::ConfigureInput(QWidget* parent)
27 : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()), 49 : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
28 timer(std::make_unique<QTimer>()) { 50 timer(std::make_unique<QTimer>()) {
@@ -31,36 +53,41 @@ ConfigureInput::ConfigureInput(QWidget* parent)
31 setFocusPolicy(Qt::ClickFocus); 53 setFocusPolicy(Qt::ClickFocus);
32 54
33 button_map = { 55 button_map = {
34 {Settings::NativeInput::Values::A, ui->buttonA}, 56 ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY, ui->buttonDpadUp,
35 {Settings::NativeInput::Values::B, ui->buttonB}, 57 ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight, ui->buttonL, ui->buttonR,
36 {Settings::NativeInput::Values::X, ui->buttonX}, 58 ui->buttonStart, ui->buttonSelect, ui->buttonZL, ui->buttonZR, ui->buttonHome,
37 {Settings::NativeInput::Values::Y, ui->buttonY},
38 {Settings::NativeInput::Values::L, ui->buttonL},
39 {Settings::NativeInput::Values::R, ui->buttonR},
40 {Settings::NativeInput::Values::ZL, ui->buttonZL},
41 {Settings::NativeInput::Values::ZR, ui->buttonZR},
42 {Settings::NativeInput::Values::START, ui->buttonStart},
43 {Settings::NativeInput::Values::SELECT, ui->buttonSelect},
44 {Settings::NativeInput::Values::HOME, ui->buttonHome},
45 {Settings::NativeInput::Values::DUP, ui->buttonDpadUp},
46 {Settings::NativeInput::Values::DDOWN, ui->buttonDpadDown},
47 {Settings::NativeInput::Values::DLEFT, ui->buttonDpadLeft},
48 {Settings::NativeInput::Values::DRIGHT, ui->buttonDpadRight},
49 {Settings::NativeInput::Values::CUP, ui->buttonCStickUp},
50 {Settings::NativeInput::Values::CDOWN, ui->buttonCStickDown},
51 {Settings::NativeInput::Values::CLEFT, ui->buttonCStickLeft},
52 {Settings::NativeInput::Values::CRIGHT, ui->buttonCStickRight},
53 {Settings::NativeInput::Values::CIRCLE_UP, ui->buttonCircleUp},
54 {Settings::NativeInput::Values::CIRCLE_DOWN, ui->buttonCircleDown},
55 {Settings::NativeInput::Values::CIRCLE_LEFT, ui->buttonCircleLeft},
56 {Settings::NativeInput::Values::CIRCLE_RIGHT, ui->buttonCircleRight},
57 {Settings::NativeInput::Values::CIRCLE_MODIFIER, ui->buttonCircleMod},
58 }; 59 };
59 60
60 for (const auto& entry : button_map) { 61 analog_map = {{
61 const Settings::NativeInput::Values input_id = entry.first; 62 {
62 connect(entry.second, &QPushButton::released, 63 ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight,
63 [this, input_id]() { handleClick(input_id); }); 64 ui->buttonCircleMod,
65 },
66 {
67 ui->buttonCStickUp, ui->buttonCStickDown, ui->buttonCStickLeft, ui->buttonCStickRight,
68 nullptr,
69 },
70 }};
71
72 for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
73 if (button_map[button_id])
74 connect(button_map[button_id], &QPushButton::released, [=]() {
75 handleClick(button_map[button_id],
76 [=](int key) { SetButtonKey(key, buttons_param[button_id]); });
77 });
78 }
79
80 for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
81 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
82 if (analog_map[analog_id][sub_button_id] != nullptr) {
83 connect(analog_map[analog_id][sub_button_id], &QPushButton::released, [=]() {
84 handleClick(analog_map[analog_id][sub_button_id], [=](int key) {
85 SetAnalogKey(key, analogs_param[analog_id],
86 analog_sub_buttons[sub_button_id]);
87 });
88 });
89 }
90 }
64 } 91 }
65 92
66 connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); }); 93 connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
@@ -69,50 +96,93 @@ ConfigureInput::ConfigureInput(QWidget* parent)
69 connect(timer.get(), &QTimer::timeout, [this]() { 96 connect(timer.get(), &QTimer::timeout, [this]() {
70 releaseKeyboard(); 97 releaseKeyboard();
71 releaseMouse(); 98 releaseMouse();
72 current_input_id = boost::none; 99 key_setter = boost::none;
73 updateButtonLabels(); 100 updateButtonLabels();
74 }); 101 });
75 102
76 this->loadConfiguration(); 103 this->loadConfiguration();
104
105 // TODO(wwylele): enable these when the input emulation for them is implemented
106 ui->buttonZL->setEnabled(false);
107 ui->buttonZR->setEnabled(false);
108 ui->buttonHome->setEnabled(false);
109 ui->buttonCStickUp->setEnabled(false);
110 ui->buttonCStickDown->setEnabled(false);
111 ui->buttonCStickLeft->setEnabled(false);
112 ui->buttonCStickRight->setEnabled(false);
77} 113}
78 114
79void ConfigureInput::applyConfiguration() { 115void ConfigureInput::applyConfiguration() {
80 for (const auto& input_id : Settings::NativeInput::All) { 116 std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
81 const size_t index = static_cast<size_t>(input_id); 117 [](const Common::ParamPackage& param) { return param.Serialize(); });
82 Settings::values.input_mappings[index] = static_cast<int>(key_map[input_id]); 118 std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
83 } 119 [](const Common::ParamPackage& param) { return param.Serialize(); });
120
84 Settings::Apply(); 121 Settings::Apply();
85} 122}
86 123
87void ConfigureInput::loadConfiguration() { 124void ConfigureInput::loadConfiguration() {
88 for (const auto& input_id : Settings::NativeInput::All) { 125 std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
89 const size_t index = static_cast<size_t>(input_id); 126 buttons_param.begin(),
90 key_map[input_id] = static_cast<Qt::Key>(Settings::values.input_mappings[index]); 127 [](const std::string& str) { return Common::ParamPackage(str); });
91 } 128 std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(),
129 analogs_param.begin(),
130 [](const std::string& str) { return Common::ParamPackage(str); });
92 updateButtonLabels(); 131 updateButtonLabels();
93} 132}
94 133
95void ConfigureInput::restoreDefaults() { 134void ConfigureInput::restoreDefaults() {
96 for (const auto& input_id : Settings::NativeInput::All) { 135 for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) {
97 const size_t index = static_cast<size_t>(input_id); 136 SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]);
98 key_map[input_id] = static_cast<Qt::Key>(Config::defaults[index].toInt()); 137 }
138
139 for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
140 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
141 SetAnalogKey(Config::default_analogs[analog_id][sub_button_id],
142 analogs_param[analog_id], analog_sub_buttons[sub_button_id]);
143 }
99 } 144 }
100 updateButtonLabels(); 145 updateButtonLabels();
101 applyConfiguration(); 146 applyConfiguration();
102} 147}
103 148
104void ConfigureInput::updateButtonLabels() { 149void ConfigureInput::updateButtonLabels() {
105 for (const auto& input_id : Settings::NativeInput::All) { 150 QString non_keyboard(tr("[non-keyboard]"));
106 button_map[input_id]->setText(getKeyName(key_map[input_id])); 151
152 auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) {
153 if (param.Get("engine", "") != "keyboard") {
154 return non_keyboard;
155 } else {
156 return getKeyName(param.Get("code", 0));
157 }
158 };
159
160 for (int button = 0; button < Settings::NativeButton::NumButtons; button++) {
161 button_map[button]->setText(KeyToText(buttons_param[button]));
162 }
163
164 for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) {
165 if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") {
166 for (QPushButton* button : analog_map[analog_id]) {
167 if (button)
168 button->setText(non_keyboard);
169 }
170 } else {
171 for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) {
172 Common::ParamPackage param(
173 analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], ""));
174 if (analog_map[analog_id][sub_button_id])
175 analog_map[analog_id][sub_button_id]->setText(KeyToText(param));
176 }
177 }
107 } 178 }
108} 179}
109 180
110void ConfigureInput::handleClick(Settings::NativeInput::Values input_id) { 181void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) {
111 QPushButton* button = button_map[input_id];
112 button->setText(tr("[press key]")); 182 button->setText(tr("[press key]"));
113 button->setFocus(); 183 button->setFocus();
114 184
115 current_input_id = input_id; 185 key_setter = new_key_setter;
116 186
117 grabKeyboard(); 187 grabKeyboard();
118 grabMouse(); 188 grabMouse();
@@ -123,23 +193,13 @@ void ConfigureInput::keyPressEvent(QKeyEvent* event) {
123 releaseKeyboard(); 193 releaseKeyboard();
124 releaseMouse(); 194 releaseMouse();
125 195
126 if (!current_input_id || !event) 196 if (!key_setter || !event)
127 return; 197 return;
128 198
129 if (event->key() != Qt::Key_Escape) 199 if (event->key() != Qt::Key_Escape)
130 setInput(*current_input_id, static_cast<Qt::Key>(event->key())); 200 (*key_setter)(event->key());
131 201
132 updateButtonLabels(); 202 updateButtonLabels();
133 current_input_id = boost::none; 203 key_setter = boost::none;
134 timer->stop(); 204 timer->stop();
135} 205}
136
137void ConfigureInput::setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed) {
138 // Remove duplicates
139 for (auto& pair : key_map) {
140 if (pair.second == key_pressed)
141 pair.second = Qt::Key_unknown;
142 }
143
144 key_map[input_id] = key_pressed;
145}