UNPKG

2.93 kBJavaScriptView Raw
1"use strict";
2var runner = require("@pixi/runner");
3class Resource {
4 /**
5 * @param width - Width of the resource
6 * @param height - Height of the resource
7 */
8 constructor(width = 0, height = 0) {
9 this._width = width, this._height = height, this.destroyed = !1, this.internal = !1, this.onResize = new runner.Runner("setRealSize"), this.onUpdate = new runner.Runner("update"), this.onError = new runner.Runner("onError");
10 }
11 /**
12 * Bind to a parent BaseTexture
13 * @param baseTexture - Parent texture
14 */
15 bind(baseTexture) {
16 this.onResize.add(baseTexture), this.onUpdate.add(baseTexture), this.onError.add(baseTexture), (this._width || this._height) && this.onResize.emit(this._width, this._height);
17 }
18 /**
19 * Unbind to a parent BaseTexture
20 * @param baseTexture - Parent texture
21 */
22 unbind(baseTexture) {
23 this.onResize.remove(baseTexture), this.onUpdate.remove(baseTexture), this.onError.remove(baseTexture);
24 }
25 /**
26 * Trigger a resize event
27 * @param width - X dimension
28 * @param height - Y dimension
29 */
30 resize(width, height) {
31 (width !== this._width || height !== this._height) && (this._width = width, this._height = height, this.onResize.emit(width, height));
32 }
33 /**
34 * Has been validated
35 * @readonly
36 */
37 get valid() {
38 return !!this._width && !!this._height;
39 }
40 /** Has been updated trigger event. */
41 update() {
42 this.destroyed || this.onUpdate.emit();
43 }
44 /**
45 * This can be overridden to start preloading a resource
46 * or do any other prepare step.
47 * @protected
48 * @returns Handle the validate event
49 */
50 load() {
51 return Promise.resolve(this);
52 }
53 /**
54 * The width of the resource.
55 * @readonly
56 */
57 get width() {
58 return this._width;
59 }
60 /**
61 * The height of the resource.
62 * @readonly
63 */
64 get height() {
65 return this._height;
66 }
67 /**
68 * Set the style, optional to override
69 * @param _renderer - yeah, renderer!
70 * @param _baseTexture - the texture
71 * @param _glTexture - texture instance for this webgl context
72 * @returns - `true` is success
73 */
74 style(_renderer, _baseTexture, _glTexture) {
75 return !1;
76 }
77 /** Clean up anything, this happens when destroying is ready. */
78 dispose() {
79 }
80 /**
81 * Call when destroying resource, unbind any BaseTexture object
82 * before calling this method, as reference counts are maintained
83 * internally.
84 */
85 destroy() {
86 this.destroyed || (this.destroyed = !0, this.dispose(), this.onError.removeAll(), this.onError = null, this.onResize.removeAll(), this.onResize = null, this.onUpdate.removeAll(), this.onUpdate = null);
87 }
88 /**
89 * Abstract, used to auto-detect resource type.
90 * @param {*} _source - The source object
91 * @param {string} _extension - The extension of source, if set
92 */
93 static test(_source, _extension) {
94 return !1;
95 }
96}
97exports.Resource = Resource;
98//# sourceMappingURL=Resource.js.map