Browse Source

Move TrackablePointer and TrackablePointerPhase to a separate file

customisations
alemart 10 months ago
parent
commit
df241432ca

+ 17
- 52
src/trackers/pointer-tracker/pointer-tracker.ts View File

22
 
22
 
23
 import Speedy from 'speedy-vision';
23
 import Speedy from 'speedy-vision';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
24
 import { SpeedyPromise } from 'speedy-vision/types/core/speedy-promise';
25
-import { Trackable, TrackerResult, TrackerOutput, Tracker } from '../tracker';
25
+import { TrackerResult, TrackerOutput, Tracker } from '../tracker';
26
+import { TrackablePointer, TrackablePointerPhase } from './trackable-pointer';
26
 import { PointerSource } from '../../sources/pointer-source';
27
 import { PointerSource } from '../../sources/pointer-source';
27
 import { Vector2 } from '../../geometry/vector2';
28
 import { Vector2 } from '../../geometry/vector2';
28
 import { Utils, Nullable } from '../../utils/utils';
29
 import { Utils, Nullable } from '../../utils/utils';
31
 import { Viewport } from '../../core/viewport';
32
 import { Viewport } from '../../core/viewport';
32
 
33
 
33
 /**
34
 /**
34
- * The possible phases of a TrackablePointer
35
- */
36
-export type TrackablePointerPhase = 'began' | 'moved' | 'stationary' | 'ended' | 'canceled';
37
-
38
-/**
39
- * A trackable representing an instance of pointer-based input such as a mouse
40
- * click or a touch on the screen
41
- */
42
-export interface TrackablePointer extends Trackable
43
-{
44
-    /** a unique identifier assigned to this trackable */
45
-    readonly id: number;
46
-
47
-    /** the phase of the trackable */
48
-    readonly phase: TrackablePointerPhase;
49
-
50
-    /** current position in normalized coordinates [-1,1]x[-1,1] */
51
-    readonly position: Vector2;
52
-
53
-    /** the position delta since the last frame */
54
-    readonly deltaPosition: Vector2;
55
-
56
-    /** the first position of this trackable, given in normalized coordinates */
57
-    readonly initialPosition: Vector2;
58
-
59
-    /** the current velocity, given in normalized coordinates per second */
60
-    readonly velocity: Vector2;
61
-
62
-    /** whether or not this is the primary pointer for this type */
63
-    readonly isPrimary: boolean;
64
-
65
-    /** the type of the originating device; typically "mouse", "touch" or "pen" */
66
-    readonly type: string;
67
-}
68
-
69
-/**
70
  * A result of a PointerTracker. It's meant to be consumed by the user/application
35
  * A result of a PointerTracker. It's meant to be consumed by the user/application
71
  */
36
  */
72
 export interface PointerTrackerResult extends TrackerResult
37
 export interface PointerTrackerResult extends TrackerResult
117
     /** new pointers */
82
     /** new pointers */
118
     private _newPointers: Map<number, TrackablePointer>;
83
     private _newPointers: Map<number, TrackablePointer>;
119
 
84
 
120
-    /** last output */
121
-    private _lastOutput: PointerTrackerOutput;
85
+    /** previous output */
86
+    private _previousOutput: PointerTrackerOutput;
122
 
87
 
123
-    /** time of the last update */
124
-    private _lastUpdateTime: DOMHighResTimeStamp;
88
+    /** time of the previous update */
89
+    private _previousUpdateTime: DOMHighResTimeStamp;
125
 
90
 
126
 
91
 
127
 
92
 
134
         this._viewport = null;
99
         this._viewport = null;
135
         this._activePointers = new Map();
100
         this._activePointers = new Map();
136
         this._newPointers = new Map();
101
         this._newPointers = new Map();
137
-        this._lastOutput = this._generateOutput();
138
-        this._lastUpdateTime = Number.POSITIVE_INFINITY;
102
+        this._previousOutput = this._generateOutput();
103
+        this._previousUpdateTime = Number.POSITIVE_INFINITY;
139
     }
104
     }
140
 
105
 
141
     /**
106
     /**
202
         const canvas = this._viewport!.canvas;
167
         const canvas = this._viewport!.canvas;
203
         const rect = canvas.getBoundingClientRect(); // may be different in different frames!
168
         const rect = canvas.getBoundingClientRect(); // may be different in different frames!
204
 
169
 
205
-        // find the time between this and the last update of this tracker
170
+        // find the time between this and the previous update of this tracker
206
         const deltaTime = this._updateTime();
171
         const deltaTime = this._updateTime();
207
         const inverseDeltaTime = (deltaTime > 1e-5) ? 1 / deltaTime : 60; // 1/dt = 1 / (1/60) with 60 fps
172
         const inverseDeltaTime = (deltaTime > 1e-5) ? 1 / deltaTime : 60; // 1/dt = 1 / (1/60) with 60 fps
208
 
173
 
321
         this._newPointers.clear();
286
         this._newPointers.clear();
322
 
287
 
323
         // generate output
288
         // generate output
324
-        this._lastOutput = this._generateOutput();
289
+        this._previousOutput = this._generateOutput();
325
 
290
 
326
         // test
291
         // test
327
-        //console.log(JSON.stringify(this._lastOutput.exports.trackables, null, 4));
292
+        //console.log(JSON.stringify(this._prevOutput.exports.trackables, null, 4));
328
 
293
 
329
         // done!
294
         // done!
330
         return Speedy.Promise.resolve();
295
         return Speedy.Promise.resolve();
331
     }
296
     }
332
 
297
 
333
     /**
298
     /**
334
-     * Output of the last frame
299
+     * Output of the previous frame
335
      * @internal
300
      * @internal
336
      */
301
      */
337
     get _output(): PointerTrackerOutput
302
     get _output(): PointerTrackerOutput
338
     {
303
     {
339
-        return this._lastOutput;
304
+        return this._previousOutput;
340
     }
305
     }
341
 
306
 
342
     /**
307
     /**
392
     {
357
     {
393
         const now = performance.now() * 0.001;
358
         const now = performance.now() * 0.001;
394
 
359
 
395
-        if(this._lastUpdateTime > now)
396
-            this._lastUpdateTime = now;
360
+        if(this._previousUpdateTime > now)
361
+            this._previousUpdateTime = now;
397
 
362
 
398
-        const prev = this._lastUpdateTime;
399
-        this._lastUpdateTime = now;
363
+        const prev = this._previousUpdateTime;
364
+        this._previousUpdateTime = now;
400
 
365
 
401
         return now - prev;
366
         return now - prev;
402
     }
367
     }

+ 64
- 0
src/trackers/pointer-tracker/trackable-pointer.ts View File

1
+/*
2
+ * encantar.js
3
+ * GPU-accelerated Augmented Reality for the web
4
+ * Copyright (C) 2022-2024 Alexandre Martins <alemartf(at)gmail.com>
5
+ *
6
+ * This program is free software: you can redistribute it and/or modify
7
+ * it under the terms of the GNU Lesser General Public License as published
8
+ * by the Free Software Foundation, either version 3 of the License, or
9
+ * (at your option) any later version.
10
+ *
11
+ * This program is distributed in the hope that it will be useful,
12
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
+ * GNU Lesser General Public License for more details.
15
+ *
16
+ * You should have received a copy of the GNU Lesser General Public License
17
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
+ *
19
+ * trackable-pointer.ts
20
+ * A trackable representing an instance of pointer-based input
21
+ */
22
+
23
+import { Trackable } from '../tracker';
24
+import { Vector2 } from '../../geometry/vector2';
25
+
26
+/**
27
+ * The phase of a TrackablePointer. Possible values:
28
+ * - "began": the tracking began in this frame (e.g., a finger has just touched the screen)
29
+ * - "stationary": the user did not move the pointer in this frame
30
+ * - "moved": the user moved the pointer in this frame
31
+ * - "ended": the tracking ended in this frame (e.g., a finger has just been lifted from the screen)
32
+ * - "canceled": the tracking was canceled in this frame (e.g., the screen orientation of the device has just been changed)
33
+ */
34
+export type TrackablePointerPhase = 'began' | 'moved' | 'stationary' | 'ended' | 'canceled';
35
+
36
+/**
37
+ * A trackable representing an instance of pointer-based input
38
+ */
39
+export interface TrackablePointer extends Trackable
40
+{
41
+    /** a unique identifier assigned to this pointer */
42
+    readonly id: number;
43
+
44
+    /** the phase of the pointer */
45
+    readonly phase: TrackablePointerPhase;
46
+
47
+    /** current position in normalized coordinates [-1,1]x[-1,1] */
48
+    readonly position: Vector2;
49
+
50
+    /** the difference between the position of the pointer in this and in the previous frame */
51
+    readonly deltaPosition: Vector2;
52
+
53
+    /** the position of the pointer when its tracking began */
54
+    readonly initialPosition: Vector2;
55
+
56
+    /** current velocity, given in normalized coordinates per second */
57
+    readonly velocity: Vector2;
58
+
59
+    /** whether or not this is the primary pointer for this type */
60
+    readonly isPrimary: boolean;
61
+
62
+    /** the type of the originating device; typically "mouse", "touch" or "pen" */
63
+    readonly type: string;
64
+}

Loading…
Cancel
Save