-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInputContext.cpp
More file actions
335 lines (283 loc) · 9.65 KB
/
Copy pathInputContext.cpp
File metadata and controls
335 lines (283 loc) · 9.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "InputContext.h"
#include "InputState.h"
#include <iostream>
InputContext& InputContext::GetInstance() {
static InputContext inputContext;
return inputContext;
}
void InputContext::Initialize() {
m_InputEventCallbackHandle = g_InputState.RegisterEventInterest( std::bind( &InputContext::HandleEvent, this, std::placeholders::_1 ) );
m_GamepadContexts.resize( INPUT_MAX_NR_OF_GAMEPADS );
}
void InputContext::Deinitialize() {
g_InputState.UnregisterEventInterest( m_InputEventCallbackHandle );
}
void InputContext::Update() {
m_KeyboardPressStack.clear();
m_KeyboardReleaseStack.clear();
m_MouseSingleClickPressStack.clear();
m_MouseSingleClickReleaseStack.clear();
m_MouseDoubleClickPressStack.clear();
m_MouseDoubleClickReleaseStack.clear();
m_MousePosDeltaX = g_InputState.GetMouseDeltaX( m_MousePosLastX );
m_MousePosDeltaY = g_InputState.GetMouseDeltaY( m_MousePosLastY );
m_MousePosLastX = g_InputState.GetMouseDeltaAccumulationX();
m_MousePosLastY = g_InputState.GetMouseDeltaAccumulationY();
m_MouseScrollDeltaX = g_InputState.GetMouseScrollDeltaX( m_MouseScrollLastPosX );
m_MouseScrollDeltaY = g_InputState.GetMouseScrollDeltaY( m_MouseScrollLastPosY );
m_MouseScrollLastPosX = g_InputState.GetMouseScrollAccumulationX();
m_MouseScrollLastPosY = g_InputState.GetMouseScrollAccumulationY();
for ( auto& gamepad : m_GamepadContexts ) {
gamepad.Update();
}
}
bool InputContext::KeyUpDown( SDL_Scancode scanCode ) const {
for ( auto& pressed : m_KeyboardPressStack ) {
if ( pressed == scanCode ) {
return true;
}
}
return false;
}
bool InputContext::KeyUpDownConsume( SDL_Scancode scanCode, INPUT_STATE state ) {
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardPressStack.begin(); it != m_KeyboardPressStack.end(); ) {
if ( *it == scanCode ) {
it = m_KeyboardPressStack.erase( it );
if ( state != INPUT_STATE_IGNORE ) {
g_InputState.SetKeyState( scanCode, state );
}
return true;
} else {
++it;
}
}
return false;
}
int InputContext::KeyUpDownConsumeAll( SDL_Scancode scanCode, INPUT_STATE state ) {
int nrOfKeysConsumed = 0;
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardPressStack.begin(); it != m_KeyboardPressStack.end(); ) {
if ( *it == scanCode ) {
it = m_KeyboardPressStack.erase( it );
++nrOfKeysConsumed;
} else {
++it;
}
}
if ( ( nrOfKeysConsumed > 0 ) && ( state != INPUT_STATE_IGNORE ) ) {
g_InputState.SetKeyState( scanCode, state );
}
return nrOfKeysConsumed;
}
bool InputContext::KeyDownUp( SDL_Scancode scanCode ) const {
for ( auto& released : m_KeyboardReleaseStack ) {
if ( released == scanCode ) {
return true;
}
}
return false;
}
bool InputContext::KeyDownUpConsume( SDL_Scancode scanCode, INPUT_STATE state ) {
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardReleaseStack.begin(); it != m_KeyboardReleaseStack.end(); ) {
if ( *it == scanCode ) {
it = m_KeyboardReleaseStack.erase( it );
if ( state != INPUT_STATE_IGNORE ) {
g_InputState.SetKeyState( scanCode, state );
}
return true;
} else {
++it;
}
}
return false;
}
int InputContext::KeyDownUpConsumeAll( SDL_Scancode scanCode, INPUT_STATE state ) {
int nrOfKeysConsumed = 0;
for ( pVector<SDL_Scancode>::iterator it = m_KeyboardReleaseStack.begin(); it != m_KeyboardReleaseStack.end(); ) {
if ( *it == scanCode ) {
it = m_KeyboardReleaseStack.erase( it );
++nrOfKeysConsumed;
} else {
++it;
}
}
if ( ( nrOfKeysConsumed > 0 ) && ( state != INPUT_STATE_IGNORE ) ) {
g_InputState.SetKeyState( scanCode, state );
}
return nrOfKeysConsumed;
}
bool InputContext::KeyDown( SDL_Scancode scanCode ) const {
return g_InputState.IsKeyDown( scanCode );
}
bool InputContext::KeyUp( SDL_Scancode scanCode ) const {
return g_InputState.IsKeyUp( scanCode );
}
const pVector<SDL_Scancode>& InputContext::GetKeyboardPressStack() const {
return m_KeyboardPressStack;
}
const pVector<SDL_Scancode>& InputContext::GetKeyboardReleaseStack() const {
return m_KeyboardReleaseStack;
}
bool InputContext::MouseButtonDown( MOUSE_BUTTON button ) const {
return g_InputState.IsMouseButtonDown( button );
}
bool InputContext::MouseButtonUp( MOUSE_BUTTON button ) const {
return g_InputState.IsMouseButtonUp( button );
}
bool InputContext::MouseButtonUpDown( MOUSE_BUTTON button ) const {
for ( auto& pressed : m_MouseSingleClickPressStack ) {
if ( pressed == button ) {
return true;
}
}
return false;
}
bool InputContext::MouseButtonUpDownConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButton( m_MouseSingleClickPressStack, button, state );
}
int InputContext::MouseButtonUpDownConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButtonAll( m_MouseSingleClickPressStack, button, state );
}
bool InputContext::MouseButtonDownUp( MOUSE_BUTTON button ) const {
for ( auto& pressed : m_MouseSingleClickReleaseStack ) {
if ( pressed == button ) {
return true;
}
}
return false;
}
bool InputContext::MouseButtonDownUpConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButton( m_MouseSingleClickReleaseStack, button, state );
}
int InputContext::MouseButtonDownUpConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButtonAll( m_MouseSingleClickReleaseStack, button, state );
}
bool InputContext::MouseButtonDoubleUpDown( MOUSE_BUTTON button ) const {
for ( auto& pressed : m_MouseDoubleClickPressStack ) {
if ( pressed == button ) {
return true;
}
}
return false;
}
bool InputContext::MouseButtonDoubleUpDownConsume( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButton( m_MouseDoubleClickPressStack, button, state );
}
int InputContext::MouseButtonDoubleUpDownConsumeAll( MOUSE_BUTTON button, INPUT_STATE state ) {
return ConsumeMouseButtonAll( m_MouseDoubleClickPressStack, button, state );
}
bool InputContext::MouseButtonDoubleDownUp( MOUSE_BUTTON button ) const {
for ( auto& pressed : m_MouseDoubleClickReleaseStack ) {
if ( pressed == button ) {
return true;
}
}
return false;
}
bool InputContext::MouseButtonDoubleDownUpConsume( MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
return ConsumeMouseButton( m_MouseDoubleClickReleaseStack, button, stateToSet );
}
int InputContext::MouseButtonDoubleDownUpConsumeAll( MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
return ConsumeMouseButtonAll( m_MouseDoubleClickReleaseStack, button, stateToSet );
}
int InputContext::GetMousePosX() const {
return g_InputState.GetMouseState().PositionX;
}
int InputContext::GetMousePosY() const {
return g_InputState.GetMouseState().PositionY;
}
int InputContext::GetMousePosDeltaX() const {
return m_MousePosDeltaX;
}
int InputContext::GetMousePosDeltaY() const {
return m_MousePosDeltaY;
}
int InputContext::GetMouseScrollDeltaX() const {
return m_MouseScrollDeltaX;
}
int InputContext::GetMouseScrollDeltaY() const {
return m_MouseScrollDeltaY;
}
const pVector<MOUSE_BUTTON>& InputContext::GetMouseSingleClickPressStack() const {
return m_MouseSingleClickPressStack;
}
const pVector<MOUSE_BUTTON>& InputContext::GetMouseSingleClickReleaseStack() const {
return m_MouseSingleClickReleaseStack;
}
const pVector<MOUSE_BUTTON>& InputContext::GetMouseDoubleClickPressStack() const {
return m_MouseDoubleClickPressStack;
}
const pVector<MOUSE_BUTTON>& InputContext::GetMouseDoubleClickReleaseStack() const {
return m_MouseDoubleClickReleaseStack;
}
const GamepadContext& InputContext::GetGamepadContext( unsigned int gamepadIndex ) const {
return m_GamepadContexts.at( gamepadIndex );
}
bool InputContext::HandleEvent( const SDL_Event& event ) {
switch ( event.type ) {
case SDL_KEYUP: {
if ( event.key.repeat == 0 ) {
m_KeyboardReleaseStack.push_back( event.key.keysym.scancode );
}
} break;
case SDL_KEYDOWN: {
if ( event.key.repeat == 0 ) {
m_KeyboardPressStack.push_back( event.key.keysym.scancode );
}
} break;
case SDL_CONTROLLERBUTTONDOWN: {
m_GamepadContexts.at( event.cbutton.which ).HandleEvent( event );
} break;
case SDL_CONTROLLERBUTTONUP: {
m_GamepadContexts.at( event.cbutton.which ).HandleEvent( event );
} break;
case SDL_CONTROLLERDEVICEADDED: {
m_GamepadContexts.at( event.cdevice.which ).Initialize( event.cdevice.which );
} break;
case SDL_MOUSEBUTTONDOWN: {
if ( event.button.clicks == 1 ) {
m_MouseSingleClickPressStack.push_back( static_cast<MOUSE_BUTTON>( event.button.button ) );
}
if ( event.button.clicks == 2 ) {
m_MouseDoubleClickPressStack.push_back( static_cast<MOUSE_BUTTON>( event.button.button ) );
}
} break;
case SDL_MOUSEBUTTONUP: {
if ( event.button.clicks == 1 ) {
m_MouseSingleClickReleaseStack.push_back( static_cast<MOUSE_BUTTON>( event.button.button ) );
}
if ( event.button.clicks == 2 ) {
m_MouseDoubleClickReleaseStack.push_back( static_cast<MOUSE_BUTTON>( event.button.button ) );
}
} break;
}
return false;
}
bool InputContext::ConsumeMouseButton( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
for ( pVector<MOUSE_BUTTON>::iterator it = stack.begin(); it != stack.end(); ) {
if ( *it == button ) {
it = stack.erase( it );
if ( stateToSet != INPUT_STATE_IGNORE ) {
g_InputState.SetMouseButtonState( button, stateToSet );
}
return true;
} else {
++it;
}
}
return false;
}
int InputContext::ConsumeMouseButtonAll( pVector<MOUSE_BUTTON>& stack, MOUSE_BUTTON button, INPUT_STATE stateToSet ) {
int nrOfConsumedButtons = 0;
for ( pVector<MOUSE_BUTTON>::iterator it = stack.begin(); it != stack.end(); ) {
if ( *it == button ) {
it = stack.erase( it );
++nrOfConsumedButtons;
} else {
++it;
}
}
if ( stateToSet != INPUT_STATE_IGNORE ) {
g_InputState.SetMouseButtonState( button, stateToSet );
}
return nrOfConsumedButtons;
}