1 | import { BaseTexture } from "../BaseTexture.mjs";
|
2 | import { autoDetectResource } from "./autoDetectResource.mjs";
|
3 | import { Resource } from "./Resource.mjs";
|
4 | class AbstractMultiResource extends Resource {
|
5 | |
6 |
|
7 |
|
8 |
|
9 |
|
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 |
|
22 |
|
23 |
|
24 |
|
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 |
|
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 |
|
38 |
|
39 |
|
40 |
|
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 |
|
49 |
|
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 |
|
60 |
|
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 |
|
69 |
|
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 | }
|
83 | export {
|
84 | AbstractMultiResource
|
85 | };
|
86 |
|