"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TapToSelectAppObject = void 0;
const core_1 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const TouchController_1 = require("../../TouchController");
const SelectAppObjectUC_1 = require("../UC/SelectAppObjectUC");
class TapToSelectAppObject extends core_1.AppObjectController {
    constructor(appObject) {
        super(appObject, TapToSelectAppObject.type);
        this.startPosition = core_1.Vector2.Zero();
        this.endPosition = core_1.Vector2.Zero();
        this.doTouchEnd = false;
        this.allow = (e) => {
            return true;
        };
        this.onTouchStart = (e) => {
            this.doTouchEnd = false;
            if (e.touches.length !== 1) {
                return;
            }
            if (!this.allow(e)) {
                return;
            }
            this.startPosition = e.touches[0].position;
            this.endPosition = this.startPosition;
            this.doTouchEnd = true;
        };
        this.onTouchMove = (e) => {
            if (e.touches.length === 1) {
                this.endPosition = e.touches[0].position;
            }
            else {
                this.doTouchEnd = false;
            }
        };
        this.onTouchEnd = () => {
            if (!this.doTouchEnd) {
                return;
            }
            if (!this.selectUC) {
                return;
            }
            const movement = core_1.Vector2.Subtract(this.startPosition, this.endPosition)
                .magnitued;
            if (movement <= TapToSelectAppObject.maxTapDistancePx) {
                const ray = (0, Babylon_1.pixelToSceneRay)(this.endPosition, this.appObjects);
                this.selectUC?.trySelectAppObject(ray);
            }
        };
        this.selectUC = appObject.getComponent(SelectAppObjectUC_1.SelectAppObjectUC.type);
        if (!this.selectUC) {
            console.warn("MouseSelect has been added to an App Object that does not have a SelectAppObjectUC. Add a SelectAppObjectUC 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.TapToSelectAppObject = TapToSelectAppObject;
TapToSelectAppObject.type = "TapToSelectAppObject";
TapToSelectAppObject.maxTapDistancePx = 5;
//# sourceMappingURL=TapToSelectAppObject.js.map