import { AppObjectController, Vector2 } from "@vived/core";
import { pixelToSceneRay } from "../../Babylon";
import { TouchInputEntity, } from "../../TouchController";
import { PressAppObjectUC } from "../UC/PressAppObjectUC";
export class TouchPressOnAppObject extends AppObjectController {
    constructor(appObject) {
        super(appObject, TouchPressOnAppObject.type);
        this.startPosition = Vector2.Zero();
        this.endPosition = Vector2.Zero();
        this.doTouchEnd = false;
        this.pressedID = null;
        this.isPressed = false;
        this.allow = (e) => {
            return true;
        };
        this.onTouchStart = (e) => {
            this.doTouchEnd = false;
            this.isPressed = true;
            if (!this.pressUC) {
                return;
            }
            if (e.touches.length !== 1) {
                return;
            }
            if (!this.allow(e)) {
                return;
            }
            this.startPosition = e.touches[0].position;
            this.endPosition = this.startPosition;
            this.doTouchEnd = true;
            const ray = pixelToSceneRay(this.endPosition, this.appObjects);
            this.pressedID = this.pressUC?.tryPressAppObject(ray);
        };
        this.onTouchMove = (e) => {
            if (e.touches.length === 1) {
                this.endPosition = e.touches[0].position;
                const movement = Vector2.Subtract(this.startPosition, this.endPosition)
                    .magnitued;
                if (movement > TouchPressOnAppObject.maxTapDistancePx) {
                    this.pressUC?.pressAppObject(null, true);
                }
            }
            else {
                this.pressUC?.pressAppObject(null, true);
                this.doTouchEnd = false;
            }
        };
        this.onTouchEnd = () => {
            if (!this.doTouchEnd) {
                return;
            }
            this.isPressed = false;
            this.pressUC?.pressAppObject(this.pressedID, this.isPressed);
        };
        this.pressUC = appObject.getComponent(PressAppObjectUC.type);
        if (!this.pressUC) {
            console.warn("TouchPress has been added to an App Object that does not have a PressAppObjectUC. Add a PressAppObjectUC before adding the Controller.");
            return;
        }
        const touchInput = TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
TouchPressOnAppObject.type = "TouchPressOnAppObject";
TouchPressOnAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=TouchPressOnAppObject.js.map