import type { ExtensionMetadata } from '@pixi/core';
import { ExtensionType, Texture } from '@pixi/core';
import { LoaderResource } from './LoaderResource';
/**
* Loader plugin for handling Texture resources.
* @memberof PIXI
*/
export class TextureLoader
{
/** @ignore */
static extension: ExtensionMetadata = ExtensionType.Loader;
/** Handle SVG elements a text, render with SVGResource. */
public static add(): void
{
LoaderResource.setExtensionLoadType('svg', LoaderResource.LOAD_TYPE.XHR);
LoaderResource.setExtensionXhrType('svg', LoaderResource.XHR_RESPONSE_TYPE.TEXT);
}
/**
* Called after a resource is loaded.
* @see PIXI.Loader.loaderMiddleware
* @param resource
* @param {Function} next
*/
public static use(resource: LoaderResource, next: (...args: any[]) => void): void
{
// create a new texture if the data is an Image object
if (resource.data && (resource.type === LoaderResource.TYPE.IMAGE || resource.extension === 'svg'))
{
const { data, url, name, metadata } = resource;
Texture.fromLoader(data, url, name, metadata).then((texture) =>
{
resource.texture = texture;
next();
})
// TODO: handle errors in Texture.fromLoader
// so we can pass them to the Loader
.catch(next);
}
else
{
next();
}
}
}