{"version":3,"file":"measureMixin.mjs","sources":["../../../../src/scene/container/container-mixins/measureMixin.ts"],"sourcesContent":["import { Matrix } from '../../../maths/matrix/Matrix';\nimport { Bounds } from '../bounds/Bounds';\nimport { getGlobalBounds } from '../bounds/getGlobalBounds';\nimport { getLocalBounds } from '../bounds/getLocalBounds';\nimport { checkChildrenDidChange } from '../utils/checkChildrenDidChange';\n\nimport type { Size } from '../../../maths/misc/Size';\nimport type { Container } from '../Container';\n\n/**\n * A utility type that makes all properties of T optional except for the specified keys K.\n * @category utils\n * @internal\n */\nexport type Optional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;\n\n/** @ignore */\nexport interface MeasureMixinConstructor\n{\n    /**\n     * The width of the display object, in pixels.\n     * @example\n     * ```ts\n     * new Container({ width: 100});\n     * ```\n     * @default 0\n     */\n    width?: number;\n    /**\n     * The height of the display object, in pixels.\n     * @example\n     * ```ts\n     * new Container({ height: 100});\n     * ```\n     * @default 0\n     */\n    height?: number;\n}\n/**\n * The MeasureMixin interface provides methods for measuring and manipulating the size and bounds of a display object.\n * It includes methods to get and set the size of the object, retrieve its local bounds,\n * and calculate its global bounds.\n * @category scene\n * @advanced\n */\nexport interface MeasureMixin extends Required<MeasureMixinConstructor>\n{\n    getSize(out?: Size): Size;\n    setSize(width: number, height?: number): void;\n    setSize(value: Optional<Size, 'height'>): void;\n    /**\n     * Retrieves the local bounds of the container as a Bounds object.\n     * Uses cached values when possible for better performance.\n     * @example\n     * ```ts\n     * // Basic bounds check\n     * const bounds = container.getLocalBounds();\n     * console.log(`Width: ${bounds.width}, Height: ${bounds.height}`);\n     * // subsequent calls will reuse the cached bounds\n     * const cachedBounds = container.getLocalBounds();\n     * console.log(bounds === cachedBounds); // true\n     * ```\n     * @returns The bounding area\n     * @see {@link Container#getBounds} For world space bounds\n     * @see {@link Bounds} For bounds properties\n     */\n    getLocalBounds(): Bounds;\n    /**\n     * Calculates and returns the (world) bounds of the display object as a Rectangle.\n     * Takes into account transforms and child bounds.\n     * @example\n     * ```ts\n     * // Basic bounds calculation\n     * const bounds = sprite.getBounds();\n     * console.log(`World bounds: ${bounds.x}, ${bounds.y}, ${bounds.width}, ${bounds.height}`);\n     *\n     * // Reuse bounds object for performance\n     * const recycleBounds = new Bounds();\n     * sprite.getBounds(false, recycleBounds);\n     *\n     * // Skip update for performance\n     * const fastBounds = sprite.getBounds(true);\n     * ```\n     * @remarks\n     * - Includes transform calculations\n     * - Updates scene graph by default\n     * - Can reuse bounds objects\n     * - Common in hit testing\n     * @param {boolean} skipUpdate - Setting to `true` will stop the transforms of the scene graph from\n     *  being updated. This means the calculation returned MAY be out of date BUT will give you a\n     *  nice performance boost.\n     * @param {Bounds} bounds - Optional bounds to store the result of the bounds calculation\n     * @returns The minimum axis-aligned rectangle in world space that fits around this object\n     * @see {@link Container#getLocalBounds} For untransformed bounds\n     * @see {@link Bounds} For bounds properties\n     */\n    getBounds(skipUpdate?: boolean, bounds?: Bounds): Bounds;\n    /** @private */\n    _localBoundsCacheData: LocalBoundsCacheData;\n    /** @private */\n    _localBoundsCacheId: number;\n    /** @private */\n    _setWidth(width: number, localWidth: number): void;\n    /** @private */\n    _setHeight(height: number, localHeight: number): void;\n}\n\ninterface LocalBoundsCacheData\n{\n    data: number[];\n    index: number;\n    didChange: boolean;\n    localBounds: Bounds;\n}\n\nconst tempMatrix = new Matrix();\n\n/** @internal */\nexport const measureMixin: Partial<Container> = {\n\n    _localBoundsCacheId: -1,\n    _localBoundsCacheData: null,\n\n    _setWidth(value: number, localWidth: number)\n    {\n        const sign = Math.sign(this.scale.x) || 1;\n\n        if (localWidth !== 0)\n        {\n            this.scale.x = (value / localWidth) * sign;\n        }\n        else\n        {\n            this.scale.x = sign;\n        }\n    },\n\n    _setHeight(value: number, localHeight: number)\n    {\n        const sign = Math.sign(this.scale.y) || 1;\n\n        if (localHeight !== 0)\n        {\n            this.scale.y = (value / localHeight) * sign;\n        }\n        else\n        {\n            this.scale.y = sign;\n        }\n    },\n\n    getLocalBounds(): Bounds\n    {\n        if (!this._localBoundsCacheData)\n        {\n            this._localBoundsCacheData = {\n                data: [],\n                index: 1,\n                didChange: false,\n                localBounds: new Bounds()\n            };\n        }\n\n        const localBoundsCacheData = this._localBoundsCacheData;\n\n        localBoundsCacheData.index = 1;\n        localBoundsCacheData.didChange = false;\n\n        if (localBoundsCacheData.data[0] !== this._didViewChangeTick)\n        {\n            localBoundsCacheData.didChange = true;\n            localBoundsCacheData.data[0] = this._didViewChangeTick;\n        }\n\n        checkChildrenDidChange(this, localBoundsCacheData);\n\n        if (localBoundsCacheData.didChange)\n        {\n            getLocalBounds(this, localBoundsCacheData.localBounds, tempMatrix);\n        }\n\n        return localBoundsCacheData.localBounds;\n    },\n\n    getBounds(skipUpdate?: boolean, bounds?: Bounds): Bounds\n    {\n        return getGlobalBounds(this, skipUpdate, bounds || new Bounds());\n    },\n} as Container;\n"],"names":[],"mappings":";;;;;;;AAmHA,MAAM,UAAA,GAAa,IAAI,MAAA,EAAO;AAGvB,MAAM,YAAA,GAAmC;AAAA,EAE5C,mBAAA,EAAqB,CAAA,CAAA;AAAA,EACrB,qBAAA,EAAuB,IAAA;AAAA,EAEvB,SAAA,CAAU,OAAe,UAAA,EACzB;AACI,IAAA,MAAM,OAAO,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,CAAC,CAAA,IAAK,CAAA;AAExC,IAAA,IAAI,eAAe,CAAA,EACnB;AACI,MAAA,IAAA,CAAK,KAAA,CAAM,CAAA,GAAK,KAAA,GAAQ,UAAA,GAAc,IAAA;AAAA,IAC1C,CAAA,MAEA;AACI,MAAA,IAAA,CAAK,MAAM,CAAA,GAAI,IAAA;AAAA,IACnB;AAAA,EACJ,CAAA;AAAA,EAEA,UAAA,CAAW,OAAe,WAAA,EAC1B;AACI,IAAA,MAAM,OAAO,IAAA,CAAK,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,CAAC,CAAA,IAAK,CAAA;AAExC,IAAA,IAAI,gBA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