-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinput_class.js
245 lines (217 loc) · 5.94 KB
/
input_class.js
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
const KEYCODE_TAB = 9;
const KEYCODE_RETURN = 13;
const KEYCODE_LEFT_ARROW = 37;
const KEYCODE_UP_ARROW = 38;
const KEYCODE_RIGHT_ARROW = 39;
const KEYCODE_DOWN_ARROW = 40;
const KEYCODE_M = 77;
const KEYCODE_F1 = 112;
const KEYCODE_F2 = 113;
const KEYCODE_F3 = 114;
const KEYCODE_F4 = 115;
const KEYCODE_F5 = 116;
const KEYCODE_F6 = 117;
const KEYCODE_F7 = 118;
const KEYCODE_F8 = 119;
const KEYCODE_F9 = 120;
const KEYCODE_F10 = 121;
const KEYCODE_F11 = 122;
const KEYCODE_F12 = 123;
const KEYCODE_A = 65;
const KEYCODE_W = 87;
const KEYCODE_S = 83;
const KEYCODE_D = 68;
const KEYCODE_SPACE = 32;
const KEYCODE_PAGEUP = 33;
const KEYCODE_PAGEDOWN = 34;
const KEYCODE_0 = 48;
const KEYCODE_1 = 49;
const KEYCODE_2 = 50;
const KEYCODE_3 = 51;
const KEYCODE_4 = 52;
const KEYCODE_5 = 53;
const KEYCODE_6 = 54;
const KEYCODE_7 = 55;
const KEYCODE_8 = 56;
const KEYCODE_9 = 57;
const KEYCODE_E = 69;
const KEYCODE_F = 70;
const KEYCODE_R = 82;
const KEYCODE_TILDE = 192;
const KEYCODE_SHIFT = 16;
const MOUSE_LEFT_BUTTON = 0;
const MOUSE_MIDDLE_BUTTON = 1;
const MOUSE_RIGHT_BUTTON = 2;
class InputClass {
constructor(window, canvas) {
this.canvas = canvas;
this.cursorX = 0;
this.cursorY = 0;
this.leftButtonDown = false;
this.middleButtonDown = false;
this.rightButtonDown = false;
this.isKeyStatus = [];
var self = this;
window.onkeydown = function (e) {
if (!self.isKeyStatus[e.keyCode]) {
self.isKeyStatus[e.keyCode] = [];
}
if (!self.isKeyStatus[e.keyCode].isPressed) {
self.isKeyStatus[e.keyCode].isPressed = true;
if (self.onkeydown) {
if (self.onkeydown(e.keyCode)) {
e.preventDefault();
}
console.log("KeyDown -> Code: " + e.keyCode);
}
}
};
window.onkeyup = function (e) {
if (!self.isKeyStatus[e.keyCode]) {
self.isKeyStatus[e.keyCode] = [];
}
if (self.isKeyStatus[e.keyCode].isPressed) {
self.isKeyStatus[e.keyCode].isPressed = false;
if (self.onkeyup) {
self.onkeyup(e.keyCode);
}
}
};
window.onmousemove = function (e) {
var x = e.clientX - self.canvas.offsetLeft;
var y = e.clientY - self.canvas.offsetTop;
if (
x >= 0 &&
y >= 0 &&
x <= self.canvas.offsetWidth &&
y <= self.canvas.offsetHeight
) {
self.cursorX = x;
self.cursorY = y;
self.cursorX *= self.canvas.width / self.canvas.offsetWidth;
self.cursorY *= self.canvas.height / self.canvas.offsetHeight;
if (self.onmousemove) {
if (
self.onmousemove(
self.cursorX,
self.cursorY,
e.movementX,
e.movementY
)
) {
e.preventDefault();
}
}
}
};
window.onmousedown = function (e) {
var x = e.clientX - self.canvas.offsetLeft;
var y = e.clientY - self.canvas.offsetTop;
if (
x >= 0 &&
y >= 0 &&
x <= self.canvas.offsetWidth &&
y <= self.canvas.offsetHeight
) {
self.cursorX = x;
self.cursorY = y;
self.cursorX *= self.canvas.width / self.canvas.offsetWidth;
self.cursorY *= self.canvas.height / self.canvas.offsetHeight;
switch (e.button) {
case 0:
self.leftButtonDown = true;
break;
case 1:
self.middleButtonDown = true;
break;
case 2:
self.rightButtonDown = true;
break;
}
if (self.onmousedown) {
if (self.onmousedown(e.button)) {
e.preventDefault();
}
}
}
};
window.onmouseup = function (e) {
var x = e.clientX - self.canvas.offsetLeft;
var y = e.clientY - self.canvas.offsetTop;
if (
x >= 0 &&
y >= 0 &&
x <= self.canvas.offsetWidth &&
y <= self.canvas.offsetHeight
) {
self.cursorX = x;
self.cursorY = y;
self.cursorX *= self.canvas.width / self.canvas.offsetWidth;
self.cursorY *= self.canvas.height / self.canvas.offsetHeight;
switch (e.button) {
case 0:
self.leftButtonDown = false;
break;
case 1:
self.middleButtonDown = false;
break;
case 2:
self.rightButtonDown = false;
break;
}
if (self.onmouseup) {
self.onmouseup(e.button);
}
}
};
document.body.addEventListener(
"touchstart",
function (e) {
//e.preventDefault();
},
false
);
document.body.addEventListener(
"touchmove",
function (e) {
var x = e.touches[0].clientX - self.canvas.offsetLeft;
var y = e.touches[0].clientY - self.canvas.offsetTop;
if (
x >= 0 &&
y >= 0 &&
x <= self.canvas.offsetWidth &&
y <= self.canvas.offsetHeight
) {
self.cursorX = x;
self.cursorY = y;
self.cursorX *= self.canvas.width / self.canvas.offsetWidth;
self.cursorY *= self.canvas.height / self.canvas.offsetHeight;
if (self.onmousemove) {
self.onmousemove(self.cursorX, self.cursorY);
}
}
e.preventDefault();
},
false
);
}
isKeyDown(keyCode) {
if (this.isKeyStatus[keyCode]) {
return this.isKeyStatus[keyCode].isPressed;
} else {
return false;
}
}
isMouseLeftButtonDown() {
return this.leftButtonDown;
}
isMouseRightButtonDown() {
return this.rightButtonDown;
}
getCursorX() {
return this.cursorX;
}
getCursorY() {
return this.cursorY;
}
}