forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse.js
More file actions
442 lines (379 loc) · 15.6 KB
/
mouse.js
File metadata and controls
442 lines (379 loc) · 15.6 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
pc.extend(pc, function () {
/**
* @name pc.MouseEvent
* @class MouseEvent object that is passed to events 'mousemove', 'mouseup', 'mousedown' and 'mousewheel'.
* @description Create an new MouseEvent
* @param {pc.Mouse} mouse The Mouse device that is firing this event
* @param {MouseEvent} event The original browser event that fired
* @property {Number} x The x co-ordinate of the mouse pointer relative to the element pc.Mouse is attached to
* @property {Number} y The y co-ordinate of the mouse pointer relative to the element pc.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 {pc.MOUSEBUTTON} button The button
* @property {Number} wheel A value representing the amount the mouse wheel has moved, only valid for {@link mousemove} 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 {MouseEvent} event The original browser event
* @since 0.88.0
*/
var MouseEvent = function (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 (pc.Mouse.isPointerLocked()) {
this.x = 0;
this.y = 0;
} else {
return;
}
// FF uses 'detail' and returns a value in 'no. of lines' to scroll
// WebKit and Opera use 'wheelDelta', WebKit goes in multiples of 120 per wheel notch
if (event.detail) {
this.wheel = -1 * event.detail;
} else if (event.wheelDelta) {
this.wheel = event.wheelDelta / 120;
} else {
this.wheel = 0;
}
// Get the movement delta in this event
if (pc.Mouse.isPointerLocked()) {
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 = pc.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;
};
// Events Documentation
/**
* @event
* @name pc.Mouse#mousemove
* @description Fired when the mouse is moved
* @param {pc.MouseEvent} event The MouseEvent object
*/
/**
* @event
* @name pc.Mouse#mousedown
* @description Fired when a mouse button is pressed
* @param {pc.MouseEvent} event The MouseEvent object
*/
/**
* @event
* @name pc.Mouse#mouseup
* @description Fired when a mouse button is released
* @param {pc.MouseEvent} event The MouseEvent object
*/
/**
* @event
* @name pc.Mouse#mousewheel
* @description Fired when a mouse wheel is moved
* @param {pc.MouseEvent} event The MouseEvent object
*/
/**
* @name pc.Mouse
* @class A Mouse Device, bound to a DOM Element.
* @description Create a new Mouse device
* @param {Element} [element] The Element that the mouse events are attached to
*/
var Mouse = function (element) {
// Clear the mouse state
this._lastX = 0;
this._lastY = 0;
this._buttons = [false,false,false];
this._lastbuttons = [false, false, false];
// Setup event handlers so they are bound to the correct 'this'
this._upHandler = this._handleUp.bind(this);
this._downHandler = this._handleDown.bind(this);
this._moveHandler = this._handleMove.bind(this);
this._wheelHandler = this._handleWheel.bind(this);
this._contextMenuHandler = function (event) { event.preventDefault(); };
this._target = null;
this._attached = false;
this.attach(element);
// Add events
pc.events.attach(this);
};
/**
* @function
* @name pc.Mouse.isPointerLocked
* @description Check if the mouse pointer has been locked, using {@link pc.Mouse#enabledPointerLock}
* @returns {Boolean} True if locked
*/
Mouse.isPointerLocked = function () {
return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement);
};
Mouse.prototype = {
/**
* @function
* @name pc.Mouse#attach
* @description Attach mouse events to an Element.
* @param {Element} element
*/
attach: function (element) {
this._target = element;
if (this._attached) return;
this._attached = true;
window.addEventListener("mouseup", this._upHandler, false);
window.addEventListener("mousedown", this._downHandler, false);
window.addEventListener("mousemove", this._moveHandler, false);
window.addEventListener("mousewheel", this._wheelHandler, false); // WekKit
window.addEventListener("DOMMouseScroll", this._wheelHandler, false); // Gecko
},
/**
* @function
* @name pc.Mouse#detach
* @description Remove mouse events from the element that it is attached to
*/
detach: function () {
if (! this._attached) return;
this._attached = false;
window.removeEventListener("mouseup", this._upHandler);
window.removeEventListener("mousedown", this._downHandler);
window.removeEventListener("mousemove", this._moveHandler);
window.removeEventListener("mousewheel", this._wheelHandler); // WekKit
window.removeEventListener("DOMMouseScroll", this._wheelHandler); // Gecko
},
/**
* @function
* @name pc.Mouse#disableContextMenu
* @description Disable the context menu usually activated with right-click
*/
disableContextMenu: function () {
if (! this._target) return;
this._target.addEventListener("contextmenu", this._contextMenuHandler);
},
/**
* @function
* @name pc.Mouse#enableContextMenu
* @description Enable the context menu usually activated with right-click. This option is active by default.
*/
enableContextMenu: function () {
if (! this._target) return;
this._target.removeEventListener("contextmenu", this._contextMenuHandler);
},
/**
* @function
* @name pc.Mouse#enablePointerLock
* @description Request that the browser hides the mouse cursor and locks the mouse to the element.
* Allowing raw access to mouse movement input without risking the mouse exiting the element.
* Notes: <br />
* <ul>
* <li>In some browsers this will only work when the browser is running in fullscreen mode. See {@link pc.Application#enableFullscreen}
* <li>Enabling pointer lock can only be initiated by a user action e.g. in the event handler for a mouse or keyboard input.
* </ul>
* @param {Function} [success] Function called if the request for mouse lock is successful.
* @param {Function} [error] Function called if the request for mouse lock is unsuccessful.
*/
enablePointerLock: function (success, error) {
if (! document.body.requestPointerLock) {
if (error)
error();
return;
}
var s = function () {
success();
document.removeEventListener('pointerlockchange', s);
};
var e = function () {
error();
document.removeEventListener('pointerlockerror', e);
};
if (success) {
document.addEventListener('pointerlockchange', s, false);
}
if (error) {
document.addEventListener('pointerlockerror', e, false);
}
document.body.requestPointerLock();
},
/**
* @function
* @name pc.Mouse#disablePointerLock
* @description Return control of the mouse cursor to the user
* @param {Function} [success] Function called when the mouse lock is disabled
*/
disablePointerLock: function (success) {
if (! document.exitPointerLock) {
return;
}
var s = function () {
success();
document.removeEventListener('pointerlockchange', s);
};
if (success) {
document.addEventListener('pointerlockchange', s, false);
}
document.exitPointerLock();
},
/**
* @function
* @name pc.Mouse#update
* @description Update method, should be called once per frame
* @param {Object} dt
*/
update: function (dt) {
// Copy current button state
this._lastbuttons[0] = this._buttons[0];
this._lastbuttons[1] = this._buttons[1];
this._lastbuttons[2] = this._buttons[2];
},
/**
* @function
* @name pc.Mouse#isPressed
* @description Returns true if the mouse button is currently pressed
* @param {pc.MOUSEBUTTON} button
* @returns {Boolean} True if the mouse button is current pressed
*/
isPressed: function (button) {
return this._buttons[button];
},
/**
* @function
* @name pc.Mouse#wasPressed
* @description Returns true if the mouse button was pressed this frame (since the last call to update).
* @param {pc.MOUSEBUTTON} button
* @returns {Boolean} True if the mouse button was pressed since the last update
*/
wasPressed: function (button) {
return (this._buttons[button] && !this._lastbuttons[button]);
},
/**
* @function
* @name pc.Mouse#wasReleased
* @description Returns true if the mouse button was released this frame (since the last call to update).
* @param {pc.MOUSEBUTTON} button
* @returns {Boolean} True if the mouse button was released since the last update
*/
wasReleased: function (button) {
return (!this._buttons[button] && this._lastbuttons[button]);
},
_handleUp: function (event) {
// disable released button
this._buttons[event.button] = false;
var e = new MouseEvent(this, event);
if (! e.event) return;
// send 'mouseup' event
this.fire(pc.EVENT_MOUSEUP, e);
},
_handleDown: function (event) {
// Store which button has affected
this._buttons[event.button] = true;
var e = new MouseEvent(this, event);
if (! e.event) return;
this.fire(pc.EVENT_MOUSEDOWN, e);
},
_handleMove: function (event) {
var e = new MouseEvent(this, event);
if (! e.event) return;
this.fire(pc.EVENT_MOUSEMOVE, e);
// Store the last offset position to calculate deltas
this._lastX = e.x;
this._lastY = e.y;
},
_handleWheel: function (event) {
var e = new MouseEvent(this, event);
if (! e.event) return;
this.fire(pc.EVENT_MOUSEWHEEL, e);
},
_getTargetCoords: function (event) {
var rect = this._target.getBoundingClientRect();
var left = Math.floor(rect.left);
var top = Math.floor(rect.top);
// mouse is outside of canvas
if (event.clientX < left ||
event.clientX >= left + this._target.clientWidth ||
event.clientY < top ||
event.clientY >= top + this._target.clientHeight) {
return null;
}
return {
x: event.clientX - left,
y: event.clientY - top
};
}
};
// Apply PointerLock shims
(function () {
// Old API
if (typeof navigator === 'undefined' || typeof document === 'undefined') {
// Not running in a browser
return;
}
navigator.pointer = navigator.pointer || navigator.webkitPointer || navigator.mozPointer;
// Events
var pointerlockchange = function () {
var e = document.createEvent('CustomEvent');
e.initCustomEvent('pointerlockchange', true, false, null);
document.dispatchEvent(e);
};
var pointerlockerror = function () {
var e = document.createEvent('CustomEvent');
e.initCustomEvent('pointerlockerror', true, false, null);
document.dispatchEvent(e);
};
document.addEventListener('webkitpointerlockchange', pointerlockchange, false);
document.addEventListener('webkitpointerlocklost', pointerlockchange, false);
document.addEventListener('mozpointerlockchange', pointerlockchange, false);
document.addEventListener('mozpointerlocklost', pointerlockchange, false);
document.addEventListener('webkitpointerlockerror', pointerlockerror, false);
document.addEventListener('mozpointerlockerror', pointerlockerror, false);
// requestPointerLock
if (Element.prototype.mozRequestPointerLock) {
// FF requires a new function for some reason
Element.prototype.requestPointerLock = function () {
this.mozRequestPointerLock();
};
} else {
Element.prototype.requestPointerLock = Element.prototype.requestPointerLock || Element.prototype.webkitRequestPointerLock || Element.prototype.mozRequestPointerLock;
}
if (!Element.prototype.requestPointerLock && navigator.pointer) {
Element.prototype.requestPointerLock = function () {
var el = this;
document.pointerLockElement = el;
navigator.pointer.lock(el, pointerlockchange, pointerlockerror);
};
}
// exitPointerLock
document.exitPointerLock = document.exitPointerLock || document.webkitExitPointerLock || document.mozExitPointerLock;
if (!document.exitPointerLock) {
document.exitPointerLock = function () {
if (navigator.pointer) {
document.pointerLockElement = null;
navigator.pointer.unlock();
}
};
}
})();
// Public Interface
return {
Mouse: Mouse,
MouseEvent: MouseEvent
};
} ());