浏览代码

add defensive check (#8617)

Cedric Guillemet 5 年之前
父节点
当前提交
f05a41db86
共有 2 个文件被更改,包括 15 次插入1 次删除
  1. 2 1
      dist/preview release/what's new.md
  2. 13 0
      src/Inputs/scene.inputManager.ts

+ 2 - 1
dist/preview release/what's new.md

@@ -81,7 +81,8 @@
 ### Cameras
 
 - Fixed up vector not correctly handled with stereoscopic rig ([cedricguillemet](https://github.com/cedricguillemet))
-- handle reattachment of panning button for `ArcRotateCamera` ([cedricguillemet](https://github.com/cedricguillemet))
+- Handle reattachment of panning button for `ArcRotateCamera` ([cedricguillemet](https://github.com/cedricguillemet))
+- Unattach previous control in `scene.InputManager` when attaching a new one ([cedricguillemet](https://github.com/cedricguillemet))
 - Added flag to TargetCamera to invert rotation direction and multiplier to adjust speed ([Exolun](https://github.com/Exolun))
 - Added upwards and downwards keyboard input to `FreeCamera` ([Pheater](https://github.com/pheater))
 - Handle scales in camera matrices ([Popov72](https://github.com/Popov72))

+ 13 - 0
src/Inputs/scene.inputManager.ts

@@ -60,6 +60,9 @@ export class InputManager {
     /** If you need to check double click without raising a single click at first click, enable this flag */
     public static ExclusiveDoubleClickMode = false;
 
+    /** This is a defensive check to not allow control attachment prior to an already active one. If already attached, previous control is unattached before attaching the new one. */
+    private _alreadyAttached = false;
+
     // Pointers
     private _wheelEventName = "";
     private _onPointerMove: (evt: PointerEvent) => void;
@@ -458,6 +461,9 @@ export class InputManager {
             return;
         }
 
+        if (this._alreadyAttached) {
+            this.detachControl();
+        }
         let engine = scene.getEngine();
 
         this._initActionManager = (act: Nullable<AbstractActionManager>, clickInfo: _ClickInfo): Nullable<AbstractActionManager> => {
@@ -817,6 +823,7 @@ export class InputManager {
                 hostWindow.addEventListener(eventPrefix + "up", <any>this._onPointerUp, false);
             }
         }
+        this._alreadyAttached = true;
     }
 
     /**
@@ -831,6 +838,10 @@ export class InputManager {
             return;
         }
 
+        if (!this._alreadyAttached) {
+            return;
+        }
+
         // Pointer
         canvas.removeEventListener(eventPrefix + "move", <any>this._onPointerMove);
         canvas.removeEventListener(this._wheelEventName, <any>this._onPointerMove);
@@ -854,6 +865,8 @@ export class InputManager {
         if (!this._scene.doNotHandleCursors) {
             canvas.style.cursor = this._scene.defaultCursor;
         }
+
+        this._alreadyAttached = false;
     }
 
     /**