All files / src/eventInput PointerEventInput.ts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115            9x   62x 62x 62x 62x         62x 62x   9x         12x 12x     12x 12x 12x     9x         1190x     1190x 1190x     9x 11x     9x 11x 11x 11x 11x     9x 1213x     9x 1190x 1190x               9x 1202x           9x 1190x 1190x           1190x           1202x 1202x 1202x 1190x 1190x 1190x     1202x 12x 12x       9x 11x 11x   11x 11x     9x  
/*
 * Copyright (c) 2015 NAVER Corp.
 * egjs projects are licensed under the MIT license
 */
import { InputEventType, ExtendedEvent } from "../types";
 
import { EventInput, SUPPORT_POINTER } from "./EventInput";
 
export class PointerEventInput extends EventInput {
  public readonly start = SUPPORT_POINTER ? ["pointerdown"] : ["MSPointerDown"];
  public readonly move = SUPPORT_POINTER ? ["pointermove"] : ["MSPointerMove"];
  public readonly end = SUPPORT_POINTER
    ? ["pointerup", "pointercancel"]
    : ["MSPointerUp", "MSPointerCancel"];
 
  // store first, recent inputs for each event id
  private _firstInputs: PointerEvent[] = [];
  private _recentInputs: PointerEvent[] = [];
 
  public onEventStart(
    event: InputEventType,
    inputKey?: string[],
    inputButton?: string[]
  ): ExtendedEvent {
    const button = this._getButton(event);
    Iif (!this._isValidEvent(event, inputKey, inputButton)) {
      return null;
    }
    this._preventMouseButton(event, button);
    this._updatePointerEvent(event as PointerEvent);
    return this.extendEvent(event);
  }
 
  public onEventMove(
    event: InputEventType,
    inputKey?: string[],
    inputButton?: string[]
  ): ExtendedEvent {
    Iif (!this._isValidEvent(event, inputKey, inputButton)) {
      return null;
    }
    this._updatePointerEvent(event as PointerEvent);
    return this.extendEvent(event);
  }
 
  public onEventEnd(event: InputEventType): void {
    this._removePointerEvent(event as PointerEvent);
  }
 
  public onRelease(): void {
    this.prevEvent = null;
    this._firstInputs = [];
    this._recentInputs = [];
    return;
  }
 
  public getTouches(): number {
    return this._recentInputs.length;
  }
 
  protected _getScale(): number {
    Eif (this._recentInputs.length !== 2) {
      return null; // TODO: consider calculating non-pinch gesture scale
    }
    return (
      this._getDistance(this._recentInputs[0], this._recentInputs[1]) /
      this._getDistance(this._firstInputs[0], this._firstInputs[1])
    );
  }
 
  protected _getCenter(event: PointerEvent): { x: number; y: number } {
    return {
      x: event.clientX,
      y: event.clientY,
    };
  }
 
  protected _getMovement(event: PointerEvent): { x: number; y: number } {
    const prev = this.prevEvent.srcEvent as PointerEvent;
    Iif (event.pointerId !== prev.pointerId) {
      return {
        x: 0,
        y: 0,
      };
    }
    return {
      x: event.clientX - prev.clientX,
      y: event.clientY - prev.clientY,
    };
  }
 
  private _updatePointerEvent(event: PointerEvent) {
    let addFlag = false;
    this._recentInputs.forEach((e, i) => {
      Eif (e.pointerId === event.pointerId) {
        addFlag = true;
        this._recentInputs[i] = event;
      }
    });
    if (!addFlag) {
      this._firstInputs.push(event);
      this._recentInputs.push(event);
    }
  }
 
  private _removePointerEvent(event?: PointerEvent) {
    this._firstInputs = this._firstInputs.filter(
      (x) => x.pointerId !== event.pointerId
    );
    this._recentInputs = this._recentInputs.filter(
      (x) => x.pointerId !== event.pointerId
    );
  }
}