Add Dead Zone to virtual input action for better usage with gamepad sticks/triggers

#3660
This commit is contained in:
Wojtek Figat
2026-03-10 19:58:50 +01:00
parent 3bbaa7025a
commit afc4d0f3d3
5 changed files with 131 additions and 44 deletions

View File

@@ -2,6 +2,71 @@
#include "Gamepad.h"
namespace
{
GamepadAxis GetButtonAxis(GamepadButton button, bool& positive)
{
positive = true;
switch (button)
{
case GamepadButton::LeftTrigger:
return GamepadAxis::LeftTrigger;
case GamepadButton::RightTrigger:
return GamepadAxis::RightTrigger;
case GamepadButton::LeftStickUp:
return GamepadAxis::LeftStickY;
case GamepadButton::LeftStickDown:
positive = false;
return GamepadAxis::LeftStickY;
case GamepadButton::LeftStickLeft:
positive = false;
return GamepadAxis::LeftStickX;
case GamepadButton::LeftStickRight:
return GamepadAxis::LeftStickX;
case GamepadButton::RightStickUp:
return GamepadAxis::RightStickY;
case GamepadButton::RightStickDown:
positive = false;
return GamepadAxis::RightStickY;
case GamepadButton::RightStickLeft:
positive = false;
return GamepadAxis::RightStickX;
case GamepadButton::RightStickRight:
return GamepadAxis::RightStickX;
default:
return GamepadAxis::None;
}
}
bool GetButtonState(const Gamepad::State& state, GamepadButton button, float deadZone)
{
if (deadZone > 0.01f)
{
switch (button)
{
case GamepadButton::LeftTrigger:
case GamepadButton::RightTrigger:
case GamepadButton::LeftStickUp:
case GamepadButton::LeftStickDown:
case GamepadButton::LeftStickLeft:
case GamepadButton::LeftStickRight:
case GamepadButton::RightStickUp:
case GamepadButton::RightStickDown:
case GamepadButton::RightStickLeft:
case GamepadButton::RightStickRight:
{
bool positive;
float axis = state.Axis[(int32)GetButtonAxis(button, positive)];
return positive ? axis >= deadZone : axis <= -deadZone;
}
default:
break;
}
}
return state.Buttons[(int32)button];
}
}
void GamepadLayout::Init()
{
for (int32 i = 0; i < (int32)GamepadButton::MAX; i++)
@@ -31,6 +96,21 @@ void Gamepad::ResetState()
_mappedPrevState.Clear();
}
bool Gamepad::GetButton(GamepadButton button, float deadZone) const
{
return GetButtonState(_mappedState, button, deadZone);
}
bool Gamepad::GetButtonDown(GamepadButton button, float deadZone) const
{
return GetButtonState(_mappedState, button, deadZone) && !GetButtonState(_mappedPrevState, button, deadZone);
}
bool Gamepad::GetButtonUp(GamepadButton button, float deadZone) const
{
return !GetButtonState(_mappedState, button, deadZone) && GetButtonState(_mappedPrevState, button, deadZone);
}
bool Gamepad::IsAnyButtonDown() const
{
// TODO: optimize with SIMD