-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputManager.cpp
More file actions
315 lines (284 loc) · 8.21 KB
/
InputManager.cpp
File metadata and controls
315 lines (284 loc) · 8.21 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
// game engine includes
#include "InputManager.h"
#include "GraphicsManager.h"
#include "LogManager.h"
#include "EventKeyboard.h"
#include "EventMouse.h"
// constructor, set type, gameover, and gameloop number
df::InputManager::InputManager(){
setType("InputManager");
}
// overloaded constructor
df::InputManager::InputManager(InputManager const&){
setType("InputManager");
}
// Get the one and only instance of the InputManager.
df::InputManager &df::InputManager::getInstance(){
static InputManager input_manager;
return input_manager;
}
// Get terminal ready to capture input.
// Return 0 if ok, else return -1.
int df::InputManager::startUp(){
// needs graphics manager to be already started up
if (!df::GraphicsManager::getInstance().isStarted())
return -1;
sf::RenderWindow *rw = df::GraphicsManager::getInstance().getWindow();
rw->setKeyRepeatEnabled(true);
Manager::startUp();
df::LogManager::getInstance().writeLog("InputManager:startUp Started up InputManager\n");
}
// Revert back to normal terminal mode.
void df::InputManager::shutDown(){
sf::RenderWindow *rw = df::GraphicsManager::getInstance().getWindow();
rw->setKeyRepeatEnabled(false);
Manager::shutDown();
df::LogManager::getInstance().writeLog("InputManager:shutDown InputManager has been shut down\n");
}
// Get input from the keyboard and mouse.
// Pass event along to all Objects.
void df::InputManager::getInput(){
// Check past window events.
sf::RenderWindow *rw = df::GraphicsManager::getInstance().getWindow();
sf::Event event;
while (rw->pollEvent(event)){
bool unrecongnizedEvent = false;
// boolean and event if it is a keyboard event
bool isKeyboardEvent = false;
df::EventKeyboard *eventKeyboard = new df::EventKeyboard();
df::EventKeyboard *eventKeyboardPressed = new df::EventKeyboard();
// boolean and event if it is a mouse event
bool isMouseButtonEvent = false;
df::EventMouse *eventMouse = new df::EventMouse();
// find out what general action it is
switch (event.type){
case sf::Event::KeyPressed:
isKeyboardEvent = true;
eventKeyboard->setKeyboardAction(EventKeyboardAction::KEY_PRESSED);
break;
case sf::Event::KeyReleased:
isKeyboardEvent = true;
eventKeyboard->setKeyboardAction(EventKeyboardAction::KEY_RELEASED);
break;
case sf::Event::MouseMoved:
eventMouse->setMouseAction(EventMouseAction::MOVED);
eventMouse->setMousePosition(df::Position(event.mouseMove.x, event.mouseMove.y));
Manager::onEvent(eventMouse);
break;
case sf::Event::MouseButtonPressed:
isMouseButtonEvent = true;
eventMouse->setMouseAction(EventMouseAction::PRESSED);
eventMouse->setMousePosition(df::Position(event.mouseButton.x, event.mouseButton.y));
break;
case sf::Event::MouseButtonReleased:
isMouseButtonEvent = true;
eventMouse->setMouseAction(EventMouseAction::CLICKED);
eventMouse->setMousePosition(df::Position(event.mouseButton.x, event.mouseButton.y));
break;
}
// if it is a mouse button event then find out what button it is and send it to all objects
if (isMouseButtonEvent){
switch (event.mouseButton.button){
case sf::Mouse::Left:
eventMouse->setMouseButton(EventMouseButton::LEFT);
break;
case sf::Mouse::Right:
eventMouse->setMouseButton(EventMouseButton::RIGHT);
break;
case sf::Mouse::Middle:
eventMouse->setMouseButton(EventMouseButton::MIDDLE);
break;
// unkown button
default:
unrecongnizedEvent = true;
break;
}
if (!unrecongnizedEvent)
Manager::onEvent(eventMouse);
}
// if it is a keyboard event then find out what button it is and send it to all objects
if (isKeyboardEvent){
switch (event.key.code){
// special keys
case sf::Keyboard::Space:
eventKeyboard->setKey(Input::SPACE);
break;
case sf::Keyboard::Return:
eventKeyboard->setKey(Input::RETURN);
break;
case sf::Keyboard::Escape:
eventKeyboard->setKey(Input::ESCAPE);
break;
case sf::Keyboard::Tab:
eventKeyboard->setKey(Input::TAB);
break;
case sf::Keyboard::Left:
eventKeyboard->setKey(Input::LEFTARROW);
break;
case sf::Keyboard::Right:
eventKeyboard->setKey(Input::RIGHTARROW);
break;
case sf::Keyboard::Up:
eventKeyboard->setKey(Input::UPARROW);
break;
case sf::Keyboard::Down:
eventKeyboard->setKey(Input::DOWNARROW);
break;
case sf::Keyboard::Pause:
eventKeyboard->setKey(Input::PAUSE);
break;
case sf::Keyboard::Subtract:
eventKeyboard->setKey(Input::MINUS);
break;
case sf::Keyboard::Add:
eventKeyboard->setKey(Input::PLUS);
break;
case sf::Keyboard::Tilde:
eventKeyboard->setKey(Input::TILDE);
break;
case sf::Keyboard::Period:
eventKeyboard->setKey(Input::PERIOD);
break;
case sf::Keyboard::Comma:
eventKeyboard->setKey(Input::COMMA);
break;
case sf::Keyboard::Slash:
eventKeyboard->setKey(Input::SLASH);
break;
case sf::Keyboard::LControl:
eventKeyboard->setKey(Input::LEFTCONTROL);
break;
case sf::Keyboard::RControl:
eventKeyboard->setKey(Input::RIGHTCONTROL);
break;
case sf::Keyboard::LShift:
eventKeyboard->setKey(Input::LEFTSHIFT);
break;
case sf::Keyboard::RShift:
eventKeyboard->setKey(Input::RIGHTSHIFT);
break;
// letters
case sf::Keyboard::A:
eventKeyboard->setKey(Input::A);
break;
case sf::Keyboard::B:
eventKeyboard->setKey(Input::B);
break;
case sf::Keyboard::C:
eventKeyboard->setKey(Input::C);
break;
case sf::Keyboard::D:
eventKeyboard->setKey(Input::D);
break;
case sf::Keyboard::E:
eventKeyboard->setKey(Input::E);
break;
case sf::Keyboard::F:
eventKeyboard->setKey(Input::F);
break;
case sf::Keyboard::G:
eventKeyboard->setKey(Input::G);
break;
case sf::Keyboard::H:
eventKeyboard->setKey(Input::H);
break;
case sf::Keyboard::I:
eventKeyboard->setKey(Input::I);
break;
case sf::Keyboard::J:
eventKeyboard->setKey(Input::J);
break;
case sf::Keyboard::K:
eventKeyboard->setKey(Input::K);
break;
case sf::Keyboard::L:
eventKeyboard->setKey(Input::L);
break;
case sf::Keyboard::M:
eventKeyboard->setKey(Input::M);
break;
case sf::Keyboard::N:
eventKeyboard->setKey(Input::N);
break;
case sf::Keyboard::O:
eventKeyboard->setKey(Input::O);
break;
case sf::Keyboard::P:
eventKeyboard->setKey(Input::P);
break;
case sf::Keyboard::Q:
eventKeyboard->setKey(Input::Q);
break;
case sf::Keyboard::R:
eventKeyboard->setKey(Input::R);
break;
case sf::Keyboard::S:
eventKeyboard->setKey(Input::S);
break;
case sf::Keyboard::T:
eventKeyboard->setKey(Input::T);
break;
case sf::Keyboard::U:
eventKeyboard->setKey(Input::U);
break;
case sf::Keyboard::V:
eventKeyboard->setKey(Input::V);
break;
case sf::Keyboard::W:
eventKeyboard->setKey(Input::W);
break;
case sf::Keyboard::X:
eventKeyboard->setKey(Input::X);
break;
case sf::Keyboard::Y:
eventKeyboard->setKey(Input::Y);
break;
case sf::Keyboard::Z:
eventKeyboard->setKey(Input::Z);
break;
// numbers
case sf::Keyboard::Num1:
eventKeyboard->setKey(Input::NUM1);
break;
case sf::Keyboard::Num2:
eventKeyboard->setKey(Input::NUM2);
break;
case sf::Keyboard::Num3:
eventKeyboard->setKey(Input::NUM3);
break;
case sf::Keyboard::Num4:
eventKeyboard->setKey(Input::NUM4);
break;
case sf::Keyboard::Num5:
eventKeyboard->setKey(Input::NUM5);
break;
case sf::Keyboard::Num6:
eventKeyboard->setKey(Input::NUM6);
break;
case sf::Keyboard::Num7:
eventKeyboard->setKey(Input::NUM7);
break;
case sf::Keyboard::Num8:
eventKeyboard->setKey(Input::NUM8);
break;
case sf::Keyboard::Num9:
eventKeyboard->setKey(Input::NUM9);
break;
case sf::Keyboard::Num0:
eventKeyboard->setKey(Input::NUM0);
break;
// any other keys
default:
unrecongnizedEvent = true;
break;
}
if (!unrecongnizedEvent)
if (sf::Keyboard::isKeyPressed(event.key.code)){
eventKeyboardPressed->setKey(eventKeyboard->getKey());
eventKeyboardPressed->setKeyboardAction(df::KEY_DOWN);
Manager::onEvent(eventKeyboardPressed);
}
Manager::onEvent(eventKeyboard);
}
}
}