Browse Source

Merge pull request #1512 from BabylonJS/revert-1511-master

Revert "Cleanup mouse move management"
David Catuhe 8 năm trước cách đây
mục cha
commit
925da8a3f4
1 tập tin đã thay đổi với 46 bổ sung7 xóa
  1. 46 7
      src/Cameras/Inputs/babylon.freecamera.input.mouse.ts

+ 46 - 7
src/Cameras/Inputs/babylon.freecamera.input.mouse.ts

@@ -9,8 +9,11 @@ module BABYLON {
         public angularSensibility = 2000.0;
 
         private _pointerInput: (p: PointerInfo, s: EventState) => void;
+        private _onMouseMove: (e: MouseEvent) => any;
         private _observer: Observer<PointerInfo>;
 
+        private previousPosition: { x: number, y: number };
+
         constructor(public touchEnabled = true) {
         }
 
@@ -18,7 +21,6 @@ module BABYLON {
             var engine = this.camera.getEngine();
 
             if (!this._pointerInput) {
-                var isPointerDown = false;
                 this._pointerInput = (p, s) => {
                     var evt = <PointerEvent>p.event;
 
@@ -37,7 +39,10 @@ module BABYLON {
                             //Nothing to do with the error. Execution will continue.
                         }
 
-                        isPointerDown = true;
+                        this.previousPosition = {
+                            x: evt.clientX,
+                            y: evt.clientY
+                        };
 
                         if (!noPreventDefault) {
                             evt.preventDefault();
@@ -51,28 +56,33 @@ module BABYLON {
                             //Nothing to do with the error.
                         }
 
-                        isPointerDown = false;
-
+                        this.previousPosition = null;
                         if (!noPreventDefault) {
                             evt.preventDefault();
                         }
                     }
 
                     else if (p.type === PointerEventTypes.POINTERMOVE) {
-                        if (!isPointerDown && !engine.isPointerLock) {
+                        if (!this.previousPosition || 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.clientX - this.previousPosition.x;
+                        var offsetY = evt.clientY - this.previousPosition.y;
 
                         if (this.camera.getScene().useRightHandedSystem) {
                             this.camera.cameraRotation.y -= offsetX / this.angularSensibility;
                         } else {
                             this.camera.cameraRotation.y += offsetX / this.angularSensibility;
                         }
+
                         this.camera.cameraRotation.x += offsetY / this.angularSensibility;
 
+                        this.previousPosition = {
+                            x: evt.clientX,
+                            y: evt.clientY
+                        };
+
                         if (!noPreventDefault) {
                             evt.preventDefault();
                         }
@@ -80,13 +90,42 @@ module BABYLON {
                 }
             }
 
+            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;
+
+                if (this.camera.getScene().useRightHandedSystem) {
+                    this.camera.cameraRotation.y -= offsetX / this.angularSensibility;
+                } else {
+                    this.camera.cameraRotation.y += offsetX / this.angularSensibility;
+                }
+
+                this.camera.cameraRotation.x += offsetY / this.angularSensibility;
+
+                this.previousPosition = null;
+
+                if (!noPreventDefault) {
+                    evt.preventDefault();
+                }
+            };
+
             this._observer = this.camera.getScene().onPointerObservable.add(this._pointerInput, PointerEventTypes.POINTERDOWN | PointerEventTypes.POINTERUP | PointerEventTypes.POINTERMOVE);
+            element.addEventListener("mousemove", this._onMouseMove, false);
+
         }
 
         detachControl(element: HTMLElement) {
             if (this._observer && element) {
                 this.camera.getScene().onPointerObservable.remove(this._observer);
+                element.removeEventListener("mousemove", this._onMouseMove);
+
                 this._observer = null;
+                this._onMouseMove = null;
+                this.previousPosition = null;
             }
         }