forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement-input.js
More file actions
638 lines (527 loc) · 23.5 KB
/
element-input.js
File metadata and controls
638 lines (527 loc) · 23.5 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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
pc.extend(pc, function () {
var targetX, targetY;
var vecA = new pc.Vec3();
var vecB = new pc.Vec3();
var _pq = new pc.Vec3();
var _pa = new pc.Vec3();
var _pb = new pc.Vec3();
var _pc = new pc.Vec3();
var _pd = new pc.Vec3();
var _m = new pc.Vec3();
var _sct = new pc.Vec3();
// Given line pq and ccw corners of a quad, return whether the line
// intersects it. (from Real-Time Collision Detection book)
var intersectLineQuad = function (p, q, corners) {
_pq.sub2(q, p);
_pa.sub2(corners[0], p);
_pb.sub2(corners[1], p);
_pc.sub2(corners[2], p);
// Determine which triangle to test against by testing against diagonal first
_m.cross(_pc, _pq);
var v = _pa.dot(_m);
if (v >= 0) {
// Test intersection against triangle abc
if (-_pb.dot(_m) < 0)
return false;
if (scalarTriple(_pq, _pb, _pa) < 0)
return false;
} else {
// Test intersection against triangle dac
_pd.sub2(corners[3], p);
if (_pd.dot(_m) < 0)
return false;
if (scalarTriple(_pq, _pa, _pd) < 0)
return false;
}
return true;
};
// pi x p2 * p3
var scalarTriple = function (p1, p2, p3) {
return _sct.cross(p1, p2).dot(p3);
};
/**
* @name pc.ElementInputEvent
* @class Represents an input event fired on a {@link pc.ElementComponent}. When an event is raised on an ElementComponent it bubbles up to its parent ElementComponents unless we call stopPropagation().
* @description Create an instance of a pc.ElementInputEvent.
* @param {MouseEvent|TouchEvent} event The MouseEvent or TouchEvent that was originally raised.
* @property {MouseEvent|TouchEvent} event The MouseEvent or TouchEvent that was originally raised.
* @property {pc.ElementComponent} element The ElementComponent that this event was originally raised on.
*/
var ElementInputEvent = function (event, element) {
this.event = event;
this.element = element;
this._stopPropagation = false;
};
ElementInputEvent.prototype = {
/**
* @function
* @name pc.ElementInputEvent#stopPropagation
* @description Stop propagation of the event to parent {@link pc.ElementComponent}s. This also stops propagation of the event to other event listeners of the original DOM Event.
*/
stopPropagation: function () {
this._stopPropagation = true;
this.event.stopImmediatePropagation();
this.event.stopPropagation();
}
};
/**
* @name pc.ElementMouseEvent
* @class Represents a Mouse event fired on a {@link pc.ElementComponent}.
* @extends pc.ElementInputEvent
* @description Create an instance of a pc.ElementMouseEvent.
* @param {MouseEvent} event The MouseEvent that was originally raised.
* @param {pc.ElementComponent} element The ElementComponent that this event was originally raised on.
* @param {Number} x The x coordinate
* @param {Number} y The y coordinate
* @param {Number} lastX The last x coordinate
* @param {Number} lastY The last y coordinate
* @property {Boolean} ctrlKey Whether the ctrl key was pressed
* @property {Boolean} altKey Whether the alt key was pressed
* @property {Boolean} shiftKey Whether the shift key was pressed
* @property {Boolean} metaKey Whether the meta key was pressed
* @property {Number} button The mouse button
* @property {Number} dx The amount of horizontal movement of the cursor
* @property {Number} dy The amount of vertical movement of the cursor
* @property {Number} wheel The amount of the wheel movement
*/
var ElementMouseEvent = function (event, element, x, y, lastX, lastY) {
this.x = x;
this.y = y;
this.ctrlKey = event.ctrlKey || false;
this.altKey = event.altKey || false;
this.shiftKey = event.shiftKey || false;
this.metaKey = event.metaKey || false;
this.button = event.button;
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 = x - lastX;
this.dy = y - lastY;
}
// 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;
}
};
ElementMouseEvent = pc.inherits(ElementMouseEvent, ElementInputEvent);
/**
* @name pc.ElementTouchEvent
* @class Represents a TouchEvent fired on a {@link pc.ElementComponent}.
* @extends pc.ElementInputEvent
* @description Create an instance of a pc.ElementTouchEvent.
* @param {TouchEvent} event The TouchEvent that was originally raised.
* @param {pc.ElementComponent} element The ElementComponent that this event was originally raised on.
* @param {pc.ElementInput} input The pc.ElementInput instance
* @property {Touch[]} touches The Touch objects representing all current points of contact with the surface, regardless of target or changed status.
* @property {Touch[]} changedTouches The Touch objects representing individual points of contact whose states changed between the previous touch event and this one.
*/
var ElementTouchEvent = function (event, element, input) {
this.touches = event.touches;
this.changedTouches = event.changedTouches;
};
ElementTouchEvent = pc.inherits(ElementTouchEvent, ElementInputEvent);
/**
* @name pc.ElementInput
* @class Handles mouse and touch events for {@link pc.ElementComponent}s. When input events
* occur on an ElementComponent this fires the appropriate events on the ElementComponent.
* @description Create a new pc.ElementInput instance.
* @param {Element} domElement The DOM element
*/
var ElementInput = function (domElement) {
this._app = null;
this._attached = false;
this._target = null;
this._lastX = 0;
this._lastY = 0;
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._touchstartHandler = this._handleTouchStart.bind(this);
this._touchendHandler = this._handleTouchEnd.bind(this);
this._touchcancelHandler = this._touchendHandler;
this._touchmoveHandler = this._handleTouchMove.bind(this);
this._elements = [];
this._hoveredElement = null;
this._pressedElement = null;
this._touchedElements = {};
if ('ontouchstart' in window) {
this._clickedEntities = {};
}
this.attach(domElement);
};
ElementInput.prototype = {
/**
* @function
* @name pc.ElementInput#attach
* @description Attach mouse and touch events to a DOM element.
* @param {Element} domElement The DOM element
*/
attach: function (domElement) {
if (this._attached) {
this._attached = false;
this.detach();
}
this._target = domElement;
this._attached = true;
window.addEventListener('mouseup', this._upHandler, {passive: true});
window.addEventListener('mousedown', this._downHandler, {passive: true});
window.addEventListener('mousemove', this._moveHandler, {passive: true});
window.addEventListener('mousewheel', this._wheelHandler, {passive: true});
window.addEventListener('DOMMouseScroll', this._wheelHandler, {passive: true});
if ('ontouchstart' in window) {
this._target.addEventListener('touchstart', this._touchstartHandler, {passive: true});
this._target.addEventListener('touchend', this._touchendHandler, {passive: true});
this._target.addEventListener('touchmove', this._touchmoveHandler, false);
this._target.addEventListener('touchcancel', this._touchcancelHandler, {passive: true});
}
},
/**
* @function
* @name pc.ElementInput#detach
* @description Remove mouse and touch events from the DOM element that it is attached to
*/
detach: function () {
if (! this._attached) return;
this._attached = false;
this._target = null;
window.removeEventListener('mouseup', this._upHandler, false);
window.removeEventListener('mousedown', this._downHandler, false);
window.removeEventListener('mousemove', this._moveHandler, false);
window.removeEventListener('mousewheel', this._wheelHandler, false);
window.removeEventListener('DOMMouseScroll', this._wheelHandler, false);
this._target.removeEventListener('touchstart', this._touchstartHandler, false);
this._target.removeEventListener('touchend', this._touchendHandler, false);
this._target.removeEventListener('touchmove', this._touchmoveHandler, false);
this._target.removeEventListener('touchcancel', this._touchcancelHandler, false);
},
/**
* @function
* @name pc.ElementInput#addElement
* @description Add a {@link pc.ElementComponent} to the internal list of ElementComponents that are being checked for input.
* @param {pc.ElementComponent} element The ElementComponent
*/
addElement: function (element) {
if (this._elements.indexOf(element) === -1)
this._elements.push(element);
},
/**
* @function
* @name pc.ElementInput#removeElement
* @description Remove a {@link pc.ElementComponent} from the internal list of ElementComponents that are being checked for input.
* @param {pc.ElementComponent} element The ElementComponent
*/
removeElement: function (element) {
var idx = this._elements.indexOf(element);
if (idx !== -1)
this._elements.splice(idx, 1);
},
_handleUp: function (event) {
if (pc.Mouse.isPointerLocked())
return;
this._calcMouseCoords(event);
if (targetX === null)
return;
this._onElementMouseEvent(event);
},
_handleDown: function (event) {
if (pc.Mouse.isPointerLocked())
return;
this._calcMouseCoords(event);
if (targetX === null)
return;
this._onElementMouseEvent(event);
},
_handleMove: function (event) {
this._calcMouseCoords(event);
if (targetX === null)
return;
this._onElementMouseEvent(event);
this._lastX = targetX;
this._lastY = targetY;
},
_handleWheel: function (event) {
this._calcMouseCoords(event);
if (targetX === null)
return;
this._onElementMouseEvent(event);
},
_handleTouchStart: function (event) {
var cameras = this.app.systems.camera.cameras;
// check cameras from last to front
// so that elements that are drawn above others
// receive events first
for (var i = cameras.length - 1; i >= 0; i--) {
var camera = cameras[i];
var done = 0;
for (var j = 0, len = event.changedTouches.length; j < len; j++) {
if (this._touchedElements[event.changedTouches[j].identifier]) {
done++;
continue;
}
var coords = this._calcTouchCoords(event.changedTouches[j]);
var element = this._getTargetElement(camera, coords.x, coords.y);
if (element) {
done++;
this._touchedElements[event.changedTouches[j].identifier] = element;
this._fireEvent(event.type, new ElementTouchEvent(event, element, this));
}
}
if (done === len) {
break;
}
}
},
_handleTouchEnd: function (event) {
var cameras = this.app.systems.camera.cameras;
// clear clicked entities first then store each clicked entity
// in _clickedEntities so that we don't fire another click
// on it in this handler or in the mouseup handler which is
// fired later
for (var key in this._clickedEntities) {
delete this._clickedEntities[key];
}
for (var i = 0, len = event.changedTouches.length; i < len; i++) {
var touch = event.changedTouches[i];
var element = this._touchedElements[touch.identifier];
if (! element)
continue;
delete this._touchedElements[touch.identifier];
this._fireEvent(event.type, new ElementTouchEvent(event, element, this));
// check if touch was released over previously touch
// element in order to fire click event
if (event.touches.length === 0) {
var coords = this._calcTouchCoords(touch);
for (var c = cameras.length - 1; c >= 0; c--) {
var hovered = this._getTargetElement(cameras[c], coords.x, coords.y);
if (hovered === element) {
if (! this._clickedEntities[element.entity.getGuid()]) {
this._fireEvent('click', new ElementTouchEvent(event, element, this));
this._clickedEntities[element.entity.getGuid()] = true;
}
}
}
}
}
},
_handleTouchMove: function (event) {
// call preventDefault to avoid issues in Chrome Android:
// http://wilsonpage.co.uk/touch-events-in-chrome-android/
event.preventDefault();
for (var i = 0, len = event.changedTouches.length; i < len; i++) {
var element = this._touchedElements[event.changedTouches[i].identifier];
if (element) {
this._fireEvent(event.type, new ElementTouchEvent(event, element, this));
}
}
},
_onElementMouseEvent: function (event) {
var element;
var hovered = this._hoveredElement;
this._hoveredElement = null;
var cameras = this.app.systems.camera.cameras;
// check cameras from last to front
// so that elements that are drawn above others
// receive events first
for (var i = cameras.length - 1; i >= 0; i--) {
var camera = cameras[i];
element = this._getTargetElement(camera, targetX, targetY);
if (element)
break;
}
// fire mouse event
if (element) {
this._fireEvent(event.type, new ElementMouseEvent(event, element, targetX, targetY, this._lastX, this._lastY));
this._hoveredElement = element;
if (event.type === pc.EVENT_MOUSEDOWN) {
this._pressedElement = element;
}
}
if (hovered !== this._hoveredElement) {
// mouseleave event
if (hovered) {
this._fireEvent('mouseleave', new ElementMouseEvent(event, hovered, targetX, targetY, this._lastX, this._lastY));
}
// mouseenter event
if (this._hoveredElement) {
this._fireEvent('mouseenter', new ElementMouseEvent(event, this._hoveredElement, targetX, targetY, this._lastX, this._lastY));
}
}
if (event.type === pc.EVENT_MOUSEUP && this._pressedElement) {
// click event
if (this._pressedElement === this._hoveredElement) {
this._pressedElement = null;
// fire click event if it hasn't been fired already by the touchup handler
if (!this._clickedEntities || !this._clickedEntities[this._hoveredElement.entity.getGuid()]) {
this._fireEvent('click', new ElementMouseEvent(event, this._hoveredElement, targetX, targetY, this._lastX, this._lastY));
}
} else {
this._pressedElement = null;
}
}
},
_fireEvent: function (name, evt) {
var element = evt.element;
while (true) {
element.fire(name, evt);
if (evt._stopPropagation)
break;
if (! element.entity.parent)
break;
element = element.entity.parent.element;
if (! element)
break;
}
},
_calcMouseCoords: 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) {
targetX = null;
targetY = null;
} else {
// calculate coords and scale them to the graphicsDevice size
targetX = (event.clientX - left);
targetY = (event.clientY - top);
}
},
_calcTouchCoords: function (touch) {
var totalOffsetX = 0;
var totalOffsetY = 0;
var target = touch.target;
while (!(target instanceof HTMLElement)) {
target = target.parentNode;
}
var currentElement = target;
do {
totalOffsetX += currentElement.offsetLeft - currentElement.scrollLeft;
totalOffsetY += currentElement.offsetTop - currentElement.scrollTop;
currentElement = currentElement.offsetParent;
} while (currentElement);
// calculate coords and scale them to the graphicsDevice size
return {
x: (touch.pageX - totalOffsetX),
y: (touch.pageY - totalOffsetY)
};
},
_sortElements: function (a, b) {
if (a.screen && ! b.screen)
return -1;
if (!a.screen && b.screen)
return 1;
if (! a.screen && ! b.screen)
return 0;
if (a.screen.screen.screenSpace && ! b.screen.screen.screenSpace)
return -1;
if (b.screen.screen.screenSpace && ! a.screen.screen.screenSpace)
return 1;
return b.drawOrder - a.drawOrder;
},
_getTargetElement: function (camera, x, y) {
var result = null;
// sort elements
this._elements.sort(this._sortElements);
for (var i = 0, len = this._elements.length; i < len; i++) {
var element = this._elements[i];
// scale x, y based on the camera's rect
if (element.screen && element.screen.screen.screenSpace) {
// 2D screen
if (this._checkElement2d(x, y, element, camera)) {
result = element;
break;
}
} else {
// 3d
if (this._checkElement3d(x, y, element, camera)) {
result = element;
break;
}
}
}
return result;
},
_checkElement2d: function (x, y, element, camera) {
var sw = this.app.graphicsDevice.width;
var sh = this.app.graphicsDevice.height;
var cameraWidth = camera.rect.z * sw;
var cameraHeight = camera.rect.w * sh;
var cameraLeft = camera.rect.x * sw;
var cameraRight = cameraLeft + cameraWidth;
// camera bottom (origin is bottom left of window)
var cameraBottom = (1 - camera.rect.y) * sh;
var cameraTop = cameraBottom - cameraHeight;
var _x = x * sw / this._target.clientWidth;
var _y = y * sh / this._target.clientHeight;
// check window coords are within camera rect
if (_x >= cameraLeft && _x <= cameraRight &&
_y <= cameraBottom && _y >= cameraTop) {
// limit window coords to camera rect coords
_x = sw * (_x - cameraLeft) / cameraWidth;
_y = sh * (_y - cameraTop) / cameraHeight;
// reverse _y
_y = sh - _y;
var screenCorners = element.screenCorners;
vecA.set(_x, _y, 1);
vecB.set(_x, _y, -1);
if (intersectLineQuad(vecA, vecB, screenCorners)) {
return true;
}
}
return false;
},
_checkElement3d: function (x, y, element, camera) {
var sw = this._target.clientWidth;
var sh = this._target.clientHeight;
var cameraWidth = camera.rect.z * sw;
var cameraHeight = camera.rect.w * sh;
var cameraLeft = camera.rect.x * sw;
var cameraRight = cameraLeft + cameraWidth;
// camera bottom - origin is bottom left of window
var cameraBottom = (1 - camera.rect.y) * sh;
var cameraTop = cameraBottom - cameraHeight;
var _x = x;
var _y = y;
// check window coords are within camera rect
if (x >= cameraLeft && x <= cameraRight &&
y <= cameraBottom && _y >= cameraTop) {
// limit window coords to camera rect coords
_x = sw * (_x - cameraLeft) / cameraWidth;
_y = sh * (_y - (cameraTop)) / cameraHeight;
// 3D screen
var worldCorners = element.worldCorners;
var start = camera.entity.getPosition();
var end = vecA;
camera.screenToWorld(_x, _y, camera.farClip, end);
if (intersectLineQuad(start, end, worldCorners)) {
return true;
}
}
return false;
}
};
Object.defineProperty(ElementInput.prototype, 'app', {
get: function () {
return this._app || pc.app;
},
set: function (value) {
this._app = value;
}
});
return {
ElementInput: ElementInput,
ElementInputEvent: ElementInputEvent,
ElementMouseEvent: ElementMouseEvent,
ElementTouchEvent: ElementTouchEvent
};
}());