-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputManager.js
61 lines (57 loc) · 1.54 KB
/
InputManager.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
class InputManager
{
constructor(canvas)
{
this.kd = [];
this.ku = [];
this.mm = [];
this.md = [];
this.mu = [];
window.oncontextmenu = (e) => { e.preventDefault(); };
window.onkeydown = (e) => { this.kd.forEach(fn => fn.fn(e, this, fn.user)); };
window.onkeyup = (e) => { this.ku.forEach(fn => fn.fn(e, this, fn.user)); };
window.onmousemove = (e) => { this.mm.forEach(fn => fn.fn(e, this, fn.user)); };
window.onmousedown = (e) => { this.md.forEach(fn => fn.fn(e, this, fn.user)); };
window.onmouseup = (e) => { this.mu.forEach(fn => fn.fn(e, this, fn.user)); };
this.canvas = canvas;
this.mouse = {
x: undefined,
y: undefined,
px: undefined,
py: undefined,
button: -1
};
this.AddMouseMove(e =>
{
const r = this.canvas.c.getBoundingClientRect();
this.mouse.x = e.clientX - r.x;
this.mouse.y = e.clientY - r.y;
this.mouse.px = parseInt(this.mouse.x / this.canvas.spsize);
this.mouse.py = parseInt(this.mouse.y / this.canvas.spsize);
});
this.AddMouseDown(e =>
{
this.mouse.button = e.button;
});
}
AddKeyUp(fn, user = {})
{
this.ku.push({ fn, user });
}
AddKeyDown(fn, user = {})
{
this.kd.push({ fn, user });
}
AddMouseMove(fn, user = {})
{
this.mm.push({ fn, user });
}
AddMouseUp(fn, user = {})
{
this.mu.push({ fn, user });
}
AddMouseDown(fn, user = {})
{
this.md.push({ fn, user });
}
}