@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 1.89 kB
JavaScript
import{path as t}from"../../core/path.js";import{http as s}from"../../platform/net/http.js";import{Sprite as e}from"../../scene/sprite.js";import{ResourceHandler as a}from"./handler.js";function r(t){const s=this;s.resource&&(s.resource.atlas=t.resource)}function o(t){this.registry.load(t)}class d extends a{constructor(t){super(t,"sprite"),this._assets=t.assets,this._device=t.graphicsDevice}load(e,a){"string"==typeof e&&(e={load:e,original:e}),".json"===t.getExtension(e.original)&&s.get(e.load,{retry:this.maxRetries>0,maxRetries:this.maxRetries},((t,s)=>{t?a(t):a(null,s)}))}open(t,s){const a=new e(this._device);return t&&(a.__data=s),a}patch(t,s){const e=t.resource;if(e.__data&&(t.data.pixelsPerUnit=e.__data.pixelsPerUnit,t.data.renderMode=e.__data.renderMode,t.data.frameKeys=e.__data.frameKeys,e.__data.textureAtlasAsset)){const a=s.getByUrl(e.__data.textureAtlasAsset);a?t.data.textureAtlasAsset=a.id:console.warn(`Could not find textureatlas with url: ${e.__data.textureAtlasAsset}`)}e.startUpdate(),e.renderMode=t.data.renderMode,e.pixelsPerUnit=t.data.pixelsPerUnit,e.frameKeys=t.data.frameKeys,this._updateAtlas(t),e.endUpdate(),t.off("change",this._onAssetChange,this),t.on("change",this._onAssetChange,this)}_updateAtlas(t){const s=t.resource;if(!t.data.textureAtlasAsset)return void(s.atlas=null);this._assets.off(`load:${t.data.textureAtlasAsset}`,r,t),this._assets.on(`load:${t.data.textureAtlasAsset}`,r,t);const e=this._assets.get(t.data.textureAtlasAsset);e&&e.resource?s.atlas=e.resource:e?this._assets.load(e):(this._assets.off(`add:${t.data.textureAtlasAsset}`,o,t),this._assets.on(`add:${t.data.textureAtlasAsset}`,o,t))}_onAssetChange(t,s,e,a){"data"===s&&e&&e.textureAtlasAsset&&a&&e.textureAtlasAsset!==a.textureAtlasAsset&&(this._assets.off(`load:${a.textureAtlasAsset}`,r,t),this._assets.off(`add:${a.textureAtlasAsset}`,o,t))}}export{d as SpriteHandler};