UNPKG

@awayjs/renderer

Version:
156 lines (155 loc) 5.74 kB
import { __extends } from "tslib"; import { EventDispatcher, AssetEvent } from '@awayjs/core'; import { StyleEvent } from '../events/StyleEvent'; /** * */ var Style = /** @class */ (function (_super) { __extends(Style, _super); function Style() { var _this = _super.call(this) || this; _this._samplers = {}; _this._images = {}; _this._color = 0xFFFFFF; _this._onImageInvalidate = function (event) { return _this._invalidateImages(event); }; _this._onImageClear = function (event) { return _this._clearImages(event); }; return _this; } Object.defineProperty(Style.prototype, "sampler", { get: function () { return this._sampler; }, set: function (value) { if (this._sampler == value) return; this._sampler = value; this._invalidateProperties(); }, enumerable: false, configurable: true }); Object.defineProperty(Style.prototype, "image", { get: function () { return this._image; }, set: function (value) { if (this._image == value) return; if (this._image) { this._image.removeEventListener(AssetEvent.INVALIDATE, this._onImageInvalidate); this._image.removeEventListener(AssetEvent.CLEAR, this._onImageClear); } this._image = value; if (this._image) { this._image.addEventListener(AssetEvent.INVALIDATE, this._onImageInvalidate); this._image.addEventListener(AssetEvent.CLEAR, this._onImageClear); } this._invalidateProperties(); }, enumerable: false, configurable: true }); Object.defineProperty(Style.prototype, "uvMatrix", { get: function () { return this._uvMatrix; }, set: function (value) { if (this._uvMatrix == value) return; this._uvMatrix = value; this._invalidateProperties(); }, enumerable: false, configurable: true }); Object.defineProperty(Style.prototype, "color", { /** * The diffuse reflectivity color of the surface. */ get: function () { return this._color; }, set: function (value) { if (this._color == value) return; this._color = value; this._invalidateProperties(); }, enumerable: false, configurable: true }); Style.prototype.getImageAt = function (texture, index) { var _a; if (index === void 0) { index = 0; } return ((_a = this._images[texture.id]) === null || _a === void 0 ? void 0 : _a[index]) || this._image; }; Style.prototype.getSamplerAt = function (texture, index) { var _a; if (index === void 0) { index = 0; } return ((_a = this._samplers[texture.id]) === null || _a === void 0 ? void 0 : _a[index]) || this._sampler; }; Style.prototype.addImageAt = function (image, texture, index) { if (index === void 0) { index = 0; } if (!this._images[texture.id]) this._images[texture.id] = {}; this._images[texture.id][index] = image; image.addEventListener(AssetEvent.INVALIDATE, this._onImageInvalidate); image.addEventListener(AssetEvent.CLEAR, this._onImageClear); this._invalidateProperties(); }; Style.prototype.addSamplerAt = function (sampler, texture, index) { if (index === void 0) { index = 0; } if (!this._samplers[texture.id]) this._samplers[texture.id] = {}; this._samplers[texture.id][index] = sampler; this._invalidateProperties(); }; Style.prototype.removeImageAt = function (texture, index) { var _a; if (index === void 0) { index = 0; } var image = (_a = this._images[texture.id]) === null || _a === void 0 ? void 0 : _a[index]; if (!image) return; image.removeEventListener(AssetEvent.INVALIDATE, this._onImageInvalidate); image.removeEventListener(AssetEvent.CLEAR, this._onImageClear); this._images[texture.id][index] = null; this._invalidateProperties(); }; Style.prototype.removeSamplerAt = function (texture, index) { if (index === void 0) { index = 0; } if (!this._samplers[texture.id]) return; this._samplers[texture.id][index] = null; this._invalidateProperties(); }; Style.prototype._invalidateProperties = function () { this.dispatchEvent(new StyleEvent(StyleEvent.INVALIDATE_PROPERTIES, this)); }; Style.prototype._invalidateImages = function (event) { this.dispatchEvent(new StyleEvent(StyleEvent.INVALIDATE_IMAGES, this)); }; Style.prototype._clearImages = function (event) { var image = event.asset; //remove image if it has been disposed if (image.isDisposed) { if (this._image == image) { this.image = null; } else { //find and remove diposed image loop: for (var id in this._images) { var images = this._images[id]; for (var index in images) { if (images[index] == image) { delete images[index]; break loop; } } } } } this._invalidateProperties(); }; return Style; }(EventDispatcher)); export { Style };