All files / ts/controller/input/types inputTypePointer.ts

100% Statements 173/173
90.25% Branches 139/154
100% Functions 40/40
100% Lines 164/164

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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311    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       7x 7x 4x 4x           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    
import { IDestroyable, IRefreshable } from "../../../environment/objectStates.js";
import { IInputReceiver } from "../inputReceiver.js";
import { IBounds2, Bounds2 } from "../../../models/bounds2.js";
import { EventMouse } from "../events/eventMouse.js";
import { EventTouch } from "../events/eventTouch.js";
import { IEntityInput } from "../../../components/entities/entityInput.js";
import { IEngineCore } from "../../../engineCore.js";
import { IEntity } from "../../../components/entities/entity.js";
import { IInputType, InputType } from "./inputType.js";
import { IEngineCanvas } from "../../../engineCanvas.js";
 
export interface IInputTypePointer extends IInputType, IRefreshable, IDestroyable {
    setStyle(cursor: string): void;
}
export class InputTypePointer extends InputType implements IInputTypePointer {
 
    private readonly _engineCanvas: IEngineCanvas;
    private readonly _mouseDown: (e: Event) => void;
    private readonly _mouseUp: (e: Event) => void;
    private readonly _mouseMove: (e: Event) => void;
    private readonly _mouseLeave: (e: Event) => void;
    private readonly _touchStart: (e: Event) => void;
    private readonly _touchMove: (e: Event) => void;
    private readonly _touchEnd: (e: Event) => void;
 
    private _hoveredEntity: IEntityInput | null;
    private _pressedEntity: IEntityInput | null;
    private _pointerPostion: IBounds2;
 
    public constructor(engineCanvas: IEngineCanvas) {
        super();
        this._hoveredEntity = null;
        this._pressedEntity = null;
        this._pointerPostion = new Bounds2();
        this._engineCanvas = engineCanvas;
 
        this._mouseDown = (e: Event): void => this.onPointerDown(<EventMouse>e);
        this._mouseUp = (e: Event): void => this.onPointerUp(<EventMouse>e);
        this._mouseMove = (e: Event): void => this.onPointerMove(<EventMouse>e);
        this._mouseLeave = (e: Event): void => this.onPointerLeave(<EventMouse>e);
        this._touchStart = (e: Event): void => this.onTouchStart(<EventTouch>e);
        this._touchMove = (e: Event): void => this.onTouchMove(<EventTouch>e);
        this._touchEnd = (e: Event): void => this.onTouchEnd(<EventTouch>e);
    }
 
    public override initialize(engineCore: IEngineCore, inputReceiver: IInputReceiver): void {
        super.initialize(engineCore, inputReceiver);
        this.inputReceiver.addEventListener("mousedown", this._mouseDown);
        this.inputReceiver.addEventListener("mouseup", this._mouseUp);
        this.inputReceiver.addEventListener("mousemove", this._mouseMove);
        this.inputReceiver.addEventListener("mouseleave", this._mouseLeave);
        this.inputReceiver.addEventListener("touchstart", this._touchStart);
        this.inputReceiver.addEventListener("touchmove", this._touchMove);
        this.inputReceiver.addEventListener("touchcancel", this._touchEnd);
        this.inputReceiver.addEventListener("touchend", this._touchEnd);
    }
 
    public destroy(): void {
        this.inputReceiver.removeEventListener("mousedown", this._mouseDown);
        this.inputReceiver.removeEventListener("mouseup", this._mouseUp);
        this.inputReceiver.removeEventListener("mousemove", this._mouseMove);
        this.inputReceiver.removeEventListener("mouseleave", this._mouseLeave);
        this.inputReceiver.removeEventListener("touchstart", this._touchStart);
        this.inputReceiver.removeEventListener("touchmove", this._touchMove);
        this.inputReceiver.removeEventListener("touchcancel", this._touchEnd);
        this.inputReceiver.removeEventListener("touchend", this._touchEnd);
    }
 
    public setStyle(cursor: string): void {
        const element = this._engineCanvas;
        if (cursor !== "default") {
            element.style.cursor = cursor;
            return;
        }
        element.style.removeProperty("cursor");
        if (!element.getAttribute("style")) {
            element.removeAttribute("style");
        }
    }
 
    public refresh(): void {
        this.onPointerUpdate(this._pointerPostion);
    }
 
    private onPointerDown(event: EventMouse): void {
        this.pressed(this._hoveredEntity, event.position);
    }
 
    private onPointerUp(event: EventMouse): void {
        this.tapped(this._hoveredEntity, event.position);
    }
 
    private onPointerMove(event: EventMouse): void {
        this.onPointerUpdate(event.position);
    }
 
    private onPointerLeave(event: EventMouse): void {
        this.onPointerExit(event.position);
    }
 
    private onTouchStart(event: EventTouch): void {
        event.positions.forEach(position => {
            if (this.onPointerUpdate(position)) {
                this.pressed(this._hoveredEntity, position, true);
                this._hoveredEntity?.touchStart(event.positions);
            }
        });
    }
 
    private onTouchMove(event: EventTouch): void {
        event.positions.forEach(position => {
            this.onPointerUpdate(position);
        });
    }
 
    private onTouchEnd(event: EventTouch): void {
        event.positions.forEach(position => this.tapped(this._hoveredEntity, position));
        this.removeHoveredEntity(this._hoveredEntity, this._pointerPostion);
    }
 
    private onPointerUpdate(position: IBounds2): boolean {
        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;
    }
 
    private moveEnitities(collection: ReadonlyArray<IEntity>, position: IBounds2): void {
        collection.forEach(entity => {
            const inputEntity = entity as IEntityInput;
            const receivePointerInput = inputEntity?.receivePointerInput ?? false;
            Eif (receivePointerInput) {
                this.move(inputEntity, position);
            }
            this.moveEnitities(entity.children, position);
        });
    }
 
    private updateEntities(collection: ReadonlyArray<IEntity>, isEntityHovered: boolean, position: IBounds2): boolean {
        const entities = [...collection].reverse();
        for (const entity of entities) {
            if (this.updateEntity(entity, isEntityHovered, position)) {
                isEntityHovered = true;
            }
        }
        return isEntityHovered;
    }
 
    private updateEntity(entity: IEntity, isEntityHovered: boolean, position: IBounds2): boolean {
        const inputEntity = entity as IEntityInput;
        const receivePointerInput = inputEntity?.receivePointerInput ?? 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;
    }
 
    private addHoveredEntity(entity: IEntity, isEntityHovered: boolean, position: IBounds2): boolean {
        let isHovered = this.updateEntities(entity.children, isEntityHovered, position);
        if (isHovered) {
            return isHovered;
        }
        const inputEntity = entity as IEntityInput;
        const receivePointerInput = inputEntity?.receivePointerInput ?? false;
        const isHoveredPressed = this._hoveredEntity?.isPointerPressed ?? 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;
    }
 
    private removeHoveredEntity(inputEntity: IEntityInput | null, position: IBounds2): void {
        if (!inputEntity?.isPointerEntered || (inputEntity.isEnabled && inputEntity.isPointerPressed))
            return;
 
        inputEntity.isHovered = false;
        this.setPointerStyle(inputEntity, false);
        this.hover(inputEntity, false, false, position);
        this._hoveredEntity = null;
    }
 
    private onPointerExit(position: IBounds2): void {
        const scenes = this.engineCore.children;
        scenes.forEach(scene => this.exitEntities(scene.children, position));
    }
 
    private exitEntities(collection: ReadonlyArray<IEntity>, position: IBounds2): void {
        collection.forEach(entity => {
            const inputEntity = entity as IEntityInput;
            const receivePointerInput = inputEntity?.receivePointerInput ?? false;
            this.exitEntities(inputEntity.children, position);
            Eif (receivePointerInput && inputEntity.isHovered) {
                this.leave(inputEntity, position);
            }
        });
    }
 
    private hover(inputEntity: IEntityInput | null, isHovered: boolean, isHoveredPressed: boolean, position: IBounds2): void {
        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);
    }
 
    private move(inputEntity: IEntityInput | null, position: IBounds2): void {
        if (!inputEntity)
            return;
 
        if (!inputEntity.isEnabled || !inputEntity.isPointerPressed)
            return;
 
        inputEntity.pointerMove(position);
    }
 
    private pressed(inputEntity: IEntityInput | null, position: IBounds2, isTouch: boolean = false): void {
        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);
    }
 
    private tapped(inputEntity: IEntityInput | null, position: IBounds2): void {
        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);
        }
    }
 
    private leave(inputEntity: IEntityInput | null, position: IBounds2): void {
        if (!inputEntity)
            return;
 
        inputEntity.isPointerPressed = false;
        this.removeHoveredEntity(inputEntity, position);
    }
 
    private isHovered(entity: IEntity | null, position: IBounds2): boolean {
        if (!entity)
            return false;
 
        return entity.isEnabled && entity.hitTest(position);
    }
 
    private setPointerStyle(entity: IEntityInput | null, isHovered: boolean): void {
        if (!entity?.isHitTestVisible || (entity.isEnabled && entity.isPointerPressed))
            return;
 
        this.setStyle(isHovered && entity.isEnabled ? "pointer" : "default");
    }
}