All files / src/controller/input inputReceiver.js

100% Statements 55/55
100% Branches 4/4
100% Functions 6/6
100% Lines 55/55

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  1x 1x 1x 1x 1x 1x     10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     2x 2x 2x 1x 1x     1x 1x     1x 1x     1x 1x 1x 1x 1x   1x     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputReceiver = void 0;
const bounds2_1 = require("../../models/bounds2");
const eventTouch_1 = require("./events/eventTouch");
const eventMouse_1 = require("./events/eventMouse");
const eventKey_1 = require("./events/eventKey");
class InputReceiver extends EventTarget {
    constructor(engineCanvas) {
        super();
        this._engineCanvas = engineCanvas;
        const document = this._engineCanvas.ownerDocument;
        const defaultView = document.defaultView;
        this.onKey = this.onKey.bind(this);
        this.onMouse = this.onMouse.bind(this);
        this.onTouch = this.onTouch.bind(this);
        defaultView.addEventListener("keydown", this.onKey);
        defaultView.addEventListener("keyup", this.onKey);
        this._engineCanvas.addEventListener("keydown", this.onKey);
        this._engineCanvas.addEventListener("keyup", this.onKey);
        this._engineCanvas.addEventListener("mousedown", this.onMouse);
        this._engineCanvas.addEventListener("mouseup", this.onMouse);
        this._engineCanvas.addEventListener("mousemove", this.onMouse);
        this._engineCanvas.addEventListener("mouseleave", this.onMouse);
        this._engineCanvas.addEventListener("touchstart", this.onTouch, false);
        this._engineCanvas.addEventListener("touchend", this.onTouch, false);
        this._engineCanvas.addEventListener("touchcancel", this.onTouch, false);
        this._engineCanvas.addEventListener("touchmove", this.onTouch, false);
    }
    destroy() {
        const document = this._engineCanvas.ownerDocument;
        const defaultView = document.defaultView;
        defaultView.removeEventListener("keydown", this.onKey);
        defaultView.removeEventListener("keyup", this.onKey);
        this._engineCanvas.removeEventListener("keydown", this.onKey);
        this._engineCanvas.removeEventListener("keyup", this.onKey);
        this._engineCanvas.removeEventListener("mousedown", this.onMouse);
        this._engineCanvas.removeEventListener("mouseup", this.onMouse);
        this._engineCanvas.removeEventListener("mousemove", this.onMouse);
        this._engineCanvas.removeEventListener("mouseleave", this.onMouse);
        this._engineCanvas.removeEventListener("touchstart", this.onTouch, false);
        this._engineCanvas.removeEventListener("touchend", this.onTouch, false);
        this._engineCanvas.removeEventListener("touchcancel", this.onTouch, false);
        this._engineCanvas.removeEventListener("touchmove", this.onTouch, false);
    }
    onKey(event) {
        const document = this._engineCanvas.ownerDocument;
        const focused = document.activeElement === this._engineCanvas;
        if (focused && event.currentTarget instanceof Window)
            return;
        this.dispatchEvent(new eventKey_1.EventKey(focused, event.type, event));
    }
    onMouse(event) {
        const position = new bounds2_1.Bounds2(event.clientX, event.clientY);
        this.dispatchEvent(new eventMouse_1.EventMouse(position, event.type, event));
    }
    onTouch(event) {
        const positions = this.getTouchPositions(event.changedTouches);
        this.dispatchEvent(new eventTouch_1.EventTouch(positions, event.type, event));
    }
    getTouchPositions(touchList) {
        const positions = new Array();
        const touches = Array.from(touchList);
        for (let touch of touches) {
            const position = new bounds2_1.Bounds2(touch.clientX, touch.clientY);
            positions.push(position);
        }
        return positions;
    }
}
exports.InputReceiver = InputReceiver;