1
1
#include " InputContext.h"
2
2
#include " InputState.h"
3
+ #include < iostream>
3
4
4
5
InputContext& InputContext::GetInstance () {
5
6
static InputContext inputContext;
@@ -45,6 +46,38 @@ bool InputContext::KeyUpDown( SDL_Scancode scanCode ) const {
45
46
return false ;
46
47
}
47
48
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
+
48
81
bool InputContext::KeyDownUp ( SDL_Scancode scanCode ) const {
49
82
for ( auto & released : m_KeyboardReleaseStack ) {
50
83
if ( released == scanCode ) {
@@ -54,6 +87,38 @@ bool InputContext::KeyDownUp( SDL_Scancode scanCode ) const {
54
87
return false ;
55
88
}
56
89
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
+
57
122
bool InputContext::KeyDown ( SDL_Scancode scanCode ) const {
58
123
return g_InputState.IsKeyDown ( scanCode );
59
124
}
@@ -87,15 +152,31 @@ bool InputContext::MouseButtonUpDown( MOUSE_BUTTON button ) const {
87
152
return false ;
88
153
}
89
154
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
+
90
163
bool InputContext::MouseButtonDownUp ( MOUSE_BUTTON button ) const {
91
- for ( auto & pressed : m_MouseDoubleClickReleaseStack ) {
164
+ for ( auto & pressed : m_MouseSingleClickReleaseStack ) {
92
165
if ( pressed == button ) {
93
166
return true ;
94
167
}
95
168
}
96
169
return false ;
97
170
}
98
171
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
+
99
180
bool InputContext::MouseButtonDoubleUpDown ( MOUSE_BUTTON button ) const {
100
181
for ( auto & pressed : m_MouseDoubleClickPressStack ) {
101
182
if ( pressed == button ) {
@@ -105,6 +186,14 @@ bool InputContext::MouseButtonDoubleUpDown( MOUSE_BUTTON button ) const {
105
186
return false ;
106
187
}
107
188
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
+
108
197
bool InputContext::MouseButtonDoubleDownUp ( MOUSE_BUTTON button ) const {
109
198
for ( auto & pressed : m_MouseDoubleClickReleaseStack ) {
110
199
if ( pressed == button ) {
@@ -114,6 +203,14 @@ bool InputContext::MouseButtonDoubleDownUp( MOUSE_BUTTON button ) const {
114
203
return false ;
115
204
}
116
205
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
+
117
214
int InputContext::GetMousePosX () const {
118
215
return g_InputState.GetMouseState ().PositionX ;
119
216
}
@@ -122,11 +219,11 @@ int InputContext::GetMousePosY() const {
122
219
return g_InputState.GetMouseState ().PositionY ;
123
220
}
124
221
125
- int InputContext::GetMousePosDeltaX ( ) const {
222
+ int InputContext::GetMousePosDeltaX () const {
126
223
return m_MousePosDeltaX;
127
224
}
128
225
129
- int InputContext::GetMousePosDeltaY ( ) const {
226
+ int InputContext::GetMousePosDeltaY () const {
130
227
return m_MousePosDeltaY;
131
228
}
132
229
@@ -200,3 +297,35 @@ bool InputContext::HandleEvent( const SDL_Event& event ) {
200
297
return false ;
201
298
}
202
299
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
+
0 commit comments