UNPKG

8.27 kBSource Map (JSON)View Raw
1{"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<Resource>\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,QACtB,CAAA;AAAA,EAoDI,WAAY,CAAA,KAAA,GAAQ,CAAG,EAAA,MAAA,GAAS,CAChC,EAAA;AACI,IAAA,IAAA,CAAK,MAAS,GAAA,KAAA,CAAA;AACd,IAAA,IAAA,CAAK,OAAU,GAAA,MAAA,CAAA;AAEf,IAAA,IAAA,CAAK,SAAY,GAAA,KAAA,CAAA;AACjB,IAAA,IAAA,CAAK,QAAW,GAAA,KAAA,CAAA;AAEhB,IAAK,IAAA,CAAA,QAAA,GAAW,IAAI,MAAA,CAAO,aAAa,CAAA,CAAA;AACxC,IAAK,IAAA,CAAA,QAAA,GAAW,IAAI,MAAA,CAAO,QAAQ,CAAA,CAAA;AACnC,IAAK,IAAA,CAAA,OAAA,GAAU,IAAI,MAAA,CAAO,SAAS,CAAA,CAAA;AAAA,GACvC;AAAA,EAMA,KAAK,WACL,EAAA;AACI,IAAK,IAAA,CAAA,QAAA,CAAS,IAAI,WAAW,CAAA,CAAA;AAC7B,IAAK,IAAA,CAAA,QAAA,CAAS,IAAI,WAAW,CAAA,CAAA;AAC7B,IAAK,IAAA,CAAA,OAAA,CAAQ,IAAI,WAAW,CAAA,CAAA;AAI5B,IAAI,IAAA,IAAA,CAAK,MAAU,IAAA,IAAA,CAAK,OACxB,EAAA;AACI,MAAA,IAAA,CAAK,QAAS,CAAA,IAAA,CAAK,IAAK,CAAA,MAAA,EAAQ,KAAK,OAAO,CAAA,CAAA;AAAA,KAChD;AAAA,GACJ;AAAA,EAMA,OAAO,WACP,EAAA;AACI,IAAK,IAAA,CAAA,QAAA,CAAS,OAAO,WAAW,CAAA,CAAA;AAChC,IAAK,IAAA,CAAA,QAAA,CAAS,OAAO,WAAW,CAAA,CAAA;AAChC,IAAK,IAAA,CAAA,OAAA,CAAQ,OAAO,WAAW,CAAA,CAAA;AAAA,G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