-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputSystem.cpp
More file actions
188 lines (161 loc) · 4.38 KB
/
InputSystem.cpp
File metadata and controls
188 lines (161 loc) · 4.38 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
#include "InputSystem.h"
InputSystem* InputSystem::sharedInstance = nullptr;
InputSystem* InputSystem::getInstance()
{
return sharedInstance;
}
void InputSystem::initialize()
{
sharedInstance = new InputSystem();
}
void InputSystem::destroy()
{
delete sharedInstance;
}
InputSystem::InputSystem()
{
this->oldMousePos = Point(0, 0);
}
InputSystem::~InputSystem()
{
this->inputListenerList.clear();
}
void InputSystem::addListener(InputListener* listener)
{
this->inputListenerList.push_back(listener);
}
void InputSystem::removeListener(InputListener* listener)
{
//find object in used
int index = -1;
for (int i = 0; i < this->inputListenerList.size(); i++) {
if (this->inputListenerList[i] == listener) {
index = i;
break;
}
}
if (index > -1) {
this->inputListenerList.erase(this->inputListenerList.begin() + index);
}
}
void InputSystem::update()
{
POINT currentPt = {};
GetCursorPos(¤tPt);
if (firstTimeCall) {
firstTimeCall = false;
this->oldMousePos = Point(currentPt.x, currentPt.y);
}
// if mouse position changed
if (oldMousePos.getX() != currentPt.x || oldMousePos.getY() != currentPt.y)
{
Point deltaPt = Point(currentPt.x - oldMousePos.getX(), currentPt.y - oldMousePos.getY());
this->callOnMouseMove(deltaPt);
}
this->oldMousePos = Point(currentPt.x, currentPt.y);
if (GetKeyboardState(keyStates)) //update keyStates
{
for (int i = 0; i < ARRAYSIZE(keyStates); i++)
{
// this->keyStates[i] & 0x80 checks whether a key has been pressed (regardless of whether it is toggled / untoggled)
if (this->keyStates[i] & 0x80 && this->keyStates[i] != this->oldKeyStates[i])
{
if (VK_LBUTTON == i && this->keyStates[i] != this->oldKeyStates[i])
{
Point deltaPt = Point(currentPt.x - this->oldMousePos.getX(), currentPt.y - this->oldMousePos.getY());
this->callOnLeftMouseDown(deltaPt);
}
else if (VK_RBUTTON == i && this->keyStates[i] != this->oldKeyStates[i])
{
Point deltaPt = Point(currentPt.x - this->oldMousePos.getX(), currentPt.y - this->oldMousePos.getY());
this->callOnRightMouseDown(deltaPt);
}
else
callOnKeyDown(i);
}
else if (this->keyStates[i] != this->oldKeyStates[i])
{
if (VK_LBUTTON == i && this->keyStates[i] != this->oldKeyStates[i])
{
Point deltaPt = Point(currentPt.x - this->oldMousePos.getX(), currentPt.y - this->oldMousePos.getY());
this->callOnLeftMouseUp(deltaPt);
}
else if (VK_RBUTTON == i && this->keyStates[i] != this->oldKeyStates[i])
{
Point deltaPt = Point(currentPt.x - this->oldMousePos.getX(), currentPt.y - this->oldMousePos.getY());
this->callOnRightMouseUp(deltaPt);
}
else
callOnKeyUp(i);
}
}
//store current keys to old states
::memcpy(this->oldKeyStates, this->keyStates, sizeof(unsigned char) * 256);
}
}
bool InputSystem::isKeyDown(int key)
{
for (int i = 0; i < ARRAYSIZE(this->keyStates); i++) {
if (this->keyStates[i] & 0x80 && i == key) {
return true;
}
else if (i == key) {
return false;
}
}
return false;
}
bool InputSystem::isKeyUp(int key)
{
for (int i = 0; i < ARRAYSIZE(this->keyStates); i++) {
if (!(this->keyStates[i] & 0x80) && i == key) {
return true;
}
else if (i == key) {
return false;
}
}
return false;
}
void InputSystem::callOnKeyDown(int key)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onKeyDown(key);
}
}
void InputSystem::callOnKeyUp(int key)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onKeyUp(key);
}
}
void InputSystem::callOnMouseMove(Point deltaPt)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onMouseMove(deltaPt);
}
}
void InputSystem::callOnLeftMouseDown(Point deltaPt)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onLeftMouseDown(deltaPt);
}
}
void InputSystem::callOnLeftMouseUp(Point deltaPt)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onLeftMouseUp(deltaPt);
}
}
void InputSystem::callOnRightMouseDown(Point deltaPt)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onRightMouseDown(deltaPt);
}
}
void InputSystem::callOnRightMouseUp(Point deltaPt)
{
for (int i = 0; i < this->inputListenerList.size(); i++) {
this->inputListenerList[i]->onRightMouseUp(deltaPt);
}
}