import { AppObjectUC, getSingletonComponent, } from "@vived/core";
import { Vector2, Vector3 } from "@vived/core";
import { scenePixelPick } from "../../Babylon";
import { dispatchStateChange } from "../../Dispatcher";
import { SceneRootEntity } from "../Entities/SceneRootEntity";
export class DragScaleSceneRootUC extends AppObjectUC {
}
DragScaleSceneRootUC.type = "DragScaleSceneRootUC";
DragScaleSceneRootUC.get = (appObjects) => getSingletonComponent(DragScaleSceneRootUC.type, appObjects);
export function makeDragScaleSceneRootUC(appObject) {
    return new DragScaleSceneRootImp(appObject);
}
class DragScaleSceneRootImp extends DragScaleSceneRootUC {
    constructor(appObject) {
        super(appObject, DragScaleSceneRootUC.type);
        this.sensitivity = 0.01;
        this.lastPixelPos = Vector2.Zero();
        this.pickPoint = Vector3.Zero();
        this.initialScale = 1;
        this.isScaling = false;
        this.doScale = (pixelPosition) => {
            if (!this.sceneRoot)
                return;
            if (!this.isScaling)
                return;
            const pixelChange = Vector2.Subtract(pixelPosition, this.lastPixelPos);
            const currentScale = this.sceneRoot.scale;
            const scaleDelta = pixelChange.y * this.sensitivity * currentScale;
            this.sceneRoot.bumpScaleAboutPoint(scaleDelta, this.pickPoint);
            this.lastPixelPos = pixelPosition;
        };
        this.initializeScale = (initialPixelPos) => {
            if (!this.sceneRoot)
                return;
            this.lastPixelPos = initialPixelPos;
            const pick = scenePixelPick(initialPixelPos, this.appObjects);
            this.pickPoint = pick?.worldPickPoint ?? Vector3.Zero();
            this.initialScale = this.sceneRoot.scale;
            this.isScaling = true;
        };
        this.finishScaling = () => {
            if (!this.sceneRoot)
                return;
            if (!this.isScaling)
                return;
            if (this.initialScale !== this.sceneRoot.scale) {
                dispatchStateChange(this.appObjects);
            }
            this.isScaling = false;
        };
        this.sceneRoot = appObject.getComponent(SceneRootEntity.type);
        if (!this.sceneRoot) {
            this.warn("DragScaleSceneRootImp UC has been added to an App Object that does not have a SceneRoot Entity");
        }
    }
}
//# sourceMappingURL=DragScaleSceneRoot.js.map