import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { ParametricPlane, Vector2, Vector3 } from "@vived/core";
import { pixelToSceneRay, scenePixelPick } from "../../Babylon";
import { dispatchStateChange } from "../../Dispatcher";
import { SceneRootEntity } from "../Entities/SceneRootEntity";
export class PinchSceneRootUC extends AppObjectUC {
}
PinchSceneRootUC.type = "PinchSceneRootUC";
PinchSceneRootUC.get = (appObjects) => getSingletonComponent(PinchSceneRootUC.type, appObjects);
export function makePinchSceneRootUC(appObject) {
    return new PinchSceneRootImp(appObject);
}
class PinchSceneRootImp extends PinchSceneRootUC {
    getTouchCenter(t1, t2) {
        const delta = Vector2.Subtract(t2, t1);
        const halfDelta = Vector2.NewVectorOfLength(delta.unit, 0.5 * delta.magnitued);
        return Vector2.Add(t1, halfDelta);
    }
    getTouchDistance(t1, t2) {
        const delta = Vector2.Subtract(t2, t1);
        return delta.magnitued;
    }
    constructor(appObject) {
        super(appObject, PinchSceneRootUC.type);
        this.sensitivity = 0.01;
        this.previousDistanceBetweenTouches = 0;
        this.panPlane = ParametricPlane.XY();
        this.pickOffset = Vector3.Zero();
        this.initialScale = 0;
        this.initializePinch = (touchPos1, touchPos2) => {
            if (!this.sceneRoot)
                return;
            const transformNode = this.sceneRoot.transform;
            this.previousDistanceBetweenTouches = this.getTouchDistance(touchPos1, touchPos2);
            const touchCenter = this.getTouchCenter(touchPos1, touchPos2);
            const scenePick = scenePixelPick(touchCenter, this.appObjects);
            const pickPoint = scenePick?.worldPickPoint ?? Vector3.Zero();
            const pickPlaneDirection = Vector3.Backward();
            this.panPlane = ParametricPlane.FromPointNormal(pickPoint, pickPlaneDirection);
            const pickRay = pixelToSceneRay(touchCenter, this.appObjects);
            const planePick = this.panPlane.intersectLine(pickRay) ?? Vector3.Zero();
            this.pickOffset = transformNode.worldPointToLocal(planePick);
            this.initialScale = this.sceneRoot.scale;
        };
        this.doPinch = (touchPos1, touchPos2) => {
            if (!this.sceneRoot)
                return;
            const transformNode = this.sceneRoot.transform;
            const currentTouchDist = this.getTouchDistance(touchPos1, touchPos2);
            const touchDelta = currentTouchDist - this.previousDistanceBetweenTouches;
            const currentScale = this.sceneRoot.scale;
            const scaleDelta = touchDelta * this.sensitivity * currentScale;
            let newScale = currentScale + scaleDelta;
            if (newScale < this.sceneRoot.minScale) {
                newScale = this.sceneRoot.minScale;
            }
            else if (newScale > this.sceneRoot.maxScale) {
                newScale = this.sceneRoot.maxScale;
            }
            transformNode.worldScale = new Vector3(newScale, newScale, newScale);
            const touchCenter = this.getTouchCenter(touchPos1, touchPos2);
            const pickRay = pixelToSceneRay(touchCenter, this.appObjects);
            const planePick = this.panPlane.intersectLine(pickRay) ?? Vector3.Zero();
            const globalOffset = transformNode.localPointToWorld(this.pickOffset);
            const delta = Vector3.Subtract(planePick, globalOffset);
            const currentWorldPos = transformNode.worldPosition;
            const newWorldPos = Vector3.Add(currentWorldPos, delta);
            transformNode.worldPosition = newWorldPos;
            this.previousDistanceBetweenTouches = currentTouchDist;
        };
        this.finishPinch = () => {
            if (this.initialScale === this.sceneRoot?.scale)
                return;
            dispatchStateChange(this.appObjects);
        };
        this.sceneRoot = appObject.getComponent(SceneRootEntity.type);
        if (!this.sceneRoot) {
            this.warn("PinchSceneRootUC UC has been added to an App Object that does not have a SceneRoot Entity");
            return;
        }
    }
}
//# sourceMappingURL=PinchSceneRoot.js.map