"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DragScaleSceneRootUC = void 0;
exports.makeDragScaleSceneRootUC = makeDragScaleSceneRootUC;
const core_1 = require("@vived/core");
const core_2 = require("@vived/core");
const Babylon_1 = require("../../Babylon");
const Dispatcher_1 = require("../../Dispatcher");
const SceneRootEntity_1 = require("../Entities/SceneRootEntity");
class DragScaleSceneRootUC extends core_1.AppObjectUC {
}
exports.DragScaleSceneRootUC = DragScaleSceneRootUC;
DragScaleSceneRootUC.type = "DragScaleSceneRootUC";
DragScaleSceneRootUC.get = (appObjects) => (0, core_1.getSingletonComponent)(DragScaleSceneRootUC.type, appObjects);
function makeDragScaleSceneRootUC(appObject) {
    return new DragScaleSceneRootImp(appObject);
}
class DragScaleSceneRootImp extends DragScaleSceneRootUC {
    constructor(appObject) {
        super(appObject, DragScaleSceneRootUC.type);
        this.sensitivity = 0.01;
        this.lastPixelPos = core_2.Vector2.Zero();
        this.pickPoint = core_2.Vector3.Zero();
        this.initialScale = 1;
        this.isScaling = false;
        this.doScale = (pixelPosition) => {
            if (!this.sceneRoot)
                return;
            if (!this.isScaling)
                return;
            const pixelChange = core_2.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 = (0, Babylon_1.scenePixelPick)(initialPixelPos, this.appObjects);
            this.pickPoint = pick?.worldPickPoint ?? core_2.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) {
                (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
            }
            this.isScaling = false;
        };
        this.sceneRoot = appObject.getComponent(SceneRootEntity_1.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