"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchPressOnAppObject = void 0;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const TouchController_1 = require("../../TouchController");
const PressAppObjectUC_1 = require("../UC/PressAppObjectUC");
class TouchPressOnAppObject extends core_1.AppObjectController {
    constructor(appObject) {
        super(appObject, TouchPressOnAppObject.type);
        this.startPosition = core_1.Vector2.Zero();
        this.endPosition = core_1.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 = (0, Babylon_1.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 = core_1.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_1.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 = TouchController_1.TouchInputEntity.get(this.appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
exports.TouchPressOnAppObject = TouchPressOnAppObject;
TouchPressOnAppObject.type = "TouchPressOnAppObject";
TouchPressOnAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=TouchPressOnAppObject.js.map