-
Notifications
You must be signed in to change notification settings - Fork 2
/
input.js
40 lines (34 loc) · 1.23 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
import { Script } from 'playcanvas';
// initialize code called once per entity
export class Input extends Script {
initialize() {
var app = this.app;
var press = function (x, y) {
var obj = {
processed: false
};
app.fire('ui:press', x, y, obj);
// If the UI didn't handle the mousedown/touch, sent it on to the game
if (!obj.processed) {
app.fire('game:press', x, y);
}
};
var mouseDown = function (e) {
e.preventDefault();
// press(e.clientX, e.clientY);
press(e.offsetX, e.offsetY);
};
var touchStart = function (e) {
e.preventDefault();
var touch = e.changedTouches[0];
press(touch.clientX, touch.clientY);
};
var release = function (e) {
app.fire('ui:release');
};
window.addEventListener('mousedown', mouseDown, { passive: false });
window.addEventListener('mouseup', release, { passive: false });
window.addEventListener('touchstart', touchStart, { passive: false });
window.addEventListener('touchend', release, { passive: false });
};
}