@openglobus/og 0.13.1 → 0.13.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1051 +1,1051 @@
1
- /**
2
- * @module og/renderer/RendererEvents
3
- */
4
-
5
- "use strict";
6
-
7
- import { Events } from "../Events.js";
8
- import { input } from "../input/input.js";
9
- import { KeyboardHandler } from "../input/KeyboardHandler.js";
10
- import { MouseHandler } from "../input/MouseHandler.js";
11
- import { TouchHandler } from "../input/TouchHandler.js";
12
- import { Vec2 } from "../math/Vec2.js";
13
- import { Vec3 } from "../math/Vec3.js";
14
-
15
- const LB_M = 0b0001;
16
- const RB_M = 0b0010;
17
- const MB_M = 0b0100;
18
-
19
- /**
20
- * Renderer events handler.
21
- * @class
22
- * @param {Renderer} renderer - Renderer object, events that works for.
23
- */
24
- class RendererEvents extends Events {
25
- constructor(renderer) {
26
- super(EVENT_NAMES);
27
-
28
- /**
29
- * Assigned renderer.
30
- * @public
31
- * @type {Renderer}
32
- */
33
- this.renderer = renderer;
34
-
35
- /**
36
- * Low level touch events handler.
37
- * @private
38
- * @type {input.TouchHandler}
39
- */
40
- this._touchHandler = new TouchHandler(renderer.handler.canvas);
41
-
42
- /**
43
- * Low level mouse events handler.
44
- * @private
45
- * @type {input.MouseHandler}
46
- */
47
- this._mouseHandler = new MouseHandler(renderer.handler.canvas);
48
-
49
- /**
50
- * Low level keyboard events handler.
51
- * @private
52
- * @type {input.KeyboardHandler}
53
- */
54
- this._keyboardHandler = new KeyboardHandler();
55
-
56
- this._active = true;
57
-
58
- this.clickRadius = 15;
59
-
60
- /**
61
- * Current mouse state.
62
- * @public
63
- * @enum {Object}
64
- */
65
- this.mouseState = {
66
- /** Current screen mouse X position. */
67
- clientX: 0,
68
- /** Current screen mouse Y position. */
69
- clientY: 0,
70
- /** Current viewport mouse X position. */
71
- x: 0,
72
- /** Current viewport mouse Y position. */
73
- y: 0,
74
- /** Current mouse X position from 0 to 1 */
75
- nx: 0,
76
- /** Current mouse Y position from 0 to 1 */
77
- ny: 0,
78
- /** Previous mouse X position. */
79
- prev_x: 0,
80
- /** Previous mouse Y position. */
81
- prev_y: 0,
82
- /** Screen mouse position world direction. */
83
- direction: new Vec3(),
84
- /** Left mouse button has stopped pushing down right now.*/
85
- leftButtonUp: false,
86
- /** Right mouse button has stopped pushing down right now.*/
87
- rightButtonUp: false,
88
- /** Middle mouse button has stopped pushing down right now.*/
89
- middleButtonUp: false,
90
- /** Left mouse button has pushed now.*/
91
- leftButtonDown: false,
92
- /** Right mouse button has pushed now.*/
93
- rightButtonDown: false,
94
- /** Middle mouse button has pushed now.*/
95
- middleButtonDown: false,
96
- /** Left mouse button is pushing.*/
97
- leftButtonHold: false,
98
- /** Right mouse button is pushing.*/
99
- rightButtonHold: false,
100
- /** Middle mouse button is pushing.*/
101
- middleButtonHold: false,
102
- /** Left mouse button has clicked twice now.*/
103
- leftButtonDoubleClick: false,
104
- /** Right mouse button has clicked twice now.*/
105
- rightButtonDoubleClick: false,
106
- /** Middle mouse button has clicked twice now.*/
107
- middleButtonDoubleClick: false,
108
- /** Left mouse button has clicked now. */
109
- leftButtonClick: false,
110
- /** Right mouse button has clicked now. */
111
- rightButtonClick: false,
112
- /** Middle mouse button has clicked now. */
113
- middleButtonClick: false,
114
- /** Mouse is moving now. */
115
- moving: false,
116
- /** Mouse has just stopped now. */
117
- justStopped: false,
118
- /** Mose double click delay response time.*/
119
- doubleClickDelay: 500,
120
- /** Mose click delay response time.*/
121
- clickDelay: 200,
122
- /** Mouse wheel. */
123
- wheelDelta: 0,
124
- /** JavaScript mouse system event message. */
125
- sys: null,
126
- /** Current picking object. */
127
- pickingObject: null,
128
- /** Renderer instanve. */
129
- renderer: renderer
130
- };
131
-
132
- /**
133
- * Current touch state.
134
- * @public
135
- * @enum {Object}
136
- */
137
- this.touchState = {
138
- /** Touching is moving now. */
139
- moving: false,
140
- /** Touch has ended right now.*/
141
- touchEnd: false,
142
- /** Touch has started right now.*/
143
- touchStart: false,
144
- /** Touch canceled.*/
145
- touchCancel: false,
146
- /** Touched twice.*/
147
- doubleTouch: false,
148
- /** Double touching responce delay.*/
149
- doubleTouchDelay: 550,
150
- /** Double touching responce radius in screen pixels.*/
151
- doubleTouchRadius: 10,
152
- /** Current touch X - coordinate. */
153
- x: 0,
154
- /** Current touch Y - coordinate. */
155
- y: 0,
156
- /** Current touch X - coordinate from 0 to 1 */
157
- nx: 0,
158
- /** Current touch Y - coordinate from 0 to 1 */
159
- ny: 0,
160
- /** Previous touch X coordinate. */
161
- prev_x: 0,
162
- /** Previous touch Y coordinate. */
163
- prev_y: 0,
164
- /** JavaScript touching system event message. */
165
- sys: null,
166
- /** Current touched(picking) object. */
167
- pickingObject: null,
168
- /** Renderer instanve. */
169
- renderer: renderer
170
- };
171
-
172
- this._dblTchCoords = new Vec2();
173
- this._oneTouchStart = false;
174
- this._dblTchBegins = 0;
175
- /**
176
- * @type {number}
177
- */
178
- this._mousestopThread = null;
179
- this._ldblClkBegins = 0;
180
- this._rdblClkBegins = 0;
181
- this._mdblClkBegins = 0;
182
- this._lClkBegins = 0;
183
- this._rClkBegins = 0;
184
- this._mClkBegins = 0;
185
- this._lclickX = 0;
186
- this._lclickY = 0;
187
- this._rclickX = 0;
188
- this._rclickY = 0;
189
- this._mclickX = 0;
190
- this._mclickY = 0;
191
- }
192
-
193
- pointerEvent() {
194
- let ms = this.mouseState,
195
- ts = this.touchState;
196
- return (
197
- ms.moving ||
198
- ms.justStopped ||
199
- ms.wheelDelta ||
200
- ts.moving ||
201
- ts.touchStart ||
202
- ts.touchEnd
203
- )
204
- }
205
-
206
- get active() {
207
- return this._active;
208
- }
209
-
210
- set active(act) {
211
- this._active = act;
212
- this._keyboardHandler.setActivity(act);
213
- }
214
-
215
- /**
216
- * Used in render node frame.
217
- * @public
218
- */
219
- handleEvents() {
220
- if (this._active) {
221
- this.mouseState.direction = this.renderer.activeCamera.unproject(
222
- this.mouseState.x,
223
- this.mouseState.y
224
- );
225
- this.entityPickingEvents();
226
- this._keyboardHandler.handleEvents();
227
- this.handleMouseEvents();
228
- this.handleTouchEvents();
229
- }
230
- }
231
-
232
- /**
233
- * Set render event callback.
234
- * @public
235
- * @param {string} name - Event name
236
- * @param {eventCallback} callback - Callback function
237
- * @param {number} [key] - Key code from og.input
238
- * @param {*} sender - Callback context
239
- * @param {number} [priority] - Event callback priority
240
- */
241
- on(name, p0, p1, p2, p3) {
242
- if (name === "keypress" || name === "charkeypress" || name === "keyfree") {
243
- this._keyboardHandler.addEvent(name, p2, p1, p0, p3);
244
- } else {
245
- super.on(name, p0, p1, p2);
246
- }
247
- }
248
-
249
- /**
250
- * TODO: DOESNT WORK!!!
251
- * @param {any} name
252
- * @param {any} callback
253
- */
254
- off(name, callback) {
255
- if (name === "keypress" || name === "charkeypress" || name === "keyfree") {
256
- this._keyboardHandler.removeEvent(name, callback);
257
- } else {
258
- super.off(name, callback);
259
- }
260
- }
261
-
262
- /**
263
- * Check key is pressed.
264
- * @public
265
- * @param {number} keyCode - Key code
266
- * @return {boolean}
267
- */
268
- isKeyPressed(keyCode) {
269
- return this._keyboardHandler.isKeyPressed(keyCode);
270
- }
271
-
272
- releaseKeys() {
273
- this._keyboardHandler.releaseKeys();
274
- }
275
-
276
- /**
277
- * Renderer events initialization.
278
- * @public
279
- */
280
- initialize() {
281
- this._mouseHandler.setEvent("mouseup", this, this.onMouseUp);
282
- this._mouseHandler.setEvent("mousemove", this, this.onMouseMove);
283
- this._mouseHandler.setEvent("mousedown", this, this.onMouseDown);
284
- this._mouseHandler.setEvent("mousewheel", this, this.onMouseWheel);
285
- this._mouseHandler.setEvent("mouseleave", this, this.onMouseLeave);
286
- this._mouseHandler.setEvent("mouseenter", this, this.onMouseEnter);
287
-
288
- this._touchHandler.setEvent("touchstart", this, this.onTouchStart);
289
- this._touchHandler.setEvent("touchend", this, this.onTouchEnd);
290
- this._touchHandler.setEvent("touchcancel", this, this.onTouchCancel);
291
- this._touchHandler.setEvent("touchmove", this, this.onTouchMove);
292
- }
293
-
294
- /**
295
- * @private
296
- */
297
- onMouseWheel(event) {
298
- this.mouseState.wheelDelta = event.wheelDelta;
299
- }
300
-
301
- /**
302
- * @protected
303
- */
304
- updateButtonsStates(buttons) {
305
- var ms = this.mouseState;
306
- if (buttons & LB_M) {
307
- ms.leftButtonDown = true;
308
- } else {
309
- ms.leftButtonHold = false;
310
- ms.leftButtonDown = false;
311
- }
312
-
313
- if (buttons & RB_M) {
314
- ms.rightButtonDown = true;
315
- } else {
316
- ms.rightButtonHold = false;
317
- ms.rightButtonDown = false;
318
- }
319
-
320
- if (buttons & MB_M) {
321
- ms.middleButtonDown = true;
322
- } else {
323
- ms.middleButtonHold = false;
324
- ms.middleButtonDown = false;
325
- }
326
- }
327
-
328
- /**
329
- * @private
330
- */
331
- onMouseMove(event, sys) {
332
- var ms = this.mouseState;
333
- this.updateButtonsStates(sys.buttons);
334
- ms.sys = event;
335
-
336
- let ex = event.clientX,
337
- ey = event.clientY,
338
- r = this.clickRadius;
339
-
340
- if (Math.abs(this._lclickX - ex) >= r && Math.abs(this._lclickY - ey) >= r) {
341
- this._ldblClkBegins = 0;
342
- this._lClkBegins = 0;
343
- }
344
-
345
- if (Math.abs(this._rclickX - ex) >= r && Math.abs(this._rclickY - ey) >= r) {
346
- this._rdblClkBegins = 0;
347
- this._rClkBegins = 0;
348
- }
349
-
350
- if (Math.abs(this._mclickX - ex) >= r && Math.abs(this._mclickY - ey) >= r) {
351
- this._mdblClkBegins = 0;
352
- this._mClkBegins = 0;
353
- }
354
-
355
- if (ms.clientX === event.clientX && ms.clientY === event.clientY) {
356
- return;
357
- }
358
-
359
- ms.clientX = event.clientX;
360
- ms.clientY = event.clientY;
361
-
362
- let h = this.renderer.handler;
363
-
364
- ms.x = event.clientX * h.pixelRatio;
365
- ms.y = event.clientY * h.pixelRatio;
366
-
367
- ms.nx = ms.x / h.canvas.width;
368
- ms.ny = ms.y / h.canvas.height;
369
-
370
- ms.moving = true;
371
-
372
- //dispatch stop mouse event
373
- clearTimeout(this._mousestopThread);
374
- this._mousestopThread = setTimeout(function () {
375
- ms.justStopped = true;
376
- }, 100);
377
- }
378
-
379
- onMouseLeave(event) {
380
- this.dispatch(this.mouseleave, event);
381
- }
382
-
383
- onMouseEnter(event) {
384
- this.dispatch(this.mouseenter, event);
385
- }
386
-
387
- /**
388
- * @private
389
- */
390
- onMouseDown(event) {
391
- if (event.button === input.MB_LEFT) {
392
- this._lClkBegins = window.performance.now();
393
- this._lclickX = event.clientX;
394
- this._lclickY = event.clientY;
395
- this.mouseState.sys = event;
396
- this.mouseState.leftButtonDown = true;
397
- } else if (event.button === input.MB_RIGHT) {
398
- this._rClkBegins = window.performance.now();
399
- this._rclickX = event.clientX;
400
- this._rclickY = event.clientY;
401
- this.mouseState.sys = event;
402
- this.mouseState.rightButtonDown = true;
403
- } else if (event.button === input.MB_MIDDLE) {
404
- this._mClkBegins = window.performance.now();
405
- this._mclickX = event.clientX;
406
- this._mclickY = event.clientY;
407
- this.mouseState.sys = event;
408
- this.mouseState.middleButtonDown = true;
409
- }
410
- }
411
-
412
- /**
413
- * @private
414
- */
415
- onMouseUp(event) {
416
- var ms = this.mouseState;
417
- ms.sys = event;
418
- var t = window.performance.now();
419
-
420
- if (event.button === input.MB_LEFT) {
421
- ms.leftButtonDown = false;
422
- ms.leftButtonUp = true;
423
-
424
- if (
425
- Math.abs(this._lclickX - event.clientX) < this.clickRadius &&
426
- Math.abs(this._lclickY - event.clientY) < this.clickRadius &&
427
- t - this._lClkBegins <= ms.clickDelay
428
- ) {
429
- if (this._ldblClkBegins) {
430
- let deltatime = window.performance.now() - this._ldblClkBegins;
431
- if (deltatime <= ms.doubleClickDelay) {
432
- ms.leftButtonDoubleClick = true;
433
- }
434
- this._ldblClkBegins = 0;
435
- } else {
436
- this._ldblClkBegins = window.performance.now();
437
- }
438
-
439
- ms.leftButtonClick = true;
440
- this._lClkBegins = 0;
441
- }
442
- } else if (event.button === input.MB_RIGHT) {
443
- ms.rightButtonDown = false;
444
- ms.rightButtonUp = true;
445
-
446
- if (
447
- Math.abs(this._rclickX - event.clientX) < this.clickRadius &&
448
- Math.abs(this._rclickY - event.clientY) < this.clickRadius &&
449
- t - this._rClkBegins <= ms.clickDelay
450
- ) {
451
- if (this._rdblClkBegins) {
452
- let deltatime = window.performance.now() - this._rdblClkBegins;
453
- if (deltatime <= ms.doubleClickDelay) {
454
- ms.rightButtonDoubleClick = true;
455
- }
456
- this._rdblClkBegins = 0;
457
- } else {
458
- this._rdblClkBegins = window.performance.now();
459
- }
460
-
461
- ms.rightButtonClick = true;
462
- this._rClkBegins = 0;
463
- }
464
- } else if (event.button === input.MB_MIDDLE) {
465
- ms.middleButtonDown = false;
466
- ms.middleButtonUp = true;
467
-
468
- if (
469
- Math.abs(this._mclickX - event.clientX) < this.clickRadius &&
470
- Math.abs(this._mclickY - event.clientY) < this.clickRadius &&
471
- t - this._mClkBegins <= ms.clickDelay
472
- ) {
473
- if (this._mdblClkBegins) {
474
- var deltatime = window.performance.now() - this._mdblClkBegins;
475
- if (deltatime <= ms.doubleClickDelay) {
476
- ms.middleButtonDoubleClick = true;
477
- }
478
- this._mdblClkBegins = 0;
479
- } else {
480
- this._mdblClkBegins = window.performance.now();
481
- }
482
-
483
- ms.middleButtonClick = true;
484
- this._mClkBegins = 0;
485
- }
486
- }
487
- }
488
-
489
- /**
490
- * @private
491
- */
492
- onTouchStart(event) {
493
- var ts = this.touchState;
494
- ts.sys = event;
495
-
496
- ts.clienX = event.touches.item(0).clientX - event.offsetLeft;
497
- ts.clientY = event.touches.item(0).clientY - event.offsetTop;
498
-
499
- let h = this.renderer.handler;
500
-
501
- ts.x = ts.clientX * h.pixelRatio;
502
- ts.y = ts.clientY * h.pixelRatio;
503
-
504
- ts.nx = ts.x / h.canvas.width;
505
- ts.ny = ts.y / h.canvas.height;
506
- ts.prev_x = ts.x;
507
- ts.prev_y = ts.y;
508
- ts.touchStart = true;
509
-
510
- if (event.touches.length === 1) {
511
- this._dblTchCoords.x = ts.x;
512
- this._dblTchCoords.y = ts.y;
513
- this._oneTouchStart = true;
514
- } else {
515
- this._oneTouchStart = false;
516
- }
517
- }
518
-
519
- /**
520
- * @private
521
- */
522
- onTouchEnd(event) {
523
- var ts = this.touchState;
524
- ts.sys = event;
525
- ts.touchEnd = true;
526
-
527
- if (event.touches.length === 0) {
528
- ts.prev_x = ts.x;
529
- ts.prev_y = ts.y;
530
-
531
- if (this._oneTouchStart) {
532
- if (this._dblTchBegins) {
533
- var deltatime = window.performance.now() - this._dblTchBegins;
534
- if (deltatime <= ts.doubleTouchDelay) {
535
- ts.doubleTouch = true;
536
- }
537
- this._dblTchBegins = 0;
538
- }
539
- this._dblTchBegins = window.performance.now();
540
- this._oneTouchStart = false;
541
- }
542
- }
543
- }
544
-
545
- /**
546
- * @private
547
- */
548
- onTouchCancel(event) {
549
- var ts = this.touchState;
550
- ts.sys = event;
551
- ts.touchCancel = true;
552
- }
553
-
554
- /**
555
- * @private
556
- */
557
- onTouchMove(event) {
558
- var ts = this.touchState;
559
- ts.clientX = event.touches.item(0).clientX - event.offsetLeft;
560
- ts.clientY = event.touches.item(0).clientY - event.offsetTop;
561
-
562
- var h = this.renderer.handler;
563
-
564
- ts.x = ts.clientX * h.pixelRatio;
565
- ts.y = ts.clientY * h.pixelRatio;
566
-
567
- ts.nx = ts.x / h.canvas.width;
568
- ts.ny = ts.y / h.canvas.height;
569
-
570
- ts.sys = event;
571
- ts.moving = true;
572
- this._dblTchBegins = 0;
573
- this._oneTouchStart = false;
574
- }
575
-
576
- /**
577
- * @private
578
- */
579
- entityPickingEvents() {
580
- var ts = this.touchState,
581
- ms = this.mouseState;
582
-
583
- if (!(ms.leftButtonHold || ms.rightButtonHold || ms.middleButtonHold)) {
584
- var r = this.renderer;
585
-
586
- var o = r.colorObjects;
587
-
588
- var c = r._currPickingColor,
589
- p = r._prevPickingColor;
590
-
591
- ms.pickingObject = null;
592
- ts.pickingObject = null;
593
-
594
- var co = o && o[c[0] + "_" + c[1] + "_" + c[2]];
595
-
596
- ms.pickingObject = co;
597
- ts.pickingObject = co;
598
-
599
- //object changed
600
- if (c[0] != p[0] || c[1] != p[1] || c[2] != p[2]) {
601
- //current black
602
- if (!(c[0] || c[1] || c[2])) {
603
- let po = o[p[0] + "_" + p[1] + "_" + p[2]];
604
- if (po) {
605
- let pe = po.rendererEvents;
606
- ms.pickingObject = po;
607
- pe && pe.dispatch(pe.mouseleave, ms);
608
- ts.pickingObject = po;
609
- pe && pe.dispatch(pe.touchleave, ts);
610
- }
611
- } else {
612
- //current not black
613
-
614
- //previous not black
615
- if (p[0] || p[1] || p[2]) {
616
- let po = o[p[0] + "_" + p[1] + "_" + p[2]];
617
- if (po) {
618
- let pe = po.rendererEvents;
619
- ms.pickingObject = po;
620
- pe && pe.dispatch(pe.mouseleave, ms);
621
- ts.pickingObject = po;
622
- pe && pe.dispatch(pe.touchleave, ts);
623
- }
624
- }
625
-
626
- if (co) {
627
- var ce = co.rendererEvents;
628
- ms.pickingObject = co;
629
- ce && ce.dispatch(ce.mouseenter, ms);
630
- ts.pickingObject = co;
631
- ce && ce.dispatch(ce.touchenter, ts);
632
- }
633
- }
634
- }
635
- }
636
- }
637
-
638
- /**
639
- * @private
640
- */
641
- handleMouseEvents() {
642
- let ms = this.mouseState;
643
- let po = ms.pickingObject,
644
- pe = null;
645
-
646
- if (ms.leftButtonClick) {
647
- if (po) {
648
- pe = po.rendererEvents;
649
- pe && pe.dispatch(pe.lclick, ms);
650
- }
651
- this.dispatch(this.lclick, ms);
652
- ms.leftButtonClick = false;
653
- }
654
-
655
- if (ms.rightButtonClick) {
656
- if (po) {
657
- pe = po.rendererEvents;
658
- pe && pe.dispatch(pe.rclick, ms);
659
- }
660
- this.dispatch(this.rclick, ms);
661
- ms.rightButtonClick = false;
662
- }
663
-
664
- if (ms.middleButtonClick) {
665
- if (po) {
666
- pe = po.rendererEvents;
667
- pe && pe.dispatch(pe.mclick, ms);
668
- }
669
- this.dispatch(this.mclick, ms);
670
- ms.middleButtonClick = false;
671
- }
672
-
673
- if (ms.leftButtonDown) {
674
- if (ms.leftButtonHold) {
675
- if (po) {
676
- pe = po.rendererEvents;
677
- pe && pe.dispatch(pe.lhold, ms);
678
- }
679
- this.dispatch(this.lhold, ms);
680
- } else {
681
- ms.leftButtonHold = true;
682
- if (po) {
683
- pe = po.rendererEvents;
684
- pe && pe.dispatch(pe.ldown, ms);
685
- }
686
- this.dispatch(this.ldown, ms);
687
- }
688
- }
689
-
690
- if (ms.rightButtonDown) {
691
- if (ms.rightButtonHold) {
692
- if (po) {
693
- pe = po.rendererEvents;
694
- pe && pe.dispatch(pe.rhold, ms);
695
- }
696
- this.dispatch(this.rhold, ms);
697
- } else {
698
- ms.rightButtonHold = true;
699
- if (po) {
700
- pe = po.rendererEvents;
701
- pe && pe.dispatch(pe.rdown, ms);
702
- }
703
- this.dispatch(this.rdown, ms);
704
- }
705
- }
706
-
707
- if (ms.middleButtonDown) {
708
- if (ms.middleButtonHold) {
709
- if (po) {
710
- pe = po.rendererEvents;
711
- pe && pe.dispatch(pe.mhold, ms);
712
- }
713
- this.dispatch(this.mhold, ms);
714
- } else {
715
- ms.middleButtonHold = true;
716
- if (po) {
717
- pe = po.rendererEvents;
718
- pe && pe.dispatch(pe.mdown, ms);
719
- }
720
- this.dispatch(this.mdown, ms);
721
- }
722
- }
723
-
724
- if (ms.leftButtonUp) {
725
- if (po) {
726
- pe = po.rendererEvents;
727
- pe && pe.dispatch(pe.lup, ms);
728
- }
729
- this.dispatch(this.lup, ms);
730
- ms.leftButtonUp = false;
731
- ms.leftButtonHold = false;
732
- }
733
-
734
- if (ms.rightButtonUp) {
735
- if (po) {
736
- pe = po.rendererEvents;
737
- pe && pe.dispatch(pe.rup, ms);
738
- }
739
- this.dispatch(this.rup, ms);
740
- ms.rightButtonUp = false;
741
- ms.rightButtonHold = false;
742
- }
743
-
744
- if (ms.middleButtonUp) {
745
- if (po) {
746
- pe = po.rendererEvents;
747
- pe && pe.dispatch(pe.mup, ms);
748
- }
749
- this.dispatch(this.mup, ms);
750
- ms.middleButtonUp = false;
751
- ms.middleButtonHold = false;
752
- }
753
-
754
- if (ms.leftButtonDoubleClick) {
755
- if (po) {
756
- pe = po.rendererEvents;
757
- pe && pe.dispatch(pe.ldblclick, ms);
758
- }
759
- this.dispatch(this.ldblclick, ms);
760
- ms.leftButtonDoubleClick = false;
761
- }
762
-
763
- if (ms.rightButtonDoubleClick) {
764
- if (po) {
765
- pe = po.rendererEvents;
766
- pe && pe.dispatch(pe.rdblclick, ms);
767
- }
768
- this.dispatch(this.rdblclick, ms);
769
- ms.rightButtonDoubleClick = false;
770
- }
771
-
772
- if (ms.middleButtonDoubleClick) {
773
- if (po) {
774
- pe = po.rendererEvents;
775
- pe && pe.dispatch(pe.mdblclick, ms);
776
- }
777
- this.dispatch(this.mdblclick, ms);
778
- ms.middleButtonDoubleClick = false;
779
- }
780
-
781
- if (ms.wheelDelta) {
782
- if (po) {
783
- pe = po.rendererEvents;
784
- pe && pe.dispatch(pe.mousewheel, ms);
785
- }
786
- this.dispatch(this.mousewheel, ms);
787
- }
788
-
789
- if (ms.moving) {
790
- if (po) {
791
- pe = po.rendererEvents;
792
- pe && pe.dispatch(pe.mousemove, ms);
793
- }
794
- this.dispatch(this.mousemove, ms);
795
- ms.prev_x = ms.x;
796
- ms.prev_y = ms.y;
797
- }
798
-
799
- if (ms.justStopped) {
800
- this.dispatch(this.mousestop, ms);
801
- }
802
- }
803
-
804
- /**
805
- * @private
806
- */
807
- handleTouchEvents() {
808
- var ts = this.touchState;
809
-
810
- var tpo = ts.pickingObject,
811
- tpe = null;
812
-
813
- if (ts.touchCancel) {
814
- this.dispatch(this.touchcancel, ts);
815
- ts.touchCancel = false;
816
- }
817
-
818
- if (ts.touchStart) {
819
- var r = this.renderer;
820
-
821
- r.pickingFramebuffer.activate();
822
- r.pickingFramebuffer.readPixels(r._currPickingColor, ts.nx, 1.0 - ts.ny, 1);
823
- r.pickingFramebuffer.deactivate();
824
-
825
- var o = r.colorObjects;
826
- var c = r._currPickingColor;
827
- var co = o[c[0] + "_" + c[1] + "_" + c[2]];
828
- tpo = ts.pickingObject = co;
829
- if (tpo) {
830
- tpe = tpo.rendererEvents;
831
- tpe && tpe.dispatch(tpe.touchstart, ts);
832
- }
833
- this.dispatch(this.touchstart, ts);
834
- ts.touchStart = false;
835
- }
836
-
837
- if (ts.doubleTouch) {
838
- if (tpo) {
839
- tpe = tpo.rendererEvents;
840
- tpe && tpe.dispatch(tpe.doubletouch, ts);
841
- }
842
- this.dispatch(this.doubletouch, ts);
843
- ts.doubleTouch = false;
844
- }
845
-
846
- if (ts.touchEnd) {
847
- if (tpo) {
848
- tpe = tpo.rendererEvents;
849
- tpe && tpe.dispatch(tpe.touchend, ts);
850
- }
851
- this.dispatch(this.touchend, ts);
852
- ts.x = 0;
853
- ts.y = 0;
854
- ts.touchEnd = false;
855
- }
856
-
857
- if (ts.moving) {
858
- if (tpo) {
859
- tpe = tpo.rendererEvents;
860
- tpe && tpe.dispatch(tpe.touchmove, ts);
861
- }
862
- this.dispatch(this.touchmove, ts);
863
- ts.prev_x = ts.x;
864
- ts.prev_y = ts.y;
865
- }
866
- }
867
- }
868
-
869
- const EVENT_NAMES = [
870
- /**
871
- * Triggered before scene frame is rendered(before render nodes).
872
- * @event og.RendererEvents#draw
873
- */
874
- "draw",
875
-
876
- /**
877
- * Triggered after scene frame is rendered(after render nodes).
878
- * @event og.RendererEvents#postdraw
879
- */
880
- "postdraw",
881
-
882
- /**
883
- * Triggered when screen is resized.
884
- * @event og.RendererEvents#resize
885
- */
886
- "resize",
887
-
888
- /**
889
- * Mouse enters the work screen
890
- * @event og.RendererEvents#mouseenter
891
- */
892
- "mouseenter",
893
-
894
- /**
895
- * Mouse leaves the work screen
896
- * @event og.RendererEvents#mouseleave
897
- */
898
- "mouseleave",
899
-
900
- /**
901
- * Mouse is moving.
902
- * @event og.RendererEvents#mousemove
903
- */
904
- "mousemove",
905
-
906
- /**
907
- * Mouse is just stopped.
908
- * @event og.RendererEvents#mousestop
909
- */
910
- "mousestop",
911
-
912
- /**
913
- * Mouse left button clicked.
914
- * @event og.RendererEvents#lclick
915
- */
916
- "lclick",
917
-
918
- /**
919
- * Mouse right button clicked.
920
- * @event og.RendererEvents#rclick
921
- */
922
- "rclick",
923
-
924
- /**
925
- * Mouse middle button clicked.
926
- * @event og.RendererEvents#mclick
927
- */
928
- "mclick",
929
-
930
- /**
931
- * Mouse left button double click.
932
- * @event og.RendererEvents#ldblclick
933
- */
934
- "ldblclick",
935
-
936
- /**
937
- * Mouse right button double click.
938
- * @event og.RendererEvents#rdblclick
939
- */
940
- "rdblclick",
941
-
942
- /**
943
- * Mouse middle button double click.
944
- * @event og.RendererEvents#mdblclick
945
- */
946
- "mdblclick",
947
-
948
- /**
949
- * Mouse left button up(stop pressing).
950
- * @event og.RendererEvents#lup
951
- */
952
- "lup",
953
-
954
- /**
955
- * Mouse right button up(stop pressing).
956
- * @event og.RendererEvents#rup
957
- */
958
- "rup",
959
-
960
- /**
961
- * Mouse middle button up(stop pressing).
962
- * @event og.RendererEvents#mup
963
- */
964
- "mup",
965
-
966
- /**
967
- * Mouse left button is just pressed down(start pressing).
968
- * @event og.RendererEvents#ldown
969
- */
970
- "ldown",
971
-
972
- /**
973
- * Mouse right button is just pressed down(start pressing).
974
- * @event og.RendererEvents#rdown
975
- */
976
- "rdown",
977
-
978
- /**
979
- * Mouse middle button is just pressed down(start pressing).
980
- * @event og.RendererEvents#mdown
981
- */
982
- "mdown",
983
-
984
- /**
985
- * Mouse left button is pressing.
986
- * @event og.RendererEvents#lhold
987
- */
988
- "lhold",
989
-
990
- /**
991
- * Mouse right button is pressing.
992
- * @event og.RendererEvents#rhold
993
- */
994
- "rhold",
995
-
996
- /**
997
- * Mouse middle button is pressing.
998
- * @event og.RendererEvents#mhold
999
- */
1000
- "mhold",
1001
-
1002
- /**
1003
- * Mouse wheel is rotated.
1004
- * @event og.RendererEvents#mousewheel
1005
- */
1006
- "mousewheel",
1007
-
1008
- /**
1009
- * Triggered when touching starts.
1010
- * @event og.RendererEvents#touchstart
1011
- */
1012
- "touchstart",
1013
-
1014
- /**
1015
- * Triggered when touching ends.
1016
- * @event og.RendererEvents#touchend
1017
- */
1018
- "touchend",
1019
-
1020
- /**
1021
- * Triggered when touching cancel.
1022
- * @event og.RendererEvents#touchcancel
1023
- */
1024
- "touchcancel",
1025
-
1026
- /**
1027
- * Triggered when touch is move.
1028
- * @event og.RendererEvents#touchmove
1029
- */
1030
- "touchmove",
1031
-
1032
- /**
1033
- * Triggered when double touch.
1034
- * @event og.RendererEvents#doubletouch
1035
- */
1036
- "doubletouch",
1037
-
1038
- /**
1039
- * Triggered when touch leaves picked object.
1040
- * @event og.RendererEvents#touchleave
1041
- */
1042
- "touchleave",
1043
-
1044
- /**
1045
- * Triggered when touch enter picking object.
1046
- * @event og.RendererEvents#touchenter
1047
- */
1048
- "touchenter"
1049
- ];
1050
-
1051
- export { RendererEvents };
1
+ /**
2
+ * @module og/renderer/RendererEvents
3
+ */
4
+
5
+ "use strict";
6
+
7
+ import { Events } from "../Events.js";
8
+ import { input } from "../input/input.js";
9
+ import { KeyboardHandler } from "../input/KeyboardHandler.js";
10
+ import { MouseHandler } from "../input/MouseHandler.js";
11
+ import { TouchHandler } from "../input/TouchHandler.js";
12
+ import { Vec2 } from "../math/Vec2.js";
13
+ import { Vec3 } from "../math/Vec3.js";
14
+
15
+ const LB_M = 0b0001;
16
+ const RB_M = 0b0010;
17
+ const MB_M = 0b0100;
18
+
19
+ /**
20
+ * Renderer events handler.
21
+ * @class
22
+ * @param {Renderer} renderer - Renderer object, events that works for.
23
+ */
24
+ class RendererEvents extends Events {
25
+ constructor(renderer) {
26
+ super(EVENT_NAMES);
27
+
28
+ /**
29
+ * Assigned renderer.
30
+ * @public
31
+ * @type {Renderer}
32
+ */
33
+ this.renderer = renderer;
34
+
35
+ /**
36
+ * Low level touch events handler.
37
+ * @private
38
+ * @type {input.TouchHandler}
39
+ */
40
+ this._touchHandler = new TouchHandler(renderer.handler.canvas);
41
+
42
+ /**
43
+ * Low level mouse events handler.
44
+ * @private
45
+ * @type {input.MouseHandler}
46
+ */
47
+ this._mouseHandler = new MouseHandler(renderer.handler.canvas);
48
+
49
+ /**
50
+ * Low level keyboard events handler.
51
+ * @private
52
+ * @type {input.KeyboardHandler}
53
+ */
54
+ this._keyboardHandler = new KeyboardHandler();
55
+
56
+ this._active = true;
57
+
58
+ this.clickRadius = 15;
59
+
60
+ /**
61
+ * Current mouse state.
62
+ * @public
63
+ * @enum {Object}
64
+ */
65
+ this.mouseState = {
66
+ /** Current screen mouse X position. */
67
+ clientX: 0,
68
+ /** Current screen mouse Y position. */
69
+ clientY: 0,
70
+ /** Current viewport mouse X position. */
71
+ x: 0,
72
+ /** Current viewport mouse Y position. */
73
+ y: 0,
74
+ /** Current mouse X position from 0 to 1 */
75
+ nx: 0,
76
+ /** Current mouse Y position from 0 to 1 */
77
+ ny: 0,
78
+ /** Previous mouse X position. */
79
+ prev_x: 0,
80
+ /** Previous mouse Y position. */
81
+ prev_y: 0,
82
+ /** Screen mouse position world direction. */
83
+ direction: new Vec3(),
84
+ /** Left mouse button has stopped pushing down right now.*/
85
+ leftButtonUp: false,
86
+ /** Right mouse button has stopped pushing down right now.*/
87
+ rightButtonUp: false,
88
+ /** Middle mouse button has stopped pushing down right now.*/
89
+ middleButtonUp: false,
90
+ /** Left mouse button has pushed now.*/
91
+ leftButtonDown: false,
92
+ /** Right mouse button has pushed now.*/
93
+ rightButtonDown: false,
94
+ /** Middle mouse button has pushed now.*/
95
+ middleButtonDown: false,
96
+ /** Left mouse button is pushing.*/
97
+ leftButtonHold: false,
98
+ /** Right mouse button is pushing.*/
99
+ rightButtonHold: false,
100
+ /** Middle mouse button is pushing.*/
101
+ middleButtonHold: false,
102
+ /** Left mouse button has clicked twice now.*/
103
+ leftButtonDoubleClick: false,
104
+ /** Right mouse button has clicked twice now.*/
105
+ rightButtonDoubleClick: false,
106
+ /** Middle mouse button has clicked twice now.*/
107
+ middleButtonDoubleClick: false,
108
+ /** Left mouse button has clicked now. */
109
+ leftButtonClick: false,
110
+ /** Right mouse button has clicked now. */
111
+ rightButtonClick: false,
112
+ /** Middle mouse button has clicked now. */
113
+ middleButtonClick: false,
114
+ /** Mouse is moving now. */
115
+ moving: false,
116
+ /** Mouse has just stopped now. */
117
+ justStopped: false,
118
+ /** Mose double click delay response time.*/
119
+ doubleClickDelay: 500,
120
+ /** Mose click delay response time.*/
121
+ clickDelay: 200,
122
+ /** Mouse wheel. */
123
+ wheelDelta: 0,
124
+ /** JavaScript mouse system event message. */
125
+ sys: null,
126
+ /** Current picking object. */
127
+ pickingObject: null,
128
+ /** Renderer instanve. */
129
+ renderer: renderer
130
+ };
131
+
132
+ /**
133
+ * Current touch state.
134
+ * @public
135
+ * @enum {Object}
136
+ */
137
+ this.touchState = {
138
+ /** Touching is moving now. */
139
+ moving: false,
140
+ /** Touch has ended right now.*/
141
+ touchEnd: false,
142
+ /** Touch has started right now.*/
143
+ touchStart: false,
144
+ /** Touch canceled.*/
145
+ touchCancel: false,
146
+ /** Touched twice.*/
147
+ doubleTouch: false,
148
+ /** Double touching responce delay.*/
149
+ doubleTouchDelay: 550,
150
+ /** Double touching responce radius in screen pixels.*/
151
+ doubleTouchRadius: 10,
152
+ /** Current touch X - coordinate. */
153
+ x: 0,
154
+ /** Current touch Y - coordinate. */
155
+ y: 0,
156
+ /** Current touch X - coordinate from 0 to 1 */
157
+ nx: 0,
158
+ /** Current touch Y - coordinate from 0 to 1 */
159
+ ny: 0,
160
+ /** Previous touch X coordinate. */
161
+ prev_x: 0,
162
+ /** Previous touch Y coordinate. */
163
+ prev_y: 0,
164
+ /** JavaScript touching system event message. */
165
+ sys: null,
166
+ /** Current touched(picking) object. */
167
+ pickingObject: null,
168
+ /** Renderer instanve. */
169
+ renderer: renderer
170
+ };
171
+
172
+ this._dblTchCoords = new Vec2();
173
+ this._oneTouchStart = false;
174
+ this._dblTchBegins = 0;
175
+ /**
176
+ * @type {number}
177
+ */
178
+ this._mousestopThread = null;
179
+ this._ldblClkBegins = 0;
180
+ this._rdblClkBegins = 0;
181
+ this._mdblClkBegins = 0;
182
+ this._lClkBegins = 0;
183
+ this._rClkBegins = 0;
184
+ this._mClkBegins = 0;
185
+ this._lclickX = 0;
186
+ this._lclickY = 0;
187
+ this._rclickX = 0;
188
+ this._rclickY = 0;
189
+ this._mclickX = 0;
190
+ this._mclickY = 0;
191
+ }
192
+
193
+ pointerEvent() {
194
+ let ms = this.mouseState,
195
+ ts = this.touchState;
196
+ return (
197
+ ms.moving ||
198
+ ms.justStopped ||
199
+ ms.wheelDelta ||
200
+ ts.moving ||
201
+ ts.touchStart ||
202
+ ts.touchEnd
203
+ )
204
+ }
205
+
206
+ get active() {
207
+ return this._active;
208
+ }
209
+
210
+ set active(act) {
211
+ this._active = act;
212
+ this._keyboardHandler.setActivity(act);
213
+ }
214
+
215
+ /**
216
+ * Used in render node frame.
217
+ * @public
218
+ */
219
+ handleEvents() {
220
+ if (this._active) {
221
+ this.mouseState.direction = this.renderer.activeCamera.unproject(
222
+ this.mouseState.x,
223
+ this.mouseState.y
224
+ );
225
+ this.entityPickingEvents();
226
+ this._keyboardHandler.handleEvents();
227
+ this.handleMouseEvents();
228
+ this.handleTouchEvents();
229
+ }
230
+ }
231
+
232
+ /**
233
+ * Set render event callback.
234
+ * @public
235
+ * @param {string} name - Event name
236
+ * @param {eventCallback} callback - Callback function
237
+ * @param {number} [key] - Key code from og.input
238
+ * @param {*} sender - Callback context
239
+ * @param {number} [priority] - Event callback priority
240
+ */
241
+ on(name, p0, p1, p2, p3) {
242
+ if (name === "keypress" || name === "charkeypress" || name === "keyfree") {
243
+ this._keyboardHandler.addEvent(name, p2, p1, p0, p3);
244
+ } else {
245
+ super.on(name, p0, p1, p2);
246
+ }
247
+ }
248
+
249
+ /**
250
+ * TODO: DOESNT WORK!!!
251
+ * @param {any} name
252
+ * @param {any} callback
253
+ */
254
+ off(name, callback) {
255
+ if (name === "keypress" || name === "charkeypress" || name === "keyfree") {
256
+ this._keyboardHandler.removeEvent(name, callback);
257
+ } else {
258
+ super.off(name, callback);
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Check key is pressed.
264
+ * @public
265
+ * @param {number} keyCode - Key code
266
+ * @return {boolean}
267
+ */
268
+ isKeyPressed(keyCode) {
269
+ return this._keyboardHandler.isKeyPressed(keyCode);
270
+ }
271
+
272
+ releaseKeys() {
273
+ this._keyboardHandler.releaseKeys();
274
+ }
275
+
276
+ /**
277
+ * Renderer events initialization.
278
+ * @public
279
+ */
280
+ initialize() {
281
+ this._mouseHandler.setEvent("mouseup", this, this.onMouseUp);
282
+ this._mouseHandler.setEvent("mousemove", this, this.onMouseMove);
283
+ this._mouseHandler.setEvent("mousedown", this, this.onMouseDown);
284
+ this._mouseHandler.setEvent("mousewheel", this, this.onMouseWheel);
285
+ this._mouseHandler.setEvent("mouseleave", this, this.onMouseLeave);
286
+ this._mouseHandler.setEvent("mouseenter", this, this.onMouseEnter);
287
+
288
+ this._touchHandler.setEvent("touchstart", this, this.onTouchStart);
289
+ this._touchHandler.setEvent("touchend", this, this.onTouchEnd);
290
+ this._touchHandler.setEvent("touchcancel", this, this.onTouchCancel);
291
+ this._touchHandler.setEvent("touchmove", this, this.onTouchMove);
292
+ }
293
+
294
+ /**
295
+ * @private
296
+ */
297
+ onMouseWheel(event) {
298
+ this.mouseState.wheelDelta = event.wheelDelta;
299
+ }
300
+
301
+ /**
302
+ * @protected
303
+ */
304
+ updateButtonsStates(buttons) {
305
+ var ms = this.mouseState;
306
+ if (buttons & LB_M) {
307
+ ms.leftButtonDown = true;
308
+ } else {
309
+ ms.leftButtonHold = false;
310
+ ms.leftButtonDown = false;
311
+ }
312
+
313
+ if (buttons & RB_M) {
314
+ ms.rightButtonDown = true;
315
+ } else {
316
+ ms.rightButtonHold = false;
317
+ ms.rightButtonDown = false;
318
+ }
319
+
320
+ if (buttons & MB_M) {
321
+ ms.middleButtonDown = true;
322
+ } else {
323
+ ms.middleButtonHold = false;
324
+ ms.middleButtonDown = false;
325
+ }
326
+ }
327
+
328
+ /**
329
+ * @private
330
+ */
331
+ onMouseMove(event, sys) {
332
+ var ms = this.mouseState;
333
+ this.updateButtonsStates(sys.buttons);
334
+ ms.sys = event;
335
+
336
+ let ex = event.clientX,
337
+ ey = event.clientY,
338
+ r = this.clickRadius;
339
+
340
+ if (Math.abs(this._lclickX - ex) >= r && Math.abs(this._lclickY - ey) >= r) {
341
+ this._ldblClkBegins = 0;
342
+ this._lClkBegins = 0;
343
+ }
344
+
345
+ if (Math.abs(this._rclickX - ex) >= r && Math.abs(this._rclickY - ey) >= r) {
346
+ this._rdblClkBegins = 0;
347
+ this._rClkBegins = 0;
348
+ }
349
+
350
+ if (Math.abs(this._mclickX - ex) >= r && Math.abs(this._mclickY - ey) >= r) {
351
+ this._mdblClkBegins = 0;
352
+ this._mClkBegins = 0;
353
+ }
354
+
355
+ if (ms.clientX === event.clientX && ms.clientY === event.clientY) {
356
+ return;
357
+ }
358
+
359
+ ms.clientX = event.clientX;
360
+ ms.clientY = event.clientY;
361
+
362
+ let h = this.renderer.handler;
363
+
364
+ ms.x = event.clientX * h.pixelRatio;
365
+ ms.y = event.clientY * h.pixelRatio;
366
+
367
+ ms.nx = ms.x / h.canvas.width;
368
+ ms.ny = ms.y / h.canvas.height;
369
+
370
+ ms.moving = true;
371
+
372
+ //dispatch stop mouse event
373
+ clearTimeout(this._mousestopThread);
374
+ this._mousestopThread = setTimeout(function () {
375
+ ms.justStopped = true;
376
+ }, 100);
377
+ }
378
+
379
+ onMouseLeave(event) {
380
+ this.dispatch(this.mouseleave, event);
381
+ }
382
+
383
+ onMouseEnter(event) {
384
+ this.dispatch(this.mouseenter, event);
385
+ }
386
+
387
+ /**
388
+ * @private
389
+ */
390
+ onMouseDown(event) {
391
+ if (event.button === input.MB_LEFT) {
392
+ this._lClkBegins = window.performance.now();
393
+ this._lclickX = event.clientX;
394
+ this._lclickY = event.clientY;
395
+ this.mouseState.sys = event;
396
+ this.mouseState.leftButtonDown = true;
397
+ } else if (event.button === input.MB_RIGHT) {
398
+ this._rClkBegins = window.performance.now();
399
+ this._rclickX = event.clientX;
400
+ this._rclickY = event.clientY;
401
+ this.mouseState.sys = event;
402
+ this.mouseState.rightButtonDown = true;
403
+ } else if (event.button === input.MB_MIDDLE) {
404
+ this._mClkBegins = window.performance.now();
405
+ this._mclickX = event.clientX;
406
+ this._mclickY = event.clientY;
407
+ this.mouseState.sys = event;
408
+ this.mouseState.middleButtonDown = true;
409
+ }
410
+ }
411
+
412
+ /**
413
+ * @private
414
+ */
415
+ onMouseUp(event) {
416
+ var ms = this.mouseState;
417
+ ms.sys = event;
418
+ var t = window.performance.now();
419
+
420
+ if (event.button === input.MB_LEFT) {
421
+ ms.leftButtonDown = false;
422
+ ms.leftButtonUp = true;
423
+
424
+ if (
425
+ Math.abs(this._lclickX - event.clientX) < this.clickRadius &&
426
+ Math.abs(this._lclickY - event.clientY) < this.clickRadius &&
427
+ t - this._lClkBegins <= ms.clickDelay
428
+ ) {
429
+ if (this._ldblClkBegins) {
430
+ let deltatime = window.performance.now() - this._ldblClkBegins;
431
+ if (deltatime <= ms.doubleClickDelay) {
432
+ ms.leftButtonDoubleClick = true;
433
+ }
434
+ this._ldblClkBegins = 0;
435
+ } else {
436
+ this._ldblClkBegins = window.performance.now();
437
+ }
438
+
439
+ ms.leftButtonClick = true;
440
+ this._lClkBegins = 0;
441
+ }
442
+ } else if (event.button === input.MB_RIGHT) {
443
+ ms.rightButtonDown = false;
444
+ ms.rightButtonUp = true;
445
+
446
+ if (
447
+ Math.abs(this._rclickX - event.clientX) < this.clickRadius &&
448
+ Math.abs(this._rclickY - event.clientY) < this.clickRadius &&
449
+ t - this._rClkBegins <= ms.clickDelay
450
+ ) {
451
+ if (this._rdblClkBegins) {
452
+ let deltatime = window.performance.now() - this._rdblClkBegins;
453
+ if (deltatime <= ms.doubleClickDelay) {
454
+ ms.rightButtonDoubleClick = true;
455
+ }
456
+ this._rdblClkBegins = 0;
457
+ } else {
458
+ this._rdblClkBegins = window.performance.now();
459
+ }
460
+
461
+ ms.rightButtonClick = true;
462
+ this._rClkBegins = 0;
463
+ }
464
+ } else if (event.button === input.MB_MIDDLE) {
465
+ ms.middleButtonDown = false;
466
+ ms.middleButtonUp = true;
467
+
468
+ if (
469
+ Math.abs(this._mclickX - event.clientX) < this.clickRadius &&
470
+ Math.abs(this._mclickY - event.clientY) < this.clickRadius &&
471
+ t - this._mClkBegins <= ms.clickDelay
472
+ ) {
473
+ if (this._mdblClkBegins) {
474
+ var deltatime = window.performance.now() - this._mdblClkBegins;
475
+ if (deltatime <= ms.doubleClickDelay) {
476
+ ms.middleButtonDoubleClick = true;
477
+ }
478
+ this._mdblClkBegins = 0;
479
+ } else {
480
+ this._mdblClkBegins = window.performance.now();
481
+ }
482
+
483
+ ms.middleButtonClick = true;
484
+ this._mClkBegins = 0;
485
+ }
486
+ }
487
+ }
488
+
489
+ /**
490
+ * @private
491
+ */
492
+ onTouchStart(event) {
493
+ var ts = this.touchState;
494
+ ts.sys = event;
495
+
496
+ ts.clienX = event.touches.item(0).clientX - event.offsetLeft;
497
+ ts.clientY = event.touches.item(0).clientY - event.offsetTop;
498
+
499
+ let h = this.renderer.handler;
500
+
501
+ ts.x = ts.clientX * h.pixelRatio;
502
+ ts.y = ts.clientY * h.pixelRatio;
503
+
504
+ ts.nx = ts.x / h.canvas.width;
505
+ ts.ny = ts.y / h.canvas.height;
506
+ ts.prev_x = ts.x;
507
+ ts.prev_y = ts.y;
508
+ ts.touchStart = true;
509
+
510
+ if (event.touches.length === 1) {
511
+ this._dblTchCoords.x = ts.x;
512
+ this._dblTchCoords.y = ts.y;
513
+ this._oneTouchStart = true;
514
+ } else {
515
+ this._oneTouchStart = false;
516
+ }
517
+ }
518
+
519
+ /**
520
+ * @private
521
+ */
522
+ onTouchEnd(event) {
523
+ var ts = this.touchState;
524
+ ts.sys = event;
525
+ ts.touchEnd = true;
526
+
527
+ if (event.touches.length === 0) {
528
+ ts.prev_x = ts.x;
529
+ ts.prev_y = ts.y;
530
+
531
+ if (this._oneTouchStart) {
532
+ if (this._dblTchBegins) {
533
+ var deltatime = window.performance.now() - this._dblTchBegins;
534
+ if (deltatime <= ts.doubleTouchDelay) {
535
+ ts.doubleTouch = true;
536
+ }
537
+ this._dblTchBegins = 0;
538
+ }
539
+ this._dblTchBegins = window.performance.now();
540
+ this._oneTouchStart = false;
541
+ }
542
+ }
543
+ }
544
+
545
+ /**
546
+ * @private
547
+ */
548
+ onTouchCancel(event) {
549
+ var ts = this.touchState;
550
+ ts.sys = event;
551
+ ts.touchCancel = true;
552
+ }
553
+
554
+ /**
555
+ * @private
556
+ */
557
+ onTouchMove(event) {
558
+ var ts = this.touchState;
559
+ ts.clientX = event.touches.item(0).clientX - event.offsetLeft;
560
+ ts.clientY = event.touches.item(0).clientY - event.offsetTop;
561
+
562
+ var h = this.renderer.handler;
563
+
564
+ ts.x = ts.clientX * h.pixelRatio;
565
+ ts.y = ts.clientY * h.pixelRatio;
566
+
567
+ ts.nx = ts.x / h.canvas.width;
568
+ ts.ny = ts.y / h.canvas.height;
569
+
570
+ ts.sys = event;
571
+ ts.moving = true;
572
+ // this._dblTchBegins = 0;
573
+ // this._oneTouchStart = false;
574
+ }
575
+
576
+ /**
577
+ * @private
578
+ */
579
+ entityPickingEvents() {
580
+ var ts = this.touchState,
581
+ ms = this.mouseState;
582
+
583
+ if (!(ms.leftButtonHold || ms.rightButtonHold || ms.middleButtonHold)) {
584
+ var r = this.renderer;
585
+
586
+ var o = r.colorObjects;
587
+
588
+ var c = r._currPickingColor,
589
+ p = r._prevPickingColor;
590
+
591
+ ms.pickingObject = null;
592
+ ts.pickingObject = null;
593
+
594
+ var co = o && o[c[0] + "_" + c[1] + "_" + c[2]];
595
+
596
+ ms.pickingObject = co;
597
+ ts.pickingObject = co;
598
+
599
+ //object changed
600
+ if (c[0] != p[0] || c[1] != p[1] || c[2] != p[2]) {
601
+ //current black
602
+ if (!(c[0] || c[1] || c[2])) {
603
+ let po = o[p[0] + "_" + p[1] + "_" + p[2]];
604
+ if (po) {
605
+ let pe = po.rendererEvents;
606
+ ms.pickingObject = po;
607
+ pe && pe.dispatch(pe.mouseleave, ms);
608
+ ts.pickingObject = po;
609
+ pe && pe.dispatch(pe.touchleave, ts);
610
+ }
611
+ } else {
612
+ //current not black
613
+
614
+ //previous not black
615
+ if (p[0] || p[1] || p[2]) {
616
+ let po = o[p[0] + "_" + p[1] + "_" + p[2]];
617
+ if (po) {
618
+ let pe = po.rendererEvents;
619
+ ms.pickingObject = po;
620
+ pe && pe.dispatch(pe.mouseleave, ms);
621
+ ts.pickingObject = po;
622
+ pe && pe.dispatch(pe.touchleave, ts);
623
+ }
624
+ }
625
+
626
+ if (co) {
627
+ var ce = co.rendererEvents;
628
+ ms.pickingObject = co;
629
+ ce && ce.dispatch(ce.mouseenter, ms);
630
+ ts.pickingObject = co;
631
+ ce && ce.dispatch(ce.touchenter, ts);
632
+ }
633
+ }
634
+ }
635
+ }
636
+ }
637
+
638
+ /**
639
+ * @private
640
+ */
641
+ handleMouseEvents() {
642
+ let ms = this.mouseState;
643
+ let po = ms.pickingObject,
644
+ pe = null;
645
+
646
+ if (ms.leftButtonClick) {
647
+ if (po) {
648
+ pe = po.rendererEvents;
649
+ pe && pe.dispatch(pe.lclick, ms);
650
+ }
651
+ this.dispatch(this.lclick, ms);
652
+ ms.leftButtonClick = false;
653
+ }
654
+
655
+ if (ms.rightButtonClick) {
656
+ if (po) {
657
+ pe = po.rendererEvents;
658
+ pe && pe.dispatch(pe.rclick, ms);
659
+ }
660
+ this.dispatch(this.rclick, ms);
661
+ ms.rightButtonClick = false;
662
+ }
663
+
664
+ if (ms.middleButtonClick) {
665
+ if (po) {
666
+ pe = po.rendererEvents;
667
+ pe && pe.dispatch(pe.mclick, ms);
668
+ }
669
+ this.dispatch(this.mclick, ms);
670
+ ms.middleButtonClick = false;
671
+ }
672
+
673
+ if (ms.leftButtonDown) {
674
+ if (ms.leftButtonHold) {
675
+ if (po) {
676
+ pe = po.rendererEvents;
677
+ pe && pe.dispatch(pe.lhold, ms);
678
+ }
679
+ this.dispatch(this.lhold, ms);
680
+ } else {
681
+ ms.leftButtonHold = true;
682
+ if (po) {
683
+ pe = po.rendererEvents;
684
+ pe && pe.dispatch(pe.ldown, ms);
685
+ }
686
+ this.dispatch(this.ldown, ms);
687
+ }
688
+ }
689
+
690
+ if (ms.rightButtonDown) {
691
+ if (ms.rightButtonHold) {
692
+ if (po) {
693
+ pe = po.rendererEvents;
694
+ pe && pe.dispatch(pe.rhold, ms);
695
+ }
696
+ this.dispatch(this.rhold, ms);
697
+ } else {
698
+ ms.rightButtonHold = true;
699
+ if (po) {
700
+ pe = po.rendererEvents;
701
+ pe && pe.dispatch(pe.rdown, ms);
702
+ }
703
+ this.dispatch(this.rdown, ms);
704
+ }
705
+ }
706
+
707
+ if (ms.middleButtonDown) {
708
+ if (ms.middleButtonHold) {
709
+ if (po) {
710
+ pe = po.rendererEvents;
711
+ pe && pe.dispatch(pe.mhold, ms);
712
+ }
713
+ this.dispatch(this.mhold, ms);
714
+ } else {
715
+ ms.middleButtonHold = true;
716
+ if (po) {
717
+ pe = po.rendererEvents;
718
+ pe && pe.dispatch(pe.mdown, ms);
719
+ }
720
+ this.dispatch(this.mdown, ms);
721
+ }
722
+ }
723
+
724
+ if (ms.leftButtonUp) {
725
+ if (po) {
726
+ pe = po.rendererEvents;
727
+ pe && pe.dispatch(pe.lup, ms);
728
+ }
729
+ this.dispatch(this.lup, ms);
730
+ ms.leftButtonUp = false;
731
+ ms.leftButtonHold = false;
732
+ }
733
+
734
+ if (ms.rightButtonUp) {
735
+ if (po) {
736
+ pe = po.rendererEvents;
737
+ pe && pe.dispatch(pe.rup, ms);
738
+ }
739
+ this.dispatch(this.rup, ms);
740
+ ms.rightButtonUp = false;
741
+ ms.rightButtonHold = false;
742
+ }
743
+
744
+ if (ms.middleButtonUp) {
745
+ if (po) {
746
+ pe = po.rendererEvents;
747
+ pe && pe.dispatch(pe.mup, ms);
748
+ }
749
+ this.dispatch(this.mup, ms);
750
+ ms.middleButtonUp = false;
751
+ ms.middleButtonHold = false;
752
+ }
753
+
754
+ if (ms.leftButtonDoubleClick) {
755
+ if (po) {
756
+ pe = po.rendererEvents;
757
+ pe && pe.dispatch(pe.ldblclick, ms);
758
+ }
759
+ this.dispatch(this.ldblclick, ms);
760
+ ms.leftButtonDoubleClick = false;
761
+ }
762
+
763
+ if (ms.rightButtonDoubleClick) {
764
+ if (po) {
765
+ pe = po.rendererEvents;
766
+ pe && pe.dispatch(pe.rdblclick, ms);
767
+ }
768
+ this.dispatch(this.rdblclick, ms);
769
+ ms.rightButtonDoubleClick = false;
770
+ }
771
+
772
+ if (ms.middleButtonDoubleClick) {
773
+ if (po) {
774
+ pe = po.rendererEvents;
775
+ pe && pe.dispatch(pe.mdblclick, ms);
776
+ }
777
+ this.dispatch(this.mdblclick, ms);
778
+ ms.middleButtonDoubleClick = false;
779
+ }
780
+
781
+ if (ms.wheelDelta) {
782
+ if (po) {
783
+ pe = po.rendererEvents;
784
+ pe && pe.dispatch(pe.mousewheel, ms);
785
+ }
786
+ this.dispatch(this.mousewheel, ms);
787
+ }
788
+
789
+ if (ms.moving) {
790
+ if (po) {
791
+ pe = po.rendererEvents;
792
+ pe && pe.dispatch(pe.mousemove, ms);
793
+ }
794
+ this.dispatch(this.mousemove, ms);
795
+ ms.prev_x = ms.x;
796
+ ms.prev_y = ms.y;
797
+ }
798
+
799
+ if (ms.justStopped) {
800
+ this.dispatch(this.mousestop, ms);
801
+ }
802
+ }
803
+
804
+ /**
805
+ * @private
806
+ */
807
+ handleTouchEvents() {
808
+ var ts = this.touchState;
809
+
810
+ var tpo = ts.pickingObject,
811
+ tpe = null;
812
+
813
+ if (ts.touchCancel) {
814
+ this.dispatch(this.touchcancel, ts);
815
+ ts.touchCancel = false;
816
+ }
817
+
818
+ if (ts.touchStart) {
819
+ var r = this.renderer;
820
+
821
+ r.pickingFramebuffer.activate();
822
+ r.pickingFramebuffer.readPixels(r._currPickingColor, ts.nx, 1.0 - ts.ny, 1);
823
+ r.pickingFramebuffer.deactivate();
824
+
825
+ var o = r.colorObjects;
826
+ var c = r._currPickingColor;
827
+ var co = o[c[0] + "_" + c[1] + "_" + c[2]];
828
+ tpo = ts.pickingObject = co;
829
+ if (tpo) {
830
+ tpe = tpo.rendererEvents;
831
+ tpe && tpe.dispatch(tpe.touchstart, ts);
832
+ }
833
+ this.dispatch(this.touchstart, ts);
834
+ ts.touchStart = false;
835
+ }
836
+
837
+ if (ts.doubleTouch) {
838
+ if (tpo) {
839
+ tpe = tpo.rendererEvents;
840
+ tpe && tpe.dispatch(tpe.doubletouch, ts);
841
+ }
842
+ this.dispatch(this.doubletouch, ts);
843
+ ts.doubleTouch = false;
844
+ }
845
+
846
+ if (ts.touchEnd) {
847
+ if (tpo) {
848
+ tpe = tpo.rendererEvents;
849
+ tpe && tpe.dispatch(tpe.touchend, ts);
850
+ }
851
+ this.dispatch(this.touchend, ts);
852
+ ts.x = 0;
853
+ ts.y = 0;
854
+ ts.touchEnd = false;
855
+ }
856
+
857
+ if (ts.moving) {
858
+ if (tpo) {
859
+ tpe = tpo.rendererEvents;
860
+ tpe && tpe.dispatch(tpe.touchmove, ts);
861
+ }
862
+ this.dispatch(this.touchmove, ts);
863
+ ts.prev_x = ts.x;
864
+ ts.prev_y = ts.y;
865
+ }
866
+ }
867
+ }
868
+
869
+ const EVENT_NAMES = [
870
+ /**
871
+ * Triggered before scene frame is rendered(before render nodes).
872
+ * @event og.RendererEvents#draw
873
+ */
874
+ "draw",
875
+
876
+ /**
877
+ * Triggered after scene frame is rendered(after render nodes).
878
+ * @event og.RendererEvents#postdraw
879
+ */
880
+ "postdraw",
881
+
882
+ /**
883
+ * Triggered when screen is resized.
884
+ * @event og.RendererEvents#resize
885
+ */
886
+ "resize",
887
+
888
+ /**
889
+ * Mouse enters the work screen
890
+ * @event og.RendererEvents#mouseenter
891
+ */
892
+ "mouseenter",
893
+
894
+ /**
895
+ * Mouse leaves the work screen
896
+ * @event og.RendererEvents#mouseleave
897
+ */
898
+ "mouseleave",
899
+
900
+ /**
901
+ * Mouse is moving.
902
+ * @event og.RendererEvents#mousemove
903
+ */
904
+ "mousemove",
905
+
906
+ /**
907
+ * Mouse is just stopped.
908
+ * @event og.RendererEvents#mousestop
909
+ */
910
+ "mousestop",
911
+
912
+ /**
913
+ * Mouse left button clicked.
914
+ * @event og.RendererEvents#lclick
915
+ */
916
+ "lclick",
917
+
918
+ /**
919
+ * Mouse right button clicked.
920
+ * @event og.RendererEvents#rclick
921
+ */
922
+ "rclick",
923
+
924
+ /**
925
+ * Mouse middle button clicked.
926
+ * @event og.RendererEvents#mclick
927
+ */
928
+ "mclick",
929
+
930
+ /**
931
+ * Mouse left button double click.
932
+ * @event og.RendererEvents#ldblclick
933
+ */
934
+ "ldblclick",
935
+
936
+ /**
937
+ * Mouse right button double click.
938
+ * @event og.RendererEvents#rdblclick
939
+ */
940
+ "rdblclick",
941
+
942
+ /**
943
+ * Mouse middle button double click.
944
+ * @event og.RendererEvents#mdblclick
945
+ */
946
+ "mdblclick",
947
+
948
+ /**
949
+ * Mouse left button up(stop pressing).
950
+ * @event og.RendererEvents#lup
951
+ */
952
+ "lup",
953
+
954
+ /**
955
+ * Mouse right button up(stop pressing).
956
+ * @event og.RendererEvents#rup
957
+ */
958
+ "rup",
959
+
960
+ /**
961
+ * Mouse middle button up(stop pressing).
962
+ * @event og.RendererEvents#mup
963
+ */
964
+ "mup",
965
+
966
+ /**
967
+ * Mouse left button is just pressed down(start pressing).
968
+ * @event og.RendererEvents#ldown
969
+ */
970
+ "ldown",
971
+
972
+ /**
973
+ * Mouse right button is just pressed down(start pressing).
974
+ * @event og.RendererEvents#rdown
975
+ */
976
+ "rdown",
977
+
978
+ /**
979
+ * Mouse middle button is just pressed down(start pressing).
980
+ * @event og.RendererEvents#mdown
981
+ */
982
+ "mdown",
983
+
984
+ /**
985
+ * Mouse left button is pressing.
986
+ * @event og.RendererEvents#lhold
987
+ */
988
+ "lhold",
989
+
990
+ /**
991
+ * Mouse right button is pressing.
992
+ * @event og.RendererEvents#rhold
993
+ */
994
+ "rhold",
995
+
996
+ /**
997
+ * Mouse middle button is pressing.
998
+ * @event og.RendererEvents#mhold
999
+ */
1000
+ "mhold",
1001
+
1002
+ /**
1003
+ * Mouse wheel is rotated.
1004
+ * @event og.RendererEvents#mousewheel
1005
+ */
1006
+ "mousewheel",
1007
+
1008
+ /**
1009
+ * Triggered when touching starts.
1010
+ * @event og.RendererEvents#touchstart
1011
+ */
1012
+ "touchstart",
1013
+
1014
+ /**
1015
+ * Triggered when touching ends.
1016
+ * @event og.RendererEvents#touchend
1017
+ */
1018
+ "touchend",
1019
+
1020
+ /**
1021
+ * Triggered when touching cancel.
1022
+ * @event og.RendererEvents#touchcancel
1023
+ */
1024
+ "touchcancel",
1025
+
1026
+ /**
1027
+ * Triggered when touch is move.
1028
+ * @event og.RendererEvents#touchmove
1029
+ */
1030
+ "touchmove",
1031
+
1032
+ /**
1033
+ * Triggered when double touch.
1034
+ * @event og.RendererEvents#doubletouch
1035
+ */
1036
+ "doubletouch",
1037
+
1038
+ /**
1039
+ * Triggered when touch leaves picked object.
1040
+ * @event og.RendererEvents#touchleave
1041
+ */
1042
+ "touchleave",
1043
+
1044
+ /**
1045
+ * Triggered when touch enter picking object.
1046
+ * @event og.RendererEvents#touchenter
1047
+ */
1048
+ "touchenter"
1049
+ ];
1050
+
1051
+ export { RendererEvents };