-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.cpp
More file actions
130 lines (110 loc) · 2.72 KB
/
InputSystem.cpp
File metadata and controls
130 lines (110 loc) · 2.72 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
#include "InputSystem.h"
// Input System is a singleton
InputSystem* InputSystem::mInputSystem = nullptr;
InputSystem::InputSystem() = default;
InputSystem::~InputSystem()
{
InputSystem::mInputSystem = nullptr;
}
void InputSystem::update()
{
POINT current_mouse_pos = {};
::GetCursorPos(¤t_mouse_pos);
// Check if the mouse is moved for the first time
if (mFirstTime)
{
mOldMousePos = Point(current_mouse_pos.x, current_mouse_pos.y);
mFirstTime = false;
}
// There is a mouse move event
if (current_mouse_pos.x != mOldMousePos.m_x || current_mouse_pos.y != mOldMousePos.m_y)
{
auto it = mSetListeners.begin();
while (it != mSetListeners.end())
{
(*it)->onMouseMove(Point(current_mouse_pos.x, current_mouse_pos.y));
++it;
}
}
mOldMousePos = Point(current_mouse_pos.x, current_mouse_pos.y);
for (int i = 0; i < 256; i++)
{
mKeysState[i] = ::GetAsyncKeyState(i);
if (mKeysState[i] & 0x8001) // KEY IS DOWN
{
auto it = mSetListeners.begin();
while (it != mSetListeners.end())
{
if (i == VK_LBUTTON)
{
if (mKeysState[i] != mOldKeysState[i])
(*it)->onLeftMouseDown(Point(current_mouse_pos.x, current_mouse_pos.y));
}
else if (i == VK_RBUTTON)
{
if (mKeysState[i] != mOldKeysState[i])
(*it)->onRightMouseDown(Point(current_mouse_pos.x, current_mouse_pos.y));
}
else
(*it)->onKeyDown(i);
++it;
}
}
else // KEY IS UP
{
if (mKeysState[i] != mOldKeysState[i])
{
auto it = mSetListeners.begin();
while (it != mSetListeners.end())
{
if (i == VK_LBUTTON)
{
if (mKeysState[i] != mOldKeysState[i])
(*it)->onLeftMouseUp(Point(current_mouse_pos.x, current_mouse_pos.y));
}
else if (i == VK_RBUTTON)
{
if (mKeysState[i] != mOldKeysState[i])
(*it)->onRightMouseUp(Point(current_mouse_pos.x, current_mouse_pos.y));
}
else
(*it)->onKeyUp(i);
++it;
}
}
}
}
// Store current keys state to old keys state buffer
::memcpy(mOldKeysState, mKeysState, sizeof(short) * 256);
}
void InputSystem::addListener(InputListener* listener)
{
this->mSetListeners.insert(listener);
}
void InputSystem::removeListener(InputListener* listener)
{
this->mSetListeners.erase(listener);
}
void InputSystem::setCursorPosition(const Point& pos)
{
::SetCursorPos(pos.m_x, pos.m_y);
}
void InputSystem::showCursor(bool show)
{
::ShowCursor(show);
}
InputSystem* InputSystem::get()
{
return mInputSystem;
}
void InputSystem::create()
{
if (InputSystem::mInputSystem)
throw std::exception("Input System already created");
InputSystem::mInputSystem = new InputSystem();
}
void InputSystem::release()
{
if (!InputSystem::mInputSystem) return;
delete InputSystem::mInputSystem;
}