"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PinchTransformUC = void 0;
exports.makePinchTransformUC = makePinchTransformUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Dispatcher_1 = require("../../Dispatcher");
const Entities_1 = require("../Entities");
class PinchTransformUC extends core_1.AppObjectUC {
}
exports.PinchTransformUC = PinchTransformUC;
PinchTransformUC.type = "PinchTransformUC";
function makePinchTransformUC(appObject) {
    return new PinchTransformUCImp(appObject);
}
class PinchTransformUCImp extends PinchTransformUC {
    getTouchCenter(t1, t2) {
        const delta = core_2.Vector2.Subtract(t2, t1);
        const halfDelta = core_2.Vector2.NewVectorOfLength(delta.unit, 0.5 * delta.magnitued);
        return core_2.Vector2.Add(t1, halfDelta);
    }
    getTouchDistance(t1, t2) {
        const delta = core_2.Vector2.Subtract(t2, t1);
        return delta.magnitued;
    }
    constructor(appObject) {
        super(appObject, PinchTransformUC.type);
        this.sensitivity = 0.01;
        this.enabled = true;
        this.previousDistanceBetweenTouches = 0;
        this._panPlane = core_2.ParametricPlane.XY();
        this.pickOffset = core_2.Vector3.Zero();
        this.initialScale = 0;
        this.initializePinch = (touchPos1, touchPos2) => {
            if (!this.transform)
                return;
            this.previousDistanceBetweenTouches = this.getTouchDistance(touchPos1, touchPos2);
            const touchCenter = this.getTouchCenter(touchPos1, touchPos2);
            const scenePick = (0, Babylon_1.scenePixelPick)(touchCenter, this.appObjects);
            if (this.pickPlane) {
                this._panPlane = this.pickPlane;
            }
            else {
                const pickPoint = scenePick?.worldPickPoint ?? core_2.Vector3.Zero();
                const pickPlaneDirection = core_2.Vector3.Backward();
                this._panPlane = core_2.ParametricPlane.FromPointNormal(pickPoint, pickPlaneDirection);
            }
            const pickRay = (0, Babylon_1.pixelToSceneRay)(touchCenter, this.appObjects);
            const planePick = this._panPlane.intersectLine(pickRay) ?? core_2.Vector3.Zero();
            this.pickOffset = this.transform.worldPointToLocal(planePick);
            this.initialScale = this.transform.worldScale.x;
        };
        this.doPinch = (touchPos1, touchPos2) => {
            if (!this.enabled)
                return;
            if (!this.transform)
                return;
            const currentTouchDist = this.getTouchDistance(touchPos1, touchPos2);
            const touchDelta = currentTouchDist - this.previousDistanceBetweenTouches;
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = touchDelta * this.sensitivity * currentScale;
            let newScale = currentScale + scaleDelta;
            if (this.minScale !== undefined && newScale < this.minScale) {
                newScale = this.minScale;
            }
            if (this.maxScale !== undefined && newScale > this.maxScale) {
                newScale = this.maxScale;
            }
            this.transform.worldScale = new core_2.Vector3(newScale, newScale, newScale);
            const touchCenter = this.getTouchCenter(touchPos1, touchPos2);
            const pickRay = (0, Babylon_1.pixelToSceneRay)(touchCenter, this.appObjects);
            const planePick = this._panPlane.intersectLine(pickRay) ?? core_2.Vector3.Zero();
            const globalOffset = this.transform.localPointToWorld(this.pickOffset);
            const delta = core_2.Vector3.Subtract(planePick, globalOffset);
            const currentWorldPos = this.transform.worldPosition;
            const newWorldPos = core_2.Vector3.Add(currentWorldPos, delta);
            this.transform.worldPosition = newWorldPos;
            this.previousDistanceBetweenTouches = currentTouchDist;
        };
        this.finishPinch = () => {
            if (this.initialScale === this.transform?.worldScale.x)
                return;
            (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
        };
        this.transform = appObject.getComponent(Entities_1.Transform3D.type);
        if (!this.transform) {
            this.warn("PinchSceneRootUC UC has been added to an App Object that does not have a Transform3D");
            return;
        }
    }
}
//# sourceMappingURL=PinchTransform.js.map