|
| 1 | +#include "GamepadState.h" |
| 2 | +#include "LogInput.h" |
| 3 | + |
| 4 | +// For making axises in the interval [-1,1] |
| 5 | +#define GAMEPAD_AXIS_FACTOR 1 / 32768.0f |
| 6 | + |
| 7 | +GamepadState::GamepadState( SDL_GameController* controller ) |
| 8 | + : m_Controller( controller ) { } |
| 9 | + |
| 10 | +GamepadState::~GamepadState() { |
| 11 | + // SDL_GameControllerClose( m_controller ); // TODOJM: Is this needed? |
| 12 | + // delete m_controller; |
| 13 | +} |
| 14 | + |
| 15 | +void GamepadState::Update() { |
| 16 | + if ( m_TrackGamepadState ) { |
| 17 | + // Only update if controller is attached |
| 18 | + if ( SDL_GameControllerGetAttached( m_Controller ) == SDL_TRUE ) { |
| 19 | + // Reset mask to no buttons being down |
| 20 | + m_ButtonsMask = 0; |
| 21 | + // Loop through all SDL controller buttons |
| 22 | + for ( int i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i ) { |
| 23 | + // Set bit corresponding to the specific button |
| 24 | + m_ButtonsMask |= |
| 25 | + SDL_GameControllerGetButton( m_Controller, static_cast<SDL_GameControllerButton>( i ) ) << i; |
| 26 | + } |
| 27 | + |
| 28 | + // Axis Axis Make [-1,1] |
| 29 | + m_RightStickX = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_RIGHTX ) * GAMEPAD_AXIS_FACTOR; |
| 30 | + m_RightStickY = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_RIGHTY ) * GAMEPAD_AXIS_FACTOR; |
| 31 | + m_LeftStickX = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_LEFTX ) * GAMEPAD_AXIS_FACTOR; |
| 32 | + m_LeftStickY = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_LEFTY ) * GAMEPAD_AXIS_FACTOR; |
| 33 | + m_LeftTrigger = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_TRIGGERLEFT ) * GAMEPAD_AXIS_FACTOR; |
| 34 | + m_RightTrigger = SDL_GameControllerGetAxis( m_Controller, SDL_CONTROLLER_AXIS_TRIGGERRIGHT ) * GAMEPAD_AXIS_FACTOR; |
| 35 | + |
| 36 | + if ( m_Connected == false ) { |
| 37 | + LogInput( string( SDL_GameControllerName( m_Controller ) ) + " connected", "GamepadState", LogSeverity::INFO_MSG ); |
| 38 | + m_Connected = true; |
| 39 | + } |
| 40 | + } else { |
| 41 | + m_Connected = false; |
| 42 | + } |
| 43 | + } else { |
| 44 | + ZeroState(); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +string GamepadState::GetName() const { |
| 49 | + return string( SDL_GameControllerName( m_Controller ) ); |
| 50 | +} |
| 51 | + |
| 52 | +bool GamepadState::ButtonDown( SDL_GameControllerButton button ) const { |
| 53 | + // Check the bit corresponding to the specified button |
| 54 | + return ( m_ButtonsMask & ( 1 << button ) ) != 0; |
| 55 | +} |
| 56 | + |
| 57 | +bool GamepadState::ButtonUp( SDL_GameControllerButton button ) const { |
| 58 | + // Check the bit corresponding to the specified button. |
| 59 | + // It shouldn't be set. |
| 60 | + return !( m_ButtonsMask & ( 1 << button ) ); |
| 61 | +} |
| 62 | + |
| 63 | +void GamepadState::ActivateStateTracking() { |
| 64 | + m_TrackGamepadState = true; |
| 65 | +} |
| 66 | + |
| 67 | +void GamepadState::DeactivateStateTracking() { |
| 68 | + m_TrackGamepadState = false; |
| 69 | + ZeroState(); |
| 70 | +} |
| 71 | + |
| 72 | +float GamepadState::GetRightStickX() const { |
| 73 | + return m_RightStickX; |
| 74 | +} |
| 75 | + |
| 76 | +float GamepadState::GetRightStickY() const { |
| 77 | + return m_RightStickY; |
| 78 | +} |
| 79 | + |
| 80 | +float GamepadState::GetLeftStickX() const { |
| 81 | + return m_LeftStickX; |
| 82 | +} |
| 83 | + |
| 84 | +float GamepadState::GetLeftStickY() const { |
| 85 | + return m_LeftStickY; |
| 86 | +} |
| 87 | + |
| 88 | +float GamepadState::GetLeftTrigger() const { |
| 89 | + return m_LeftTrigger; |
| 90 | +} |
| 91 | + |
| 92 | +float GamepadState::GetRightTrigger() const { |
| 93 | + return m_RightTrigger; |
| 94 | +} |
| 95 | + |
| 96 | +void GamepadState::ZeroState() { |
| 97 | + m_RightStickX = 0.0f; |
| 98 | + m_RightStickY = 0.0f; |
| 99 | + m_LeftStickX = 0.0f; |
| 100 | + m_LeftStickY = 0.0f; |
| 101 | + m_RightTrigger = 0.0f; |
| 102 | + m_LeftTrigger = 0.0f; |
| 103 | + m_ButtonsMask = 0; |
| 104 | +} |
| 105 | + |
0 commit comments