浏览代码

Simplify and refactor the Viewport implementation

customisations
alemart 1年前
父节点
当前提交
9957421ba4
共有 4 个文件被更改,包括 529 次插入616 次删除
  1. 9
    7
      src/core/hud.ts
  2. 22
    10
      src/core/session.ts
  3. 496
    597
      src/core/viewport.ts
  4. 2
    2
      src/main.ts

+ 9
- 7
src/core/hud.ts 查看文件

21
  */
21
  */
22
 
22
 
23
 import { Nullable, Utils } from "../utils/utils";
23
 import { Nullable, Utils } from "../utils/utils";
24
-import { ViewportContainer } from "./viewport";
25
-import { IllegalArgumentError } from "../utils/errors";
26
 
24
 
27
 /** HUD container */
25
 /** HUD container */
28
 export type HUDContainer = HTMLDivElement;
26
 export type HUDContainer = HTMLDivElement;
36
     private _container: HUDContainer;
34
     private _container: HUDContainer;
37
 
35
 
38
     /** Whether or not we have created our own container */
36
     /** Whether or not we have created our own container */
39
-    private _ownContainer: boolean;
37
+    private _isOwnContainer: boolean;
40
 
38
 
41
 
39
 
42
 
40
 
48
     constructor(parent: HTMLElement, hudContainer: Nullable<HUDContainer>)
46
     constructor(parent: HTMLElement, hudContainer: Nullable<HUDContainer>)
49
     {
47
     {
50
         this._container = hudContainer || this._createContainer(parent);
48
         this._container = hudContainer || this._createContainer(parent);
51
-        this._ownContainer = (hudContainer == null);
49
+        this._isOwnContainer = (hudContainer == null);
52
 
50
 
53
         // move the HUD container to the parent node
51
         // move the HUD container to the parent node
54
         if(this._container.parentElement !== parent) {
52
         if(this._container.parentElement !== parent) {
57
         }
55
         }
58
 
56
 
59
         // the HUD should be hidden initially
57
         // the HUD should be hidden initially
60
-        if(!this._container.hidden)
58
+        if(!this._container.hidden) {
61
             Utils.warning(`The container of the HUD should have the hidden attribute`);
59
             Utils.warning(`The container of the HUD should have the hidden attribute`);
60
+            this._container.hidden = true;
61
+        }
62
     }
62
     }
63
 
63
 
64
     /**
64
     /**
100
         container.style.padding = container.style.margin = '0px';
100
         container.style.padding = container.style.margin = '0px';
101
         container.style.zIndex = String(zIndex);
101
         container.style.zIndex = String(zIndex);
102
         container.style.userSelect = 'none';
102
         container.style.userSelect = 'none';
103
+
104
+        this.visible = true;
103
     }
105
     }
104
 
106
 
105
     /**
107
     /**
108
      */
110
      */
109
     _release(): void
111
     _release(): void
110
     {
112
     {
111
-        if(this._ownContainer) {
112
-            this._ownContainer = false;
113
+        if(this._isOwnContainer) {
114
+            this._isOwnContainer = false;
113
             this._container.remove();
115
             this._container.remove();
114
         }
116
         }
115
     }
117
     }

+ 22
- 10
src/core/session.ts 查看文件

26
 import { Nullable, Utils } from '../utils/utils';
26
 import { Nullable, Utils } from '../utils/utils';
27
 import { AREvent, AREventTarget } from '../utils/ar-events';
27
 import { AREvent, AREventTarget } from '../utils/ar-events';
28
 import { IllegalArgumentError, IllegalOperationError, NotSupportedError } from '../utils/errors';
28
 import { IllegalArgumentError, IllegalOperationError, NotSupportedError } from '../utils/errors';
29
-import { Viewport, BaseViewport, ImmersiveViewport, InlineViewport } from './viewport';
29
+import { Viewport } from './viewport';
30
 import { Settings } from './settings';
30
 import { Settings } from './settings';
31
 import { Stats } from './stats';
31
 import { Stats } from './stats';
32
 import { StatsPanel } from './stats-panel';
32
 import { StatsPanel } from './stats-panel';
53
     sources: Source[];
53
     sources: Source[];
54
 
54
 
55
     /** viewport */
55
     /** viewport */
56
-    viewport: Nullable<BaseViewport>;
56
+    viewport: Nullable<Viewport>;
57
 
57
 
58
     /** show stats? */
58
     /** show stats? */
59
     stats?: boolean;
59
     stats?: boolean;
141
      * @param stats render stats panel?
141
      * @param stats render stats panel?
142
      * @param gizmos render gizmos?
142
      * @param gizmos render gizmos?
143
      */
143
      */
144
-    private constructor(sources: Source[], mode: SessionMode, viewport: BaseViewport, stats: boolean, gizmos: boolean)
144
+    private constructor(sources: Source[], mode: SessionMode, viewport: Viewport, stats: boolean, gizmos: boolean)
145
     {
145
     {
146
         super();
146
         super();
147
 
147
 
157
         this._gizmos = new Gizmos();
157
         this._gizmos = new Gizmos();
158
         this._gizmos.visible = gizmos;
158
         this._gizmos.visible = gizmos;
159
 
159
 
160
+        // validate the mode
161
+        if(mode == 'immersive') {
162
+            if(viewport.style != 'best-fit' && viewport.style != 'stretch') {
163
+                Utils.warning(`Invalid viewport style \"${viewport.style}\" for the \"${mode}\" mode`);
164
+                viewport.style = 'best-fit';
165
+            }
166
+        }
167
+        else if(mode == 'inline') {
168
+            if(viewport.style != 'inline') {
169
+                Utils.warning(`Invalid viewport style \"${viewport.style}\" for the \"${mode}\" mode`);
170
+                viewport.style = 'inline';
171
+            }
172
+        }
173
+        else
174
+            throw new IllegalArgumentError(`Invalid session mode "${mode}"`);
175
+
160
         // get media
176
         // get media
161
         const media = this.media;
177
         const media = this.media;
178
+        const getMediaSize = () => media.size;
162
 
179
 
163
         // setup the viewport
180
         // setup the viewport
164
-        if(mode == 'immersive')
165
-            this._viewport = new ImmersiveViewport(viewport, () => media.size);
166
-        else if(mode == 'inline')
167
-            this._viewport = new InlineViewport(viewport, () => media.size);
168
-        else
169
-            throw new IllegalArgumentError(`Invalid session mode "${mode}"`);
170
-        this._viewport._init();
181
+        this._viewport = viewport;
182
+        this._viewport._init(getMediaSize);
171
 
183
 
172
         // setup the main loop
184
         // setup the main loop
173
         this._setupUpdateLoop();
185
         this._setupUpdateLoop();

+ 496
- 597
src/core/viewport.ts
文件差异内容过多而无法显示
查看文件


+ 2
- 2
src/main.ts 查看文件

26
 import { Session, SessionOptions } from './core/session';
26
 import { Session, SessionOptions } from './core/session';
27
 import { TrackerFactory } from './trackers/tracker-factory';
27
 import { TrackerFactory } from './trackers/tracker-factory';
28
 import { SourceFactory } from './sources/source-factory';
28
 import { SourceFactory } from './sources/source-factory';
29
-import { Viewport, ViewportSettings, BaseViewport } from './core/viewport';
29
+import { Viewport, ViewportSettings } from './core/viewport';
30
 import { Utils } from './utils/utils';
30
 import { Utils } from './utils/utils';
31
 declare const __MARTINS_VERSION__: string;
31
 declare const __MARTINS_VERSION__: string;
32
 declare const __MARTINS_DEVELOPMENT_MODE__: string;
32
 declare const __MARTINS_DEVELOPMENT_MODE__: string;
70
      */
70
      */
71
     static Viewport(settings: ViewportSettings): Viewport
71
     static Viewport(settings: ViewportSettings): Viewport
72
     {
72
     {
73
-        return new BaseViewport(settings);
73
+        return new Viewport(settings);
74
     }
74
     }
75
 
75
 
76
     /**
76
     /**

正在加载...
取消
保存