"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BumpTransformScaleAboutPixelPointUC = void 0;
exports.makeBumpTransformScaleAboutPixelPointUC = makeBumpTransformScaleAboutPixelPointUC;
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 BumpTransformScaleAboutPixelPointUC extends core_1.AppObjectUC {
}
exports.BumpTransformScaleAboutPixelPointUC = BumpTransformScaleAboutPixelPointUC;
BumpTransformScaleAboutPixelPointUC.type = "BumpTransformScaleAboutPixelPointUC";
function makeBumpTransformScaleAboutPixelPointUC(appObject) {
    return new BumpTransformScaleAboutPixelPointUCImp(appObject);
}
class BumpTransformScaleAboutPixelPointUCImp extends BumpTransformScaleAboutPixelPointUC {
    constructor(appObject) {
        super(appObject, BumpTransformScaleAboutPixelPointUC.type);
        this.enabled = true;
        this.sensitivity = 0.1;
        this.allow = () => {
            return true;
        };
        this.bump = (direction, pixelPosition) => {
            if (!this.enabled)
                return;
            if (!this.transform)
                return;
            if (!this.allow())
                return;
            let bumpDirection = -1;
            if (direction === "IN") {
                bumpDirection = 1;
            }
            const currentScale = this.transform.worldScale.x;
            const scaleDelta = bumpDirection * this.sensitivity * currentScale;
            let pickPoint = core_2.Vector3.Zero();
            if (this.pickPlane) {
                const pickRay = (0, Babylon_1.pixelToSceneRay)(pixelPosition, this.appObjects);
                pickPoint = this.pickPlane.intersectLine(pickRay) ?? core_2.Vector3.Zero();
            }
            else {
                pickPoint =
                    (0, Babylon_1.scenePixelPick)(pixelPosition, this.appObjects)?.worldPickPoint ??
                        core_2.Vector3.Zero();
            }
            this.bumpScaleAboutPoint(scaleDelta, pickPoint);
            (0, Dispatcher_1.dispatchStateChange)(this.appObjects);
        };
        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("BumpSceneRootScaleAboutPixelPointImp UC has been added to an App Object that does not have a Transform");
            return;
        }
    }
}
//# sourceMappingURL=BumpTransformScaleAboutPixelPoint.js.map