Преглед изворни кода

PointerTracker: normalize IDs across browsers

customisations
alemart пре 10 месеци
родитељ
комит
70ec82cc12
1 измењених фајлова са 34 додато и 4 уклоњено
  1. 34
    4
      src/trackers/pointer-tracker/pointer-tracker.ts

+ 34
- 4
src/trackers/pointer-tracker/pointer-tracker.ts Прегледај датотеку

82
     /** new pointers */
82
     /** new pointers */
83
     private _newPointers: Map<number, TrackablePointer>;
83
     private _newPointers: Map<number, TrackablePointer>;
84
 
84
 
85
+    /** helper map for normalizing IDs */
86
+    private _idMap: Map<number, number>;
87
+
85
     /** previous output */
88
     /** previous output */
86
     private _previousOutput: PointerTrackerOutput;
89
     private _previousOutput: PointerTrackerOutput;
87
 
90
 
91
     /** helper flag */
94
     /** helper flag */
92
     private _wantToReset: boolean;
95
     private _wantToReset: boolean;
93
 
96
 
97
+    /** auto-increment ID */
98
+    private _nextId: number;
99
+
94
 
100
 
95
 
101
 
96
     /**
102
     /**
102
         this._viewport = null;
108
         this._viewport = null;
103
         this._activePointers = new Map();
109
         this._activePointers = new Map();
104
         this._newPointers = new Map();
110
         this._newPointers = new Map();
111
+        this._idMap = new Map();
112
+        this._nextId = 1;
105
         this._previousOutput = this._generateOutput();
113
         this._previousOutput = this._generateOutput();
106
         this._previousUpdateTime = Number.POSITIVE_INFINITY;
114
         this._previousUpdateTime = Number.POSITIVE_INFINITY;
107
         this._wantToReset = false;
115
         this._wantToReset = false;
161
         this._viewport = null;
169
         this._viewport = null;
162
         this._activePointers.clear();
170
         this._activePointers.clear();
163
         this._newPointers.clear();
171
         this._newPointers.clear();
172
+        this._idMap.clear();
164
 
173
 
165
         document.removeEventListener('visibilitychange', this._resetInTheNextUpdate);
174
         document.removeEventListener('visibilitychange', this._resetInTheNextUpdate);
166
 
175
 
210
                 return Speedy.Promise.reject(new IllegalOperationError('Invalid PointerEvent type ' + event.type));
219
                 return Speedy.Promise.reject(new IllegalOperationError('Invalid PointerEvent type ' + event.type));
211
 
220
 
212
             // determine the ID
221
             // determine the ID
213
-            // XXX different hardware devices acting simultaneously may produce
214
-            // events with the same pointerId - handling this seems overkill?
215
-            const id = event.pointerId;
222
+            const id = this._normalizeId(event.pointerId, event.pointerType);
216
 
223
 
217
             // determine the previous states, if any, of the trackable
224
             // determine the previous states, if any, of the trackable
218
             const previous = this._activePointers.get(id); // state in the previous frame
225
             const previous = this._activePointers.get(id); // state in the previous frame
289
 
296
 
290
             // determine the initial position
297
             // determine the initial position
291
             const initialPosition = previous ? previous.initialPosition :
298
             const initialPosition = previous ? previous.initialPosition :
292
-                                    Object.freeze(position._clone());
299
+                                    Object.freeze(position._clone()) as Vector2;
293
 
300
 
294
             // determine the velocity
301
             // determine the velocity
295
             const velocity = deltaPosition._clone()._scale(inverseDeltaTime);
302
             const velocity = deltaPosition._clone()._scale(inverseDeltaTime);
314
         this._newPointers.clear();
321
         this._newPointers.clear();
315
         this._advanceAllStationaryTrackables(deltaTime);
322
         this._advanceAllStationaryTrackables(deltaTime);
316
 
323
 
324
+        // discard unused IDs
325
+        if(this._activePointers.size == 0 && this._idMap.size > 0)
326
+            this._idMap.clear();
327
+
317
         // generate output
328
         // generate output
318
         this._previousOutput = this._generateOutput();
329
         this._previousOutput = this._generateOutput();
319
 
330
 
392
     }
403
     }
393
 
404
 
394
     /**
405
     /**
406
+     * Normalize pointer IDs across browsers
407
+     * @param pointerId browser-provided pointer ID
408
+     * @param pointerType pointer type
409
+     * @returns a normalized pointer ID
410
+     */
411
+    private _normalizeId(pointerId: number, pointerType: string): number
412
+    {
413
+        // XXX different hardware devices acting simultaneously may produce
414
+        // events with the same pointerId - handling this seems overkill?
415
+        if(pointerType == 'mouse')
416
+            return 0;
417
+
418
+        if(!this._idMap.has(pointerId))
419
+            this._idMap.set(pointerId, this._nextId++);
420
+
421
+        return this._idMap.get(pointerId)!;
422
+    }
423
+
424
+    /**
395
      * Cancel all active pointers and consume all events
425
      * Cancel all active pointers and consume all events
396
      * @param deltaTime
426
      * @param deltaTime
397
      */
427
      */

Loading…
Откажи
Сачувај