Browse Source

Make Viewport.style read-only. Improve the code.

customisations
alemart 11 months ago
parent
commit
6dff540a11
3 changed files with 27 additions and 20 deletions
  1. 1
    1
      docs/api/viewport.md
  2. 2
    18
      src/core/session.ts
  3. 24
    1
      src/core/viewport.ts

+ 1
- 1
docs/api/viewport.md View File

67
 
67
 
68
 ### style
68
 ### style
69
 
69
 
70
-`viewport.style: string`
70
+`viewport.style: string, read-only`
71
 
71
 
72
 The style determines the way the viewport appears on the screen. Different styles are applicable to different [session modes](session.md#mode). The following are valid styles:
72
 The style determines the way the viewport appears on the screen. Different styles are applicable to different [session modes](session.md#mode). The following are valid styles:
73
 
73
 

+ 2
- 18
src/core/session.ts View File

158
         this._gizmos.visible = gizmos;
158
         this._gizmos.visible = gizmos;
159
 
159
 
160
         // validate the mode
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
161
+        if(mode != 'immersive' && mode != 'inline')
174
             throw new IllegalArgumentError(`Invalid session mode "${mode}"`);
162
             throw new IllegalArgumentError(`Invalid session mode "${mode}"`);
175
 
163
 
176
-        // get media
177
-        const media = this.media;
178
-        const getMediaSize = () => media.size;
179
-
180
         // setup the viewport
164
         // setup the viewport
181
         this._viewport = viewport;
165
         this._viewport = viewport;
182
-        this._viewport._init(getMediaSize);
166
+        this._viewport._init(() => this.media.size, mode);
183
 
167
 
184
         // setup the main loop
168
         // setup the main loop
185
         this._setupUpdateLoop();
169
         this._setupUpdateLoop();

+ 24
- 1
src/core/viewport.ts View File

27
 import { Nullable } from '../utils/utils';
27
 import { Nullable } from '../utils/utils';
28
 import { Resolution } from '../utils/resolution';
28
 import { Resolution } from '../utils/resolution';
29
 import { Utils } from '../utils/utils';
29
 import { Utils } from '../utils/utils';
30
+import { SessionMode } from './session';
30
 import { HUD, HUDContainer } from './hud';
31
 import { HUD, HUDContainer } from './hud';
31
 import { FullscreenButton } from '../ui/fullscreen-button';
32
 import { FullscreenButton } from '../ui/fullscreen-button';
32
 import { AREvent, AREventTarget, AREventListener } from '../utils/ar-events';
33
 import { AREvent, AREventTarget, AREventListener } from '../utils/ar-events';
847
     /**
848
     /**
848
      * Set viewport style
849
      * Set viewport style
849
      */
850
      */
851
+    /*
850
     set style(value: ViewportStyle)
852
     set style(value: ViewportStyle)
851
     {
853
     {
852
         // note: the viewport style is independent of the session mode!
854
         // note: the viewport style is independent of the session mode!
855
             this._style = value;
857
             this._style = value;
856
         }
858
         }
857
     }
859
     }
860
+    */
858
 
861
 
859
     /**
862
     /**
860
      * HUD
863
      * HUD
955
 
958
 
956
     /**
959
     /**
957
      * Initialize the viewport (when the session starts)
960
      * Initialize the viewport (when the session starts)
961
+     * @param getMediaSize
962
+     * @param sessionMode
958
      * @internal
963
      * @internal
959
      */
964
      */
960
-    _init(getMediaSize: ViewportSizeGetter): void
965
+    _init(getMediaSize: ViewportSizeGetter, sessionMode: SessionMode): void
961
     {
966
     {
967
+        // validate if the viewport style matches the session mode
968
+        if(sessionMode == 'immersive') {
969
+            if(this._style != 'best-fit' && this._style != 'stretch') {
970
+                Utils.warning(`Invalid viewport style \"${this._style}\" for the \"${sessionMode}\" mode`);
971
+                this._style = 'best-fit';
972
+                this._resizer.setStrategyByName(this._style);
973
+            }
974
+        }
975
+        else if(sessionMode == 'inline') {
976
+            if(this._style != 'inline') {
977
+                Utils.warning(`Invalid viewport style \"${this._style}\" for the \"${sessionMode}\" mode`);
978
+                this._style = 'inline';
979
+                this._resizer.setStrategyByName(this._style);
980
+            }
981
+        }
982
+
983
+        // set the media size getter
962
         this._mediaSize = getMediaSize;
984
         this._mediaSize = getMediaSize;
963
 
985
 
986
+        // initialize the components
964
         this._containers.init();
987
         this._containers.init();
965
         this._hud._init(HUD_ZINDEX);
988
         this._hud._init(HUD_ZINDEX);
966
         this._canvases.init();
989
         this._canvases.init();

Loading…
Cancel
Save