"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TouchPinch = void 0;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const TouchController_1 = require("../../TouchController");
const PinchSceneRoot_1 = require("../UC/PinchSceneRoot");
class TouchPinch extends core_1.AppObjectController {
    get pinchRootUC() {
        return this.getCachedLocalComponent(PinchSceneRoot_1.PinchSceneRootUC.type);
    }
    constructor(appObject) {
        super(appObject, TouchPinch.type);
        this.isPinching = false;
        this.PAN_FINGER_DISTANCE_PX = 80;
        this.allow = (e) => {
            if (e.touches.length === 2) {
                const dist = core_2.Vector2.Subtract(e.touches[0].position, e.touches[1].position).magnitued;
                if (dist > this.PAN_FINGER_DISTANCE_PX) {
                    return true;
                }
            }
            return false;
        };
        this.onTouchStart = (e) => {
            this.isPinching = false;
            if (e.touches.length !== 2) {
                return;
            }
            if (!this.allow(e)) {
                return;
            }
            this.pinchRootUC?.initializePinch(e.touches[0].position, e.touches[1].position);
            this.isPinching = true;
        };
        this.onTouchMove = (e) => {
            if (!this.isPinching)
                return;
            if (e.touches.length !== 2)
                return;
            this.pinchRootUC?.doPinch(e.touches[0].position, e.touches[1].position);
        };
        this.onTouchEnd = () => {
            this.isPinching = false;
            this.pinchRootUC?.finishPinch();
        };
        const appObjects = appObject.appObjectRepo;
        const touchInput = TouchController_1.TouchInputEntity.get(appObjects);
        if (touchInput) {
            touchInput.registerController(this);
        }
        else {
            this.warn("Unable to find TouchInputEntity");
        }
    }
}
exports.TouchPinch = TouchPinch;
TouchPinch.type = "TouchPinch";
//# sourceMappingURL=TouchPinch.js.map