diff options
| author | 2016-12-21 23:49:36 -0500 | |
|---|---|---|
| committer | 2017-04-03 20:43:13 -0600 | |
| commit | 1631e99eed644bb9d313bc49ef6023ac3afe1e52 (patch) | |
| tree | 523eade6ec8d6779a4a55a46f1471079b88bf4f9 /src/citra_qt/configuration/configure_input.cpp | |
| parent | Merge pull request #2622 from jfmherokiller/socufix (diff) | |
| download | yuzu-1631e99eed644bb9d313bc49ef6023ac3afe1e52.tar.gz yuzu-1631e99eed644bb9d313bc49ef6023ac3afe1e52.tar.xz yuzu-1631e99eed644bb9d313bc49ef6023ac3afe1e52.zip | |
citra-qt: Move config dialog code to its own directory
Diffstat (limited to 'src/citra_qt/configuration/configure_input.cpp')
| -rw-r--r-- | src/citra_qt/configuration/configure_input.cpp | 205 |
1 files changed, 205 insertions, 0 deletions
diff --git a/src/citra_qt/configuration/configure_input.cpp b/src/citra_qt/configuration/configure_input.cpp new file mode 100644 index 000000000..daac9b63a --- /dev/null +++ b/src/citra_qt/configuration/configure_input.cpp | |||
| @@ -0,0 +1,205 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <memory> | ||
| 7 | #include <utility> | ||
| 8 | #include <QTimer> | ||
| 9 | #include "citra_qt/configuration/config.h" | ||
| 10 | #include "citra_qt/configuration/configure_input.h" | ||
| 11 | #include "common/param_package.h" | ||
| 12 | #include "input_common/main.h" | ||
| 13 | |||
| 14 | const std::array<std::string, ConfigureInput::ANALOG_SUB_BUTTONS_NUM> | ||
| 15 | ConfigureInput::analog_sub_buttons{{ | ||
| 16 | "up", "down", "left", "right", "modifier", | ||
| 17 | }}; | ||
| 18 | |||
| 19 | static QString getKeyName(int key_code) { | ||
| 20 | switch (key_code) { | ||
| 21 | case Qt::Key_Shift: | ||
| 22 | return QObject::tr("Shift"); | ||
| 23 | case Qt::Key_Control: | ||
| 24 | return QObject::tr("Ctrl"); | ||
| 25 | case Qt::Key_Alt: | ||
| 26 | return QObject::tr("Alt"); | ||
| 27 | case Qt::Key_Meta: | ||
| 28 | return ""; | ||
| 29 | default: | ||
| 30 | return QKeySequence(key_code).toString(); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | static void SetButtonKey(int key, Common::ParamPackage& button_param) { | ||
| 35 | button_param = Common::ParamPackage{InputCommon::GenerateKeyboardParam(key)}; | ||
| 36 | } | ||
| 37 | |||
| 38 | static 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 | |||
| 48 | ConfigureInput::ConfigureInput(QWidget* parent) | ||
| 49 | : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()), | ||
| 50 | timer(std::make_unique<QTimer>()) { | ||
| 51 | |||
| 52 | ui->setupUi(this); | ||
| 53 | setFocusPolicy(Qt::ClickFocus); | ||
| 54 | |||
| 55 | button_map = { | ||
| 56 | ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY, ui->buttonDpadUp, | ||
| 57 | ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight, ui->buttonL, ui->buttonR, | ||
| 58 | ui->buttonStart, ui->buttonSelect, ui->buttonZL, ui->buttonZR, ui->buttonHome, | ||
| 59 | }; | ||
| 60 | |||
| 61 | analog_map = {{ | ||
| 62 | { | ||
| 63 | ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight, | ||
| 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 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); }); | ||
| 94 | |||
| 95 | timer->setSingleShot(true); | ||
| 96 | connect(timer.get(), &QTimer::timeout, [this]() { | ||
| 97 | releaseKeyboard(); | ||
| 98 | releaseMouse(); | ||
| 99 | key_setter = boost::none; | ||
| 100 | updateButtonLabels(); | ||
| 101 | }); | ||
| 102 | |||
| 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); | ||
| 113 | } | ||
| 114 | |||
| 115 | void ConfigureInput::applyConfiguration() { | ||
| 116 | std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(), | ||
| 117 | [](const Common::ParamPackage& param) { return param.Serialize(); }); | ||
| 118 | std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(), | ||
| 119 | [](const Common::ParamPackage& param) { return param.Serialize(); }); | ||
| 120 | |||
| 121 | Settings::Apply(); | ||
| 122 | } | ||
| 123 | |||
| 124 | void ConfigureInput::loadConfiguration() { | ||
| 125 | std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(), | ||
| 126 | buttons_param.begin(), | ||
| 127 | [](const std::string& str) { return Common::ParamPackage(str); }); | ||
| 128 | std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(), | ||
| 129 | analogs_param.begin(), | ||
| 130 | [](const std::string& str) { return Common::ParamPackage(str); }); | ||
| 131 | updateButtonLabels(); | ||
| 132 | } | ||
| 133 | |||
| 134 | void ConfigureInput::restoreDefaults() { | ||
| 135 | for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) { | ||
| 136 | SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]); | ||
| 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 | } | ||
| 144 | } | ||
| 145 | updateButtonLabels(); | ||
| 146 | applyConfiguration(); | ||
| 147 | } | ||
| 148 | |||
| 149 | void ConfigureInput::updateButtonLabels() { | ||
| 150 | QString non_keyboard(tr("[non-keyboard]")); | ||
| 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 | } | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) { | ||
| 182 | button->setText(tr("[press key]")); | ||
| 183 | button->setFocus(); | ||
| 184 | |||
| 185 | key_setter = new_key_setter; | ||
| 186 | |||
| 187 | grabKeyboard(); | ||
| 188 | grabMouse(); | ||
| 189 | timer->start(5000); // Cancel after 5 seconds | ||
| 190 | } | ||
| 191 | |||
| 192 | void ConfigureInput::keyPressEvent(QKeyEvent* event) { | ||
| 193 | releaseKeyboard(); | ||
| 194 | releaseMouse(); | ||
| 195 | |||
| 196 | if (!key_setter || !event) | ||
| 197 | return; | ||
| 198 | |||
| 199 | if (event->key() != Qt::Key_Escape) | ||
| 200 | (*key_setter)(event->key()); | ||
| 201 | |||
| 202 | updateButtonLabels(); | ||
| 203 | key_setter = boost::none; | ||
| 204 | timer->stop(); | ||
| 205 | } | ||