diff options
Diffstat (limited to 'src/input_common/helpers')
| -rw-r--r-- | src/input_common/helpers/stick_from_buttons.cpp | 45 |
1 files changed, 13 insertions, 32 deletions
diff --git a/src/input_common/helpers/stick_from_buttons.cpp b/src/input_common/helpers/stick_from_buttons.cpp index 096c23b07..a6be6dac1 100644 --- a/src/input_common/helpers/stick_from_buttons.cpp +++ b/src/input_common/helpers/stick_from_buttons.cpp | |||
| @@ -15,6 +15,9 @@ public: | |||
| 15 | // do not play nicely with the theoretical maximum range. | 15 | // do not play nicely with the theoretical maximum range. |
| 16 | // Using a value one lower from the maximum emulates real stick behavior. | 16 | // Using a value one lower from the maximum emulates real stick behavior. |
| 17 | static constexpr float MAX_RANGE = 32766.0f / 32767.0f; | 17 | static constexpr float MAX_RANGE = 32766.0f / 32767.0f; |
| 18 | static constexpr float TAU = Common::PI * 2.0f; | ||
| 19 | // Use wider angle to ease the transition. | ||
| 20 | static constexpr float APERTURE = TAU * 0.15f; | ||
| 18 | 21 | ||
| 19 | using Button = std::unique_ptr<Common::Input::InputDevice>; | 22 | using Button = std::unique_ptr<Common::Input::InputDevice>; |
| 20 | 23 | ||
| @@ -61,30 +64,23 @@ public: | |||
| 61 | } | 64 | } |
| 62 | 65 | ||
| 63 | bool IsAngleGreater(float old_angle, float new_angle) const { | 66 | bool IsAngleGreater(float old_angle, float new_angle) const { |
| 64 | constexpr float TAU = Common::PI * 2.0f; | 67 | const float top_limit = new_angle + APERTURE; |
| 65 | // Use wider angle to ease the transition. | ||
| 66 | constexpr float aperture = TAU * 0.15f; | ||
| 67 | const float top_limit = new_angle + aperture; | ||
| 68 | return (old_angle > new_angle && old_angle <= top_limit) || | 68 | return (old_angle > new_angle && old_angle <= top_limit) || |
| 69 | (old_angle + TAU > new_angle && old_angle + TAU <= top_limit); | 69 | (old_angle + TAU > new_angle && old_angle + TAU <= top_limit); |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | bool IsAngleSmaller(float old_angle, float new_angle) const { | 72 | bool IsAngleSmaller(float old_angle, float new_angle) const { |
| 73 | constexpr float TAU = Common::PI * 2.0f; | 73 | const float bottom_limit = new_angle - APERTURE; |
| 74 | // Use wider angle to ease the transition. | ||
| 75 | constexpr float aperture = TAU * 0.15f; | ||
| 76 | const float bottom_limit = new_angle - aperture; | ||
| 77 | return (old_angle >= bottom_limit && old_angle < new_angle) || | 74 | return (old_angle >= bottom_limit && old_angle < new_angle) || |
| 78 | (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle); | 75 | (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle); |
| 79 | } | 76 | } |
| 80 | 77 | ||
| 81 | float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const { | 78 | float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const { |
| 82 | constexpr float TAU = Common::PI * 2.0f; | ||
| 83 | float new_angle = angle; | 79 | float new_angle = angle; |
| 84 | 80 | ||
| 85 | auto time_difference = static_cast<float>( | 81 | auto time_difference = static_cast<float>( |
| 86 | std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count()); | 82 | std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count()); |
| 87 | time_difference /= 1000.0f * 1000.0f; | 83 | time_difference /= 1000.0f; |
| 88 | if (time_difference > 0.5f) { | 84 | if (time_difference > 0.5f) { |
| 89 | time_difference = 0.5f; | 85 | time_difference = 0.5f; |
| 90 | } | 86 | } |
| @@ -201,8 +197,6 @@ public: | |||
| 201 | } | 197 | } |
| 202 | 198 | ||
| 203 | void UpdateStatus() { | 199 | void UpdateStatus() { |
| 204 | const float coef = modifier_status.value ? modifier_scale : MAX_RANGE; | ||
| 205 | |||
| 206 | bool r = right_status; | 200 | bool r = right_status; |
| 207 | bool l = left_status; | 201 | bool l = left_status; |
| 208 | bool u = up_status; | 202 | bool u = up_status; |
| @@ -220,7 +214,7 @@ public: | |||
| 220 | 214 | ||
| 221 | // Move if a key is pressed | 215 | // Move if a key is pressed |
| 222 | if (r || l || u || d) { | 216 | if (r || l || u || d) { |
| 223 | amplitude = coef; | 217 | amplitude = modifier_status.value ? modifier_scale : MAX_RANGE; |
| 224 | } else { | 218 | } else { |
| 225 | amplitude = 0; | 219 | amplitude = 0; |
| 226 | } | 220 | } |
| @@ -274,30 +268,17 @@ public: | |||
| 274 | Common::Input::StickStatus status{}; | 268 | Common::Input::StickStatus status{}; |
| 275 | status.x.properties = properties; | 269 | status.x.properties = properties; |
| 276 | status.y.properties = properties; | 270 | status.y.properties = properties; |
| 271 | |||
| 277 | if (Settings::values.emulate_analog_keyboard) { | 272 | if (Settings::values.emulate_analog_keyboard) { |
| 278 | const auto now = std::chrono::steady_clock::now(); | 273 | const auto now = std::chrono::steady_clock::now(); |
| 279 | float angle_ = GetAngle(now); | 274 | const float angle_ = GetAngle(now); |
| 280 | status.x.raw_value = std::cos(angle_) * amplitude; | 275 | status.x.raw_value = std::cos(angle_) * amplitude; |
| 281 | status.y.raw_value = std::sin(angle_) * amplitude; | 276 | status.y.raw_value = std::sin(angle_) * amplitude; |
| 282 | return status; | 277 | return status; |
| 283 | } | 278 | } |
| 284 | constexpr float SQRT_HALF = 0.707106781f; | 279 | |
| 285 | int x = 0, y = 0; | 280 | status.x.raw_value = std::cos(goal_angle) * amplitude; |
| 286 | if (right_status) { | 281 | status.y.raw_value = std::sin(goal_angle) * amplitude; |
| 287 | ++x; | ||
| 288 | } | ||
| 289 | if (left_status) { | ||
| 290 | --x; | ||
| 291 | } | ||
| 292 | if (up_status) { | ||
| 293 | ++y; | ||
| 294 | } | ||
| 295 | if (down_status) { | ||
| 296 | --y; | ||
| 297 | } | ||
| 298 | const float coef = modifier_status.value ? modifier_scale : MAX_RANGE; | ||
| 299 | status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF); | ||
| 300 | status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF); | ||
| 301 | return status; | 282 | return status; |
| 302 | } | 283 | } |
| 303 | 284 | ||