input_common: Simplify stick from button

This commit is contained in:
Narr the Reg
2023-02-02 09:51:23 -06:00
committed by GPUCode
parent b714114ab0
commit 0382b275c0

View File

@ -53,30 +53,23 @@ public:
} }
bool IsAngleGreater(float old_angle, float new_angle) const { bool IsAngleGreater(float old_angle, float new_angle) const {
constexpr float TAU = Common::PI * 2.0f; const float top_limit = new_angle + APERTURE;
// Use wider angle to ease the transition.
constexpr float aperture = TAU * 0.15f;
const float top_limit = new_angle + aperture;
return (old_angle > new_angle && old_angle <= top_limit) || return (old_angle > new_angle && old_angle <= top_limit) ||
(old_angle + TAU > new_angle && old_angle + TAU <= top_limit); (old_angle + TAU > new_angle && old_angle + TAU <= top_limit);
} }
bool IsAngleSmaller(float old_angle, float new_angle) const { bool IsAngleSmaller(float old_angle, float new_angle) const {
constexpr float TAU = Common::PI * 2.0f; const float bottom_limit = new_angle - APERTURE;
// Use wider angle to ease the transition.
constexpr float aperture = TAU * 0.15f;
const float bottom_limit = new_angle - aperture;
return (old_angle >= bottom_limit && old_angle < new_angle) || return (old_angle >= bottom_limit && old_angle < new_angle) ||
(old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle); (old_angle - TAU >= bottom_limit && old_angle - TAU < new_angle);
} }
float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const { float GetAngle(std::chrono::time_point<std::chrono::steady_clock> now) const {
constexpr float TAU = Common::PI * 2.0f;
float new_angle = angle; float new_angle = angle;
auto time_difference = static_cast<float>( auto time_difference = static_cast<float>(
std::chrono::duration_cast<std::chrono::microseconds>(now - last_update).count()); std::chrono::duration_cast<std::chrono::milliseconds>(now - last_update).count());
time_difference /= 1000.0f * 1000.0f; time_difference /= 1000.0f;
if (time_difference > 0.5f) { if (time_difference > 0.5f) {
time_difference = 0.5f; time_difference = 0.5f;
} }
@ -193,8 +186,6 @@ public:
} }
void UpdateStatus() { void UpdateStatus() {
const float coef = modifier_status.value ? modifier_scale : 1.0f;
bool r = right_status; bool r = right_status;
bool l = left_status; bool l = left_status;
bool u = up_status; bool u = up_status;
@ -212,7 +203,7 @@ public:
// Move if a key is pressed // Move if a key is pressed
if (r || l || u || d) { if (r || l || u || d) {
amplitude = coef; amplitude = modifier_status.value ? modifier_scale : MAX_RANGE;
} else { } else {
amplitude = 0; amplitude = 0;
} }
@ -266,23 +257,17 @@ public:
Common::Input::StickStatus status{}; Common::Input::StickStatus status{};
status.x.properties = properties; status.x.properties = properties;
status.y.properties = properties; status.y.properties = properties;
constexpr float SQRT_HALF = 0.707106781f;
int x = 0, y = 0; if (Settings::values.emulate_analog_keyboard) {
if (right_status) { const auto now = std::chrono::steady_clock::now();
++x; const float angle_ = GetAngle(now);
status.x.raw_value = std::cos(angle_) * amplitude;
status.y.raw_value = std::sin(angle_) * amplitude;
return status;
} }
if (left_status) {
--x; status.x.raw_value = std::cos(goal_angle) * amplitude;
} status.y.raw_value = std::sin(goal_angle) * amplitude;
if (up_status) {
++y;
}
if (down_status) {
--y;
}
const float coef = modifier_status.value ? modifier_scale : 1.0f;
status.x.raw_value = static_cast<float>(x) * coef * (y == 0 ? 1.0f : SQRT_HALF);
status.y.raw_value = static_cast<float>(y) * coef * (x == 0 ? 1.0f : SQRT_HALF);
return status; return status;
} }