1 | "use strict";
|
2 | var BaseTexture = require("../BaseTexture.js"), autoDetectResource = require("./autoDetectResource.js"), Resource = require("./Resource.js");
|
3 | class AbstractMultiResource extends Resource.Resource {
|
4 | |
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | constructor(length, options) {
|
11 | const { width, height } = options || {};
|
12 | super(width, height), this.items = [], this.itemDirtyIds = [];
|
13 | for (let i = 0; i < length; i++) {
|
14 | const partTexture = new BaseTexture.BaseTexture();
|
15 | this.items.push(partTexture), this.itemDirtyIds.push(-2);
|
16 | }
|
17 | this.length = length, this._load = null, this.baseTexture = null;
|
18 | }
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | initFromArray(resources, options) {
|
26 | for (let i = 0; i < this.length; i++)
|
27 | resources[i] && (resources[i].castToBaseTexture ? this.addBaseTextureAt(resources[i].castToBaseTexture(), i) : resources[i] instanceof Resource.Resource ? this.addResourceAt(resources[i], i) : this.addResourceAt(autoDetectResource.autoDetectResource(resources[i], options), i));
|
28 | }
|
29 |
|
30 | dispose() {
|
31 | for (let i = 0, len = this.length; i < len; i++)
|
32 | this.items[i].destroy();
|
33 | this.items = null, this.itemDirtyIds = null, this._load = null;
|
34 | }
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | addResourceAt(resource, index) {
|
42 | if (!this.items[index])
|
43 | throw new Error(`Index ${index} is out of bounds`);
|
44 | return resource.valid && !this.valid && this.resize(resource.width, resource.height), this.items[index].setResource(resource), this;
|
45 | }
|
46 | |
47 |
|
48 |
|
49 |
|
50 | bind(baseTexture) {
|
51 | if (this.baseTexture !== null)
|
52 | throw new Error("Only one base texture per TextureArray is allowed");
|
53 | super.bind(baseTexture);
|
54 | for (let i = 0; i < this.length; i++)
|
55 | this.items[i].parentTextureArray = baseTexture, this.items[i].on("update", baseTexture.update, baseTexture);
|
56 | }
|
57 | |
58 |
|
59 |
|
60 |
|
61 | unbind(baseTexture) {
|
62 | super.unbind(baseTexture);
|
63 | for (let i = 0; i < this.length; i++)
|
64 | this.items[i].parentTextureArray = null, this.items[i].off("update", baseTexture.update, baseTexture);
|
65 | }
|
66 | |
67 |
|
68 |
|
69 |
|
70 | load() {
|
71 | if (this._load)
|
72 | return this._load;
|
73 | const promises = this.items.map((item) => item.resource).filter((item) => item).map((item) => item.load());
|
74 | return this._load = Promise.all(promises).then(
|
75 | () => {
|
76 | const { realWidth, realHeight } = this.items[0];
|
77 | return this.resize(realWidth, realHeight), this.update(), Promise.resolve(this);
|
78 | }
|
79 | ), this._load;
|
80 | }
|
81 | }
|
82 | exports.AbstractMultiResource = AbstractMultiResource;
|
83 |
|