forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse-event.js
More file actions
96 lines (86 loc) · 3.59 KB
/
mouse-event.js
File metadata and controls
96 lines (86 loc) · 3.59 KB
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
import { MOUSEBUTTON_NONE } from './constants.js';
function isMousePointerLocked() {
return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement);
}
/**
* @class
* @name MouseEvent
* @classdesc MouseEvent object that is passed to events 'mousemove', 'mouseup', 'mousedown' and 'mousewheel'.
* @description Create an new MouseEvent.
* @param {Mouse} mouse - The Mouse device that is firing this event.
* @param {globalThis.MouseEvent} event - The original browser event that fired.
* @property {number} x The x co-ordinate of the mouse pointer relative to the element {@link Mouse} is attached to.
* @property {number} y The y co-ordinate of the mouse pointer relative to the element {@link Mouse} is attached to.
* @property {number} dx The change in x co-ordinate since the last mouse event.
* @property {number} dy The change in y co-ordinate since the last mouse event.
* @property {number} button The mouse button associated with this event. Can be:
*
* * {@link MOUSEBUTTON_LEFT}
* * {@link MOUSEBUTTON_MIDDLE}
* * {@link MOUSEBUTTON_RIGHT}
*
* @property {number} wheelDelta A value representing the amount the mouse wheel has moved, only
* valid for {@link mousewheel} events.
* @property {Element} element The element that the mouse was fired from.
* @property {boolean} ctrlKey True if the ctrl key was pressed when this event was fired.
* @property {boolean} shiftKey True if the shift key was pressed when this event was fired.
* @property {boolean} altKey True if the alt key was pressed when this event was fired.
* @property {boolean} metaKey True if the meta key was pressed when this event was fired.
* @property {globalThis.MouseEvent} event The original browser event.
*/
class MouseEvent {
constructor(mouse, event) {
var coords = {
x: 0,
y: 0
};
if (event) {
if (event instanceof MouseEvent) {
throw Error("Expected MouseEvent");
}
coords = mouse._getTargetCoords(event);
} else {
event = { };
}
if (coords) {
this.x = coords.x;
this.y = coords.y;
} else if (isMousePointerLocked()) {
this.x = 0;
this.y = 0;
} else {
return;
}
// deltaY is in a different range across different browsers. The only thing
// that is consistent is the sign of the value so snap to -1/+1.
this.wheelDelta = 0;
if (event.type === 'wheel') {
if (event.deltaY > 0) {
this.wheelDelta = 1;
} else if (event.deltaY < 0) {
this.wheelDelta = -1;
}
}
// Get the movement delta in this event
if (isMousePointerLocked()) {
this.dx = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
this.dy = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
} else {
this.dx = this.x - mouse._lastX;
this.dy = this.y - mouse._lastY;
}
if (event.type === 'mousedown' || event.type === 'mouseup') {
this.button = event.button;
} else {
this.button = MOUSEBUTTON_NONE;
}
this.buttons = mouse._buttons.slice(0);
this.element = event.target;
this.ctrlKey = event.ctrlKey || false;
this.altKey = event.altKey || false;
this.shiftKey = event.shiftKey || false;
this.metaKey = event.metaKey || false;
this.event = event;
}
}
export { isMousePointerLocked, MouseEvent };