"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScaleTransformUC = void 0;
exports.makeScaleTransformUC = makeScaleTransformUC;
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 ScaleTransformUC extends core_1.AppObjectUC {
}
exports.ScaleTransformUC = ScaleTransformUC;
ScaleTransformUC.type = "ScaleTransformUC";
function makeScaleTransformUC(appObject) {
    return new ScaleTransformUCImp(appObject);
}
class ScaleTransformUCImp extends ScaleTransformUC {
    doInitialize(ray) {
        this.isScaling = false;
        if (!this.transform)
            return;
        if (this.pickPlane) {
            this.pickPoint = this.pickPlane.intersectLine(ray) ?? core_2.Vector3.Zero();
        }
        else {
            this.pickPoint =
                (0, Babylon_1.sceneRayPick)(ray, this.appObjects)?.worldPickPoint ?? core_2.Vector3.Zero();
        }
        this.initialScale = this.transform.worldScale.x;
        this.isScaling = true;
    }
    constructor(appObject) {
        super(appObject, ScaleTransformUC.type);
        this.raySensitivity = 1;
        this.pixelSensitivity = 0.1;
        this.enabled = true;
        this.lastPosition = 0;
        this.pickPoint = core_2.Vector3.Zero();
        this.initialScale = 1;
        this.isScaling = false;
        this.initializeRay = (ray) => {
            this.lastPosition = ray.origin.z;
            this.doInitialize(ray);
        };
        this.updateRay = (ray) => {
            if (!this.isScaling)
                return;
            if (!this.transform)
                return;
            if (!this.enabled)
                return;
            const movement = this.lastPosition - ray.origin.z;
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = movement * this.raySensitivity * currentScale;
            this.bumpScaleAboutPoint(scaleDelta, this.pickPoint);
            this.lastPosition = ray.origin.z;
        };
        this.initializePixel = (initialPixelPos) => {
            this.lastPosition = initialPixelPos.y;
            const ray = (0, Babylon_1.pixelToSceneRay)(initialPixelPos, this.appObjects);
            this.doInitialize(ray);
        };
        this.updatePixel = (pixelPosition) => {
            if (!this.enabled)
                return;
            if (!this.transform)
                return;
            if (!this.isScaling)
                return;
            const pixelChange = pixelPosition.y - this.lastPosition;
            this.lastPosition = pixelPosition.y;
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = pixelChange * this.pixelSensitivity * currentScale;
            this.bumpScaleAboutPoint(scaleDelta, this.pickPoint);
        };
        this.finish = () => {
            if (!this.transform)
                return;
            if (!this.isScaling)
                return;
            if (this.initialScale !== this.transform.worldScale.x) {
                (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
            }
            this.isScaling = false;
        };
        this.bumpScaleAboutPoint = (amount, worldPoint) => {
            if (!this.transform)
                return;
            const localPickOffset = this.transform.worldPointToLocal(worldPoint);
            const currentScale = this.transform.worldScale.x;
            let newScale = currentScale + amount;
            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 worldPickOffset = this.transform.localPointToWorld(localPickOffset);
            const bump = core_2.Vector3.Subtract(worldPoint, worldPickOffset);
            this.transform.worldPosition = core_2.Vector3.Add(bump, this.transform.worldPosition);
        };
        this.transform = appObject.getComponent(Entities_1.Transform3D.type);
        if (!this.transform) {
            this.error("App Object does not have a Transform3D Entity");
        }
    }
}
//# sourceMappingURL=ScaleTransform.js.map