Browse Source

More Cicular dep

sebastien 6 years ago
parent
commit
6ef001c34b

+ 10 - 1
src/Gamepads/Controllers/daydreamController.ts

@@ -7,7 +7,7 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { GamepadButtonChanges } from "Gamepads/gamepad";
 import { WebVRController } from "./webVRController";
-import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledController";
+import { PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 
     /**
      * Google Daydream controller
@@ -72,3 +72,12 @@ import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledC
             }
         }
     }
+
+    PoseEnabledControllerHelper._ControllerFactories.push({
+        canCreate: (gamepadInfo) => {
+            return gamepadInfo.id.indexOf(DaydreamController.GAMEPAD_ID_PREFIX) === 0;
+        },
+        create: (gamepadInfo) => {
+            return new DaydreamController(gamepadInfo);
+        }
+    });

+ 11 - 1
src/Gamepads/Controllers/gearVRController.ts

@@ -8,7 +8,7 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { GamepadButtonChanges } from "Gamepads/gamepad";
 import { WebVRController } from "./webVRController";
-import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledController";
+import { PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 
 /**
      * Gear VR Controller
@@ -82,3 +82,13 @@ import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledC
             }
         }
     }
+
+    PoseEnabledControllerHelper._ControllerFactories.push({
+        canCreate: (gamepadInfo) => {
+            return gamepadInfo.id.indexOf(GearVRController.GAMEPAD_ID_PREFIX) === 0 ||
+                gamepadInfo.id.indexOf('Oculus Go') !== -1;
+        },
+        create: (gamepadInfo) => {
+            return new GearVRController(gamepadInfo);
+        }
+    });

+ 3 - 1
src/Gamepads/Controllers/genericController.ts

@@ -5,7 +5,7 @@ import { SceneLoader } from "Loading/sceneLoader";
 import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { WebVRController } from "./webVRController";
-import { ExtendedGamepadButton } from "./poseEnabledController";
+import { ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 import { GamepadButtonChanges } from "Gamepads/gamepad";
 
     /**
@@ -55,3 +55,5 @@ import { GamepadButtonChanges } from "Gamepads/gamepad";
             console.dir(state);
         }
     }
+
+    PoseEnabledControllerHelper._DefaultControllerFactory = (gamepadInfo: any) => new GenericController(gamepadInfo);

+ 10 - 2
src/Gamepads/Controllers/oculusTouchController.ts

@@ -6,7 +6,7 @@ import { SceneLoader } from "Loading/sceneLoader";
 import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { WebVRController } from "./webVRController";
-import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledController";
+import { PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 import { GamepadButtonChanges } from "Gamepads/gamepad";
     /**
      * Oculus Touch Controller
@@ -184,5 +184,13 @@ import { GamepadButtonChanges } from "Gamepads/gamepad";
                     return;
             }
         }
-
     }
+
+    PoseEnabledControllerHelper._ControllerFactories.push({
+        canCreate: (gamepadInfo) => {
+            return gamepadInfo.id.indexOf('Oculus Touch') !== -1;
+        },
+        create: (gamepadInfo) => {
+            return new OculusTouchController(gamepadInfo);
+        }
+    });

+ 32 - 30
src/Gamepads/Controllers/poseEnabledController.ts

@@ -11,14 +11,6 @@ import { Engine } from "Engines/engine";
 
 import { Gamepad } from "Gamepads/gamepad";
 import { ExtendedGamepadButton } from "./poseEnabledController";
-
-import { OculusTouchController } from "Gamepads/Controllers/oculusTouchController";
-import { WindowsMotionController } from "Gamepads/Controllers/windowsMotionController";
-import { ViveController } from "Gamepads/Controllers/viveController";
-import { GearVRController } from "Gamepads/Controllers/gearVRController";
-import { DaydreamController } from "Gamepads/Controllers/daydreamController";
-import { GenericController } from "Gamepads/Controllers/genericController";
-
 import { WebVRFreeCamera, PoseControlled, DevicePose } from "Cameras/VR/webVRCamera";
 import { TargetCamera } from "Cameras/targetCamera";
     /**
@@ -88,40 +80,50 @@ import { TargetCamera } from "Cameras/targetCamera";
         readonly value: number;
     }
 
+    /** @hidden */
+    export interface _GamePadFactory {
+        /**
+         * Returns wether or not the current gamepad can be created for this type of controller.
+         * @param gamepadInfo Defines the gamepad info as receveid from the controller APIs.
+         * @returns true if it can be created, otherwise false
+         */
+        canCreate(gamepadInfo: any): boolean;
+
+        /**
+         * Creates a new instance of the Gamepad.
+         * @param gamepadInfo Defines the gamepad info as receveid from the controller APIs.
+         * @returns the new gamepad instance
+         */
+        create(gamepadInfo: any): Gamepad;
+    }
+
     /**
      * Defines the PoseEnabledControllerHelper object that is used initialize a gamepad as the controller type it is specified as (eg. windows mixed reality controller)
      */
     export class PoseEnabledControllerHelper {
+        /** @hidden */
+        public static _ControllerFactories: _GamePadFactory[] = [];
+
+        /** @hidden */
+        public static _DefaultControllerFactory: Nullable<(gamepadInfo: any) => Gamepad> = null;
+
         /**
          * Initializes a gamepad as the controller type it is specified as (eg. windows mixed reality controller)
          * @param vrGamepad the gamepad to initialized
          * @returns a vr controller of the type the gamepad identified as
          */
         public static InitiateController(vrGamepad: any) {
-            // Oculus Touch
-            if (vrGamepad.id.indexOf('Oculus Touch') !== -1) {
-                return new OculusTouchController(vrGamepad);
-            }
-            // Windows Mixed Reality controllers
-            else if (vrGamepad.id.indexOf(WindowsMotionController.GAMEPAD_ID_PREFIX) === 0) {
-                return new WindowsMotionController(vrGamepad);
-            }
-            // HTC Vive
-            else if (vrGamepad.id.toLowerCase().indexOf('openvr') !== -1) {
-                return new ViveController(vrGamepad);
-            }
-            // Samsung/Oculus Gear VR or Oculus Go
-            else if (vrGamepad.id.indexOf(GearVRController.GAMEPAD_ID_PREFIX) === 0 || vrGamepad.id.indexOf('Oculus Go') !== -1) {
-                return new GearVRController(vrGamepad);
-            }
-            // Google Daydream
-            else if (vrGamepad.id.indexOf(DaydreamController.GAMEPAD_ID_PREFIX) === 0) {
-                return new DaydreamController(vrGamepad);
+            for (let factory of this._ControllerFactories) {
+                if (factory.canCreate(vrGamepad)) {
+                    return factory.create(vrGamepad);
+                }
             }
-            // Generic
-            else {
-                return new GenericController(vrGamepad);
+
+            if (this._DefaultControllerFactory) {
+                return this._DefaultControllerFactory(vrGamepad);
             }
+
+            throw "The type of gamepad you are trying to load needs to be imported first or is not supported.";
         }
     }
 

+ 10 - 1
src/Gamepads/Controllers/viveController.ts

@@ -5,7 +5,7 @@ import { SceneLoader } from "Loading/sceneLoader";
 import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { WebVRController } from "./webVRController";
-import { PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledController";
+import { PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 import { GamepadButtonChanges } from "Gamepads/gamepad";
 
     /**
@@ -118,3 +118,12 @@ import { GamepadButtonChanges } from "Gamepads/gamepad";
             }
         }
     }
+
+    PoseEnabledControllerHelper._ControllerFactories.push({
+        canCreate: (gamepadInfo) => {
+            return gamepadInfo.id.toLowerCase().indexOf('openvr') !== -1;
+        },
+        create: (gamepadInfo) => {
+            return new ViveController(gamepadInfo);
+        }
+    });

+ 11 - 1
src/Gamepads/Controllers/windowsMotionController.ts

@@ -14,8 +14,9 @@ import { _DepthCullingState, _StencilState, _AlphaState } from "States";
 
 import { WebVRController } from "./webVRController";
 import { GenericController } from "./genericController";
-import { PoseEnabledController, PoseEnabledControllerType, ExtendedGamepadButton } from "./poseEnabledController";
+import { PoseEnabledController, PoseEnabledControllerType, ExtendedGamepadButton, PoseEnabledControllerHelper } from "./poseEnabledController";
 import { StickValues, GamepadButtonChanges } from "Gamepads/gamepad";
+
     /**
      * Defines the LoadedMeshInfo object that describes information about the loaded webVR controller mesh
      */
@@ -538,3 +539,12 @@ import { StickValues, GamepadButtonChanges } from "Gamepads/gamepad";
             this.onTrackpadChangedObservable.clear();
         }
     }
+
+    PoseEnabledControllerHelper._ControllerFactories.push({
+        canCreate: (gamepadInfo) => {
+            return gamepadInfo.id.indexOf(WindowsMotionController.GAMEPAD_ID_PREFIX) === 0;
+        },
+        create: (gamepadInfo) => {
+            return new WindowsMotionController(gamepadInfo);
+        }
+    });

+ 23 - 1
src/Rendering/renderingManager.ts

@@ -1,5 +1,4 @@
 import { Nullable } from "types";
-import { RenderingGroupInfo, Scene } from "scene";
 import { AbstractMesh } from "Meshes/abstractMesh";
 import { SubMesh } from "Meshes/subMesh";
 import { SmartArray } from "Misc/smartArray";
@@ -8,6 +7,9 @@ import { IParticleSystem } from "Particles/IParticleSystem";
 import { Material } from "Materials/material";
 import { RenderingGroup } from "./renderingGroup";
 
+declare type Scene = import("scene").Scene;
+declare type Camera = import("Cameras/camera").Camera;
+
     /**
      * Interface describing the different options available in the rendering manager
      * regarding Auto Clear between groups.
@@ -28,6 +30,26 @@ import { RenderingGroup } from "./renderingGroup";
     }
 
     /**
+     * This class is used by the onRenderingGroupObservable
+     */
+    export class RenderingGroupInfo {
+        /**
+         * The Scene that being rendered
+         */
+        scene: Scene;
+
+        /**
+         * The camera currently used for the rendering pass
+         */
+        camera: Nullable<Camera>;
+
+        /**
+         * The ID of the renderingGroup being processed
+         */
+        renderingGroupId: number;
+    }
+
+    /**
      * This is the manager responsible of all the rendering for meshes sprites and particles.
      * It is enable to manage the different groups as well as the different necessary sort functions.
      * This should not be used directly aside of the few static configurations

+ 1 - 21
src/scene.ts

@@ -41,7 +41,7 @@ import { ActionManager } from "Actions/actionManager";
 import { PostProcess } from "PostProcesses/postProcess";
 import { PostProcessManager } from "PostProcesses/postProcessManager";
 import { IOfflineProvider } from "Offline/IOfflineProvider";
-import { RenderingManager, IRenderingManagerAutoClearSetup } from "Rendering/renderingManager";
+import { RenderingGroupInfo, RenderingManager, IRenderingManagerAutoClearSetup } from "Rendering/renderingManager";
 import { ISceneComponent, ISceneSerializableComponent, Stage, SimpleStageAction, RenderTargetsStageAction, RenderTargetStageAction, MeshStageAction, EvaluateSubMeshStageAction, ActiveMeshStageAction, CameraStageAction, RenderingGroupStageAction, RenderingMeshStageAction, PointerMoveStageAction, PointerUpDownStageAction } from "sceneComponent";
 import { Engine } from "Engines/engine";
 import { Ray } from "Culling/ray";
@@ -95,26 +95,6 @@ import { Logger } from "Misc/logger";
         }
     }
 
-    /**
-     * This class is used by the onRenderingGroupObservable
-     */
-    export class RenderingGroupInfo {
-        /**
-         * The Scene that being rendered
-         */
-        scene: Scene;
-
-        /**
-         * The camera currently used for the rendering pass
-         */
-        camera: Nullable<Camera>;
-
-        /**
-         * The ID of the renderingGroup being processed
-         */
-        renderingGroupId: number;
-    }
-
     /** Interface defining initialization parameters for Scene class */
     export interface SceneOptions {
         /**