소스 검색

Adding optional scene object to the gamepad manager

If scene is defined, the update will be done during the render loop for consistancy. If scene is not provided, the behavior will stay the same.
Raanan Weber 7 년 전
부모
커밋
79fcb2538d
2개의 변경된 파일22개의 추가작업 그리고 15개의 파일을 삭제
  1. 16 14
      src/Gamepad/babylon.gamepadManager.ts
  2. 6 1
      src/babylon.scene.ts

+ 16 - 14
src/Gamepad/babylon.gamepadManager.ts

@@ -3,7 +3,7 @@
         private _babylonGamepads: Array<Gamepad> = [];
         private _babylonGamepads: Array<Gamepad> = [];
         private _oneGamepadConnected: boolean = false;
         private _oneGamepadConnected: boolean = false;
 
 
-        private _isMonitoring: boolean = false;
+        public _isMonitoring: boolean = false;
         private _gamepadEventSupported: boolean;
         private _gamepadEventSupported: boolean;
         private _gamepadSupport: () => Array<any>;
         private _gamepadSupport: () => Array<any>;
 
 
@@ -13,24 +13,23 @@
         private _onGamepadConnectedEvent: Nullable<(evt: any) => void>;
         private _onGamepadConnectedEvent: Nullable<(evt: any) => void>;
         private _onGamepadDisconnectedEvent: Nullable<(evt: any) => void>;
         private _onGamepadDisconnectedEvent: Nullable<(evt: any) => void>;
 
 
-        constructor() {
+        constructor(private _scene?: Scene) {
             if (!Tools.IsWindowObjectExist()) {
             if (!Tools.IsWindowObjectExist()) {
                 this._gamepadEventSupported = false;
                 this._gamepadEventSupported = false;
-            } else  {
+            } else {
                 this._gamepadEventSupported = 'GamepadEvent' in window;
                 this._gamepadEventSupported = 'GamepadEvent' in window;
                 this._gamepadSupport = (navigator.getGamepads ||
                 this._gamepadSupport = (navigator.getGamepads ||
                     navigator.webkitGetGamepads || navigator.msGetGamepads || navigator.webkitGamepads);
                     navigator.webkitGetGamepads || navigator.msGetGamepads || navigator.webkitGamepads);
             }
             }
 
 
-
             this.onGamepadConnectedObservable = new Observable<Gamepad>((observer) => {
             this.onGamepadConnectedObservable = new Observable<Gamepad>((observer) => {
                 // This will be used to raise the onGamepadConnected for all gamepads ALREADY connected
                 // This will be used to raise the onGamepadConnected for all gamepads ALREADY connected
                 for (var i in this._babylonGamepads) {
                 for (var i in this._babylonGamepads) {
                     let gamepad = this._babylonGamepads[i];
                     let gamepad = this._babylonGamepads[i];
-                    if (gamepad && gamepad._isConnected) {                      
+                    if (gamepad && gamepad._isConnected) {
                         this.onGamepadConnectedObservable.notifyObserver(observer, gamepad);
                         this.onGamepadConnectedObservable.notifyObserver(observer, gamepad);
                     }
                     }
-                }   
+                }
             });
             });
 
 
             this._onGamepadConnectedEvent = (evt) => {
             this._onGamepadConnectedEvent = (evt) => {
@@ -52,10 +51,10 @@
                     newGamepad = this._addNewGamepad(gamepad);
                     newGamepad = this._addNewGamepad(gamepad);
                 }
                 }
                 this.onGamepadConnectedObservable.notifyObservers(newGamepad);
                 this.onGamepadConnectedObservable.notifyObservers(newGamepad);
-                this._startMonitoringGamepads();                
+                this._startMonitoringGamepads();
             };
             };
 
 
-            this._onGamepadDisconnectedEvent  = (evt) => {
+            this._onGamepadDisconnectedEvent = (evt) => {
                 let gamepad = evt.gamepad;
                 let gamepad = evt.gamepad;
 
 
                 // Remove the gamepad from the list of gamepads to monitor.
                 // Remove the gamepad from the list of gamepads to monitor.
@@ -63,11 +62,11 @@
                     if (this._babylonGamepads[i].index === gamepad.index) {
                     if (this._babylonGamepads[i].index === gamepad.index) {
                         let disconnectedGamepad = this._babylonGamepads[i];
                         let disconnectedGamepad = this._babylonGamepads[i];
                         disconnectedGamepad._isConnected = false;
                         disconnectedGamepad._isConnected = false;
-                        
+
                         this.onGamepadDisconnectedObservable.notifyObservers(disconnectedGamepad);
                         this.onGamepadDisconnectedObservable.notifyObservers(disconnectedGamepad);
                         break;
                         break;
                     }
                     }
-                }            
+                }
             };
             };
 
 
             if (this._gamepadSupport) {
             if (this._gamepadSupport) {
@@ -150,7 +149,10 @@
         private _startMonitoringGamepads() {
         private _startMonitoringGamepads() {
             if (!this._isMonitoring) {
             if (!this._isMonitoring) {
                 this._isMonitoring = true;
                 this._isMonitoring = true;
-                this._checkGamepadsStatus();
+                //back-comp
+                if (!this._scene) {
+                    this._checkGamepadsStatus();
+                }
             }
             }
         }
         }
 
 
@@ -158,7 +160,7 @@
             this._isMonitoring = false;
             this._isMonitoring = false;
         }
         }
 
 
-        private _checkGamepadsStatus() {
+        public _checkGamepadsStatus() {
             // Hack to be compatible Chrome
             // Hack to be compatible Chrome
             this._updateGamepadObjects();
             this._updateGamepadObjects();
 
 
@@ -170,7 +172,7 @@
                 gamepad.update();
                 gamepad.update();
             }
             }
 
 
-            if (this._isMonitoring) {
+            if (this._isMonitoring && !this._scene) {
                 Tools.QueueNewFrame(() => { this._checkGamepadsStatus(); });
                 Tools.QueueNewFrame(() => { this._checkGamepadsStatus(); });
             }
             }
         }
         }
@@ -190,7 +192,7 @@
                         this._babylonGamepads[i].browserGamepad = gamepads[i];
                         this._babylonGamepads[i].browserGamepad = gamepads[i];
 
 
                         if (!this._babylonGamepads[i].isConnected) {
                         if (!this._babylonGamepads[i].isConnected) {
-                            this._babylonGamepads[i]._isConnected = true;                            
+                            this._babylonGamepads[i]._isConnected = true;
                             this.onGamepadConnectedObservable.notifyObservers(this._babylonGamepads[i]);
                             this.onGamepadConnectedObservable.notifyObservers(this._babylonGamepads[i]);
                         }
                         }
                     }
                     }

+ 6 - 1
src/babylon.scene.ts

@@ -484,7 +484,7 @@
 
 
         public get gamepadManager(): GamepadManager {
         public get gamepadManager(): GamepadManager {
             if (!this._gamepadManager) {
             if (!this._gamepadManager) {
-                this._gamepadManager = new GamepadManager();
+                this._gamepadManager = new GamepadManager(this);
             }
             }
 
 
             return this._gamepadManager;
             return this._gamepadManager;
@@ -3430,6 +3430,11 @@
                 }
                 }
             }
             }
 
 
+            // update gamepad manager
+            if (this._gamepadManager && this._gamepadManager._isMonitoring) {
+                this._gamepadManager._checkGamepadsStatus();
+            }
+
             // Before render
             // Before render
             this.onBeforeRenderObservable.notifyObservers(this);
             this.onBeforeRenderObservable.notifyObservers(this);