From a5f402e1a90345bc8214c8f9ebe0e7b0b09df8a4 Mon Sep 17 00:00:00 2001 From: Kuukitenshi Date: Fri, 27 Sep 2024 19:05:33 +0100 Subject: [PATCH] feat(input): add deadzone to axis --- engine/include/cubos/engine/input/axis.hpp | 12 +++++++++- engine/src/input/axis.cpp | 27 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/engine/include/cubos/engine/input/axis.hpp b/engine/include/cubos/engine/input/axis.hpp index 11d60e47e..32d0297e7 100644 --- a/engine/include/cubos/engine/input/axis.hpp +++ b/engine/include/cubos/engine/input/axis.hpp @@ -35,10 +35,11 @@ namespace cubos::engine /// @param negative Negative input combinations. /// @param gamepadAxes Gamepad axis bindings. InputAxis(std::vector positive, std::vector negative, - std::vector gamepadAxes) + std::vector gamepadAxes, float deadzone = 0.0F) : mPositive(std::move(positive)) , mNegative(std::move(negative)) , mGamepadAxes(std::move(gamepadAxes)) + , mDeadzone(deadzone) { } @@ -74,11 +75,20 @@ namespace cubos::engine /// @param value New value. void value(float value); + /// @brief Gets the deadzone. + /// @return Deadzone. + float deadzone() const; + + /// @brief Sets the deadzone. + /// @param deadzone New deadzone. + void setDeadzone(float deadzone); + private: std::vector mPositive; std::vector mNegative; std::vector mGamepadAxes; float mValue{0.0F}; ///< Not serialized. + float mDeadzone{0.0F}; }; } // namespace cubos::engine diff --git a/engine/src/input/axis.cpp b/engine/src/input/axis.cpp index de32779c9..a4c2037f4 100644 --- a/engine/src/input/axis.cpp +++ b/engine/src/input/axis.cpp @@ -60,7 +60,14 @@ float InputAxis::value() const void InputAxis::value(float value) { - mValue = value; + if (std::abs(value) < mDeadzone) + { + mValue = 0.0F; + } + else + { + mValue = value; + } if (std::abs(mValue) > 1.0F) { @@ -68,3 +75,21 @@ void InputAxis::value(float value) mValue = mValue > 1.0F ? 1.0F : -1.0F; } } + +float InputAxis::deadzone() const +{ + return mDeadzone; +} + +void InputAxis::setDeadzone(float deadzone) +{ + if (deadzone < 0.0F || deadzone > 1.0F) + { + CUBOS_WARN("Invalid deadzone value: {}. Value must be between 0.0 and 1.0.", deadzone); + mDeadzone = deadzone > 1.0F ? 1.0F : 0.0F; + } + else + { + mDeadzone = deadzone; + } +}