Browse Source

Merge pull request #1037 from gleborgne/master

fix bug for detaching elements, improve VRDeviceOrientation input
David Catuhe 9 years ago
parent
commit
ce4858940d

+ 4 - 7
src/Cameras/Inputs/babylon.arcrotatecamera.input.gamepad.ts

@@ -11,16 +11,13 @@ module BABYLON {
         @serialize()
         public gamepadMoveSensibility = 40;
 
-        constructor() {
+        attachControl(element : HTMLElement, noPreventDefault?: boolean) {
             this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
         }
-
-        attachCamera(camera: ArcRotateCamera) {
-            this.camera = camera;
-        }
-
-        detach() {
+        
+        detachControl(element : HTMLElement) {
             this._gamepads.dispose();
+            this.gamepad = null;
         }
 
         checkInputs() {

+ 4 - 5
src/Cameras/Inputs/babylon.arcrotatecamera.input.keyboard.ts

@@ -18,8 +18,7 @@ module BABYLON {
         @serialize()
         public keysRight = [39];
 
-        public attachCamera(camera: ArcRotateCamera) {
-            this.camera = camera;
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean) {
 
             this._onKeyDown = evt => {
                 if (this.keysUp.indexOf(evt.keyCode) !== -1 ||
@@ -33,7 +32,7 @@ module BABYLON {
                     }
 
                     if (evt.preventDefault) {
-                        if (!camera._noPreventDefault) {
+                        if (!noPreventDefault) {
                             evt.preventDefault();
                         }
                     }
@@ -52,7 +51,7 @@ module BABYLON {
                     }
 
                     if (evt.preventDefault) {
-                        if (!camera._noPreventDefault) {
+                        if (!noPreventDefault) {
                             evt.preventDefault();
                         }
                     }
@@ -70,7 +69,7 @@ module BABYLON {
             ]);
         }
 
-        public detach() {
+        public detachControl(element: HTMLElement) {
             Tools.UnregisterTopRootEvents([
                 { name: "keydown", handler: this._onKeyDown },
                 { name: "keyup", handler: this._onKeyUp },

+ 7 - 11
src/Cameras/Inputs/babylon.arcrotatecamera.input.mousewheel.ts

@@ -1,19 +1,13 @@
 module BABYLON {
     export class ArcRotateCameraMouseWheelInput implements ICameraInput<ArcRotateCamera> {
         camera: ArcRotateCamera;
-        attachedElement: HTMLElement;
-
+        
         private _wheel: (e: MouseWheelEvent) => void;
 
         @serialize()
         public wheelPrecision = 3.0;
 
-        public attachCamera(camera: ArcRotateCamera) {
-            this.camera = camera;
-        }
-
-        public attachElement(element: HTMLElement) {
-            this.attachedElement = element;
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean) {
             this._wheel = event => {
                 var delta = 0;
                 if (event.wheelDelta) {
@@ -35,9 +29,11 @@ module BABYLON {
             element.addEventListener('DOMMouseScroll', this._wheel, false);
         }
 
-        public detach() {
-            this.attachedElement.removeEventListener('mousewheel', this._wheel);
-            this.attachedElement.removeEventListener('DOMMouseScroll', this._wheel);
+        public detachControl(element: HTMLElement) {
+            if (this._wheel && element){
+                element.removeEventListener('mousewheel', this._wheel);
+                element.removeEventListener('DOMMouseScroll', this._wheel);
+            }
         }
 
         getTypeName(): string {

+ 144 - 151
src/Cameras/Inputs/babylon.arcrotatecamera.input.pointers.ts

@@ -3,11 +3,10 @@ module BABYLON {
 
     export class ArcRotateCameraPointersInput implements ICameraInput<ArcRotateCamera> {
         camera: ArcRotateCamera;
-        attachedElement: HTMLElement;
         private _isRightClick: boolean = false;
         private _isCtrlPushed: boolean = false;
         public pinchInwards = true;
-        
+
         @serialize()
         public angularSensibilityX = 1000.0;
 
@@ -18,7 +17,7 @@ module BABYLON {
         public pinchPrecision = 6.0;
 
         @serialize()
-        public panningSensibility: number = 50.0;       
+        public panningSensibility: number = 50.0;
 
         private _onKeyDown: (e: KeyboardEvent) => any;
         private _onKeyUp: (e: KeyboardEvent) => any;
@@ -31,199 +30,193 @@ module BABYLON {
         private _MSGestureHandler: MSGesture;
         private _onLostFocus: (e: FocusEvent) => any;
         private _onContextMenu: (e: PointerEvent) => void;
-        
-        public attachCamera(camera: ArcRotateCamera) {
-            this.camera = camera;
-
-        }
-
-        public attachElement(element: HTMLElement, noPreventDefault?: boolean) {
-            this.attachedElement = element;
 
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean) {
             var engine = this.camera.getEngine();
             var cacheSoloPointer; // cache pointer object for better perf on camera rotation
             var pointers = new SmartCollection();
             var previousPinchDistance = 0;
 
-            if (this._onPointerDown === undefined) {
-                if (!this.camera._useCtrlForPanning) {
-                    element.addEventListener("contextmenu", this._onContextMenu, false);
+            if (!this.camera._useCtrlForPanning) {
+                element.addEventListener("contextmenu", this._onContextMenu, false);
+            }
+
+            this._onLostFocus = () => {
+                //this._keys = [];
+                pointers.empty();
+                previousPinchDistance = 0;
+                cacheSoloPointer = null;
+            };
+
+            this._onKeyDown = evt => {
+                this._isCtrlPushed = evt.ctrlKey;
+            };
+
+            this._onKeyUp = evt => {
+                this._isCtrlPushed = evt.ctrlKey;
+            };
+
+            this._onPointerDown = evt => {
+                // Manage panning with right click
+                this._isRightClick = evt.button === 2;
+
+                // manage pointers
+                pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
+                cacheSoloPointer = pointers.item(evt.pointerId);
+                if (!noPreventDefault) {
+                    evt.preventDefault();
                 }
-                
-                this._onLostFocus = () => {
-                    //this._keys = [];
-                    pointers.empty();
-                    previousPinchDistance = 0;
-                    cacheSoloPointer = null;
-                };
-
-                this._onKeyDown = evt => {
-                    this._isCtrlPushed = evt.ctrlKey;
-                };
-
-                this._onKeyUp = evt => {
-                    this._isCtrlPushed = evt.ctrlKey;
-                };
-
-                this._onPointerDown = evt => {
-                    // Manage panning with right click
-                    this._isRightClick = evt.button === 2;
-
-                    // manage pointers
-                    pointers.add(evt.pointerId, { x: evt.clientX, y: evt.clientY, type: evt.pointerType });
-                    cacheSoloPointer = pointers.item(evt.pointerId);
-                    if (!noPreventDefault) {
-                        evt.preventDefault();
-                    }
-                };
+            };
 
-                this._onPointerUp = evt => {
-                    cacheSoloPointer = null;
-                    previousPinchDistance = 0;
+            this._onPointerUp = evt => {
+                cacheSoloPointer = null;
+                previousPinchDistance = 0;
 
-                    //would be better to use pointers.remove(evt.pointerId) for multitouch gestures, 
-                    //but emptying completly pointers collection is required to fix a bug on iPhone : 
-                    //when changing orientation while pinching camera, one pointer stay pressed forever if we don't release all pointers  
-                    //will be ok to put back pointers.remove(evt.pointerId); when iPhone bug corrected
-                    pointers.empty();
+                //would be better to use pointers.remove(evt.pointerId) for multitouch gestures, 
+                //but emptying completly pointers collection is required to fix a bug on iPhone : 
+                //when changing orientation while pinching camera, one pointer stay pressed forever if we don't release all pointers  
+                //will be ok to put back pointers.remove(evt.pointerId); when iPhone bug corrected
+                pointers.empty();
 
-                    if (!noPreventDefault) {
-                        evt.preventDefault();
-                    }
-                };
+                if (!noPreventDefault) {
+                    evt.preventDefault();
+                }
+            };
+
+            this._onContextMenu = evt => {
+                evt.preventDefault();
+            };
 
-                this._onContextMenu = evt => {
+            this._onPointerMove = evt => {
+                if (!noPreventDefault) {
                     evt.preventDefault();
-                };
+                }
 
-                this._onPointerMove = evt => {
-                    if (!noPreventDefault) {
-                        evt.preventDefault();
-                    }
+                switch (pointers.count) {
+
+                    case 1: //normal camera rotation
+                        if (this.panningSensibility !== 0 && ((this._isCtrlPushed && this.camera._useCtrlForPanning) || (!this.camera._useCtrlForPanning && this._isRightClick))) {
+                            this.camera.inertialPanningX += -(evt.clientX - cacheSoloPointer.x) / this.panningSensibility;
+                            this.camera.inertialPanningY += (evt.clientY - cacheSoloPointer.y) / this.panningSensibility;
+                        } else {
+                            var offsetX = evt.clientX - cacheSoloPointer.x;
+                            var offsetY = evt.clientY - cacheSoloPointer.y;
+                            this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
+                            this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
+                        }
+                        cacheSoloPointer.x = evt.clientX;
+                        cacheSoloPointer.y = evt.clientY;
+                        break;
+
+                    case 2: //pinch
+                        //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be usefull to force preventDefault to avoid html page scroll/zoom in some mobile browsers
+                        pointers.item(evt.pointerId).x = evt.clientX;
+                        pointers.item(evt.pointerId).y = evt.clientY;
+                        var direction = this.pinchInwards ? 1 : -1;
+                        var distX = pointers.getItemByIndex(0).x - pointers.getItemByIndex(1).x;
+                        var distY = pointers.getItemByIndex(0).y - pointers.getItemByIndex(1).y;
+                        var pinchSquaredDistance = (distX * distX) + (distY * distY);
+                        if (previousPinchDistance === 0) {
+                            previousPinchDistance = pinchSquaredDistance;
+                            return;
+                        }
 
-                    switch (pointers.count) {
-
-                        case 1: //normal camera rotation
-                            if (this.panningSensibility !== 0 && ((this._isCtrlPushed && this.camera._useCtrlForPanning) || (!this.camera._useCtrlForPanning && this._isRightClick))) {
-                                this.camera.inertialPanningX += -(evt.clientX - cacheSoloPointer.x) / this.panningSensibility;
-                                this.camera.inertialPanningY += (evt.clientY - cacheSoloPointer.y) / this.panningSensibility;
-                            } else {
-                                var offsetX = evt.clientX - cacheSoloPointer.x;
-                                var offsetY = evt.clientY - cacheSoloPointer.y;
-                                this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
-                                this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
-                            }
-                            cacheSoloPointer.x = evt.clientX;
-                            cacheSoloPointer.y = evt.clientY;
-                            break;
-
-                        case 2: //pinch
-                            //if (noPreventDefault) { evt.preventDefault(); } //if pinch gesture, could be usefull to force preventDefault to avoid html page scroll/zoom in some mobile browsers
+                        if (pinchSquaredDistance !== previousPinchDistance) {
+                            this.camera.inertialRadiusOffset += (pinchSquaredDistance - previousPinchDistance) / (this.pinchPrecision * ((this.angularSensibilityX + this.angularSensibilityY) / 2) * direction);
+                            previousPinchDistance = pinchSquaredDistance;
+                        }
+                        break;
+
+                    default:
+                        if (pointers.item(evt.pointerId)) {
                             pointers.item(evt.pointerId).x = evt.clientX;
                             pointers.item(evt.pointerId).y = evt.clientY;
-                            var direction = this.pinchInwards ? 1 : -1;
-                            var distX = pointers.getItemByIndex(0).x - pointers.getItemByIndex(1).x;
-                            var distY = pointers.getItemByIndex(0).y - pointers.getItemByIndex(1).y;
-                            var pinchSquaredDistance = (distX * distX) + (distY * distY);
-                            if (previousPinchDistance === 0) {
-                                previousPinchDistance = pinchSquaredDistance;
-                                return;
-                            }
-
-                            if (pinchSquaredDistance !== previousPinchDistance) {
-                                this.camera.inertialRadiusOffset += (pinchSquaredDistance - previousPinchDistance) / (this.pinchPrecision * ((this.angularSensibilityX + this.angularSensibilityY) / 2) * direction);
-                                previousPinchDistance = pinchSquaredDistance;
-                            }
-                            break;
-
-                        default:
-                            if (pointers.item(evt.pointerId)) {
-                                pointers.item(evt.pointerId).x = evt.clientX;
-                                pointers.item(evt.pointerId).y = evt.clientY;
-                            }
-                    }
-                };
+                        }
+                }
+            };
 
-                this._onMouseMove = evt => {
-                    if (!engine.isPointerLock) {
-                        return;
-                    }
+            this._onMouseMove = evt => {
+                if (!engine.isPointerLock) {
+                    return;
+                }
 
-                    var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
-                    var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
+                var offsetX = evt.movementX || evt.mozMovementX || evt.webkitMovementX || evt.msMovementX || 0;
+                var offsetY = evt.movementY || evt.mozMovementY || evt.webkitMovementY || evt.msMovementY || 0;
 
-                    this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
-                    this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
+                this.camera.inertialAlphaOffset -= offsetX / this.angularSensibilityX;
+                this.camera.inertialBetaOffset -= offsetY / this.angularSensibilityY;
 
-                    if (!noPreventDefault) {
-                        evt.preventDefault();
-                    }
-                };
+                if (!noPreventDefault) {
+                    evt.preventDefault();
+                }
+            };
 
-                this._onGestureStart = e => {
-                    if (window.MSGesture === undefined) {
-                        return;
-                    }
+            this._onGestureStart = e => {
+                if (window.MSGesture === undefined) {
+                    return;
+                }
 
-                    if (!this._MSGestureHandler) {
-                        this._MSGestureHandler = new MSGesture();
-                        this._MSGestureHandler.target = element;
-                    }
+                if (!this._MSGestureHandler) {
+                    this._MSGestureHandler = new MSGesture();
+                    this._MSGestureHandler.target = element;
+                }
 
-                    this._MSGestureHandler.addPointer(e.pointerId);
-                };
+                this._MSGestureHandler.addPointer(e.pointerId);
+            };
 
-                this._onGesture = e => {
-                    this.camera.radius *= e.scale;
+            this._onGesture = e => {
+                this.camera.radius *= e.scale;
 
 
-                    if (e.preventDefault) {
-                        if (!noPreventDefault) {
-                            e.stopPropagation();
-                            e.preventDefault();
-                        }
+                if (e.preventDefault) {
+                    if (!noPreventDefault) {
+                        e.stopPropagation();
+                        e.preventDefault();
                     }
-                };
-            }
+                }
+            };
+
 
-            this.attachedElement.addEventListener(eventPrefix + "down", this._onPointerDown, false);
-            this.attachedElement.addEventListener(eventPrefix + "up", this._onPointerUp, false);
-            this.attachedElement.addEventListener(eventPrefix + "out", this._onPointerUp, false);
-            this.attachedElement.addEventListener(eventPrefix + "move", this._onPointerMove, false);
-            this.attachedElement.addEventListener("mousemove", this._onMouseMove, false);
-            this.attachedElement.addEventListener("MSPointerDown", this._onGestureStart, false);
-            this.attachedElement.addEventListener("MSGestureChange", this._onGesture, false);
+            element.addEventListener(eventPrefix + "down", this._onPointerDown, false);
+            element.addEventListener(eventPrefix + "up", this._onPointerUp, false);
+            element.addEventListener(eventPrefix + "out", this._onPointerUp, false);
+            element.addEventListener(eventPrefix + "move", this._onPointerMove, false);
+            element.addEventListener("mousemove", this._onMouseMove, false);
+            element.addEventListener("MSPointerDown", this._onGestureStart, false);
+            element.addEventListener("MSGestureChange", this._onGesture, false);
 
             Tools.RegisterTopRootEvents([
                 { name: "blur", handler: this._onLostFocus }
             ]);
         }
 
-        public detach() {
+        public detachControl(element: HTMLElement) {
             this._MSGestureHandler = null;
 
-            this.attachedElement.removeEventListener("contextmenu", this._onContextMenu);
-            this.attachedElement.removeEventListener(eventPrefix + "down", this._onPointerDown);
-            this.attachedElement.removeEventListener(eventPrefix + "up", this._onPointerUp);
-            this.attachedElement.removeEventListener(eventPrefix + "out", this._onPointerUp);
-            this.attachedElement.removeEventListener(eventPrefix + "move", this._onPointerMove);
-            this.attachedElement.removeEventListener("mousemove", this._onMouseMove);
-            this.attachedElement.removeEventListener("MSPointerDown", this._onGestureStart);
-            this.attachedElement.removeEventListener("MSGestureChange", this._onGesture);
-
+            if (element && this._onPointerDown){
+                element.removeEventListener("contextmenu", this._onContextMenu);
+                element.removeEventListener(eventPrefix + "down", this._onPointerDown);
+                element.removeEventListener(eventPrefix + "up", this._onPointerUp);
+                element.removeEventListener(eventPrefix + "out", this._onPointerUp);
+                element.removeEventListener(eventPrefix + "move", this._onPointerMove);
+                element.removeEventListener("mousemove", this._onMouseMove);
+                element.removeEventListener("MSPointerDown", this._onGestureStart);
+                element.removeEventListener("MSGestureChange", this._onGesture);
+            }
+            
             Tools.UnregisterTopRootEvents([
                 { name: "blur", handler: this._onLostFocus }
             ]);
-        }        
+        }
 
         getTypeName(): string {
             return "ArcRotateCameraPointersInput";
         }
-                
-        getSimpleName(){
+
+        getSimpleName() {
             return "pointers";
         }
     }
-    
+
     CameraInputTypes["ArcRotateCameraPointersInput"] = ArcRotateCameraPointersInput;
 }

+ 2 - 4
src/Cameras/Inputs/babylon.freecamera.input.deviceorientation.ts

@@ -22,9 +22,7 @@ module BABYLON {
             this._orientationChanged = this.orientationChanged.bind(this);
         }
 
-        attachCamera(camera: FreeCamera) {
-            this.camera = camera;
-
+        attachControl(element : HTMLElement, noPreventDefault?: boolean) {
             window.addEventListener("resize", this._resetOrientationGamma, false);
             window.addEventListener("deviceorientation", this._orientationChanged);
         }
@@ -46,7 +44,7 @@ module BABYLON {
             this._offsetX = (this._initialOrientationGamma - this._orientationGamma);
         }
 
-        detach() {
+        detachControl(element : HTMLElement) {
             window.removeEventListener("resize", this._resetOrientationGamma);
             window.removeEventListener("deviceorientation", this._orientationChanged);
             

+ 3 - 6
src/Cameras/Inputs/babylon.freecamera.input.gamepad.ts

@@ -11,16 +11,13 @@ module BABYLON {
         @serialize()
         public gamepadMoveSensibility = 40;
         
-        constructor(){
+        attachControl(element : HTMLElement, noPreventDefault?: boolean){
             this._gamepads = new Gamepads((gamepad: Gamepad) => { this._onNewGameConnected(gamepad); });
         }
         
-        attachCamera(camera : FreeCamera){
-            this.camera = camera;
-        }
-        
-        detach(){
+        detachControl(element : HTMLElement){
             this._gamepads.dispose();
+            this.gamepad = null;
         }
         
         checkInputs(){

+ 4 - 6
src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts

@@ -17,9 +17,7 @@ module BABYLON {
         @serialize()
         public keysRight = [39];
 
-        attachCamera(camera: FreeCamera) {
-            this.camera = camera;
-            
+        attachControl(element : HTMLElement, noPreventDefault?: boolean) {
             if (this._onKeyDown === undefined) {
 
                 this._onKeyDown = evt => {
@@ -32,7 +30,7 @@ module BABYLON {
                         if (index === -1) {
                             this._keys.push(evt.keyCode);
                         }
-                        if (!camera._noPreventDefault) {
+                        if (!noPreventDefault) {
                             evt.preventDefault();
                         }
                     }
@@ -48,7 +46,7 @@ module BABYLON {
                         if (index >= 0) {
                             this._keys.splice(index, 1);
                         }
-                        if (!camera._noPreventDefault) {
+                        if (!noPreventDefault) {
                             evt.preventDefault();
                         }
                     }
@@ -62,7 +60,7 @@ module BABYLON {
             }
         }
 
-        detach() {
+        detachControl(element : HTMLElement) {
             Tools.UnregisterTopRootEvents([
                 { name: "keydown", handler: this._onKeyDown },
                 { name: "keyup", handler: this._onKeyUp },

+ 8 - 23
src/Cameras/Inputs/babylon.freecamera.input.mouse.ts

@@ -1,7 +1,6 @@
 module BABYLON {       
     export class FreeCameraMouseInput implements ICameraInput<FreeCamera> {
         camera : FreeCamera;
-        attachedElement : HTMLElement;
         
         @serialize()
         public angularSensibility = 2000.0;
@@ -11,13 +10,8 @@ module BABYLON {
         private _onMouseOut: (e: MouseEvent) => any;
         private _onMouseMove: (e: MouseEvent) => any;
         
-        attachCamera(camera : FreeCamera){
-            this.camera = camera;
-        }
-        
-        attachElement(element: HTMLElement, noPreventDefault?: boolean){     
+        attachControl(element: HTMLElement, noPreventDefault?: boolean){     
             var previousPosition;
-            this.attachedElement = element;
                 
             if (this._onMouseDown === undefined) {
                 var camera = this.camera;
@@ -86,22 +80,13 @@ module BABYLON {
    
         }
         
-        detachElement(element : HTMLElement){   
-            if (this.attachedElement !== element) {
-                return;
-            }
-
-            element.removeEventListener("mousedown", this._onMouseDown);
-            element.removeEventListener("mouseup", this._onMouseUp);
-            element.removeEventListener("mouseout", this._onMouseOut);
-            element.removeEventListener("mousemove", this._onMouseMove); 
-            this.attachedElement = null;        
-        }
-        
-        detach(){          
-            if (this.attachedElement){
-                this.detachElement(this.attachedElement);
-            }  
+        detachControl(element : HTMLElement){   
+            if (this._onMouseDown && element){
+                element.removeEventListener("mousedown", this._onMouseDown);
+                element.removeEventListener("mouseup", this._onMouseUp);
+                element.removeEventListener("mouseout", this._onMouseOut);
+                element.removeEventListener("mousemove", this._onMouseMove);
+            }        
         }
         
         getTypeName(): string{

+ 2 - 24
src/Cameras/Inputs/babylon.freecamera.input.touch.ts

@@ -6,7 +6,6 @@ module BABYLON {
         private _offsetY: number = null;
         private _pointerCount: number = 0;
         private _pointerPressed = [];
-        private _attachedElement: HTMLElement;
         private _onPointerDown: (e: PointerEvent) => any;
         private _onPointerUp: (e: PointerEvent) => any;
         private _onPointerMove: (e: PointerEvent) => any;
@@ -18,19 +17,9 @@ module BABYLON {
         @serialize()
         public touchMoveSensibility: number = 250.0;
 
-        attachCamera(camera: FreeCamera) {
-            this.camera = camera;
-        }
-        
-        attachElement(element: HTMLElement, noPreventDefault?: boolean) {
+        attachControl(element: HTMLElement, noPreventDefault?: boolean) {
             var previousPosition;
 
-            if (this._attachedElement) {
-                return;
-            }
-
-            this._attachedElement = element;
-
             if (this._onPointerDown === undefined) {
                 this._onLostFocus = (evt) => {
                     this._offsetX = null;
@@ -117,17 +106,12 @@ module BABYLON {
             element.addEventListener("pointermove", this._onPointerMove);
         }
 
-        detachElement(element: HTMLElement) {
-            if (this._attachedElement !== element) {
-                return;
-            }
-
+        detachControl(element: HTMLElement) {
             element.removeEventListener("blur", this._onLostFocus);
             element.removeEventListener("pointerdown", this._onPointerDown);
             element.removeEventListener("pointerup", this._onPointerUp);
             element.removeEventListener("pointerout", this._onPointerUp);
             element.removeEventListener("pointermove", this._onPointerMove);
-            this._attachedElement = null;
         }
 
         checkInputs() {
@@ -147,12 +131,6 @@ module BABYLON {
             }
         }
 
-        detach() {
-            if (this._attachedElement) {
-                this.detachElement(this._attachedElement);
-            }
-        }
-
         getTypeName(): string {
             return "FreeCameraTouchInput";
         }

+ 17 - 16
src/Cameras/Inputs/babylon.freecamera.input.virtualjoystick.ts

@@ -14,24 +14,24 @@ module BABYLON {
         }
 
         public checkInputs() {
-            var camera = this.camera;
-            var speed = camera._computeLocalCameraSpeed() * 50;
-            var cameraTransform = Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
-            var deltaTransform = Vector3.TransformCoordinates(new Vector3(this._leftjoystick.deltaPosition.x * speed, this._leftjoystick.deltaPosition.y * speed, this._leftjoystick.deltaPosition.z * speed), cameraTransform);
-            camera.cameraDirection = camera.cameraDirection.add(deltaTransform);
-            camera.cameraRotation = camera.cameraRotation.addVector3(this._rightjoystick.deltaPosition);
-            
-            if (!this._leftjoystick.pressed) {
-                this._leftjoystick.deltaPosition = this._leftjoystick.deltaPosition.scale(0.9);
-            }
-            if (!this._rightjoystick.pressed) {
-                this._rightjoystick.deltaPosition = this._rightjoystick.deltaPosition.scale(0.9);
+            if (this._leftjoystick){
+                var camera = this.camera;
+                var speed = camera._computeLocalCameraSpeed() * 50;
+                var cameraTransform = Matrix.RotationYawPitchRoll(camera.rotation.y, camera.rotation.x, 0);
+                var deltaTransform = Vector3.TransformCoordinates(new Vector3(this._leftjoystick.deltaPosition.x * speed, this._leftjoystick.deltaPosition.y * speed, this._leftjoystick.deltaPosition.z * speed), cameraTransform);
+                camera.cameraDirection = camera.cameraDirection.add(deltaTransform);
+                camera.cameraRotation = camera.cameraRotation.addVector3(this._rightjoystick.deltaPosition);
+                
+                if (!this._leftjoystick.pressed) {
+                    this._leftjoystick.deltaPosition = this._leftjoystick.deltaPosition.scale(0.9);
+                }
+                if (!this._rightjoystick.pressed) {
+                    this._rightjoystick.deltaPosition = this._rightjoystick.deltaPosition.scale(0.9);
+                }
             }
         }
         
-        attachCamera(camera: FreeCamera) {
-            this.camera = camera;
-            
+        attachControl(element : HTMLElement, noPreventDefault?: boolean) {
             this._leftjoystick = new VirtualJoystick(true);
             this._leftjoystick.setAxisForUpDown(JoystickAxis.Z);
             this._leftjoystick.setAxisForLeftRight(JoystickAxis.X);
@@ -44,8 +44,9 @@ module BABYLON {
             this._rightjoystick.setJoystickColor("yellow");
         }
 
-        detach() {
+        detachControl(element : HTMLElement) {
             this._leftjoystick.releaseCanvas();
+            this._rightjoystick.releaseCanvas();
         }
 
         getTypeName(): string {

+ 31 - 21
src/Cameras/Inputs/babylon.freecamera.input.vrdeviceorientation.ts

@@ -2,9 +2,14 @@ module BABYLON {
     export class FreeCameraVRDeviceOrientationInput implements ICameraInput<FreeCamera> {
         camera: FreeCamera;
 
-        public _alpha = 0;
-        public _beta = 0;
-        public _gamma = 0;
+        public alphaCorrection = 1;
+        public betaCorrection = 1;
+        public gammaCorrection = 1;
+
+        private _alpha = 0;
+        private _beta = 0;
+        private _gamma = 0;
+        private _dirty = false;
     
         private _offsetOrientation: { yaw: number; pitch: number; roll: number };
         private _deviceOrientationHandler;
@@ -13,32 +18,37 @@ module BABYLON {
             this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
         }
 
-        attachCamera(camera: FreeCamera) {
-            this.camera = camera;
-
+        attachControl(element : HTMLElement, noPreventDefault?: boolean) {
             window.addEventListener("deviceorientation", this._deviceOrientationHandler);
         }
 
         public _onOrientationEvent(evt: DeviceOrientationEvent): void {
             var camera = this.camera;
-            this._alpha = +evt.alpha|0;
-            this._beta = +evt.beta|0;
-            this._gamma = +evt.gamma|0;
+            this._alpha = +evt.alpha | 0;
+            this._beta = +evt.beta | 0;
+            this._gamma = +evt.gamma | 0;
+            this._dirty = true;
+        }
 
-            if (this._gamma < 0) {
-                this._gamma = 90 + this._gamma;
-            }
-            else {
-                // Incline it in the correct angle.
-                this._gamma = 270 - this._gamma;
-            }
+        public checkInputs() {
+            if (this._dirty){
+                this._dirty = false;
+                var rotationX = this._gamma;
+                if (rotationX < 0) {
+                    rotationX = 90 + rotationX;
+                }
+                else {
+                    // Incline it in the correct angle.
+                    rotationX = 270 - rotationX;
+                }
 
-            camera.rotation.x = this._gamma / 180.0 * Math.PI;   
-            camera.rotation.y = -this._alpha / 180.0 * Math.PI;   
-            camera.rotation.z = this._beta / 180.0 * Math.PI;     
-        }
+                this.camera.rotation.x = this.gammaCorrection * rotationX / 180.0 * Math.PI;
+                this.camera.rotation.y = this.alphaCorrection * -this._alpha / 180.0 * Math.PI;
+                this.camera.rotation.z = this.betaCorrection * this._beta / 180.0 * Math.PI;
+            }
+        }              
 
-        detach() {
+        detachControl(element : HTMLElement) {
             window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
         }
 

+ 0 - 7
src/Cameras/babylon.arcRotateCameraInputsManager.ts

@@ -4,13 +4,6 @@ module BABYLON {
             super(camera);    
         }
         
-        add(input: ICameraInput<ArcRotateCamera>){
-            super.add(input);
-            if (this.camera._attachedElement && input.attachElement) {
-                input.attachElement(this.camera._attachedElement, this.camera._noPreventDefault);
-            }
-        }
-        
         public addMouseWheel(){
             this.add(new ArcRotateCameraMouseWheelInput());
             return this;

+ 17 - 17
src/Cameras/babylon.cameraInputsManager.ts

@@ -2,14 +2,11 @@ module BABYLON {
     export var CameraInputTypes = {};
 
     export interface ICameraInput<TCamera extends BABYLON.Camera> {
-        camera: TCamera;
-        attachCamera(camera: TCamera);
-        detach();
+        camera: TCamera;        
         getTypeName(): string;
         getSimpleName(): string;
-
-        attachElement?: (element: HTMLElement, noPreventDefault?: boolean) => void;
-        detachElement?: (element: HTMLElement) => void;
+        attachControl: (element: HTMLElement, noPreventDefault?: boolean) => void;
+        detachControl: (element: HTMLElement) => void;        
         checkInputs?: () => void;
     }
 
@@ -20,6 +17,7 @@ module BABYLON {
 
     export class CameraInputsManager<TCamera extends BABYLON.Camera> {
         attached: CameraInputsMap<TCamera>;
+        attachedElement: HTMLElement;
         camera: TCamera;
         checkInputs: () => void;
 
@@ -37,20 +35,25 @@ module BABYLON {
             }
 
             this.attached[type] = input;
-            input.attachCamera(this.camera);
+            
+            input.camera = this.camera;
             
             //for checkInputs, we are dynamically creating a function
             //the goal is to avoid the performance penalty of looping for inputs in the render loop
             if (input.checkInputs) {
                 this.checkInputs = this._addCheckInputs(input.checkInputs.bind(input));
             }
+            
+            if (this.attachedElement){
+                input.attachControl(this.attachedElement);
+            }
         }
 
         public remove(inputToRemove: ICameraInput<TCamera>) {
             for (var cam in this.attached) {
                 var input = this.attached[cam];
                 if (input == inputToRemove) {
-                    input.detach();
+                    input.detachControl(this.attachedElement);
                     delete this.attached[cam];
                 }
             }
@@ -60,7 +63,7 @@ module BABYLON {
             for (var cam in this.attached) {
                 var input = this.attached[cam];
                 if (input.getTypeName() == inputType) {
-                    input.detach();
+                    input.detachControl(this.attachedElement);
                     delete this.attached[cam];
                 }
             }
@@ -75,18 +78,17 @@ module BABYLON {
         }
 
         public attachElement(element: HTMLElement, noPreventDefault?: boolean) {
+            this.attachedElement = element;
             for (var cam in this.attached) {
                 var input = this.attached[cam];
-                if (input.attachElement)
-                    this.attached[cam].attachElement(element, noPreventDefault);
+                this.attached[cam].attachControl(element, noPreventDefault);
             }
         }
 
         public detachElement(element: HTMLElement) {
             for (var cam in this.attached) {
                 var input = this.attached[cam];
-                if (input.detachElement)
-                    this.attached[cam].detachElement(element);
+                this.attached[cam].detachControl(element);
             }
         }
 
@@ -102,11 +104,9 @@ module BABYLON {
         }
 
         public clear() {
-            for (var cam in this.attached) {
-                this.attached[cam].detach();
-            }
-
+            this.detachElement(this.attachedElement);
             this.attached = {};
+            this.attachedElement = null;
             this.checkInputs = () => { };
         }
 

+ 1 - 8
src/Cameras/babylon.freeCameraInputsManager.ts

@@ -2,14 +2,7 @@ module BABYLON {
     export class FreeCameraInputsManager extends CameraInputsManager<FreeCamera> {
         constructor(camera : FreeCamera){
             super(camera);    
-        }
-        
-        add(input: ICameraInput<FreeCamera>){
-            super.add(input);
-            if (this.camera._attachedElement && input.attachElement) {
-                input.attachElement(this.camera._attachedElement, this.camera._noPreventDefault);
-            }
-        }
+        }        
         
         addKeyboard(){
             this.add(new FreeCameraKeyboardMoveInput());