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