All files / src/x x-pointer.ts

0% Statements 0/27
0% Branches 0/2
0% Functions 0/9
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                                                                                                                                                                                                                                                         
import * as mathf from '../mathf/mathf';
import {Vector} from '../mathf/vector';
import type {XGameObject} from './x-game-object';
 
/**
 * The main pointer class for x-engine.  Use this to detect collision with
 * GameObjects and respond to the pointer.
 * @unstable
 */
export class XPointer {
  public position: Vector;
  private radius: number;
 
  /**
   * Wehther the mouse is down.
   */
  public isMouseDown = false;
 
  /**
   * The html element to track pointer related events in.
   */
  private element: HTMLElement;
  constructor(element: HTMLElement) {
    this.position = Vector.ZERO;
    this.radius = 1;
    this.element = element;
 
    this.element.addEventListener(
      'mousedown',
      this.pointerDownHandler.bind(this),
      {passive: true}
    );
    this.element.addEventListener(
      'touchstart',
      this.pointerDownHandler.bind(this),
      {passive: true}
    );
    this.element.addEventListener('mouseup', this.pointerUpHandler.bind(this), {
      passive: true,
    });
    this.element.addEventListener(
      'touchend',
      this.pointerUpHandler.bind(this),
      {passive: true}
    );
    this.element.addEventListener(
      'mousemove',
      this.pointerMoveHandler.bind(this),
      {passive: true}
    );
    this.element.addEventListener(
      'touchmove',
      this.pointerMoveHandler.bind(this),
      {passive: true}
    );
  }
 
  get x() {
    return this.position.x;
  }
 
  set x(x: number) {
    this.position.x = x;
  }
 
  get y() {
    return this.position.y;
  }
 
  set y(y: number) {
    this.position.y = y;
  }
 
  pointerDownHandler() {
    this.isMouseDown = true;
  }
 
  pointerUpHandler() {
    this.isMouseDown = false;
  }
 
  pointerMoveHandler(event: MouseEvent | TouchEvent) {
    //Find the pointer’s x and y position (for mouse).
    //Subtract the element's top and left offset from the browser window.
    let pageX;
    let pageY;
    if ((event as TouchEvent).touches) {
      pageX = (event as TouchEvent).touches[0].pageX;
      pageY = (event as TouchEvent).touches[0].pageY;
    } else {
      pageX = (event as MouseEvent).pageX;
      pageY = (event as MouseEvent).pageY;
    }
 
    // TODO (uxder) Optimization point. Cache offset value to avoid thrashing.
    const x = pageX - this.element.offsetLeft;
    const y = pageY - this.element.offsetTop;
 
    this.position.set(x, y);
  }
 
  /**
   * Tests a collision with a GameObject.
   * @param object
   */
  testCollidingWithGameObject(object: XGameObject) {
    const point = {
      x: this.x,
      y: this.y,
    };
 
    // Generate a polygon out of the (possibly rotated)
    // box coordinates.
    const polygon = [
      object.globalComputedBox.topLeft,
      object.globalComputedBox.topRight,
      object.globalComputedBox.bottomRight,
      object.globalComputedBox.bottomLeft,
    ];
 
    // console.log(this.position);
 
    // Account for anchor.
    // rect.x += object.width * object.anchorX;
    // rect.y += object.height * object.anchorY;
 
    // console.log('rect.x', rect.x);
    // console.log('rect.y', rect.y);
 
    return mathf.collisionPointVersusConvexPolygon(point, polygon);
  }
}