-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputHandler.cpp
More file actions
109 lines (85 loc) · 2.06 KB
/
InputHandler.cpp
File metadata and controls
109 lines (85 loc) · 2.06 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
#include "InputHandler.h"
InputHandler* InputHandler::instance_ = nullptr;
void InputHandler::clearState() {
keyDown_ = false;
keyUp_ = false;
kbState_ = nullptr;
for(int i=0; i<3; i++) {
mbState_[i] = false;
}
}
void InputHandler::update() {
SDL_Event event;
clearState();
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
onKeyDown(event);
break;
case SDL_KEYUP:
onKeyUp(event);
break;
case SDL_MOUSEMOTION:
onMouseMotion(event);
break;
case SDL_MOUSEBUTTONUP:
onMouseButtonUp(event);
break;
case SDL_MOUSEBUTTONDOWN:
onMouseButtonDown(event);
break;
}
}
}
bool InputHandler::isAnyKeyDown() {
return keyDown_;
}
bool InputHandler::isAnyKeyUp() {
return keyUp_;
}
bool InputHandler::isKeyDown(SDL_Scancode key) {
return kbState_ != nullptr && kbState_[key] == 1;
}
bool InputHandler::isKeyDown(SDL_Keycode key) {
return isKeyDown(SDL_GetScancodeFromKey(key));
}
const Vector2D& InputHandler::getMousePos() {
return mousePos_;
}
int InputHandler::getMouseButtonState(int b) {
return mbState_[b];
}
InputHandler::InputHandler() {
clearState();
}
InputHandler::~InputHandler() {
}
void InputHandler::onKeyDown(SDL_Event& event) {
keyDown_ = true;
kbState_ = SDL_GetKeyboardState(0);
}
void InputHandler::onKeyUp(SDL_Event& event) {
keyUp_ = true;
kbState_ = SDL_GetKeyboardState(0);
}
void InputHandler::onMouseMotion(SDL_Event& event) {
mousePos_.set(event.motion.x,event.motion.y);
}
void InputHandler::onMouseButtonUp(SDL_Event& event) {
if ( event.button.button == SDL_BUTTON_LEFT ) {
mbState_[LEFT] = false;
} else if ( event.button.button == SDL_BUTTON_MIDDLE ) {
mbState_[MIDDLE] = false;
} else if ( event.button.button == SDL_BUTTON_RIGHT ) {
mbState_[RIGHT] = false;
}
}
void InputHandler::onMouseButtonDown(SDL_Event& event) {
if ( event.button.button == SDL_BUTTON_LEFT ) {
mbState_[LEFT] = true;
} else if ( event.button.button == SDL_BUTTON_MIDDLE ) {
mbState_[MIDDLE] = true;
} else if ( event.button.button == SDL_BUTTON_RIGHT ) {
mbState_[RIGHT] = true;
}
}