Sfoglia il codice sorgente

Merge pull request #1044 from gleborgne/master

Improve management of attach / detach
David Catuhe 9 anni fa
parent
commit
849d5fe6e4

+ 19 - 12
src/Cameras/Inputs/babylon.arcrotatecamera.input.keyboard.ts

@@ -75,21 +75,28 @@ module BABYLON {
                 { name: "keyup", handler: this._onKeyUp },
                 { name: "blur", handler: this._onLostFocus }
             ]);
+            
+            this._keys = [];
+            this._onKeyDown = null;
+            this._onKeyUp = null;
+            this._onLostFocus = null;
         }
 
         public checkInputs() {
-            var camera = this.camera;
-
-            for (var index = 0; index < this._keys.length; index++) {
-                var keyCode = this._keys[index];
-                if (this.keysLeft.indexOf(keyCode) !== -1) {
-                    camera.inertialAlphaOffset -= 0.01;
-                } else if (this.keysUp.indexOf(keyCode) !== -1) {
-                    camera.inertialBetaOffset -= 0.01;
-                } else if (this.keysRight.indexOf(keyCode) !== -1) {
-                    camera.inertialAlphaOffset += 0.01;
-                } else if (this.keysDown.indexOf(keyCode) !== -1) {
-                    camera.inertialBetaOffset += 0.01;
+            if (this._onKeyDown){
+                var camera = this.camera;
+
+                for (var index = 0; index < this._keys.length; index++) {
+                    var keyCode = this._keys[index];
+                    if (this.keysLeft.indexOf(keyCode) !== -1) {
+                        camera.inertialAlphaOffset -= 0.01;
+                    } else if (this.keysUp.indexOf(keyCode) !== -1) {
+                        camera.inertialBetaOffset -= 0.01;
+                    } else if (this.keysRight.indexOf(keyCode) !== -1) {
+                        camera.inertialAlphaOffset += 0.01;
+                    } else if (this.keysDown.indexOf(keyCode) !== -1) {
+                        camera.inertialBetaOffset += 0.01;
+                    }
                 }
             }
         }

+ 2 - 1
src/Cameras/Inputs/babylon.arcrotatecamera.input.mousewheel.ts

@@ -20,7 +20,7 @@ module BABYLON {
                     this.camera.inertialRadiusOffset += delta;
 
                 if (event.preventDefault) {
-                    if (!this.camera._noPreventDefault) {
+                    if (!noPreventDefault) {
                         event.preventDefault();
                     }
                 }
@@ -33,6 +33,7 @@ module BABYLON {
             if (this._wheel && element){
                 element.removeEventListener('mousewheel', this._wheel);
                 element.removeEventListener('DOMMouseScroll', this._wheel);
+                this._wheel = null;
             }
         }
 

+ 22 - 6
src/Cameras/Inputs/babylon.arcrotatecamera.input.pointers.ts

@@ -3,10 +3,7 @@ module BABYLON {
 
     export class ArcRotateCameraPointersInput implements ICameraInput<ArcRotateCamera> {
         camera: ArcRotateCamera;
-        private _isRightClick: boolean = false;
-        private _isCtrlPushed: boolean = false;
-        public pinchInwards = true;
-
+        
         @serialize()
         public angularSensibilityX = 1000.0;
 
@@ -19,6 +16,10 @@ module BABYLON {
         @serialize()
         public panningSensibility: number = 50.0;
 
+        private _isRightClick: boolean = false;
+        private _isCtrlPushed: boolean = false;
+        public pinchInwards = true;
+
         private _onKeyDown: (e: KeyboardEvent) => any;
         private _onKeyUp: (e: KeyboardEvent) => any;
         private _onPointerDown: (e: PointerEvent) => void;
@@ -191,8 +192,6 @@ module BABYLON {
         }
 
         public detachControl(element: HTMLElement) {
-            this._MSGestureHandler = null;
-
             if (element && this._onPointerDown){
                 element.removeEventListener("contextmenu", this._onContextMenu);
                 element.removeEventListener(eventPrefix + "down", this._onPointerDown);
@@ -202,6 +201,23 @@ module BABYLON {
                 element.removeEventListener("mousemove", this._onMouseMove);
                 element.removeEventListener("MSPointerDown", this._onGestureStart);
                 element.removeEventListener("MSGestureChange", this._onGesture);
+                
+                this._isRightClick = false;
+                this._isCtrlPushed = false;
+                this.pinchInwards = true;
+
+                this._onKeyDown= null;
+                this._onKeyUp= null;
+                this._onPointerDown= null;
+                this._onPointerUp= null;
+                this._onPointerMove= null;
+                this._onMouseMove= null;
+                this._onGestureStart= null;
+                this._onGesture= null;
+                this._MSGestureHandler= null;
+                this._onLostFocus= null;
+                this._onContextMenu = null;
+
             }
             
             Tools.UnregisterTopRootEvents([

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

@@ -52,6 +52,8 @@ module BABYLON {
             this._orientationBeta = 0;
             this._initialOrientationGamma = 0;
             this._initialOrientationBeta = 0;
+            this._offsetX = null;
+            this._offsetY = null;
         }
 
         public checkInputs() {

+ 29 - 22
src/Cameras/Inputs/babylon.freecamera.input.keyboard.ts

@@ -61,33 +61,40 @@ module BABYLON {
         }
 
         detachControl(element : HTMLElement) {
-            Tools.UnregisterTopRootEvents([
-                { name: "keydown", handler: this._onKeyDown },
-                { name: "keyup", handler: this._onKeyUp },
-                { name: "blur", handler: this._onLostFocus }
-            ]);
+            if (this._onKeyDown){
+                Tools.UnregisterTopRootEvents([
+                    { name: "keydown", handler: this._onKeyDown },
+                    { name: "keyup", handler: this._onKeyUp },
+                    { name: "blur", handler: this._onLostFocus }
+                ]);
+                this._keys = [];
+                this._onKeyDown = null;
+                this._onKeyUp = null;
+            }
         }
         
         public checkInputs() {
-            var camera = this.camera;
-            // Keyboard
-            for (var index = 0; index < this._keys.length; index++) {
-                var keyCode = this._keys[index];
-                var speed = camera._computeLocalCameraSpeed();
+            if (this._onKeyDown){
+                var camera = this.camera;
+                // Keyboard
+                for (var index = 0; index < this._keys.length; index++) {
+                    var keyCode = this._keys[index];
+                    var speed = camera._computeLocalCameraSpeed();
 
-                if (this.keysLeft.indexOf(keyCode) !== -1) {
-                    camera._localDirection.copyFromFloats(-speed, 0, 0);
-                } else if (this.keysUp.indexOf(keyCode) !== -1) {
-                    camera._localDirection.copyFromFloats(0, 0, speed);
-                } else if (this.keysRight.indexOf(keyCode) !== -1) {
-                    camera._localDirection.copyFromFloats(speed, 0, 0);
-                } else if (this.keysDown.indexOf(keyCode) !== -1) {
-                    camera._localDirection.copyFromFloats(0, 0, -speed);
-                }
+                    if (this.keysLeft.indexOf(keyCode) !== -1) {
+                        camera._localDirection.copyFromFloats(-speed, 0, 0);
+                    } else if (this.keysUp.indexOf(keyCode) !== -1) {
+                        camera._localDirection.copyFromFloats(0, 0, speed);
+                    } else if (this.keysRight.indexOf(keyCode) !== -1) {
+                        camera._localDirection.copyFromFloats(speed, 0, 0);
+                    } else if (this.keysDown.indexOf(keyCode) !== -1) {
+                        camera._localDirection.copyFromFloats(0, 0, -speed);
+                    }
 
-                camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
-                Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
-                camera.cameraDirection.addInPlace(camera._transformedDirection);
+                    camera.getViewMatrix().invertToRef(camera._cameraTransformMatrix);
+                    Vector3.TransformNormalToRef(camera._localDirection, camera._cameraTransformMatrix, camera._transformedDirection);
+                    camera.cameraDirection.addInPlace(camera._transformedDirection);
+                }
             }
         }
 

+ 5 - 0
src/Cameras/Inputs/babylon.freecamera.input.mouse.ts

@@ -86,6 +86,11 @@ module BABYLON {
                 element.removeEventListener("mouseup", this._onMouseUp);
                 element.removeEventListener("mouseout", this._onMouseOut);
                 element.removeEventListener("mousemove", this._onMouseMove);
+                
+                this._onMouseDown = null;
+                this._onMouseUp = null;
+                this._onMouseOut = null;
+                this._onMouseMove = null;
             }        
         }
         

+ 16 - 5
src/Cameras/Inputs/babylon.freecamera.input.touch.ts

@@ -107,11 +107,22 @@ module BABYLON {
         }
 
         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);
+            if (this._onPointerDown && element){
+                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._onPointerDown = null;
+                this._onPointerUp = null;
+                this._onPointerMove = null;
+                this._onLostFocus = null;
+                this._pointerPressed = [];
+                this._offsetX = null;
+                this._offsetY = null;
+                this._pointerCount = 0;
+            }
         }
 
         checkInputs() {

+ 2 - 14
src/Cameras/babylon.arcRotateCamera.ts

@@ -169,8 +169,6 @@
         public allowUpsideDown = true;
 
         public _viewMatrix = new Matrix();
-        public _attachedElement: HTMLElement;
-        public _noPreventDefault : boolean;
         public _useCtrlForPanning : boolean;
         public inputs : ArcRotateCameraInputsManager;
         
@@ -257,11 +255,6 @@
 
         // Methods
         public attachControl(element: HTMLElement, noPreventDefault?: boolean, useCtrlForPanning: boolean = true): void {
-            if (this._attachedElement) {
-                return;
-            }
-            this._attachedElement = element;
-            this._noPreventDefault = Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
             this._useCtrlForPanning = useCtrlForPanning;
 
             this.inputs.attachElement(element, noPreventDefault);
@@ -275,13 +268,8 @@
         }
 
         public detachControl(element: HTMLElement): void {
-            if (this._attachedElement !== element) {
-                return;
-            }
-
-            this.inputs.detachElement(this._attachedElement);
-            this._attachedElement = null;
-
+            this.inputs.detachElement(element);
+            
             if (this._reset) {
                 this._reset();
             }

+ 21 - 2
src/Cameras/babylon.cameraInputsManager.ts

@@ -17,7 +17,8 @@ module BABYLON {
 
     export class CameraInputsManager<TCamera extends BABYLON.Camera> {
         attached: CameraInputsMap<TCamera>;
-        attachedElement: HTMLElement;
+        public attachedElement: HTMLElement;
+        public noPreventDefault: boolean;
         camera: TCamera;
         checkInputs: () => void;
 
@@ -55,6 +56,7 @@ module BABYLON {
                 if (input === inputToRemove) {
                     input.detachControl(this.attachedElement);
                     delete this.attached[cam];
+                    this.rebuildInputCheck();
                 }
             }
         }
@@ -65,6 +67,7 @@ module BABYLON {
                 if (input.getTypeName() === inputType) {
                     input.detachControl(this.attachedElement);
                     delete this.attached[cam];
+                    this.rebuildInputCheck();
                 }
             }
         }
@@ -77,8 +80,18 @@ module BABYLON {
             }
         }
 
+        public attachInput(input : ICameraInput<TCamera>){
+            input.attachControl(this.attachedElement, this.noPreventDefault);
+        }
+        
         public attachElement(element: HTMLElement, noPreventDefault?: boolean) {
+            if (this.attachedElement) {
+                return;
+            }
+            noPreventDefault = Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
             this.attachedElement = element;
+            this.noPreventDefault = noPreventDefault;
+            
             for (var cam in this.attached) {
                 var input = this.attached[cam];
                 this.attached[cam].attachControl(element, noPreventDefault);
@@ -86,13 +99,19 @@ module BABYLON {
         }
 
         public detachElement(element: HTMLElement) {
+            if (this.attachedElement !== element) {
+                return;
+            }
+            
             for (var cam in this.attached) {
                 var input = this.attached[cam];
                 this.attached[cam].detachControl(element);
             }
+            
+            this.attachedElement = null;
         }
 
-        public rebuildInputCheck(element: HTMLElement) {
+        public rebuildInputCheck() {
             this.checkInputs = () => { };
 
             for (var cam in this.attached) {

+ 3 - 15
src/Cameras/babylon.freeCamera.ts

@@ -81,8 +81,8 @@
         private _oldPosition = Vector3.Zero();
         private _diffPosition = Vector3.Zero();
         private _newPosition = Vector3.Zero();
-        public _attachedElement: HTMLElement;
-        public _noPreventDefault: boolean;
+        //public _attachedElement: HTMLElement;
+        //public _noPreventDefault: boolean;
         
         public _localDirection: Vector3;
         public _transformedDirection: Vector3;        
@@ -95,23 +95,11 @@
 
         // Controls
         public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
-            if (this._attachedElement) {
-                return;
-            }
-            this._noPreventDefault = noPreventDefault;
-            this._attachedElement = element;
-            noPreventDefault = Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
-
             this.inputs.attachElement(element, noPreventDefault);
         }        
 
         public detachControl(element: HTMLElement): void {
-            if (this._attachedElement !== element) {
-                return;
-            }
-
-            this.inputs.detachElement(this._attachedElement);
-            this._attachedElement = null;
+            this.inputs.detachElement(element);
             
             this.cameraDirection = new Vector3(0, 0, 0);
             this.cameraRotation = new Vector2(0, 0);