Browse Source

Refactor so buttonCount is not passed in onButtonDown/Up.

duncan law 6 years ago
parent
commit
90ae842e07

+ 46 - 37
src/Cameras/Inputs/BaseCameraPointersInput.ts

@@ -38,16 +38,23 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
     public buttons = [0, 1, 2];
 
     /**
+     * Last event on up to 2 Touch events.
+     */
+    protected pointA: Nullable<PointerTouch>;
+    protected pointB: Nullable<PointerTouch>;
+
+    /**
      * Attach the input controls to a specific dom element to get the input from.
      * @param element Defines the element the controls should be listened from
      * @param noPreventDefault Defines whether event caught by the controls should call preventdefault() (https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault)
      */
     public attachControl(element: HTMLElement, noPreventDefault?: boolean): void {
         var engine = this.camera.getEngine();
-        var pointA: Nullable<PointerTouch> = null;
-        var pointB: Nullable<PointerTouch> = null;
         var previousPinchSquaredDistance = 0;
         var previousMultiTouchPanPosition: Nullable<PointerTouch> = null;
+      
+        this.pointA = null;
+        this.pointB = null;
 
         this._altKey = false;
         this._ctrlKey = false;
@@ -89,8 +96,8 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                               0;
 
                 this.onTouch(null, offsetX, offsetY);
-                pointA = null;
-                pointB = null;
+                this.pointA = null;
+                this.pointB = null;
             } else if (p.type === PointerEventTypes.POINTERDOWN && srcElement) {
                 try {
                     srcElement.setPointerCapture(evt.pointerId);
@@ -98,19 +105,19 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                     //Nothing to do with the error. Execution will continue.
                 }
 
-                if (pointA === null) {
-                    pointA = {x: evt.clientX,
+                if (this.pointA === null) {
+                    this.pointA = {x: evt.clientX,
                               y: evt.clientY,
                               pointerId: evt.pointerId,
                               type: evt.pointerType };
-                } else if (pointB === null) {
-                    pointB = {x: evt.clientX,
+                } else if (this.pointB === null) {
+                    this.pointB = {x: evt.clientX,
                               y: evt.clientY,
                               pointerId: evt.pointerId,
                               type: evt.pointerType };
                 }
 
-                this.onButtonDown(evt, pointB ? 2 : 1);
+                this.onButtonDown(evt);
 
                 if (!noPreventDefault) {
                     evt.preventDefault();
@@ -126,7 +133,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                 }
 
                 if (!isTouch) {
-                    pointB = null; // Mouse and pen are mono pointer
+                    this.pointB = null; // Mouse and pen are mono pointer
                 }
 
                 //would be better to use pointers.remove(evt.pointerId) for multitouch gestures,
@@ -135,17 +142,18 @@ export abstract class BaseCameraPointersInput implements ICameraInput<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
                 if (engine._badOS) {
-                    pointA = pointB = null;
+                    this.pointA = this.pointB = null;
                 } else {
                     //only remove the impacted pointer in case of multitouch allowing on most
                     //platforms switching from rotate to zoom and pan seamlessly.
-                    if (pointB && pointA && pointA.pointerId == evt.pointerId) {
-                        pointA = pointB;
-                        pointB = null;
-                    } else if (pointA && pointB && pointB.pointerId == evt.pointerId) {
-                        pointB = null;
+                    if (this.pointB && this.pointA && this.pointA.pointerId == evt.pointerId) {
+                        this.pointA = this.pointB;
+                        this.pointB = null;
+                    } else if (this.pointA && this.pointB &&
+                               this.pointB.pointerId == evt.pointerId) {
+                        this.pointB = null;
                     } else {
-                        pointA = pointB = null;
+                        this.pointA = this.pointB = null;
                     }
                 }
 
@@ -153,8 +161,8 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                     // Previous pinch data is populated but a button has been lifted
                     // so pinch has ended.
                     this.onMultiTouch(
-                      pointA,
-                      pointB,
+                      this.pointA,
+                      this.pointB,
                       previousPinchSquaredDistance,
                       0,  // pinchSquaredDistance
                       previousMultiTouchPanPosition,
@@ -164,7 +172,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                   previousMultiTouchPanPosition = null;
                 }
 
-                this.onButtonUp(evt, pointA !== null ? 1 : 0);
+                this.onButtonUp(evt);
 
                 if (!noPreventDefault) {
                     evt.preventDefault();
@@ -175,30 +183,31 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
                 }
 
                 // One button down
-                if (pointA && pointB === null) {
-                    var offsetX = evt.clientX - pointA.x;
-                    var offsetY = evt.clientY - pointA.y;
-                    this.onTouch(pointA, offsetX, offsetY);
+                if (this.pointA && this.pointB === null) {
+                    var offsetX = evt.clientX - this.pointA.x;
+                    var offsetY = evt.clientY - this.pointA.y;
+                    this.onTouch(this.pointA, offsetX, offsetY);
 
-                    pointA.x = evt.clientX;
-                    pointA.y = evt.clientY;
+                    this.pointA.x = evt.clientX;
+                    this.pointA.y = evt.clientY;
                 }
                 // Two buttons down: pinch
-                else if (pointA && pointB) {
-                    var ed = (pointA.pointerId === evt.pointerId) ? pointA : pointB;
+                else if (this.pointA && this.pointB) {
+                    var ed = (this.pointA.pointerId === evt.pointerId) ?
+                             this.pointA : this.pointB;
                     ed.x = evt.clientX;
                     ed.y = evt.clientY;
-                    var distX = pointA.x - pointB.x;
-                    var distY = pointA.y - pointB.y;
+                    var distX = this.pointA.x - this.pointB.x;
+                    var distY = this.pointA.y - this.pointB.y;
                     var pinchSquaredDistance = (distX * distX) + (distY * distY);
-                    var multiTouchPanPosition = {x: (pointA.x + pointB.x) / 2,
-                                                 y: (pointA.y + pointB.y) / 2,
+                    var multiTouchPanPosition = {x: (this.pointA.x + this.pointB.x) / 2,
+                                                 y: (this.pointA.y + this.pointB.y) / 2,
                                                  pointerId: evt.pointerId,
                                                  type: p.type};
 
                     this.onMultiTouch(
-                      pointA,
-                      pointB,
+                      this.pointA,
+                      this.pointB,
                       previousPinchSquaredDistance,
                       pinchSquaredDistance,
                       previousMultiTouchPanPosition,
@@ -216,7 +225,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
             PointerEventTypes.POINTERMOVE);
 
         this._onLostFocus = () => {
-            pointA = pointB = null;
+            this.pointA = this.pointB = null;
             previousPinchSquaredDistance = 0;
             previousMultiTouchPanPosition = null;
             this.onLostFocus();
@@ -316,7 +325,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
      * press.
      * Override this method to provide functionality.
      */
-    protected onButtonDown(evt: PointerEvent, buttonCount: number): void {
+    protected onButtonDown(evt: PointerEvent): void {
     }
 
     /**
@@ -324,7 +333,7 @@ export abstract class BaseCameraPointersInput implements ICameraInput<Camera> {
      * release.
      * Override this method to provide functionality.
      */
-    protected onButtonUp(evt: PointerEvent, buttonCount: number): void {
+    protected onButtonUp(evt: PointerEvent): void {
     }
 
     /**

+ 0 - 236
src/Cameras/Inputs/StubCameraPointersInput.ts

@@ -1,236 +0,0 @@
-import { Nullable } from "../../types";
-import { Camera } from "../../Cameras/camera";
-import { CameraInputTypes } from "../../Cameras/cameraInputsManager";
-import { PointerTouch } from "../../Events/pointerEvents";
-import { BaseCameraPointersInput } from "../../Cameras/Inputs/BaseCameraPointersInput";
-
-/**
- * Mock class to be used by UnitTests.
- */
-export class StubCameraPointersInput extends BaseCameraPointersInput {
-  /**
-   * Defines the camera the input is attached to.
-   */
-  public camera: Camera;
-
-  /**
-   * Count how many times onDoubleTap method is called.
-   */
-  public countOnDoubleTap: number;
-
-  /**
-   * Count how many times onTouch method is called.
-   */
-  public countOnTouch: number;
-
-  /**
-   * Count how many times onMultiTouch method is called.
-   */
-  public countOnMultiTouch: number;
-
-  /**
-   * Count how many times onContextMenu method is called.
-   */
-  public countOnContextMenu: number;
-
-  /**
-   * Count how many times onButtonDown method is called.
-   */
-  public countOnButtonDown: number;
-
-  /**
-   * Count how many times onButtonUp method is called.
-   */
-  public countOnButtonUp: number;
-
-  /**
-   * Count how many times onLostFocus method is called.
-   */
-  public countOnLostFocus: number;
-
-  /**
-   * Store arguments of last time onDoubleTap method was called.
-   */
-  public lastOnDoubleTap: any;
-
-  /**
-   * Store arguments of last time onTouch method was called.
-   */
-  public lastOnTouch: any;
-
-  /**
-   * Store arguments of last time onMultiTouch method was called.
-   */
-  public lastOnMultiTouch: any;
-
-  /**
-   * Store arguments of last time onContextMenu method was called.
-   */
-  public lastOnContextMenu: any;
-
-  /**
-   * Store arguments of last time onButtonDown method was called.
-   */
-  public lastOnButtonDown: any;
-
-  /**
-   * Store arguments of last time onButtonUp method was called.
-   */
-  public lastOnButtonUp: any;
-
-  /**
-   * Store arguments when onDoubleTap method is called.
-   */
-  public valuesOnDoubleTap: any[];
-
-  /**
-   * Store arguments when onTouch method is called.
-   */
-  public valuesOnTouch: any[];
-
-  /**
-   * Store arguments when onMultiTouch method is called.
-   */
-  public valuesOnMultiTouch: any[];
-
-  /**
-   * Store arguments when onContextMenu method is called.
-   */
-  public valuesOnContextMenu: any[];
-
-  /**
-   * Store arguments when onButtonDown method is called.
-   */
-  public valuesOnButtonDown: any[];
-
-  /**
-   * Store arguments when onButtonUp method is called.
-   */
-  public valuesOnButtonUp: any[];
-
-  /**
-   * Reset instance of this class to default values.
-   */
-  public reset(): void {
-    this.countOnDoubleTap = 0;
-    this.countOnTouch = 0;
-    this.countOnMultiTouch = 0;
-    this.countOnContextMenu = 0;
-    this.countOnButtonDown = 0;
-    this.countOnButtonUp = 0;
-    this.countOnLostFocus = 0;
-
-    this.valuesOnDoubleTap = [];
-    this.valuesOnTouch = [];
-    this.valuesOnMultiTouch = [];
-    this.valuesOnContextMenu = [];
-    this.valuesOnButtonDown = [];
-    this.valuesOnButtonUp = [];
-
-    this.lastOnDoubleTap = undefined;
-    this.lastOnTouch = undefined;
-    this.lastOnMultiTouch = undefined;
-    this.lastOnContextMenu = undefined;
-    this.lastOnButtonDown = undefined;
-    this.lastOnButtonUp = undefined;
-  }
-
-  /**
-   * Mock class to be used by UnitTests.
-   */
-  constructor() {
-    super();
-    this.reset();
-  }
-
-  /**
-   * Gets the class name of the current input.
-   * @returns the class name
-   */
-  public getClassName(): string {
-    return "StubCameraPointersInput";
-  }
-
-  /**
-   * Called on pointer POINTERDOUBLETAP event.
-   */
-  protected onDoubleTap(type: string) {
-    this.countOnDoubleTap++;
-    this.valuesOnDoubleTap.push(type);
-    this.lastOnDoubleTap = type;
-  }
-
-  /**
-   * Called on pointer POINTERMOVE event if only a single touch is active.
-   */
-  protected onTouch(
-    point: Nullable<PointerTouch>,
-    offsetX: number,
-    offsetY: number):
-  void {
-    this.countOnTouch++;
-    this.lastOnTouch = {point, offsetX, offsetY};
-    this.valuesOnTouch.push(this.lastOnTouch);
-  }
-
-  /**
-   * Called on pointer POINTERMOVE event if multiple touches are active.
-   */
-  protected onMultiTouch(
-    pointA: Nullable<PointerTouch>,
-    pointB: Nullable<PointerTouch>,
-    previousPinchSquaredDistance: number,
-    pinchSquaredDistance: number,
-    previousMultiTouchPanPosition: Nullable<PointerTouch>,
-    multiTouchPanPosition: Nullable<PointerTouch>):
-  void {
-    this.countOnMultiTouch++;
-    this.lastOnMultiTouch = {
-      pointA,
-      pointB,
-      previousPinchSquaredDistance,
-      pinchSquaredDistance,
-      previousMultiTouchPanPosition,
-      multiTouchPanPosition,
-    };
-    this.valuesOnMultiTouch.push(this.lastOnMultiTouch);
-  }
-
-  /**
-   * Called on JS contextmenu event.
-   */
-  protected onContextMenu(evt: PointerEvent): void {
-    evt.preventDefault();
-    this.countOnContextMenu++;
-    this.lastOnContextMenu = evt;
-    this.valuesOnContextMenu.push(evt);
-  }
-
-  /**
-   * Called each time a new POINTERDOWN event occurs. Ie, for each button
-   * press.
-   */
-  protected onButtonDown(evt: PointerEvent, buttonCount: number): void {
-    this.countOnButtonDown++;
-    this.lastOnButtonDown = {evt, buttonCount};
-    this.valuesOnButtonDown.push(this.lastOnButtonDown);
-  }
-
-  /**
-   * Called each time a new POINTERUP event occurs. Ie, for each button
-   * release.
-   */
-  protected onButtonUp(evt: PointerEvent, buttonCount: number): void {
-    this.countOnButtonUp++;
-    this.lastOnButtonUp = {evt, buttonCount};
-    this.valuesOnButtonUp.push(this.lastOnButtonUp);
-  }
-
-  /**
-   * Called when window becomes inactive.
-   */
-  protected onLostFocus(): void {
-    this.countOnLostFocus++;
-  }
-}
-(<any>CameraInputTypes)["StubCameraPointersInput"] = StubCameraPointersInput;

+ 2 - 2
src/Cameras/Inputs/arcRotateCameraPointersInput.ts

@@ -193,7 +193,7 @@ export class ArcRotateCameraPointersInput extends BaseCameraPointersInput {
      * Called each time a new POINTERDOWN event occurs. Ie, for each button
      * press.
      */
-    protected onButtonDown(evt: PointerEvent, buttonCount: number): void {
+    protected onButtonDown(evt: PointerEvent): void {
         this._isPanClick = evt.button === this.camera._panningMouseButton;
     }
 
@@ -201,7 +201,7 @@ export class ArcRotateCameraPointersInput extends BaseCameraPointersInput {
      * Called each time a new POINTERUP event occurs. Ie, for each button
      * release.
      */
-    protected onButtonUp(evt: PointerEvent, buttonCount: number): void {
+    protected onButtonUp(evt: PointerEvent): void {
         this._twoFingerActivityCount = 0;
         this._isPinching = false;
     }

+ 0 - 1
src/Cameras/Inputs/index.ts

@@ -3,7 +3,6 @@ export * from "./arcRotateCameraKeyboardMoveInput";
 export * from "./arcRotateCameraMouseWheelInput";
 export * from "./arcRotateCameraPointersInput";
 export * from "./arcRotateCameraVRDeviceOrientationInput";
-export * from "./StubCameraPointersInput";
 export * from "./flyCameraKeyboardInput";
 export * from "./flyCameraMouseInput";
 export * from "./followCameraKeyboardMoveInput";

+ 15 - 3
tests/unit/babylon/src/Cameras/babylon.pointerInput.tests.ts

@@ -75,11 +75,17 @@ function simulateEvent(cameraInput: BABYLON.ICameraInput<BABYLON.Camera>,
 }
 
 /**
- * Override the methods of an existing camera to use when testing BaseCameraPointersInput.
+ * Override the methods of an existing camera to create a stub for testing
+ * BaseCameraPointersInput.
+ * @returns An instance of ArcRotateCameraPointersInput with the interesting
+ *   methods stubbed out.
  */
 function StubCameraInput() {
   let cameraInput: any = (<any>new BABYLON.ArcRotateCameraPointersInput());
 
+  /**
+   * Reset all counters.
+   */
   cameraInput.reset = ((): void => {
     cameraInput.countOnDoubleTap = 0;
     cameraInput.countOnTouch = 0;
@@ -99,6 +105,10 @@ function StubCameraInput() {
 
   cameraInput.reset();
 
+  /**
+   * Stub out all mothods we want to test as part of the BaseCameraPointersInput testing.
+   * These stubs keep track of how many times they were called and 
+   */
   cameraInput.onTouch = 
     ((point: BABYLON.Nullable<BABYLON.PointerTouch>, offsetX: number, offsetY: number) => {
       cameraInput.countOnTouch++;
@@ -129,13 +139,15 @@ function StubCameraInput() {
       };
     });
 
-  cameraInput.onButtonDown = ((evt: PointerEvent, buttonCount: number) => {
+  cameraInput.onButtonDown = ((evt: PointerEvent) => {
     cameraInput.countOnButtonDown++;
+    let buttonCount = cameraInput.pointB !== null ? 2 : 1;
     cameraInput.lastOnButtonDown = {evt, buttonCount};
   });
 
-  cameraInput.onButtonUp = ((evt: PointerEvent, buttonCount: number) => {
+  cameraInput.onButtonUp = ((evt: PointerEvent) => {
     cameraInput.countOnButtonUp++;
+    let buttonCount = cameraInput.pointA !== null ? 1 : 0;
     cameraInput.lastOnButtonUp = {evt, buttonCount};
   });