2
1
Fork 0
mirror of https://github.com/yuzu-emu/yuzu.git synced 2024-07-04 23:31:19 +01:00

Qt: rework input configuration for new input system

This commit is contained in:
wwylele 2017-01-22 21:02:29 +02:00
parent 51b1c1f211
commit e7a602fe16
2 changed files with 144 additions and 68 deletions

View file

@ -2,13 +2,21 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <QTimer> #include <QTimer>
#include "citra_qt/config.h" #include "citra_qt/config.h"
#include "citra_qt/configure_input.h" #include "citra_qt/configure_input.h"
#include "common/param_package.h"
#include "input_common/main.h"
static QString getKeyName(Qt::Key key_code) { 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) { switch (key_code) {
case Qt::Key_Shift: case Qt::Key_Shift:
return QObject::tr("Shift"); return QObject::tr("Shift");
@ -23,6 +31,20 @@ static QString getKeyName(Qt::Key key_code) {
} }
} }
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) ConfigureInput::ConfigureInput(QWidget* parent)
: QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()), : QWidget(parent), ui(std::make_unique<Ui::ConfigureInput>()),
timer(std::make_unique<QTimer>()) { timer(std::make_unique<QTimer>()) {
@ -31,36 +53,38 @@ ConfigureInput::ConfigureInput(QWidget* parent)
setFocusPolicy(Qt::ClickFocus); setFocusPolicy(Qt::ClickFocus);
button_map = { button_map = {
{Settings::NativeInput::Values::A, ui->buttonA}, ui->buttonA, ui->buttonB, ui->buttonX, ui->buttonY, ui->buttonDpadUp,
{Settings::NativeInput::Values::B, ui->buttonB}, ui->buttonDpadDown, ui->buttonDpadLeft, ui->buttonDpadRight, ui->buttonL, ui->buttonR,
{Settings::NativeInput::Values::X, ui->buttonX}, ui->buttonStart, ui->buttonSelect, ui->buttonZL, ui->buttonZR, ui->buttonHome,
{Settings::NativeInput::Values::Y, ui->buttonY},
{Settings::NativeInput::Values::L, ui->buttonL},
{Settings::NativeInput::Values::R, ui->buttonR},
{Settings::NativeInput::Values::ZL, ui->buttonZL},
{Settings::NativeInput::Values::ZR, ui->buttonZR},
{Settings::NativeInput::Values::START, ui->buttonStart},
{Settings::NativeInput::Values::SELECT, ui->buttonSelect},
{Settings::NativeInput::Values::HOME, ui->buttonHome},
{Settings::NativeInput::Values::DUP, ui->buttonDpadUp},
{Settings::NativeInput::Values::DDOWN, ui->buttonDpadDown},
{Settings::NativeInput::Values::DLEFT, ui->buttonDpadLeft},
{Settings::NativeInput::Values::DRIGHT, ui->buttonDpadRight},
{Settings::NativeInput::Values::CUP, ui->buttonCStickUp},
{Settings::NativeInput::Values::CDOWN, ui->buttonCStickDown},
{Settings::NativeInput::Values::CLEFT, ui->buttonCStickLeft},
{Settings::NativeInput::Values::CRIGHT, ui->buttonCStickRight},
{Settings::NativeInput::Values::CIRCLE_UP, ui->buttonCircleUp},
{Settings::NativeInput::Values::CIRCLE_DOWN, ui->buttonCircleDown},
{Settings::NativeInput::Values::CIRCLE_LEFT, ui->buttonCircleLeft},
{Settings::NativeInput::Values::CIRCLE_RIGHT, ui->buttonCircleRight},
{Settings::NativeInput::Values::CIRCLE_MODIFIER, ui->buttonCircleMod},
}; };
for (const auto& entry : button_map) { analog_map = {{
const Settings::NativeInput::Values input_id = entry.first; {
connect(entry.second, &QPushButton::released, ui->buttonCircleUp, ui->buttonCircleDown, ui->buttonCircleLeft, ui->buttonCircleRight,
[this, input_id]() { handleClick(input_id); }); 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(); }); connect(ui->buttonRestoreDefaults, &QPushButton::released, [this]() { restoreDefaults(); });
@ -69,43 +93,93 @@ ConfigureInput::ConfigureInput(QWidget* parent)
connect(timer.get(), &QTimer::timeout, [this]() { connect(timer.get(), &QTimer::timeout, [this]() {
releaseKeyboard(); releaseKeyboard();
releaseMouse(); releaseMouse();
current_input_id = boost::none; key_setter = boost::none;
updateButtonLabels(); updateButtonLabels();
}); });
this->loadConfiguration(); 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() { void ConfigureInput::applyConfiguration() {
for (const auto& input_id : Settings::NativeInput::All) { std::transform(buttons_param.begin(), buttons_param.end(), Settings::values.buttons.begin(),
const size_t index = static_cast<size_t>(input_id); [](const Common::ParamPackage& param) { return param.Serialize(); });
Settings::values.input_mappings[index] = static_cast<int>(key_map[input_id]); std::transform(analogs_param.begin(), analogs_param.end(), Settings::values.analogs.begin(),
} [](const Common::ParamPackage& param) { return param.Serialize(); });
Settings::Apply(); Settings::Apply();
} }
void ConfigureInput::loadConfiguration() { void ConfigureInput::loadConfiguration() {
for (const auto& input_id : Settings::NativeInput::All) { std::transform(Settings::values.buttons.begin(), Settings::values.buttons.end(),
const size_t index = static_cast<size_t>(input_id); buttons_param.begin(),
key_map[input_id] = static_cast<Qt::Key>(Settings::values.input_mappings[index]); [](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(); updateButtonLabels();
} }
void ConfigureInput::restoreDefaults() {} 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() { void ConfigureInput::updateButtonLabels() {
for (const auto& input_id : Settings::NativeInput::All) { QString non_keyboard(tr("[non-keyboard]"));
button_map[input_id]->setText(getKeyName(key_map[input_id]));
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(Settings::NativeInput::Values input_id) { void ConfigureInput::handleClick(QPushButton* button, std::function<void(int)> new_key_setter) {
QPushButton* button = button_map[input_id];
button->setText(tr("[press key]")); button->setText(tr("[press key]"));
button->setFocus(); button->setFocus();
current_input_id = input_id; key_setter = new_key_setter;
grabKeyboard(); grabKeyboard();
grabMouse(); grabMouse();
@ -116,23 +190,13 @@ void ConfigureInput::keyPressEvent(QKeyEvent* event) {
releaseKeyboard(); releaseKeyboard();
releaseMouse(); releaseMouse();
if (!current_input_id || !event) if (!key_setter || !event)
return; return;
if (event->key() != Qt::Key_Escape) if (event->key() != Qt::Key_Escape)
setInput(*current_input_id, static_cast<Qt::Key>(event->key())); (*key_setter)(event->key());
updateButtonLabels(); updateButtonLabels();
current_input_id = boost::none; key_setter = boost::none;
timer->stop(); timer->stop();
} }
void ConfigureInput::setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed) {
// Remove duplicates
for (auto& pair : key_map) {
if (pair.second == key_pressed)
pair.second = Qt::Key_unknown;
}
key_map[input_id] = key_pressed;
}

View file

@ -4,10 +4,14 @@
#pragma once #pragma once
#include <array>
#include <functional>
#include <memory> #include <memory>
#include <string>
#include <QKeyEvent> #include <QKeyEvent>
#include <QWidget> #include <QWidget>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "common/param_package.h"
#include "core/settings.h" #include "core/settings.h"
#include "ui_configure_input.h" #include "ui_configure_input.h"
@ -31,15 +35,25 @@ public:
private: private:
std::unique_ptr<Ui::ConfigureInput> ui; std::unique_ptr<Ui::ConfigureInput> ui;
/// This input is currently awaiting configuration.
/// (i.e.: its corresponding QPushButton has been pressed.)
boost::optional<Settings::NativeInput::Values> current_input_id;
std::unique_ptr<QTimer> timer; std::unique_ptr<QTimer> timer;
/// Each input is represented by a QPushButton. /// This will be the the setting function when an input is awaiting configuration.
std::map<Settings::NativeInput::Values, QPushButton*> button_map; boost::optional<std::function<void(int)>> key_setter;
/// Each input is configured to respond to the press of a Qt::Key.
std::map<Settings::NativeInput::Values, Qt::Key> key_map; std::array<Common::ParamPackage, Settings::NativeButton::NumButtons> buttons_param;
std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs> analogs_param;
static constexpr int ANALOG_SUB_BUTTONS_NUM = 5;
/// Each button input is represented by a QPushButton.
std::array<QPushButton*, Settings::NativeButton::NumButtons> button_map;
/// Each analog input is represented by five QPushButtons which represents up, down, left, right
/// and modifier
std::array<std::array<QPushButton*, ANALOG_SUB_BUTTONS_NUM>, Settings::NativeAnalog::NumAnalogs>
analog_map;
static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons;
/// Load configuration settings. /// Load configuration settings.
void loadConfiguration(); void loadConfiguration();
@ -48,10 +62,8 @@ private:
/// Update UI to reflect current configuration. /// Update UI to reflect current configuration.
void updateButtonLabels(); void updateButtonLabels();
/// Called when the button corresponding to input_id was pressed. /// Called when the button was pressed.
void handleClick(Settings::NativeInput::Values input_id); void handleClick(QPushButton* button, std::function<void(int)> new_key_setter);
/// Handle key press events. /// Handle key press events.
void keyPressEvent(QKeyEvent* event) override; void keyPressEvent(QKeyEvent* event) override;
/// Configure input input_id to respond to key key_pressed.
void setInput(Settings::NativeInput::Values input_id, Qt::Key key_pressed);
}; };