Skip to content

Commit 6518cd7

Browse files
committed
Made functions that can consume mouse and keyboard keys/buttons
1 parent 8dedf24 commit 6518cd7

File tree

5 files changed

+207
-23
lines changed

5 files changed

+207
-23
lines changed

InputContext.cpp

+132-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "InputContext.h"
22
#include "InputState.h"
3+
#include <iostream>
34

45
InputContext& InputContext::GetInstance() {
56
static InputContext inputContext;
@@ -45,6 +46,38 @@ bool InputContext::KeyUpDown( SDL_Scancode scanCode ) const {
4546
return false;
4647
}
4748

49+
bool InputContext::KeyUpDownConsume( SDL_Scancode scanCode, INPUT_STATE state ) {
50+
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardPressStack.begin(); it != m_KeyboardPressStack.end(); ) {
51+
if ( *it == scanCode ) {
52+
it = m_KeyboardPressStack.erase( it );
53+
if ( state != INPUT_STATE_IGNORE ) {
54+
g_InputState.SetKeyState( scanCode, state );
55+
}
56+
return true;
57+
} else {
58+
++it;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
int InputContext::KeyUpDownConsumeAll( SDL_Scancode scanCode, INPUT_STATE state ) {
65+
int nrOfKeysConsumed = 0;
66+
67+
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardPressStack.begin(); it != m_KeyboardPressStack.end(); ) {
68+
if ( *it == scanCode ) {
69+
it = m_KeyboardPressStack.erase( it );
70+
++nrOfKeysConsumed;
71+
} else {
72+
++it;
73+
}
74+
}
75+
if ( ( nrOfKeysConsumed > 0 ) && ( state != INPUT_STATE_IGNORE ) ) {
76+
g_InputState.SetKeyState( scanCode, state );
77+
}
78+
return nrOfKeysConsumed;
79+
}
80+
4881
bool InputContext::KeyDownUp( SDL_Scancode scanCode ) const {
4982
for ( auto& released : m_KeyboardReleaseStack ) {
5083
if ( released == scanCode ) {
@@ -54,6 +87,38 @@ bool InputContext::KeyDownUp( SDL_Scancode scanCode ) const {
5487
return false;
5588
}
5689

90+
bool InputContext::KeyDownUpConsume( SDL_Scancode scanCode, INPUT_STATE state ) {
91+
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardReleaseStack.begin(); it != m_KeyboardReleaseStack.end(); ) {
92+
if ( *it == scanCode ) {
93+
it = m_KeyboardReleaseStack.erase( it );
94+
if ( state != INPUT_STATE_IGNORE ) {
95+
g_InputState.SetKeyState( scanCode, state );
96+
}
97+
return true;
98+
} else {
99+
++it;
100+
}
101+
}
102+
return false;
103+
}
104+
105+
int InputContext::KeyDownUpConsumeAll( SDL_Scancode scanCode, INPUT_STATE state ) {
106+
int nrOfKeysConsumed = 0;
107+
108+
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardReleaseStack.begin(); it != m_KeyboardReleaseStack.end(); ) {
109+
if ( *it == scanCode ) {
110+
it = m_KeyboardReleaseStack.erase( it );
111+
++nrOfKeysConsumed;
112+
} else {
113+
++it;
114+
}
115+
}
116+
if ( ( nrOfKeysConsumed > 0 ) && ( state != INPUT_STATE_IGNORE ) ) {
117+
g_InputState.SetKeyState( scanCode, state );
118+
}
119+
return nrOfKeysConsumed;
120+
}
121+
57122
bool InputContext::KeyDown( SDL_Scancode scanCode ) const {
58123
return g_InputState.IsKeyDown( scanCode );
59124
}
@@ -87,15 +152,31 @@ bool InputContext::MouseButtonUpDown( MOUSE_BUTTON button ) const {
87152
return false;
88153
}
89154

155+
bool InputContext::MouseButtonUpDownConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
156+
return ConsumeMouseButton( m_MouseSingleClickPressStack, button, state );
157+
}
158+
159+
int InputContext::MouseButtonUpDownConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
160+
return ConsumeMouseButtonAll( m_MouseSingleClickPressStack, button, state );
161+
}
162+
90163
bool InputContext::MouseButtonDownUp( MOUSE_BUTTON button ) const {
91-
for ( auto& pressed : m_MouseDoubleClickReleaseStack ) {
164+
for ( auto& pressed : m_MouseSingleClickReleaseStack ) {
92165
if ( pressed == button ) {
93166
return true;
94167
}
95168
}
96169
return false;
97170
}
98171

172+
bool InputContext::MouseButtonDownUpConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
173+
return ConsumeMouseButton( m_MouseSingleClickReleaseStack, button, state );
174+
}
175+
176+
int InputContext::MouseButtonDownUpConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
177+
return ConsumeMouseButtonAll( m_MouseSingleClickReleaseStack, button, state );
178+
}
179+
99180
bool InputContext::MouseButtonDoubleUpDown( MOUSE_BUTTON button ) const {
100181
for ( auto& pressed : m_MouseDoubleClickPressStack ) {
101182
if ( pressed == button ) {
@@ -105,6 +186,14 @@ bool InputContext::MouseButtonDoubleUpDown( MOUSE_BUTTON button ) const {
105186
return false;
106187
}
107188

189+
bool InputContext::MouseButtonDoubleUpDownConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
190+
return ConsumeMouseButton( m_MouseDoubleClickPressStack, button, state );
191+
}
192+
193+
int InputContext::MouseButtonDoubleUpDownConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
194+
return ConsumeMouseButtonAll( m_MouseDoubleClickPressStack, button, state );
195+
}
196+
108197
bool InputContext::MouseButtonDoubleDownUp( MOUSE_BUTTON button ) const {
109198
for ( auto& pressed : m_MouseDoubleClickReleaseStack ) {
110199
if ( pressed == button ) {
@@ -114,6 +203,14 @@ bool InputContext::MouseButtonDoubleDownUp( MOUSE_BUTTON button ) const {
114203
return false;
115204
}
116205

206+
bool InputContext::MouseButtonDoubleDownUpConsume( MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
207+
return ConsumeMouseButton( m_MouseDoubleClickReleaseStack, button, stateToSet );
208+
}
209+
210+
int InputContext::MouseButtonDoubleDownUpConsumeAll( MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
211+
return ConsumeMouseButtonAll( m_MouseDoubleClickReleaseStack, button, stateToSet );
212+
}
213+
117214
int InputContext::GetMousePosX() const {
118215
return g_InputState.GetMouseState().PositionX;
119216
}
@@ -122,11 +219,11 @@ int InputContext::GetMousePosY() const {
122219
return g_InputState.GetMouseState().PositionY;
123220
}
124221

125-
int InputContext::GetMousePosDeltaX( ) const {
222+
int InputContext::GetMousePosDeltaX() const {
126223
return m_MousePosDeltaX;
127224
}
128225

129-
int InputContext::GetMousePosDeltaY( ) const {
226+
int InputContext::GetMousePosDeltaY() const {
130227
return m_MousePosDeltaY;
131228
}
132229

@@ -200,3 +297,35 @@ bool InputContext::HandleEvent( const SDL_Event& event ) {
200297
return false;
201298
}
202299

300+
bool InputContext::ConsumeMouseButton( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
301+
for ( pVector<MOUSE_BUTTON>::iterator it = stack.begin(); it != stack.end(); ) {
302+
if ( *it == button ) {
303+
it = stack.erase( it );
304+
if ( stateToSet != INPUT_STATE_IGNORE ) {
305+
g_InputState.SetMouseButtonState( button, stateToSet );
306+
}
307+
return true;
308+
} else {
309+
++it;
310+
}
311+
}
312+
return false;
313+
}
314+
315+
int InputContext::ConsumeMouseButtonAll( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
316+
int nrOfConsumedButtons = 0;
317+
318+
for ( pVector<MOUSE_BUTTON>::iterator it = stack.begin(); it != stack.end(); ) {
319+
if ( *it == button ) {
320+
it = stack.erase( it );
321+
++nrOfConsumedButtons;
322+
} else {
323+
++it;
324+
}
325+
}
326+
if ( stateToSet != INPUT_STATE_IGNORE ) {
327+
g_InputState.SetMouseButtonState( button, stateToSet );
328+
}
329+
return nrOfConsumedButtons;
330+
}
331+

InputContext.h

+43-17
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,49 @@ class InputContext {
1717
INPUT_API void Update ();
1818

1919
INPUT_API bool KeyUpDown ( SDL_Scancode scanCode ) const;
20+
// Checks if key was pressed. Consumes one entry for the key from the press stack if it was pressed.
21+
INPUT_API bool KeyUpDownConsume ( SDL_Scancode scanCode, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
22+
// Checks if key was pressed. Consumes all entries for the key from the press stack if it was pressed.
23+
INPUT_API int KeyUpDownConsumeAll ( SDL_Scancode scanCode, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
2024
INPUT_API bool KeyDownUp ( SDL_Scancode scanCode ) const;
25+
// Checks if key was released. Consumes one entry for the key from the release stack if it was released.
26+
INPUT_API bool KeyDownUpConsume ( SDL_Scancode scanCode, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
27+
// Checks if key was released. Consumes all entries for the key from the release stack if it was released.
28+
INPUT_API int KeyDownUpConsumeAll ( SDL_Scancode scanCode, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
2129
INPUT_API bool KeyDown ( SDL_Scancode scanCode ) const;
2230
INPUT_API bool KeyUp ( SDL_Scancode scanCode ) const;
2331

2432
INPUT_API const pVector<SDL_Scancode>& GetKeyboardPressStack () const;
2533
INPUT_API const pVector<SDL_Scancode>& GetKeyboardReleaseStack () const;
2634

27-
INPUT_API bool MouseButtonDown ( MOUSE_BUTTON button ) const;
28-
INPUT_API bool MouseButtonUp ( MOUSE_BUTTON button ) const;
29-
INPUT_API bool MouseButtonUpDown ( MOUSE_BUTTON button ) const;
30-
INPUT_API bool MouseButtonDownUp ( MOUSE_BUTTON button ) const;
31-
INPUT_API bool MouseButtonDoubleUpDown ( MOUSE_BUTTON button ) const;
32-
INPUT_API bool MouseButtonDoubleDownUp ( MOUSE_BUTTON button ) const;
33-
INPUT_API int GetMousePosX( ) const;
34-
INPUT_API int GetMousePosY( ) const;
35-
INPUT_API int GetMousePosDeltaX( ) const;
36-
INPUT_API int GetMousePosDeltaY( ) const;
37-
INPUT_API int GetMouseScrollDeltaX( ) const;
38-
INPUT_API int GetMouseScrollDeltaY( ) const;
35+
INPUT_API bool MouseButtonDown ( MOUSE_BUTTON button ) const;
36+
INPUT_API bool MouseButtonUp ( MOUSE_BUTTON button ) const;
37+
INPUT_API bool MouseButtonUpDown ( MOUSE_BUTTON button ) const;
38+
// Checks if button was pressed. Consumes one entry for the button from the press stack if it was pressed.
39+
INPUT_API bool MouseButtonUpDownConsume ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
40+
// Checks if button was pressed. Consumes all entries for the button from the press stack if it was pressed.
41+
INPUT_API int MouseButtonUpDownConsumeAll ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
42+
INPUT_API bool MouseButtonDownUp ( MOUSE_BUTTON button ) const;
43+
// Checks if button was released. Consumes one entry for the button from the release stack if it was released.
44+
INPUT_API bool MouseButtonDownUpConsume ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
45+
// Checks if button was released. Consumes all entries for the button from the release stack if it was released.
46+
INPUT_API int MouseButtonDownUpConsumeAll ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
47+
INPUT_API bool MouseButtonDoubleUpDown ( MOUSE_BUTTON button ) const;
48+
// Checks if button was double pressed. Consumes one entry for the button from the release stack if it was released.
49+
INPUT_API bool MouseButtonDoubleUpDownConsume ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
50+
// Checks if button was double pressed. Consumes all entries for the button from the release stack if it was released.
51+
INPUT_API int MouseButtonDoubleUpDownConsumeAll ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
52+
INPUT_API bool MouseButtonDoubleDownUp ( MOUSE_BUTTON button ) const;
53+
// Checks if button was double released. Consumes one entry for the button from the release stack if it was released.
54+
INPUT_API bool MouseButtonDoubleDownUpConsume ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
55+
// Checks if button was double released. Consumes all entries for the button from the release stack if it was released.
56+
INPUT_API int MouseButtonDoubleDownUpConsumeAll ( MOUSE_BUTTON button, INPUT_STATE stateToSet = INPUT_STATE_IGNORE );
57+
INPUT_API int GetMousePosX () const;
58+
INPUT_API int GetMousePosY () const;
59+
INPUT_API int GetMousePosDeltaX () const;
60+
INPUT_API int GetMousePosDeltaY () const;
61+
INPUT_API int GetMouseScrollDeltaX () const;
62+
INPUT_API int GetMouseScrollDeltaY () const;
3963

4064
INPUT_API const pVector<MOUSE_BUTTON>& GetMouseSingleClickPressStack () const;
4165
INPUT_API const pVector<MOUSE_BUTTON>& GetMouseSingleClickReleaseStack () const;
@@ -46,6 +70,8 @@ class InputContext {
4670

4771
private:
4872
bool HandleEvent ( const SDL_Event& event );
73+
bool ConsumeMouseButton ( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet );
74+
int ConsumeMouseButtonAll ( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet );
4975

5076
InputEventCallbackHandle m_InputEventCallbackHandle;
5177

@@ -55,15 +81,15 @@ class InputContext {
5581
pVector<MOUSE_BUTTON> m_MouseSingleClickPressStack;
5682
pVector<MOUSE_BUTTON> m_MouseSingleClickReleaseStack;
5783
pVector<MOUSE_BUTTON> m_MouseDoubleClickPressStack;
58-
pVector<MOUSE_BUTTON> m_MouseDoubleClickReleaseStack; // Does this even make sense?
84+
pVector<MOUSE_BUTTON> m_MouseDoubleClickReleaseStack; // Does this even make sense?
5985

6086
int m_MousePosDeltaX = 0;
6187
int m_MousePosDeltaY = 0;
62-
int m_MousePosLastX = 0;
63-
int m_MousePosLastY = 0;
88+
int m_MousePosLastX = 0;
89+
int m_MousePosLastY = 0;
6490

65-
int m_MouseScrollDeltaX = 0;
66-
int m_MouseScrollDeltaY = 0;
91+
int m_MouseScrollDeltaX = 0;
92+
int m_MouseScrollDeltaY = 0;
6793
int m_MouseScrollLastPosX = 0;
6894
int m_MouseScrollLastPosY = 0;
6995

InputState.cpp

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "InputState.h"
22
#include <iostream>
33
#include <cassert>
4+
#include <cstring>
45
#include <SDL2/SDL.h>
56
#include "GamepadState.h"
67
#include "LogInput.h"
@@ -30,13 +31,22 @@ void InputState::Deinitialize() {
3031
}
3132
LogInput( "Input state was destructed while still having callbacks registered to it. Callback values: " + ss.str(), "InputState", LogSeverity::WARNING_MSG );
3233
}
34+
if ( m_KeyboardState ) {
35+
pDelete( m_KeyboardState );
36+
m_KeyboardState = nullptr;
37+
}
3338
}
3439

3540
void InputState::Update() {
3641
SDL_PumpEvents();
3742

3843
if ( m_KeyboardStateTracking ) {
39-
m_KeyboardState = SDL_GetKeyboardState( nullptr );
44+
int size;
45+
const Uint8* keyboardState = SDL_GetKeyboardState( &size );
46+
if ( m_KeyboardState == nullptr ) {
47+
m_KeyboardState = pNewArray( Uint8, size );
48+
}
49+
memcpy( m_KeyboardState, keyboardState, size );
4050
}
4151

4252
m_MouseState.ButtonState = SDL_GetMouseState( &m_MouseState.PositionX, &m_MouseState.PositionY );
@@ -170,6 +180,11 @@ bool InputState::IsMouseButtonUp( MOUSE_BUTTON mouseButton ) const {
170180
return !( m_MouseState.ButtonState & SDL_BUTTON( mouseButton ) );
171181
}
172182

183+
void InputState::SetMouseButtonState( MOUSE_BUTTON mouseButton, INPUT_STATE state ) {
184+
// Set the corresponding bit
185+
m_MouseState.ButtonState ^= (-state ^ m_MouseState.ButtonState) & (1 << mouseButton);
186+
}
187+
173188
bool InputState::IsKeyDown( SDL_Scancode scanCode ) const {
174189
return m_KeyboardState[scanCode] && m_KeyboardStateTracking;
175190
}
@@ -190,6 +205,12 @@ bool InputState::IsKeyboardStateTrackingActivated() const {
190205
return m_KeyboardStateTracking;
191206
}
192207

208+
void InputState::SetKeyState( SDL_Scancode scanCode, INPUT_STATE state ) {
209+
if ( state != INPUT_STATE_IGNORE ) {
210+
m_KeyboardState[ scanCode ] = static_cast<int>( state );
211+
}
212+
}
213+
193214
const GamepadState* InputState::GetGamepadState( unsigned int gamepadIndex ) const {
194215
if ( gamepadIndex >= m_Gamepads.size() ) {
195216
return nullptr;

InputState.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,14 @@ class InputState {
3333
INPUT_API int GetMouseScrollAccumulationY () const;
3434
INPUT_API bool IsMouseButtonDown ( MOUSE_BUTTON mouseButton ) const;
3535
INPUT_API bool IsMouseButtonUp ( MOUSE_BUTTON mouseButton ) const;
36+
INPUT_API void SetMouseButtonState( MOUSE_BUTTON mouseButton, INPUT_STATE state );
3637

3738
INPUT_API bool IsKeyDown ( SDL_Scancode scanCode ) const;
3839
INPUT_API bool IsKeyUp ( SDL_Scancode scanCode ) const;
3940
INPUT_API void ActivateKeyboardStateTracking ();
4041
INPUT_API void DeactivateKeyboardStateTracking ();
4142
INPUT_API bool IsKeyboardStateTrackingActivated () const;
43+
INPUT_API void SetKeyState ( SDL_Scancode scanCode, INPUT_STATE state );
4244

4345
INPUT_API const GamepadState* GetGamepadState ( unsigned int gamepadIndex ) const;
4446
INPUT_API size_t GetNrOfGamepads () const;
@@ -54,8 +56,8 @@ class InputState {
5456
pUnorderedMap<InputEventCallbackHandle, InputEventCallbackFunction, InputEventCallbackHandleHasher> m_Callbacks;// TODOJM: Make priority queue when consume is needed
5557
int m_NextHandle = 0;
5658

57-
KeyboardState m_KeyboardState = nullptr;
58-
bool m_KeyboardStateTracking = true;
59+
Uint8* m_KeyboardState = nullptr;
60+
bool m_KeyboardStateTracking = true;
5961

6062
MouseState m_MouseState;
6163
bool m_MouseInsideWindow = true;

InputStateTypes.h

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ struct MouseState {
1616
int PositionY = 0;
1717
};
1818

19+
enum INPUT_STATE {
20+
INPUT_STATE_IGNORE = -1,
21+
INPUT_STATE_UP = 0,
22+
INPUT_STATE_DOWN = 1,
23+
};
24+
1925
typedef const Uint8* KeyboardState;
2026

2127
typedef Uint32 GamepadButtonState;

0 commit comments

Comments
 (0)