From ed51c2abdae8539e5d549bcbe0aa867f77cdf7e2 Mon Sep 17 00:00:00 2001 From: Ameer Date: Mon, 13 Jul 2020 11:58:40 -0400 Subject: [PATCH 1/5] Add range slider for analog sticks --- src/input_common/sdl/sdl_impl.cpp | 25 ++++---- .../configuration/configure_input_player.cpp | 23 ++++++- .../configuration/configure_input_player.h | 2 + .../configuration/configure_input_player.ui | 63 ++++++++++++++++++- 4 files changed, 99 insertions(+), 14 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 675b477fa3..20a658a266 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -66,14 +66,14 @@ public: state.axes.insert_or_assign(axis, value); } - float GetAxis(int axis) const { + float GetAxis(int axis, float range) const { std::lock_guard lock{mutex}; - return state.axes.at(axis) / 32767.0f; + return state.axes.at(axis) / (32767.0f * range); } - std::tuple GetAnalog(int axis_x, int axis_y) const { - float x = GetAxis(axis_x); - float y = GetAxis(axis_y); + std::tuple GetAnalog(int axis_x, int axis_y, float range) const { + float x = GetAxis(axis_x, range); + float y = GetAxis(axis_y, range); y = -y; // 3DS uses an y-axis inverse from SDL // Make sure the coordinates are in the unit circle, @@ -313,7 +313,7 @@ public: trigger_if_greater(trigger_if_greater_) {} bool GetStatus() const override { - const float axis_value = joystick->GetAxis(axis); + const float axis_value = joystick->GetAxis(axis, 1.0f); if (trigger_if_greater) { return axis_value > threshold; } @@ -329,11 +329,13 @@ private: class SDLAnalog final : public Input::AnalogDevice { public: - SDLAnalog(std::shared_ptr joystick_, int axis_x_, int axis_y_, float deadzone_) - : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} + SDLAnalog(std::shared_ptr joystick_, int axis_x_, int axis_y_, float deadzone_, + float range_) + : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), + range(range_) {} std::tuple GetStatus() const override { - const auto [x, y] = joystick->GetAnalog(axis_x, axis_y); + const auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range); const float r = std::sqrt((x * x) + (y * y)); if (r > deadzone) { return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), @@ -363,6 +365,7 @@ private: const int axis_x; const int axis_y; const float deadzone; + const float range; }; /// A button device factory that creates button devices from SDL joystick @@ -458,13 +461,13 @@ public: const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - + const float range = std::clamp(params.Get("range", 0.0f), 0.0f, .99f) + 0.50f; auto joystick = state.GetSDLJoystickByGUID(guid, port); // This is necessary so accessing GetAxis with axis_x and axis_y won't crash joystick->SetAxis(axis_x, 0); joystick->SetAxis(axis_y, 0); - return std::make_unique(joystick, axis_x, axis_y, deadzone); + return std::make_unique(joystick, axis_x, axis_y, deadzone, range); } private: diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index b1850bc95f..d60f13ffb4 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -272,6 +272,8 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i ui->sliderRStickDeadzoneAndModifier}; analog_map_deadzone_and_modifier_slider_label = {ui->labelLStickDeadzoneAndModifier, ui->labelRStickDeadzoneAndModifier}; + analog_map_range_slider = {ui->sliderLStickRange, ui->sliderRStickRange}; + analog_map_range_slider_label = {ui->labelLStickRange, ui->labelRStickRange}; for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) { auto* const button = button_map[button_id]; @@ -364,7 +366,6 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i InputCommon::Polling::DeviceType::Analog); } }); - connect(analog_map_deadzone_and_modifier_slider[analog_id], &QSlider::valueChanged, [=, this] { const float slider_value = @@ -380,6 +381,15 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i analogs_param[analog_id].Set("modifier_scale", slider_value / 100.0f); } }); + connect(analog_map_range_slider[analog_id], &QSlider::valueChanged, [=, this] { + const float slider_value = analog_map_range_slider[analog_id]->value(); + const auto engine = analogs_param[analog_id].Get("engine", ""); + if (engine == "sdl" || engine == "gcpad") { + analog_map_range_slider_label[analog_id]->setText( + tr("Range: %1%").arg(slider_value + 50.0f)); + analogs_param[analog_id].Set("range", slider_value / 100.0f); + } + }); } connect(ui->buttonClearAll, &QPushButton::clicked, [this] { ClearAll(); }); @@ -585,6 +595,9 @@ void ConfigureInputPlayer::UpdateButtonLabels() { auto* const analog_stick_slider_label = analog_map_deadzone_and_modifier_slider_label[analog_id]; + auto* const analog_stick_range = analog_map_range_slider[analog_id]; + auto* const analog_stick_range_label = analog_map_range_slider_label[analog_id]; + if (param.Has("engine")) { if (param.Get("engine", "") == "sdl" || param.Get("engine", "") == "gcpad") { if (!param.Has("deadzone")) { @@ -595,6 +608,14 @@ void ConfigureInputPlayer::UpdateButtonLabels() { if (analog_stick_slider->value() == 0) { analog_stick_slider_label->setText(tr("Deadzone: 0%")); } + if (!param.Has("range")) { + param.Set("range", 0.50f); + } + + analog_stick_range->setValue(static_cast(param.Get("range", 0.1f) * 100)); + if (analog_stick_range->value() == 0) { + analog_stick_range_label->setText(tr("Range: 0%")); + } } else { if (!param.Has("modifier_scale")) { param.Set("modifier_scale", 0.5f); diff --git a/src/yuzu/configuration/configure_input_player.h b/src/yuzu/configuration/configure_input_player.h index 95afa53752..88cc3b3b1d 100644 --- a/src/yuzu/configuration/configure_input_player.h +++ b/src/yuzu/configuration/configure_input_player.h @@ -101,6 +101,8 @@ private: analog_map_deadzone_and_modifier_slider; std::array analog_map_deadzone_and_modifier_slider_label; + std::array analog_map_range_slider; + std::array analog_map_range_slider_label; static const std::array analog_sub_buttons; diff --git a/src/yuzu/configuration/configure_input_player.ui b/src/yuzu/configuration/configure_input_player.ui index f27a77180a..c68c9228b5 100644 --- a/src/yuzu/configuration/configure_input_player.ui +++ b/src/yuzu/configuration/configure_input_player.ui @@ -195,7 +195,36 @@ - + + + + QLayout::SetDefaultConstraint + + + + + + + Range: 0 + + + Qt::AlignHCenter + + + + + + + + + Qt::Horizontal + + + + + + + Qt::Vertical @@ -811,7 +840,37 @@ - + + + + + QLayout::SetDefaultConstraint + + + + + + + Range: 0 + + + Qt::AlignHCenter + + + + + + + + + Qt::Horizontal + + + + + + + Qt::Vertical From 504095cea9bc5e8710dc7846c7706614fa959641 Mon Sep 17 00:00:00 2001 From: Ameer Date: Mon, 13 Jul 2020 13:41:30 -0400 Subject: [PATCH 2/5] Address c++20 warning, fix inaccurate range text display when slide == 0 --- src/yuzu/configuration/configure_input_player.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index d60f13ffb4..2ef952b670 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -611,10 +611,10 @@ void ConfigureInputPlayer::UpdateButtonLabels() { if (!param.Has("range")) { param.Set("range", 0.50f); } - + // QtSlider returns values from 0.0-0.99. We want range to be from 50% to 150% analog_stick_range->setValue(static_cast(param.Get("range", 0.1f) * 100)); if (analog_stick_range->value() == 0) { - analog_stick_range_label->setText(tr("Range: 0%")); + analog_stick_range_label->setText(tr("Range: 50%")); } } else { if (!param.Has("modifier_scale")) { From 8928aa30088c102986d247b8e823d2540d9a6462 Mon Sep 17 00:00:00 2001 From: Ameer Date: Wed, 15 Jul 2020 00:44:08 -0400 Subject: [PATCH 3/5] undo unnecessary newlines, slider range 50-150 --- src/input_common/sdl/sdl_impl.cpp | 2 +- src/yuzu/configuration/configure_input_player.cpp | 6 ++++-- src/yuzu/configuration/configure_input_player.ui | 3 --- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 20a658a266..1853f535d4 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -461,7 +461,7 @@ public: const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - const float range = std::clamp(params.Get("range", 0.0f), 0.0f, .99f) + 0.50f; + const float range = std::clamp(params.Get("range", 0.0f), 0.50f, 1.50f); auto joystick = state.GetSDLJoystickByGUID(guid, port); // This is necessary so accessing GetAxis with axis_x and axis_y won't crash diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index 2ef952b670..8bc86b706c 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -272,6 +272,8 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i ui->sliderRStickDeadzoneAndModifier}; analog_map_deadzone_and_modifier_slider_label = {ui->labelLStickDeadzoneAndModifier, ui->labelRStickDeadzoneAndModifier}; + ui->sliderLStickRange->setRange(50, 150); + ui->sliderRStickRange->setRange(50, 150); analog_map_range_slider = {ui->sliderLStickRange, ui->sliderRStickRange}; analog_map_range_slider_label = {ui->labelLStickRange, ui->labelRStickRange}; @@ -386,7 +388,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i const auto engine = analogs_param[analog_id].Get("engine", ""); if (engine == "sdl" || engine == "gcpad") { analog_map_range_slider_label[analog_id]->setText( - tr("Range: %1%").arg(slider_value + 50.0f)); + tr("Range: %1%").arg(slider_value)); analogs_param[analog_id].Set("range", slider_value / 100.0f); } }); @@ -613,7 +615,7 @@ void ConfigureInputPlayer::UpdateButtonLabels() { } // QtSlider returns values from 0.0-0.99. We want range to be from 50% to 150% analog_stick_range->setValue(static_cast(param.Get("range", 0.1f) * 100)); - if (analog_stick_range->value() == 0) { + if (analog_stick_range->value() == 50) { analog_stick_range_label->setText(tr("Range: 50%")); } } else { diff --git a/src/yuzu/configuration/configure_input_player.ui b/src/yuzu/configuration/configure_input_player.ui index c68c9228b5..91065dcdee 100644 --- a/src/yuzu/configuration/configure_input_player.ui +++ b/src/yuzu/configuration/configure_input_player.ui @@ -222,7 +222,6 @@ - @@ -840,7 +839,6 @@ - @@ -868,7 +866,6 @@ - From 9c6ae697f58af43a99ced5766f6b1020aa387e31 Mon Sep 17 00:00:00 2001 From: ameerj Date: Fri, 17 Jul 2020 11:56:15 -0400 Subject: [PATCH 4/5] Add range slider functionality for gc adapter --- src/input_common/gcadapter/gc_poller.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index f45983f3f6..323b934591 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -148,19 +148,17 @@ void GCButtonFactory::EndConfiguration() { class GCAnalog final : public Input::AnalogDevice { public: - GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter) + GCAnalog(int port_, int axis_x_, int axis_y_, float deadzone_, GCAdapter::Adapter* adapter, + float range_) : port(port_), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_), gcadapter(adapter), origin_value_x(adapter->GetOriginValue(port_, axis_x_)), - origin_value_y(adapter->GetOriginValue(port_, axis_y_)) {} + origin_value_y(adapter->GetOriginValue(port_, axis_y_)), range(range_) {} float GetAxis(int axis) const { if (gcadapter->DeviceConnected(port)) { std::lock_guard lock{mutex}; const auto origin_value = axis % 2 == 0 ? origin_value_x : origin_value_y; - // division is not by a perfect 128 to account for some variance in center location - // e.g. my device idled at 131 in X, 120 in Y, and full range of motion was in range - // [20-230] - return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / 95.0f; + return (gcadapter->GetPadState()[port].axes.at(axis) - origin_value) / (100.0f * range); } return 0.0f; } @@ -215,6 +213,7 @@ private: GCAdapter::Adapter* gcadapter; const float origin_value_x; const float origin_value_y; + const float range; mutable std::mutex mutex; }; @@ -234,8 +233,9 @@ std::unique_ptr GCAnalogFactory::Create(const Common::Param const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); + const float range = std::clamp(params.Get("range", 0.0f), 0.50f, 1.50f); - return std::make_unique(port, axis_x, axis_y, deadzone, adapter.get()); + return std::make_unique(port, axis_x, axis_y, deadzone, adapter.get(), range); } void GCAnalogFactory::BeginConfiguration() { From d6672501ac74b9d0645e97f045ec2d8b9e5da289 Mon Sep 17 00:00:00 2001 From: ameerj Date: Mon, 10 Aug 2020 12:39:42 -0400 Subject: [PATCH 5/5] Remove UI changes This PR is now only the Analog devices handling the range value defaulting at 100% --- src/input_common/gcadapter/gc_poller.cpp | 2 +- src/input_common/sdl/sdl_impl.cpp | 2 +- .../configuration/configure_input_player.cpp | 25 +------- .../configuration/configure_input_player.h | 2 - .../configuration/configure_input_player.ui | 60 +------------------ 5 files changed, 5 insertions(+), 86 deletions(-) diff --git a/src/input_common/gcadapter/gc_poller.cpp b/src/input_common/gcadapter/gc_poller.cpp index 323b934591..b346fdf8e9 100644 --- a/src/input_common/gcadapter/gc_poller.cpp +++ b/src/input_common/gcadapter/gc_poller.cpp @@ -233,7 +233,7 @@ std::unique_ptr GCAnalogFactory::Create(const Common::Param const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - const float range = std::clamp(params.Get("range", 0.0f), 0.50f, 1.50f); + const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); return std::make_unique(port, axis_x, axis_y, deadzone, adapter.get(), range); } diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 1853f535d4..d76c279d32 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp @@ -461,7 +461,7 @@ public: const int axis_x = params.Get("axis_x", 0); const int axis_y = params.Get("axis_y", 1); const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); - const float range = std::clamp(params.Get("range", 0.0f), 0.50f, 1.50f); + const float range = std::clamp(params.Get("range", 1.0f), 0.50f, 1.50f); auto joystick = state.GetSDLJoystickByGUID(guid, port); // This is necessary so accessing GetAxis with axis_x and axis_y won't crash diff --git a/src/yuzu/configuration/configure_input_player.cpp b/src/yuzu/configuration/configure_input_player.cpp index 8bc86b706c..b1850bc95f 100644 --- a/src/yuzu/configuration/configure_input_player.cpp +++ b/src/yuzu/configuration/configure_input_player.cpp @@ -272,10 +272,6 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i ui->sliderRStickDeadzoneAndModifier}; analog_map_deadzone_and_modifier_slider_label = {ui->labelLStickDeadzoneAndModifier, ui->labelRStickDeadzoneAndModifier}; - ui->sliderLStickRange->setRange(50, 150); - ui->sliderRStickRange->setRange(50, 150); - analog_map_range_slider = {ui->sliderLStickRange, ui->sliderRStickRange}; - analog_map_range_slider_label = {ui->labelLStickRange, ui->labelRStickRange}; for (int button_id = 0; button_id < Settings::NativeButton::NumButtons; button_id++) { auto* const button = button_map[button_id]; @@ -368,6 +364,7 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i InputCommon::Polling::DeviceType::Analog); } }); + connect(analog_map_deadzone_and_modifier_slider[analog_id], &QSlider::valueChanged, [=, this] { const float slider_value = @@ -383,15 +380,6 @@ ConfigureInputPlayer::ConfigureInputPlayer(QWidget* parent, std::size_t player_i analogs_param[analog_id].Set("modifier_scale", slider_value / 100.0f); } }); - connect(analog_map_range_slider[analog_id], &QSlider::valueChanged, [=, this] { - const float slider_value = analog_map_range_slider[analog_id]->value(); - const auto engine = analogs_param[analog_id].Get("engine", ""); - if (engine == "sdl" || engine == "gcpad") { - analog_map_range_slider_label[analog_id]->setText( - tr("Range: %1%").arg(slider_value)); - analogs_param[analog_id].Set("range", slider_value / 100.0f); - } - }); } connect(ui->buttonClearAll, &QPushButton::clicked, [this] { ClearAll(); }); @@ -597,9 +585,6 @@ void ConfigureInputPlayer::UpdateButtonLabels() { auto* const analog_stick_slider_label = analog_map_deadzone_and_modifier_slider_label[analog_id]; - auto* const analog_stick_range = analog_map_range_slider[analog_id]; - auto* const analog_stick_range_label = analog_map_range_slider_label[analog_id]; - if (param.Has("engine")) { if (param.Get("engine", "") == "sdl" || param.Get("engine", "") == "gcpad") { if (!param.Has("deadzone")) { @@ -610,14 +595,6 @@ void ConfigureInputPlayer::UpdateButtonLabels() { if (analog_stick_slider->value() == 0) { analog_stick_slider_label->setText(tr("Deadzone: 0%")); } - if (!param.Has("range")) { - param.Set("range", 0.50f); - } - // QtSlider returns values from 0.0-0.99. We want range to be from 50% to 150% - analog_stick_range->setValue(static_cast(param.Get("range", 0.1f) * 100)); - if (analog_stick_range->value() == 50) { - analog_stick_range_label->setText(tr("Range: 50%")); - } } else { if (!param.Has("modifier_scale")) { param.Set("modifier_scale", 0.5f); diff --git a/src/yuzu/configuration/configure_input_player.h b/src/yuzu/configuration/configure_input_player.h index 88cc3b3b1d..95afa53752 100644 --- a/src/yuzu/configuration/configure_input_player.h +++ b/src/yuzu/configuration/configure_input_player.h @@ -101,8 +101,6 @@ private: analog_map_deadzone_and_modifier_slider; std::array analog_map_deadzone_and_modifier_slider_label; - std::array analog_map_range_slider; - std::array analog_map_range_slider_label; static const std::array analog_sub_buttons; diff --git a/src/yuzu/configuration/configure_input_player.ui b/src/yuzu/configuration/configure_input_player.ui index 91065dcdee..f27a77180a 100644 --- a/src/yuzu/configuration/configure_input_player.ui +++ b/src/yuzu/configuration/configure_input_player.ui @@ -195,35 +195,7 @@ - - - - QLayout::SetDefaultConstraint - - - - - - - Range: 0 - - - Qt::AlignHCenter - - - - - - - - - Qt::Horizontal - - - - - - + Qt::Vertical @@ -839,35 +811,7 @@ - - - - QLayout::SetDefaultConstraint - - - - - - - Range: 0 - - - Qt::AlignHCenter - - - - - - - - - Qt::Horizontal - - - - - - + Qt::Vertical