Skip to content

Commit

Permalink
feat(input): add deadzone to axis
Browse files Browse the repository at this point in the history
  • Loading branch information
kuukitenshi committed Sep 27, 2024
1 parent 3a4258d commit a5f402e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
12 changes: 11 additions & 1 deletion engine/include/cubos/engine/input/axis.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ namespace cubos::engine
/// @param negative Negative input combinations.
/// @param gamepadAxes Gamepad axis bindings.
InputAxis(std::vector<InputCombination> positive, std::vector<InputCombination> negative,
std::vector<core::io::GamepadAxis> gamepadAxes)
std::vector<core::io::GamepadAxis> gamepadAxes, float deadzone = 0.0F)
: mPositive(std::move(positive))
, mNegative(std::move(negative))
, mGamepadAxes(std::move(gamepadAxes))
, mDeadzone(deadzone)
{
}

Expand Down Expand Up @@ -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<InputCombination> mPositive;
std::vector<InputCombination> mNegative;
std::vector<core::io::GamepadAxis> mGamepadAxes;

float mValue{0.0F}; ///< Not serialized.
float mDeadzone{0.0F};
};
} // namespace cubos::engine
27 changes: 26 additions & 1 deletion engine/src/input/axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,36 @@ float InputAxis::value() const

void InputAxis::value(float value)
{
mValue = value;
if (std::abs(value) < mDeadzone)

Check warning on line 63 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L63

Added line #L63 was not covered by tests
{
mValue = 0.0F;

Check warning on line 65 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L65

Added line #L65 was not covered by tests
}
else
{
mValue = value;

Check warning on line 69 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L69

Added line #L69 was not covered by tests
}

if (std::abs(mValue) > 1.0F)
{
CUBOS_WARN("Axis value out of range: {}", mValue);
mValue = mValue > 1.0F ? 1.0F : -1.0F;
}
}

float InputAxis::deadzone() const

Check warning on line 79 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L79

Added line #L79 was not covered by tests
{
return mDeadzone;

Check warning on line 81 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L81

Added line #L81 was not covered by tests
}

void InputAxis::setDeadzone(float deadzone)

Check warning on line 84 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L84

Added line #L84 was not covered by tests
{
if (deadzone < 0.0F || deadzone > 1.0F)

Check warning on line 86 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L86

Added line #L86 was not covered by tests
{
CUBOS_WARN("Invalid deadzone value: {}. Value must be between 0.0 and 1.0.", deadzone);
mDeadzone = deadzone > 1.0F ? 1.0F : 0.0F;

Check warning on line 89 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L88-L89

Added lines #L88 - L89 were not covered by tests
}
else
{
mDeadzone = deadzone;

Check warning on line 93 in engine/src/input/axis.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/axis.cpp#L93

Added line #L93 was not covered by tests
}
}

0 comments on commit a5f402e

Please sign in to comment.