{"version":3,"file":"Resource.mjs","sources":["../../../src/textures/resources/Resource.ts"],"sourcesContent":["import { Runner } from '@pixi/runner';\n\nimport type { Renderer } from '../../Renderer';\nimport type { BaseTexture } from '../BaseTexture';\nimport type { GLTexture } from '../GLTexture';\n\n/**\n * Base resource class for textures that manages validation and uploading, depending on its type.\n *\n * Uploading of a base texture to the GPU is required.\n * @memberof PIXI\n */\nexport abstract class Resource\n{\n    /** The url of the resource */\n    public src: string;\n\n    /**\n     * If resource has been destroyed.\n     * @readonly\n     * @default false\n     */\n    public destroyed: boolean;\n\n    /**\n     * `true` if resource is created by BaseTexture\n     * useful for doing cleanup with BaseTexture destroy\n     * and not cleaning up resources that were created\n     * externally.\n     */\n    public internal: boolean;\n\n    /** Internal width of the resource. */\n    protected _width: number;\n\n    /** Internal height of the resource. */\n    protected _height: number;\n\n    /**\n     * Mini-runner for handling resize events\n     * accepts 2 parameters: width, height\n     * @member {Runner}\n     * @private\n     */\n    protected onResize: Runner; // TODO: Should this be private? It doesn't seem to be used anywhere else.\n\n    /**\n     * Mini-runner for handling update events\n     * @member {Runner}\n     * @private\n     */\n    protected onUpdate: Runner;\n\n    /**\n     * Handle internal errors, such as loading errors\n     * accepts 1 param: error\n     * @member {Runner}\n     * @private\n     */\n    protected onError: Runner;\n\n    /**\n     * @param width - Width of the resource\n     * @param height - Height of the resource\n     */\n    constructor(width = 0, height = 0)\n    {\n        this._width = width;\n        this._height = height;\n\n        this.destroyed = false;\n        this.internal = false;\n\n        this.onResize = new Runner('setRealSize');\n        this.onUpdate = new Runner('update');\n        this.onError = new Runner('onError');\n    }\n\n    /**\n     * Bind to a parent BaseTexture\n     * @param baseTexture - Parent texture\n     */\n    bind(baseTexture: BaseTexture): void\n    {\n        this.onResize.add(baseTexture);\n        this.onUpdate.add(baseTexture);\n        this.onError.add(baseTexture);\n\n        // Call a resize immediate if we already\n        // have the width and height of the resource\n        if (this._width || this._height)\n        {\n            this.onResize.emit(this._width, this._height);\n        }\n    }\n\n    /**\n     * Unbind to a parent BaseTexture\n     * @param baseTexture - Parent texture\n     */\n    unbind(baseTexture: BaseTexture): void\n    {\n        this.onResize.remove(baseTexture);\n        this.onUpdate.remove(baseTexture);\n        this.onError.remove(baseTexture);\n    }\n\n    /**\n     * Trigger a resize event\n     * @param width - X dimension\n     * @param height - Y dimension\n     */\n    resize(width: number, height: number): void\n    {\n        if (width !== this._width || height !== this._height)\n        {\n            this._width = width;\n            this._height = height;\n            this.onResize.emit(width, height);\n        }\n    }\n\n    /**\n     * Has been validated\n     * @readonly\n     */\n    get valid(): boolean\n    {\n        return !!this._width && !!this._height;\n    }\n\n    /** Has been updated trigger event. */\n    update(): void\n    {\n        if (!this.destroyed)\n        {\n            this.onUpdate.emit();\n        }\n    }\n\n    /**\n     * This can be overridden to start preloading a resource\n     * or do any other prepare step.\n     * @protected\n     * @returns Handle the validate event\n     */\n    load(): Promise<this>\n    {\n        return Promise.resolve(this);\n    }\n\n    /**\n     * The width of the resource.\n     * @readonly\n     */\n    get width(): number\n    {\n        return this._width;\n    }\n\n    /**\n     * The height of the resource.\n     * @readonly\n     */\n    get height(): number\n    {\n        return this._height;\n    }\n\n    /**\n     * Uploads the texture or returns false if it cant for some reason. Override this.\n     * @param renderer - yeah, renderer!\n     * @param baseTexture - the texture\n     * @param glTexture - texture instance for this webgl context\n     * @returns - true is success\n     */\n    abstract upload(renderer: Renderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean;\n\n    /**\n     * Set the style, optional to override\n     * @param _renderer - yeah, renderer!\n     * @param _baseTexture - the texture\n     * @param _glTexture - texture instance for this webgl context\n     * @returns - `true` is success\n     */\n    style(_renderer: Renderer, _baseTexture: BaseTexture, _glTexture: GLTexture): boolean\n    {\n        return false;\n    }\n\n    /** Clean up anything, this happens when destroying is ready. */\n    dispose(): void\n    {\n        // override\n    }\n\n    /**\n     * Call when destroying resource, unbind any BaseTexture object\n     * before calling this method, as reference counts are maintained\n     * internally.\n     */\n    destroy(): void\n    {\n        if (!this.destroyed)\n        {\n            this.destroyed = true;\n            this.dispose();\n            this.onError.removeAll();\n            this.onError = null;\n            this.onResize.removeAll();\n            this.onResize = null;\n            this.onUpdate.removeAll();\n            this.onUpdate = null;\n        }\n    }\n\n    /**\n     * Abstract, used to auto-detect resource type.\n     * @param {*} _source - The source object\n     * @param {string} _extension - The extension of source, if set\n     */\n    static test(_source: unknown, _extension?: string): boolean\n    {\n        return false;\n    }\n}\n"],"names":[],"mappings":";AAYO,MAAe,SACtB;AAAA;AAAA;AAAA;AAAA;AAAA,EAoDI,YAAY,QAAQ,GAAG,SAAS,GAChC;AACS,SAAA,SAAS,OACd,KAAK,UAAU,QAEf,KAAK,YAAY,IACjB,KAAK,WAAW,IAEhB,KAAK,WAAW,IAAI,OAAO,aAAa,GACxC,KAAK,WAAW,IAAI,OAAO,QAAQ,GACnC,KAAK,UAAU,IAAI,OAAO,SAAS;AAAA,EACvC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,KAAK,aACL;AACS,SAAA,SAAS,IAAI,WAAW,GAC7B,KAAK,SAAS,IAAI,WAAW,GAC7B,KAAK,QAAQ,IAAI,WAAW,IAIxB,KAAK,UAAU,KAAK,YAEpB,KAAK,SAAS,KAAK,KAAK,QAAQ,KAAK,OAAO;AAAA,EAEpD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,OAAO,aACP;AACI,SAAK,SAAS,OAAO,WAAW,GAChC,KAAK,SAAS,OAAO,WAAW,GAChC,KAAK,QAAQ,OAAO,WAAW;AAAA,EACnC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,OAAe,QACtB;AACI,KAAI,UAAU,KAAK,UAAU,WAAW,KAAK,aAEzC,KAAK,SAAS,OACd,KAAK,UAAU,QACf,KAAK,SAAS,KAAK,OAAO,MAAM;AAAA,EAExC;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAI,QACJ;AACI,WAAO,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,KAAK;AAAA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