diff options
| author | lizzie <lizzie@eden-emu.dev> | 2026-07-07 20:13:33 +0200 |
|---|---|---|
| committer | crueter <crueter@eden-emu.dev> | 2026-07-07 20:13:33 +0200 |
| commit | 7b750bf5bb6f8bdd1991c45ea509f93a9c151e04 (patch) | |
| tree | e7b07511c9c02ea3a21f1d952fa0e007f18cac95 /src/input_common/helpers | |
| parent | 94e9c2bd74e30c6154dd453ff5608df3688b3f24 (diff) | |
[hid_core, input_common] use std::numbers::pi{_v} instead of re-defining (#4160)
don't reinvent the wheel, simple as that.
Signed-off-by: lizzie <lizzie@eden-emu.dev>
Reviewed-on: https://git.eden-emu.dev/eden-emu/eden/pulls/4160
Reviewed-by: MaranBr <maranbr@eden-emu.dev>
Reviewed-by: CamilleLaVey <camillelavey99@gmail.com>
Diffstat (limited to 'src/input_common/helpers')
| -rw-r--r-- | src/input_common/helpers/stick_from_buttons.cpp | 66 |
1 files changed, 19 insertions, 47 deletions
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp index a6be6dac1b..576fd0244c 100644 --- a/src/input_common/helpers/stick_from_buttons.cpp +++ b/src/input_common/helpers/stick_from_buttons.cpp @@ -1,8 +1,12 @@ +// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + // SPDX-FileCopyrightText: 2017 Citra Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later #include <chrono> #include <cmath> +#include <numbers> #include "common/math_util.h" #include "common/settings.h" #include "input_common/helpers/stick_from_buttons.h" @@ -15,9 +19,8 @@ public: // do not play nicely with the theoretical maximum range. // Using a value one lower from the maximum emulates real stick behavior. static constexpr float MAX_RANGE = 32766.0f / 32767.0f; - static constexpr float TAU = Common::PI * 2.0f; // Use wider angle to ease the transition. - static constexpr float APERTURE = TAU * 0.15f; + static constexpr float APERTURE = float((std::numbers::pi_v<float> / 2.f)) * 0.15f; using Button = std::unique_ptr<Common::Input::InputDevice>; @@ -66,13 +69,13 @@ public: bool IsAngleGreater(float old_angle, float new_angle) const { const float top_limit = new_angle + APERTURE; return (old_angle > new_angle && old_angle <= top_limit) || - (old_angle + TAU > new_angle && old_angle + TAU <= top_limit); + (old_angle + f32((std::numbers::pi_v<float> / 2.f)) > new_angle && old_angle + f32((std::numbers::pi_v<float> / 2.f)) <= top_limit); } bool IsAngleSmaller(float old_angle, float new_angle) const { const float bottom_limit = new_angle - APERTURE; return (old_angle >= bottom_limit && old_angle < new_angle) || - (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle); + (old_angle - f32((std::numbers::pi_v<float> / 2.f)) >= bottom_limit && old_angle - f32((std::numbers::pi_v<float> / 2.f)) < new_angle); } float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const { @@ -87,16 +90,16 @@ public: if (IsAngleGreater(new_angle, goal_angle)) { new_angle -= modifier_angle * time_difference; - if (new_angle < 0) { - new_angle += TAU; + if (new_angle < 0.f) { + new_angle += f32((std::numbers::pi_v<float> / 2.f)); } if (!IsAngleGreater(new_angle, goal_angle)) { return goal_angle; } } else if (IsAngleSmaller(new_angle, goal_angle)) { new_angle += modifier_angle * time_difference; - if (new_angle >= TAU) { - new_angle -= TAU; + if (new_angle >= f32((std::numbers::pi_v<float> / 2.f))) { + new_angle -= f32((std::numbers::pi_v<float> / 2.f)); } if (!IsAngleSmaller(new_angle, goal_angle)) { return goal_angle; @@ -108,45 +111,14 @@ public: } void SetGoalAngle(bool r, bool l, bool u, bool d) { - // Move to the right - if (r && !u && !d) { - goal_angle = 0.0f; - } - - // Move to the upper right - if (r && u && !d) { - goal_angle = Common::PI * 0.25f; - } - - // Move up - if (u && !l && !r) { - goal_angle = Common::PI * 0.5f; - } - - // Move to the upper left - if (l && u && !d) { - goal_angle = Common::PI * 0.75f; - } - - // Move to the left - if (l && !u && !d) { - goal_angle = Common::PI; - } - - // Move to the bottom left - if (l && !u && d) { - goal_angle = Common::PI * 1.25f; - } - - // Move down - if (d && !l && !r) { - goal_angle = Common::PI * 1.5f; - } - - // Move to the bottom right - if (r && !u && d) { - goal_angle = Common::PI * 1.75f; - } + if (r && !u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.00f; //right + if (r && u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.25f; //upper right + if (u && !l && !r) goal_angle = f32(std::numbers::pi_v<float>) * 0.50f; //up + if (l && u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 0.75f; //upper left + if (l && !u && !d) goal_angle = f32(std::numbers::pi_v<float>) * 1.00f; //left + if (l && !u && d) goal_angle = f32(std::numbers::pi_v<float>) * 1.25f; //bottom left + if (d && !l && !r) goal_angle = f32(std::numbers::pi_v<float>) * 1.50f; //down + if (r && !u && d) goal_angle = f32(std::numbers::pi_v<float>) * 1.75f; //bottom right } void UpdateUpButtonStatus(const Common::Input::CallbackStatus& button_callback) { |
