Add range slider for analog sticks

merge-requests/60/head
Ameer 2020-07-13 11:58:40 +07:00 committed by ameerj
parent acfd771e79
commit ed51c2abda
4 changed files with 99 additions and 14 deletions

@ -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<float, float> GetAnalog(int axis_x, int axis_y) const {
float x = GetAxis(axis_x);
float y = GetAxis(axis_y);
std::tuple<float, float> 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<SDLJoystick> 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<SDLJoystick> 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<float, float> 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<SDLAnalog>(joystick, axis_x, axis_y, deadzone);
return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone, range);
}
private:

@ -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<int>(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);

@ -101,6 +101,8 @@ private:
analog_map_deadzone_and_modifier_slider;
std::array<QLabel*, Settings::NativeAnalog::NumAnalogs>
analog_map_deadzone_and_modifier_slider_label;
std::array<QSlider*, Settings::NativeAnalog::NumAnalogs> analog_map_range_slider;
std::array<QLabel*, Settings::NativeAnalog::NumAnalogs> analog_map_range_slider_label;
static const std::array<std::string, ANALOG_SUB_BUTTONS_NUM> analog_sub_buttons;

@ -195,7 +195,36 @@
</item>
</layout>
</item>
<item row="5" column="0">
<item row="5" column="0" colspan="2">
<layout class="QVBoxLayout" name="sliderRStickRangeVerticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<layout class="QHBoxLayout" name="sliderRStickRangeHorizontalLayout">
<item>
<widget class="QLabel" name="labelRStickRange">
<property name="text">
<string>Range: 0</string>
</property>
<property name="alignment">
<enum>Qt::AlignHCenter</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSlider" name="sliderRStickRange">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="6" column="0">
<spacer name="RStick_verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
@ -811,7 +840,37 @@
</item>
</layout>
</item>
<item row="6" column="1">
<item row="6" column="1" colspan="2">
<layout class="QVBoxLayout" name="sliderLStickRangeVerticalLayout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<item>
<layout class="QHBoxLayout" name="sliderLStickRangeHorizontalLayout">
<item>
<widget class="QLabel" name="labelLStickRange">
<property name="text">
<string>Range: 0</string>
</property>
<property name="alignment">
<enum>Qt::AlignHCenter</enum>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QSlider" name="sliderLStickRange">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="7" column="1">
<spacer name="LStick_verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>