Skip to content

Commit ff0ad0b

Browse files
committed
Revamped almost all input with separation of context and state. A state is how the HID state is. A context accumulates and processes states and messages
1 parent ad7d0b7 commit ff0ad0b

20 files changed

+993
-607
lines changed

Alloc.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <vector>
55
#include <map>
6+
#include <unordered_map>
67
#include <string>
78
#include <sstream>
89

@@ -50,6 +51,10 @@ template<typename T, typename U> using pMap = std::map < T, U, std::less<T>, std
5051
template<typename T, typename U> using tMap = std::map < T, U, std::less<T>, std::allocator< std::pair< const T, U > > >;
5152
template<typename T, typename U> using fMap = std::map < T, U, std::less<T>, std::allocator< std::pair< const T, U > > >;
5253

54+
template<typename Key, typename Value, typename H = std::hash<Key>> using pUnorderedMap = std::unordered_map < Key, Value, H, std::equal_to<Key>, std::allocator< std::pair< const Key, Value > > >;
55+
template<typename Key, typename Value, typename H = std::hash<Key>> using tUnorderedMap = std::unordered_map < Key, Value, H, std::equal_to<Key>, std::allocator< std::pair< const Key, Value > > >;
56+
template<typename Key, typename Value, typename H = std::hash<Key>> using fUnorderedMap = std::unordered_map < Key, Value, H, std::equal_to<Key>, std::allocator< std::pair< const Key, Value > > >;
57+
5358
#define rAlloc( Type, count ) (Type*) malloc( sizeof(Type) * (count) )
5459
#define rNew( Type, ... ) new Type(__VA_ARGS__)
5560
#define rNewArray( Type, count ) new Type[(count)]
@@ -82,4 +87,4 @@ template<typename T, typename U> using fMap = std::map < T, U, std::less<T>, std
8287
#define fDelete( ptr ) rDelete( (ptr) )
8388
#define fDeleteArray( ptr ) rDeleteArray( (ptr) )
8489

85-
#endif
90+
#endif

CMakeLists.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
file(GLOB_RECURSE InputSources
2-
"Input.h"
3-
"Input.cpp"
4-
"Gamepad.cpp"
5-
"Gamepad.h"
2+
"InputState.h"
3+
"InputState.cpp"
4+
"InputContext.h"
5+
"InputContext.cpp"
6+
"GamepadState.cpp"
7+
"GamepadState.h"
8+
"GamepadContext.h"
9+
"GamepadContext.cpp"
610
"TextInput.h"
711
"TextInput.cpp"
812
"KeyBindings.h"

Gamepad.cpp

-98
This file was deleted.

Gamepad.h

-47
This file was deleted.

GamepadContext.cpp

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include "GamepadContext.h"
2+
#include "InputState.h"
3+
#include "GamepadState.h"
4+
5+
GamepadContext::GamepadContext( ) { }
6+
7+
void GamepadContext::Initialize( int gamepadIndex ) {
8+
m_GamepadIndex = gamepadIndex;
9+
}
10+
11+
void GamepadContext::Deinitialize( ) {
12+
m_GamepadIndex = INVALID_GAMEPAD_INDEX;
13+
}
14+
15+
void GamepadContext::Update() {
16+
m_PressStack.clear();
17+
m_ReleaseStack.clear();
18+
}
19+
20+
bool GamepadContext::HandleEvent( const SDL_Event& event ) {
21+
switch ( event.type ) {
22+
case SDL_CONTROLLERBUTTONDOWN: {
23+
if ( event.cbutton.which == m_GamepadIndex ) {
24+
m_PressStack.push_back( event.cbutton.button );
25+
}
26+
} break;
27+
case SDL_CONTROLLERBUTTONUP: {
28+
if ( event.cbutton.which == m_GamepadIndex ) {
29+
m_ReleaseStack.push_back( event.cbutton.button );
30+
}
31+
} break;
32+
}
33+
return false;
34+
}
35+
36+
bool GamepadContext::ButtonUpDown( SDL_GameControllerButton button ) const {
37+
for ( auto& pressed : m_PressStack ) {
38+
if ( pressed == button ) {
39+
return true;
40+
}
41+
}
42+
return false;
43+
}
44+
45+
bool GamepadContext::ButtonDownUp( SDL_GameControllerButton button ) const {
46+
for ( auto& pressed : m_ReleaseStack ) {
47+
if ( pressed == button ) {
48+
return true;
49+
}
50+
}
51+
return false;
52+
}
53+
54+
bool GamepadContext::ButtonDown( SDL_GameControllerButton button ) const {
55+
const GamepadState* state = g_InputState.GetGamepadState( m_GamepadIndex );
56+
57+
if ( state ) {
58+
return state->ButtonDown( button );
59+
} else {
60+
return false;
61+
}
62+
}
63+
64+
bool GamepadContext::ButtonUp( SDL_GameControllerButton button ) const {
65+
const GamepadState* state = g_InputState.GetGamepadState( m_GamepadIndex );
66+
67+
if ( state ) {
68+
return state->ButtonUp( button );
69+
} else {
70+
return false;
71+
}
72+
}
73+

GamepadContext.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include <SDL2/SDL_gamecontroller.h>
3+
#include <SDL2/SDL_events.h>
4+
#include INPUT_ALLOCATION_HEADER
5+
#include "InputLibraryDefine.h"
6+
7+
class GamepadContext {
8+
public:
9+
INPUT_API GamepadContext( );
10+
11+
INPUT_API void Initialize( int gamepadIndex );
12+
INPUT_API void Deinitialize( );
13+
INPUT_API void Update( );
14+
15+
INPUT_API bool HandleEvent ( const SDL_Event& event );
16+
17+
INPUT_API bool ButtonUpDown ( SDL_GameControllerButton button ) const;
18+
INPUT_API bool ButtonDownUp ( SDL_GameControllerButton button ) const;
19+
INPUT_API bool ButtonDown ( SDL_GameControllerButton button ) const;
20+
INPUT_API bool ButtonUp ( SDL_GameControllerButton button ) const;
21+
22+
private:
23+
const int INVALID_GAMEPAD_INDEX = -1;
24+
25+
pVector<Uint8> m_PressStack;
26+
pVector<Uint8> m_ReleaseStack;
27+
int m_GamepadIndex = INVALID_GAMEPAD_INDEX;
28+
};
29+

GamepadState.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)