UNPKG

3.27 kBJavaScriptView Raw
1import { BaseTexture } from "../BaseTexture.mjs";
2import { autoDetectResource } from "./autoDetectResource.mjs";
3import { Resource } from "./Resource.mjs";
4class AbstractMultiResource extends Resource {
5 /**
6 * @param length
7 * @param options - Options to for Resource constructor
8 * @param {number} [options.width] - Width of the resource
9 * @param {number} [options.height] - Height of the resource
10 */
11 constructor(length, options) {
12 const { width, height } = options || {};
13 super(width, height), this.items = [], this.itemDirtyIds = [];
14 for (let i = 0; i < length; i++) {
15 const partTexture = new BaseTexture();
16 this.items.push(partTexture), this.itemDirtyIds.push(-2);
17 }
18 this.length = length, this._load = null, this.baseTexture = null;
19 }
20 /**
21 * Used from ArrayResource and CubeResource constructors.
22 * @param resources - Can be resources, image elements, canvas, etc. ,
23 * length should be same as constructor length
24 * @param options - Detect options for resources
25 */
26 initFromArray(resources, options) {
27 for (let i = 0; i < this.length; i++)
28 resources[i] && (resources[i].castToBaseTexture ? this.addBaseTextureAt(resources[i].castToBaseTexture(), i) : resources[i] instanceof Resource ? this.addResourceAt(resources[i], i) : this.addResourceAt(autoDetectResource(resources[i], options), i));
29 }
30 /** Destroy this BaseImageResource. */
31 dispose() {
32 for (let i = 0, len = this.length; i < len; i++)
33 this.items[i].destroy();
34 this.items = null, this.itemDirtyIds = null, this._load = null;
35 }
36 /**
37 * Set a resource by ID
38 * @param resource
39 * @param index - Zero-based index of resource to set
40 * @returns - Instance for chaining
41 */
42 addResourceAt(resource, index) {
43 if (!this.items[index])
44 throw new Error(`Index ${index} is out of bounds`);
45 return resource.valid && !this.valid && this.resize(resource.width, resource.height), this.items[index].setResource(resource), this;
46 }
47 /**
48 * Set the parent base texture.
49 * @param baseTexture
50 */
51 bind(baseTexture) {
52 if (this.baseTexture !== null)
53 throw new Error("Only one base texture per TextureArray is allowed");
54 super.bind(baseTexture);
55 for (let i = 0; i < this.length; i++)
56 this.items[i].parentTextureArray = baseTexture, this.items[i].on("update", baseTexture.update, baseTexture);
57 }
58 /**
59 * Unset the parent base texture.
60 * @param baseTexture
61 */
62 unbind(baseTexture) {
63 super.unbind(baseTexture);
64 for (let i = 0; i < this.length; i++)
65 this.items[i].parentTextureArray = null, this.items[i].off("update", baseTexture.update, baseTexture);
66 }
67 /**
68 * Load all the resources simultaneously
69 * @returns - When load is resolved
70 */
71 load() {
72 if (this._load)
73 return this._load;
74 const promises = this.items.map((item) => item.resource).filter((item) => item).map((item) => item.load());
75 return this._load = Promise.all(promises).then(
76 () => {
77 const { realWidth, realHeight } = this.items[0];
78 return this.resize(realWidth, realHeight), this.update(), Promise.resolve(this);
79 }
80 ), this._load;
81 }
82}
83export {
84 AbstractMultiResource
85};
86//# sourceMappingURL=AbstractMultiResource.mjs.map