forked from UOLS-VJ1-1516/BaseCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputHandler.cpp
99 lines (88 loc) · 1.96 KB
/
InputHandler.cpp
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
#include "InputHandler.h"
const Uint8* m_keystates;
InputHandler* InputHandler::s_pInstanceI = 0;
Vector2D m_mousePosition;
std::vector<bool> m_mouseButtonStates;
void InputHandler::update() {
SDL_Event event;
while (SDL_PollEvent(&event))
{
m_keystates = SDL_GetKeyboardState(0);
if (event.type == SDL_MOUSEBUTTONDOWN) {
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = true;
}
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = true;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = true;
}
}
if (event.type == SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_MIDDLE)
{
m_mouseButtonStates[MIDDLE] = false;
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_mouseButtonStates[RIGHT] = false;
}
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = false;
}
}
if (event.type == SDL_MOUSEMOTION)
{
m_mousePosition.setX(event.motion.x);
m_mousePosition.setY(event.motion.y);
}
}
if (event.type == SDL_MOUSEBUTTONUP)
{
if (event.button.button == SDL_BUTTON_LEFT)
{
m_mouseButtonStates[LEFT] = false;
}
}
}
void InputHandler::clean() {}
// Métodos de acceso
bool InputHandler::isKeyDown(SDL_Scancode key) {
if (m_keystates != 0)
{
if (m_keystates[key] == 1)
{
return true;
}
else
{
return false;
}
}
return false;
}
InputHandler::InputHandler() {
for (int i = 0; i < 3; i++)
{
m_mouseButtonStates.push_back(false);
}
}
bool InputHandler::getMouseButtonState(int buttonNumber)
{
return m_mouseButtonStates[buttonNumber];
}
Vector2D InputHandler::getMousePosition()
{
return m_mousePosition;
}
void InputHandler::reset() {
m_mouseButtonStates[MIDDLE] = false;
m_mouseButtonStates[RIGHT] = false;
m_mouseButtonStates[LEFT] = false;
}