Browse Source

Merge pull request #4349 from RaananW/configuration-stuff

Camera behaviors can now be disabled using configuration
David Catuhe 7 years ago
parent
commit
4384c14d71

+ 6 - 4
Viewer/src/configuration/configuration.ts

@@ -438,10 +438,7 @@ export interface ICameraConfiguration {
     exposure?: number;
     pinchPrecision?: number;
     behaviors?: {
-        [name: string]: number | {
-            type: number;
-            [propName: string]: any;
-        };
+        [name: string]: boolean | number | ICameraBehaviorConfiguration;
     };
     disableCameraControl?: boolean;
     disableCtrlForPanning?: boolean;
@@ -450,6 +447,11 @@ export interface ICameraConfiguration {
     [propName: string]: any;
 }
 
+export interface ICameraBehaviorConfiguration {
+    type: number;
+    [propName: string]: any;
+}
+
 export interface ILightConfiguration {
     type: number;
     name?: string;

+ 25 - 7
Viewer/src/viewer/sceneManager.ts

@@ -768,8 +768,8 @@ export class SceneManager {
 
         if (cameraConfig.behaviors) {
             for (let name in cameraConfig.behaviors) {
-                if (cameraConfig.behaviors[name]) {
-                    this._setCameraBehavior(cameraConfig.behaviors[name]);
+                if (cameraConfig.behaviors[name] !== undefined) {
+                    this._setCameraBehavior(name, cameraConfig.behaviors[name]);
                 }
             }
         };
@@ -1290,28 +1290,44 @@ export class SceneManager {
         }
     }
 
-    private _setCameraBehavior(behaviorConfig: number | {
+    private _cameraBehaviorMapping: { [name: string]: number } = {};
+
+    private _setCameraBehavior(name: string, behaviorConfig: boolean | number | {
         type: number;
         [propName: string]: any;
     }, payload?: any) {
 
         let behavior: Behavior<ArcRotateCamera> | null;
-        let type = (typeof behaviorConfig !== "object") ? behaviorConfig : behaviorConfig.type;
+        let type: number;
+        if (typeof behaviorConfig === 'object') {
+            type = behaviorConfig.type
+        } else if (typeof behaviorConfig === 'number') {
+            type = behaviorConfig;
+        } else {
+            type = this._cameraBehaviorMapping[name];
+        }
+
+        if (type === undefined) return;
 
         let config: { [propName: string]: any } = (typeof behaviorConfig === "object") ? behaviorConfig : {};
 
+        let enabled = true;
+        if (typeof behaviorConfig === 'boolean') {
+            enabled = behaviorConfig;
+        }
+
         // constructing behavior
         switch (type) {
             case CameraBehavior.AUTOROTATION:
-                this.camera.useAutoRotationBehavior = true;
+                this.camera.useAutoRotationBehavior = enabled;
                 behavior = this.camera.autoRotationBehavior;
                 break;
             case CameraBehavior.BOUNCING:
-                this.camera.useBouncingBehavior = true;
+                this.camera.useBouncingBehavior = enabled;
                 behavior = this.camera.bouncingBehavior;
                 break;
             case CameraBehavior.FRAMING:
-                this.camera.useFramingBehavior = true;
+                this.camera.useFramingBehavior = enabled;
                 behavior = this.camera.framingBehavior;
                 break;
             default:
@@ -1320,6 +1336,8 @@ export class SceneManager {
         }
 
         if (behavior) {
+            this._cameraBehaviorMapping[name] = type;
+
             if (typeof behaviorConfig === "object") {
                 extendClassWithConfig(behavior, behaviorConfig);
             }

+ 31 - 0
Viewer/tests/unit/src/configuration/updateConfiguration.ts

@@ -312,3 +312,34 @@ describe(name + " scene optimizer", () => {
         });
     });
 });
+
+describe(name + " camera", () => {
+
+    it("should enable and disable camera behaviors", (done) => {
+        let viewer = Helper.getNewViewerInstance(undefined, { extends: "none" });
+
+        viewer.onInitDoneObservable.add(() => {
+            assert.isFalse(viewer.sceneManager.camera.useAutoRotationBehavior);
+            viewer.updateConfiguration({
+                camera: {
+                    behaviors: {
+                        autoRotate: {
+                            type: 0
+                        }
+                    }
+                }
+            });
+            assert.isTrue(viewer.sceneManager.camera.useAutoRotationBehavior);
+            viewer.updateConfiguration({
+                camera: {
+                    behaviors: {
+                        autoRotate: false
+                    }
+                }
+            });
+            assert.isFalse(viewer.sceneManager.camera.useAutoRotationBehavior);
+            viewer.dispose();
+            done();
+        });
+    })
+})

+ 1 - 0
dist/preview release/what's new.md

@@ -54,5 +54,6 @@
 - Fix Animation Slider Clickable area size Cross Plat ([sebavan](http://www.github.com/sebavan))
 - Ground material didn't take the default main color is no material definition was provided ([RaananW](https://github.com/RaananW))
 - Model configuration was not extended correctly if loaded more than one model ([RaananW](https://github.com/RaananW))
+- It wasn't possible to disable camera behavior(s) using configuration  [#4348](https://github.com/BabylonJS/Babylon.js/issues/4348) ([RaananW](https://github.com/RaananW))
 
 ## Breaking changes