-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.js
131 lines (100 loc) · 3 KB
/
input.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
function initInput(){
window.usingTouch=false;
let touchCountDown=0;
let keyboardState = {};
let keysDown = {};
let touchKeyState = {};
let touchKeysDown = {};
document.addEventListener('keydown', keyDownHandler);
document.addEventListener('keyup', keyUpHandler);
function keyDownHandler(event) {
keyboardState[event.keyCode] = true;
keysDown[event.keyCode] = true;
}
function keyUpHandler(event) {
keyboardState[event.keyCode] = false;
}
function isKeyDown(keyCode) {
return (keyboardState[keyCode] ? true : false) ||
(touchKeyState[keyCode] ? true : false)
;
}
function keyWentDown(keyCode) {
return (keysDown[keyCode] ? true : false) ||
(touchKeysDown[keyCode] ? true : false);
}
function flushKeysDown() {
keysDown = {};
touchKeysDown = {};
if (touchCountDown>0) {
touchCountDown-=1;
}
window.usingTouch=(touchCountDown>0);
}
let mainElement=document.querySelector("#main");
mainElement.addEventListener("touchstart", touchStartHandler);
mainElement.addEventListener("touchend", touchHandler);
mainElement.addEventListener("touchcancel", touchHandler);
mainElement.addEventListener("touchmove", touchHandler);
function localCoordinates([x, y]) {
return [x / mainElement.clientWidth * mainElement.width, y / mainElement.clientHeight * mainElement.height];
}
window.touchList=[];
function wasdTouch(touch) {
let [dx,dy]=vdiff(touch,[200,900]);
//note=JSON.stringify({dx,dy});
if (vlength([dx,dy]) > 300) return [];
if (vlength([dx,dy]) < 50) return [];
result =[];
if (dy<-25) result.push(87);
if (dy>25) result.push(83);
if (dx<-25) result.push(65);
if (dx>25) result.push(68);
return result;
}
function touchButton(keycode,pos,radius) {
return touch=>vdistance(pos,touch)<radius?[keycode]:[];
}
let touchMappers =[wasdTouch,touchButton(32,[0,0],200), touchButton(69,[1820,980],100)];
function flatten(array) { //there's a method for this, except for edge.
return array.reduce( (a,v)=>a.concat(v),[]);
}
function touchStartHandler(e){
window.touchList=e.touches;
e.preventDefault();
for (let touch of e.changedTouches) {
let pos = localCoordinates([touch.clientX,touch.clientY]);
let keyCodes = touchMappers.map(a=>a(pos));
note=JSON.stringify(flatten(keyCodes));
flatten(keyCodes).forEach(a=>touchKeysDown[a]=true);
}
touchKeyState={};
for (let touch of e.touches) {
let pos = localCoordinates([touch.clientX,touch.clientY]);
let keyCodes = touchMappers.map(a=>a(pos));
for (let keyCode of flatten(keyCodes)) {
touchKeyState[keyCode]=true;
}
}
touchCountDown=200;
}
function touchHandler(e){
touchCountDown=200;
window.touchList=e.touches;
touchKeyState={};
for (let touch of e.touches) {
let pos = localCoordinates([touch.clientX,touch.clientY]);
let keyCodes = touchMappers.map(a=>a(pos))
note=JSON.stringify(keyCodes) " *** " JSON.stringify(flatten(keyCodes)) ;
for (let keyCode of flatten(keyCodes)) {
touchKeyState[keyCode]=true;
}
}
e.preventDefault();
}
window.input = {
isKeyDown,
keyWentDown,
flushKeysDown
};
};