All files / src/eventInput MouseEventInput.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          9x   9x   9x 7x 7x 7x   9x         1x 1x     1x 1x     9x         2x     2x     9x 1x     9x 1x 1x     9x 4x 4x               9x 2x     9x 3x           9x 2x 2x         9x  
/*
 * Copyright (c) 2015 NAVER Corp.
 * egjs projects are licensed under the MIT license
 */
import { InputEventType, ExtendedEvent } from "../types";
import { MOUSE_BUTTON_CODE_MAP } from "../const";
 
import { EventInput } from "./EventInput";
 
export class MouseEventInput extends EventInput {
  public readonly start = ["mousedown"];
  public readonly move = ["mousemove"];
  public readonly end = ["mouseup"];
 
  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);
    return this.extendEvent(event);
  }
 
  public onEventMove(
    event: InputEventType,
    inputKey?: string[],
    inputButton?: string[]
  ): ExtendedEvent {
    Iif (!this._isValidEvent(event, inputKey, inputButton)) {
      return null;
    }
    return this.extendEvent(event);
  }
 
  public onEventEnd(): void {
    return;
  }
 
  public onRelease(): void {
    this.prevEvent = null;
    return;
  }
 
  public getTouches(event: InputEventType, inputButton?: string[]): number {
    Eif (inputButton) {
      return this._isValidButton(MOUSE_BUTTON_CODE_MAP[event.which], inputButton) &&
        this.end.indexOf(event.type) === -1
        ? 1
        : 0;
    }
    return 0;
  }
 
  protected _getScale(): number {
    return 1;
  }
 
  protected _getCenter(event: MouseEvent): { x: number; y: number } {
    return {
      x: event.clientX,
      y: event.clientY,
    };
  }
 
  protected _getMovement(event: MouseEvent): { x: number; y: number } {
    const prev = this.prevEvent.srcEvent as MouseEvent;
    return {
      x: event.clientX - prev.clientX,
      y: event.clientY - prev.clientY,
    };
  }
}