All files / src/controller/input/types inputTypePointer.js

100% Statements 175/175
90.25% Branches 139/154
100% Functions 33/33
100% Lines 173/173

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263  1x 1x 1x 1x     30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x     26x 26x 26x 26x 26x 26x 26x 26x 26x     1x 1x 1x 1x 1x 1x 1x 1x     10x 10x 3x 3x   7x 7x 6x       1x     1x 1x     1x 1x     1x 1x     1x 1x     7x 7x   7x 4x 4x         1x 1x 1x       1x 1x 1x     11x 11x 11x 11x 11x 11x 1x 10x 10x 5x     11x 6x   11x     22x   12x 12x 12x 12x   12x       16x 16x 12x 6x     16x       12x 12x 12x 6x 6x     6x 4x 1x     6x       6x 6x 1x   5x 5x 5x 5x 5x 5x 2x 2x 2x   5x   5x 5x   5x     14x 8x 6x 6x 6x 6x     1x 1x     2x   1x 1x 1x 1x 1x         14x 1x 13x 6x 2x 2x   6x 6x   7x 1x 6x 6x     13x 1x 12x 11x 1x     6x 1x 5x 5x 5x 1x 4x 4x 4x     4x 2x 1x 1x   3x 3x 3x 1x   3x 3x 3x 3x 1x       3x 1x 2x 2x     21x 1x 20x     8x 1x 7x     1x  
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InputTypePointer = void 0;
const bounds2_1 = require("../../../models/bounds2");
const inputType_1 = require("./inputType");
class InputTypePointer extends inputType_1.InputType {
    constructor(engineCanvas) {
        super();
        this._hoveredEntity = null;
        this._pressedEntity = null;
        this._pointerPostion = new bounds2_1.Bounds2();
        this._engineCanvas = engineCanvas;
        this.onPointerDown = this.onPointerDown.bind(this);
        this.onPointerUp = this.onPointerUp.bind(this);
        this.onPointerMove = this.onPointerMove.bind(this);
        this.onPointerLeave = this.onPointerLeave.bind(this);
        this.onTouchStart = this.onTouchStart.bind(this);
        this.onTouchMove = this.onTouchMove.bind(this);
        this.onTouchEnd = this.onTouchEnd.bind(this);
    }
    initialize(engineCore, inputReceiver) {
        super.initialize(engineCore, inputReceiver);
        this.inputReceiver.addEventListener("mousedown", this.onPointerDown);
        this.inputReceiver.addEventListener("mouseup", this.onPointerUp);
        this.inputReceiver.addEventListener("mousemove", this.onPointerMove);
        this.inputReceiver.addEventListener("mouseleave", this.onPointerLeave);
        this.inputReceiver.addEventListener("touchstart", this.onTouchStart, { passive: true });
        this.inputReceiver.addEventListener("touchmove", this.onTouchMove, { passive: true });
        this.inputReceiver.addEventListener("touchcancel", this.onTouchEnd, { passive: true });
        this.inputReceiver.addEventListener("touchend", this.onTouchEnd, { passive: true });
    }
    destroy() {
        this.inputReceiver.removeEventListener("mousedown", this.onPointerDown);
        this.inputReceiver.removeEventListener("mouseup", this.onPointerUp);
        this.inputReceiver.removeEventListener("mousemove", this.onPointerMove);
        this.inputReceiver.removeEventListener("mouseleave", this.onPointerLeave);
        this.inputReceiver.removeEventListener("touchstart", this.onTouchStart);
        this.inputReceiver.removeEventListener("touchmove", this.onTouchMove);
        this.inputReceiver.removeEventListener("touchcancel", this.onTouchEnd);
        this.inputReceiver.removeEventListener("touchend", this.onTouchEnd);
    }
    setStyle(cursor) {
        const element = this._engineCanvas;
        if (cursor !== "default") {
            element.style.cursor = cursor;
            return;
        }
        element.style.removeProperty("cursor");
        if (!element.getAttribute("style")) {
            element.removeAttribute("style");
        }
    }
    refresh() {
        this.onPointerUpdate(this._pointerPostion);
    }
    onPointerDown(event) {
        const mouseEvent = event;
        this.pressed(this._hoveredEntity, mouseEvent.position);
    }
    onPointerUp(event) {
        const mouseEvent = event;
        this.tapped(this._hoveredEntity, mouseEvent.position);
    }
    onPointerMove(event) {
        const mouseEvent = event;
        this.onPointerUpdate(mouseEvent.position);
    }
    onPointerLeave(event) {
        const mouseEvent = event;
        this.onPointerExit(mouseEvent.position);
    }
    onTouchStart(event) {
        const touchEvent = event;
        touchEvent.positions.forEach(position => {
            var _a;
            if (this.onPointerUpdate(position)) {
                this.pressed(this._hoveredEntity, position, true);
                (_a = this._hoveredEntity) === null || _a === void 0 ? void 0 : _a.touchStart(touchEvent.positions);
            }
        });
    }
    onTouchMove(event) {
        const touchEvent = event;
        touchEvent.positions.forEach(position => {
            this.onPointerUpdate(position);
        });
    }
    onTouchEnd(event) {
        const touchEvent = event;
        touchEvent.positions.forEach(position => this.tapped(this._hoveredEntity, position));
        this.removeHoveredEntity(this._hoveredEntity, this._pointerPostion);
    }
    onPointerUpdate(position) {
        let isEntityHovered = false;
        this._pointerPostion = position;
        let scenes = this.engineCore.children;
        scenes = [...scenes].reverse();
        for (const scene of scenes) {
            if (!scene.isVisible || !scene.isEnabled)
                continue;
            this.moveEnitities(scene.children, position);
            if (this.updateEntities(scene.children, isEntityHovered, position)) {
                isEntityHovered = true;
            }
        }
        if (!isEntityHovered) {
            this.removeHoveredEntity(this._hoveredEntity, position);
        }
        return isEntityHovered;
    }
    moveEnitities(collection, position) {
        collection.forEach(entity => {
            var _a;
            const inputEntity = entity;
            const receivePointerInput = (_a = inputEntity === null || inputEntity === void 0 ? void 0 : inputEntity.receivePointerInput) !== null && _a !== void 0 ? _a : false;
            Eif (receivePointerInput) {
                this.move(inputEntity, position);
            }
            this.moveEnitities(entity.children, position);
        });
    }
    updateEntities(collection, isEntityHovered, position) {
        const entities = [...collection].reverse();
        for (const entity of entities) {
            if (this.updateEntity(entity, isEntityHovered, position)) {
                isEntityHovered = true;
            }
        }
        return isEntityHovered;
    }
    updateEntity(entity, isEntityHovered, position) {
        var _a;
        const inputEntity = entity;
        const receivePointerInput = (_a = inputEntity === null || inputEntity === void 0 ? void 0 : inputEntity.receivePointerInput) !== null && _a !== void 0 ? _a : false;
        if (this.isHovered(entity, position)) {
            Eif (!isEntityHovered) {
                return this.addHoveredEntity(entity, isEntityHovered, position);
            }
        }
        else if (receivePointerInput && inputEntity.isHovered) {
            if (!inputEntity.isHitTestVisible) {
                this.leave(inputEntity, position);
            }
        }
        return false;
    }
    addHoveredEntity(entity, isEntityHovered, position) {
        var _a, _b, _c;
        let isHovered = this.updateEntities(entity.children, isEntityHovered, position);
        if (isHovered) {
            return isHovered;
        }
        const inputEntity = entity;
        const receivePointerInput = (_a = inputEntity === null || inputEntity === void 0 ? void 0 : inputEntity.receivePointerInput) !== null && _a !== void 0 ? _a : false;
        const isHoveredPressed = (_c = (_b = this._hoveredEntity) === null || _b === void 0 ? void 0 : _b.isPointerPressed) !== null && _c !== void 0 ? _c : false;
        Eif (receivePointerInput && !isHovered) {
            Eif (inputEntity.isHitTestVisible && !isHoveredPressed) {
                if (inputEntity !== this._hoveredEntity) {
                    this.removeHoveredEntity(this._hoveredEntity, position);
                    this.setPointerStyle(inputEntity, true);
                    this._hoveredEntity = inputEntity;
                }
                isHovered = true;
            }
            inputEntity.isHovered = true;
            this.hover(inputEntity, true, isHoveredPressed, position);
        }
        return isHovered;
    }
    removeHoveredEntity(inputEntity, position) {
        if (!(inputEntity === null || inputEntity === void 0 ? void 0 : inputEntity.isPointerEntered) || (inputEntity.isEnabled && inputEntity.isPointerPressed))
            return;
        inputEntity.isHovered = false;
        this.setPointerStyle(inputEntity, false);
        this.hover(inputEntity, false, false, position);
        this._hoveredEntity = null;
    }
    onPointerExit(position) {
        const scenes = this.engineCore.children;
        scenes.forEach(scene => this.exitEntities(scene.children, position));
    }
    exitEntities(collection, position) {
        collection.forEach(entity => {
            var _a;
            const inputEntity = entity;
            const receivePointerInput = (_a = inputEntity === null || inputEntity === void 0 ? void 0 : inputEntity.receivePointerInput) !== null && _a !== void 0 ? _a : false;
            this.exitEntities(inputEntity.children, position);
            Eif (receivePointerInput && inputEntity.isHovered) {
                this.leave(inputEntity, position);
            }
        });
    }
    hover(inputEntity, isHovered, isHoveredPressed, position) {
        if (!inputEntity)
            return;
        if (isHovered || inputEntity.isPointerPressed) {
            if (!inputEntity.isPointerEntered && !isHoveredPressed) {
                inputEntity.isPointerEntered = true;
                inputEntity.pointerEnter(position);
            }
            inputEntity.pointerHover(position);
            return;
        }
        if (!inputEntity.isPointerEntered)
            return;
        inputEntity.isPointerEntered = false;
        inputEntity.pointerLeave(position);
    }
    move(inputEntity, position) {
        if (!inputEntity)
            return;
        if (!inputEntity.isEnabled || !inputEntity.isPointerPressed)
            return;
        inputEntity.pointerMove(position);
    }
    pressed(inputEntity, position, isTouch = false) {
        if (!inputEntity)
            return;
        const active = this.isHovered(inputEntity, position);
        const checkTouchHitBox = isTouch && inputEntity.isTouchHitBoxDisabled;
        if (!inputEntity.isEnabled || (!checkTouchHitBox && !active))
            return;
        this._pressedEntity = inputEntity;
        this._pressedEntity.isPointerPressed = true;
        this._pressedEntity.pointerPressed(position);
    }
    tapped(inputEntity, position) {
        if (!inputEntity) {
            if (!this._pressedEntity)
                return;
            inputEntity = this._pressedEntity;
        }
        const active = this.isHovered(inputEntity, position);
        const pressed = inputEntity.isPointerPressed;
        if (active || pressed) {
            inputEntity.pointerTapped(position);
        }
        inputEntity.isPointerPressed = false;
        inputEntity.pointerReleased(position);
        this.removeHoveredEntity(inputEntity, position);
        if (active) {
            this.onPointerUpdate(this._pointerPostion);
        }
    }
    leave(inputEntity, position) {
        if (!inputEntity)
            return;
        inputEntity.isPointerPressed = false;
        this.removeHoveredEntity(inputEntity, position);
    }
    isHovered(entity, position) {
        if (!entity)
            return false;
        return entity.isEnabled && entity.hitTest(position);
    }
    setPointerStyle(entity, isHovered) {
        if (!(entity === null || entity === void 0 ? void 0 : entity.isHitTestVisible) || (entity.isEnabled && entity.isPointerPressed))
            return;
        this.setStyle(isHovered && entity.isEnabled ? "pointer" : "default");
    }
}
exports.InputTypePointer = InputTypePointer;