-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseTracker.js
71 lines (65 loc) · 2.47 KB
/
MouseTracker.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
MouseTracker = function() {
this.left = false;
this.wasLeft = false;
this.right = false;
this.wasRight = false;
this.middle = false;
this.wasMiddle = false;
this.position = new THREE.Vector2();
this.oldPosition = new THREE.Vector2();
this.mouseRay = new THREE.Raycaster();
this.hovered = null;
};
MouseTracker.prototype = {
constructor: MouseTracker,
preUpdate: function() {
this.mouseRay.setFromCamera(this.position, game.camera.camera);
this.intersects = this.mouseRay.intersectObjects(game.scene.children);
if(this.intersects.length > 0) {
for(var i = 0; i < this.intersects.length; i++) {
if(typeof this.intersects[i].object.userData.onmouseover === 'function') {
if(this.intersects[i].object.userData !== this.hovered) {
if(this.hovered !== null && typeof this.hovered.onmouseout === 'function') {
this.hovered.onmouseout();
}
this.hovered = this.intersects[i].object.userData;
this.hovered.onmouseover();
}
break;
}
}
}
else {
if(this.hovered !== null) {
if(typeof this.hovered.onmouseout === 'function') this.hovered.onmouseout();
this.hovered = null;
}
}
},
postUpdate: function() {
this.wasLeft = this.left;
this.wasRight = this.right;
this.wasMiddle = this.middle;
this.oldPosition.copy(this.position);
},
onMouseMove: function(event) {
this.position.x = (event.clientX / window.innerWidth) * 2 - 1;
this.position.y = -(event.clientY / window.innerHeight) * 2 + 1;
},
onMouseDown: function(event) {
if(event.button == 0) this.left = true;
else if(event.button == 1) this.middle = true;
else if(event.button == 2) this.right = true;
if(this.hovered !== null && typeof this.hovered.onmousedown === 'function') {
this.hovered.onmousedown(event);
}
},
onMouseUp: function(event) {
if(event.button == 0) this.left = false;
else if(event.button == 1) this.middle = false;
else if(event.button == 2) this.right = false;
if(this.hovered !== null && typeof this.hovered.onmouseup === 'function') {
this.hovered.onmouseup(event);
}
},
};