diff options
| author | 2017-01-22 21:02:29 +0200 | |
|---|---|---|
| committer | 2017-03-01 23:30:57 +0200 | |
| commit | e7a602fe1690f6cafc4a40987340bc852effd2d3 (patch) | |
| tree | b0255b1d3b93fd30a27fd695266baba21e148e50 /src | |
| parent | InputCommon: add SDL joystick support (diff) | |
| download | yuzu-e7a602fe1690f6cafc4a40987340bc852effd2d3.tar.gz yuzu-e7a602fe1690f6cafc4a40987340bc852effd2d3.tar.xz yuzu-e7a602fe1690f6cafc4a40987340bc852effd2d3.zip | |
Qt: rework input configuration for new input system
Diffstat (limited to 'src')
| -rw-r--r-- | src/citra_qt/configure_input.cpp | 178 | ||||
| -rw-r--r-- | src/citra_qt/configure_input.h | 34 |
2 files changed, 144 insertions, 68 deletions
diff --git a/src/citra_qt/configure_input.cpp b/src/citra_qt/configure_input.cpp index 8846a68b2..4a14757ad 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 | ||
| 11 | static QString getKeyName(Qt::Key key_code) { | 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) { | ||
| 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 | ||
| 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 | |||
| 26 | ConfigureInput::ConfigureInput(QWidget* parent) | 48 | ConfigureInput::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,38 @@ 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 | connect(analog_map[analog_id][sub_button_id], &QPushButton::released, [=]() { | ||
| 83 | handleClick(analog_map[analog_id][sub_button_id], [=](int key) { | ||
| 84 | SetAnalogKey(key, analogs_param[analog_id], analog_sub_buttons[sub_button_id]); | ||
| 85 | }); | ||
| 86 | }); | ||
| 87 | } | ||
| 64 | } | 88 | } |
| 65 | 89 | ||
| 66 | connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); }); | 90 | connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); }); |
| @@ -69,43 +93,93 @@ ConfigureInput::ConfigureInput(QWidget* parent) | |||
| 69 | connect(timer.get(), &QTimer::timeout, [this]() { | 93 | connect(timer.get(), &QTimer::timeout, [this]() { |
| 70 | releaseKeyboard(); | 94 | releaseKeyboard(); |
| 71 | releaseMouse(); | 95 | releaseMouse(); |
| 72 | current_input_id = boost::none; | 96 | key_setter = boost::none; |
| 73 | updateButtonLabels(); | 97 | updateButtonLabels(); |
| 74 | }); | 98 | }); |
| 75 | 99 | ||
| 76 | this->loadConfiguration(); | 100 | this->loadConfiguration(); |
| 101 | |||
| 102 | // TODO(wwylele): enable these when the input emulation for them is implemented | ||
| 103 | ui->buttonZL->setEnabled(false); | ||
| 104 | ui->buttonZR->setEnabled(false); | ||
| 105 | ui->buttonHome->setEnabled(false); | ||
| 106 | ui->buttonCStickUp->setEnabled(false); | ||
| 107 | ui->buttonCStickDown->setEnabled(false); | ||
| 108 | ui->buttonCStickLeft->setEnabled(false); | ||
| 109 | ui->buttonCStickRight->setEnabled(false); | ||
| 77 | } | 110 | } |
| 78 | 111 | ||
| 79 | void ConfigureInput::applyConfiguration() { | 112 | void ConfigureInput::applyConfiguration() { |
| 80 | for (const auto& input_id : Settings::NativeInput::All) { | 113 | std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(), |
| 81 | const size_t index = static_cast<size_t>(input_id); | 114 | [](const Common::ParamPackage& param) { return param.Serialize(); }); |
| 82 | Settings::values.input_mappings[index] = static_cast<int>(key_map[input_id]); | 115 | std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(), |
| 83 | } | 116 | [](const Common::ParamPackage& param) { return param.Serialize(); }); |
| 117 | |||
| 84 | Settings::Apply(); | 118 | Settings::Apply(); |
| 85 | } | 119 | } |
| 86 | 120 | ||
| 87 | void ConfigureInput::loadConfiguration() { | 121 | void ConfigureInput::loadConfiguration() { |
| 88 | for (const auto& input_id : Settings::NativeInput::All) { | 122 | std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(), |
| 89 | const size_t index = static_cast<size_t>(input_id); | 123 | buttons_param.begin(), |
| 90 | key_map[input_id] = static_cast<Qt::Key>(Settings::values.input_mappings[index]); | 124 | [](const std::string& str) { return Common::ParamPackage(str); }); |
| 91 | } | 125 | std::transform(Settings::values.analogs.begin(), Settings::values.analogs.end(), |
| 126 | analogs_param.begin(), | ||
| 127 | [](const std::string& str) { return Common::ParamPackage(str); }); | ||
| 92 | updateButtonLabels(); | 128 | updateButtonLabels(); |
| 93 | } | 129 | } |
| 94 | 130 | ||
| 95 | void ConfigureInput::restoreDefaults() {} | 131 | void ConfigureInput::restoreDefaults() { |
| 132 | for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) { | ||
| 133 | SetButtonKey(Config::default_buttons[button_id], buttons_param[button_id]); | ||
| 134 | } | ||
| 135 | |||
| 136 | for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) { | ||
| 137 | for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) { | ||
| 138 | SetAnalogKey(Config::default_analogs[analog_id][sub_button_id], | ||
| 139 | analogs_param[analog_id], analog_sub_buttons[sub_button_id]); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | updateButtonLabels(); | ||
| 143 | applyConfiguration(); | ||
| 144 | } | ||
| 96 | 145 | ||
| 97 | void ConfigureInput::updateButtonLabels() { | 146 | void ConfigureInput::updateButtonLabels() { |
| 98 | for (const auto& input_id : Settings::NativeInput::All) { | 147 | QString non_keyboard(tr("[non-keyboard]")); |
| 99 | button_map[input_id]->setText(getKeyName(key_map[input_id])); | 148 | |
| 149 | auto KeyToText = [&non_keyboard](const Common::ParamPackage& param) { | ||
| 150 | if (param.Get("engine", "") != "keyboard") { | ||
| 151 | return non_keyboard; | ||
| 152 | } else { | ||
| 153 | return getKeyName(param.Get("code", 0)); | ||
| 154 | } | ||
| 155 | }; | ||
| 156 | |||
| 157 | for (int button = 0; button < Settings::NativeButton::NumButtons; button++) { | ||
| 158 | button_map[button]->setText(KeyToText(buttons_param[button])); | ||
| 159 | } | ||
| 160 | |||
| 161 | for (int analog_id = 0; analog_id < Settings::NativeAnalog::NumAnalogs; analog_id++) { | ||
| 162 | if (analogs_param[analog_id].Get("engine", "") != "analog_from_button") { | ||
| 163 | for (QPushButton* button : analog_map[analog_id]) { | ||
| 164 | if (button) | ||
| 165 | button->setText(non_keyboard); | ||
| 166 | } | ||
| 167 | } else { | ||
| 168 | for (int sub_button_id = 0; sub_button_id < ANALOG_SUB_BUTTONS_NUM; sub_button_id++) { | ||
| 169 | Common::ParamPackage param( | ||
| 170 | analogs_param[analog_id].Get(analog_sub_buttons[sub_button_id], "")); | ||
| 171 | if (analog_map[analog_id][sub_button_id]) | ||
| 172 | analog_map[analog_id][sub_button_id]->setText(KeyToText(param)); | ||
| 173 | } | ||
| 174 | } | ||
| 100 | } | 175 | } |
| 101 | } | 176 | } |
| 102 | 177 | ||
| 103 | void ConfigureInput::handleClick(Settings::NativeInput::Values input_id) { | 178 | void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) { |
| 104 | QPushButton* button = button_map[input_id]; | ||
| 105 | button->setText(tr("[press key]")); | 179 | button->setText(tr("[press key]")); |
| 106 | button->setFocus(); | 180 | button->setFocus(); |
| 107 | 181 | ||
| 108 | current_input_id = input_id; | 182 | key_setter = new_key_setter; |
| 109 | 183 | ||
| 110 | grabKeyboard(); | 184 | grabKeyboard(); |
| 111 | grabMouse(); | 185 | grabMouse(); |
| @@ -116,23 +190,13 @@ void ConfigureInput::keyPressEvent(QKeyEvent* event) { | |||
| 116 | releaseKeyboard(); | 190 | releaseKeyboard(); |
| 117 | releaseMouse(); | 191 | releaseMouse(); |
| 118 | 192 | ||
| 119 | if (!current_input_id || !event) | 193 | if (!key_setter || !event) |
| 120 | return; | 194 | return; |
| 121 | 195 | ||
| 122 | if (event->key() != Qt::Key_Escape) | 196 | if (event->key() != Qt::Key_Escape) |
| 123 | setInput(*current_input_id, static_cast<Qt::Key>(event->key())); | 197 | (*key_setter)(event->key()); |
| 124 | 198 | ||
| 125 | updateButtonLabels(); | 199 | updateButtonLabels(); |
| 126 | current_input_id = boost::none; | 200 | key_setter = boost::none; |
| 127 | timer->stop(); | 201 | timer->stop(); |
| 128 | } | 202 | } |
| 129 | |||
| 130 | void ConfigureInput::setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed) { | ||
| 131 | // Remove duplicates | ||
| 132 | for (auto& pair : key_map) { | ||
| 133 | if (pair.second == key_pressed) | ||
| 134 | pair.second = Qt::Key_unknown; | ||
| 135 | } | ||
| 136 | |||
| 137 | key_map[input_id] = key_pressed; | ||
| 138 | } | ||
diff --git a/src/citra_qt/configure_input.h b/src/citra_qt/configure_input.h index bc343db83..c950fbcb4 100644 --- a/src/citra_qt/configure_input.h +++ b/src/citra_qt/configure_input.h | |||
| @@ -4,10 +4,14 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <array> | ||
| 8 | #include <functional> | ||
| 7 | #include <memory> | 9 | #include <memory> |
| 10 | #include <string> | ||
| 8 | #include <QKeyEvent> | 11 | #include <QKeyEvent> |
| 9 | #include <QWidget> | 12 | #include <QWidget> |
| 10 | #include <boost/optional.hpp> | 13 | #include <boost/optional.hpp> |
| 14 | #include "common/param_package.h" | ||
| 11 | #include "core/settings.h" | 15 | #include "core/settings.h" |
| 12 | #include "ui_configure_input.h" | 16 | #include "ui_configure_input.h" |
| 13 | 17 | ||
| @@ -31,15 +35,25 @@ public: | |||
| 31 | private: | 35 | private: |
| 32 | std::unique_ptr<Ui::ConfigureInput> ui; | 36 | std::unique_ptr<Ui::ConfigureInput> ui; |
| 33 | 37 | ||
| 34 | /// This input is currently awaiting configuration. | ||
| 35 | /// (i.e.: its corresponding QPushButton has been pressed.) | ||
| 36 | boost::optional<Settings::NativeInput::Values> current_input_id; | ||
| 37 | std::unique_ptr<QTimer> timer; | 38 | std::unique_ptr<QTimer> timer; |
| 38 | 39 | ||
| 39 | /// Each input is represented by a QPushButton. | 40 | /// This will be the the setting function when an input is awaiting configuration. |
| 40 | std::map<Settings::NativeInput::Values, QPushButton*> button_map; | 41 | boost::optional<std::function<void(int)>> key_setter; |
| 41 | /// Each input is configured to respond to the press of a Qt::Key. | 42 | |
| 42 | std::map<Settings::NativeInput::Values, Qt::Key> key_map; | 43 | std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param; |
| 44 | std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param; | ||
| 45 | |||
| 46 | static constexpr int ANALOG_SUB_BUTTONS_NUM = 5; | ||
| 47 | |||
| 48 | /// Each button input is represented by a QPushButton. | ||
| 49 | std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map; | ||
| 50 | |||
| 51 | /// Each analog input is represented by five QPushButtons which represents up, down, left, right | ||
| 52 | /// and modifier | ||
| 53 | std::array<std::array<QPushButton*, ANALOG_SUB_BUTTONS_NUM>, Settings::NativeAnalog::NumAnalogs> | ||
| 54 | analog_map; | ||
| 55 | |||
| 56 | static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons; | ||
| 43 | 57 | ||
| 44 | /// Load configuration settings. | 58 | /// Load configuration settings. |
| 45 | void loadConfiguration(); | 59 | void loadConfiguration(); |
| @@ -48,10 +62,8 @@ private: | |||
| 48 | /// Update UI to reflect current configuration. | 62 | /// Update UI to reflect current configuration. |
| 49 | void updateButtonLabels(); | 63 | void updateButtonLabels(); |
| 50 | 64 | ||
| 51 | /// Called when the button corresponding to input_id was pressed. | 65 | /// Called when the button was pressed. |
| 52 | void handleClick(Settings::NativeInput::Values input_id); | 66 | void handleClick(QPushButton* button, std::function<void(int)> new_key_setter); |
| 53 | /// Handle key press events. | 67 | /// Handle key press events. |
| 54 | void keyPressEvent(QKeyEvent* event) override; | 68 | void keyPressEvent(QKeyEvent* event) override; |
| 55 | /// Configure input input_id to respond to key key_pressed. | ||
| 56 | void setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed); | ||
| 57 | }; | 69 | }; |