summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 675b477fa..20a658a26 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -66,14 +66,14 @@ public:
66 state.axes.insert_or_assign(axis, value); 66 state.axes.insert_or_assign(axis, value);
67 } 67 }
68 68
69 float GetAxis(int axis) const { 69 float GetAxis(int axis, float range) const {
70 std::lock_guard lock{mutex}; 70 std::lock_guard lock{mutex};
71 return state.axes.at(axis) / 32767.0f; 71 return state.axes.at(axis) / (32767.0f * range);
72 } 72 }
73 73
74 std::tuple<float, float> GetAnalog(int axis_x, int axis_y) const { 74 std::tuple<float, float> GetAnalog(int axis_x, int axis_y, float range) const {
75 float x = GetAxis(axis_x); 75 float x = GetAxis(axis_x, range);
76 float y = GetAxis(axis_y); 76 float y = GetAxis(axis_y, range);
77 y = -y; // 3DS uses an y-axis inverse from SDL 77 y = -y; // 3DS uses an y-axis inverse from SDL
78 78
79 // Make sure the coordinates are in the unit circle, 79 // Make sure the coordinates are in the unit circle,
@@ -313,7 +313,7 @@ public:
313 trigger_if_greater(trigger_if_greater_) {} 313 trigger_if_greater(trigger_if_greater_) {}
314 314
315 bool GetStatus() const override { 315 bool GetStatus() const override {
316 const float axis_value = joystick->GetAxis(axis); 316 const float axis_value = joystick->GetAxis(axis, 1.0f);
317 if (trigger_if_greater) { 317 if (trigger_if_greater) {
318 return axis_value > threshold; 318 return axis_value > threshold;
319 } 319 }
@@ -329,11 +329,13 @@ private:
329 329
330class SDLAnalog final : public Input::AnalogDevice { 330class SDLAnalog final : public Input::AnalogDevice {
331public: 331public:
332 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_) 332 SDLAnalog(std::shared_ptr<SDLJoystick> joystick_, int axis_x_, int axis_y_, float deadzone_,
333 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_) {} 333 float range_)
334 : joystick(std::move(joystick_)), axis_x(axis_x_), axis_y(axis_y_), deadzone(deadzone_),
335 range(range_) {}
334 336
335 std::tuple<float, float> GetStatus() const override { 337 std::tuple<float, float> GetStatus() const override {
336 const auto [x, y] = joystick->GetAnalog(axis_x, axis_y); 338 const auto [x, y] = joystick->GetAnalog(axis_x, axis_y, range);
337 const float r = std::sqrt((x * x) + (y * y)); 339 const float r = std::sqrt((x * x) + (y * y));
338 if (r > deadzone) { 340 if (r > deadzone) {
339 return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone), 341 return std::make_tuple(x / r * (r - deadzone) / (1 - deadzone),
@@ -363,6 +365,7 @@ private:
363 const int axis_x; 365 const int axis_x;
364 const int axis_y; 366 const int axis_y;
365 const float deadzone; 367 const float deadzone;
368 const float range;
366}; 369};
367 370
368/// A button device factory that creates button devices from SDL joystick 371/// A button device factory that creates button devices from SDL joystick
@@ -458,13 +461,13 @@ public:
458 const int axis_x = params.Get("axis_x", 0); 461 const int axis_x = params.Get("axis_x", 0);
459 const int axis_y = params.Get("axis_y", 1); 462 const int axis_y = params.Get("axis_y", 1);
460 const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f); 463 const float deadzone = std::clamp(params.Get("deadzone", 0.0f), 0.0f, .99f);
461 464 const float range = std::clamp(params.Get("range", 0.0f), 0.0f, .99f) + 0.50f;
462 auto joystick = state.GetSDLJoystickByGUID(guid, port); 465 auto joystick = state.GetSDLJoystickByGUID(guid, port);
463 466
464 // This is necessary so accessing GetAxis with axis_x and axis_y won't crash 467 // This is necessary so accessing GetAxis with axis_x and axis_y won't crash
465 joystick->SetAxis(axis_x, 0); 468 joystick->SetAxis(axis_x, 0);
466 joystick->SetAxis(axis_y, 0); 469 joystick->SetAxis(axis_y, 0);
467 return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone); 470 return std::make_unique<SDLAnalog>(joystick, axis_x, axis_y, deadzone, range);
468 } 471 }
469 472
470private: 473private: