UNPKG

@awayjs/scene

Version:
133 lines (132 loc) 4.77 kB
import { __extends } from "tslib"; import { Graphics } from '@awayjs/graphics'; import { DisplayObjectContainer } from './DisplayObjectContainer'; /** * Sprite is an instance of a Graphics, augmenting it with a presence in the scene graph, a material, and an animation * state. It consists out of Graphices, which in turn correspond to SubGeometries. Graphices allow different parts * of the graphics to be assigned different materials. */ var Sprite = /** @class */ (function (_super) { __extends(Sprite, _super); /** * Create a new Sprite object. * * @param material [optional] The material with which to render the Sprite. */ function Sprite(graphics, material) { if (graphics === void 0) { graphics = null; } if (material === void 0) { material = null; } var _this = _super.call(this) || this; _this.graphics = graphics; _this.material = material; return _this; // this.dropBitmapCache = this.dropBitmapCache.bind(this); // this.generateBitmapCache = this.generateBitmapCache.bind(this); } Sprite.getNewSprite = function (graphics, material) { if (graphics === void 0) { graphics = null; } if (material === void 0) { material = null; } if (Sprite._sprites.length) { var sprite = Sprite._sprites.pop(); sprite.graphics = graphics; sprite.material = material; return sprite; } return new Sprite(graphics, material); }; Sprite.clearPool = function () { Sprite._sprites = []; }; Object.defineProperty(Sprite.prototype, "assetType", { /** * */ get: function () { return Sprite.assetType; }, enumerable: false, configurable: true }); Object.defineProperty(Sprite.prototype, "graphics", { /** * Specifies the Graphics object belonging to this Sprite object, where * drawing commands can occur. */ get: function () { //create new graphics object if none exists if (!this._graphics) this.graphics = Graphics.getGraphics(); return this._graphics; }, set: function (value) { if (this._graphics == value) return; if (this._graphics) this._graphics.removeOwner(this); this._graphics = value; if (this._graphics) this._graphics.addOwner(this); this.invalidate(); }, enumerable: false, configurable: true }); Sprite.prototype.getEntity = function () { _super.prototype.getEntity.call(this); if (this._iSourcePrefab) this._iSourcePrefab._iValidate(); return this._graphics; }; /** * @inheritDoc */ Sprite.prototype.dispose = function () { this.disposeValues(); Sprite._sprites.push(this); }; /** * @inheritDoc */ Sprite.prototype.disposeValues = function () { _super.prototype.disposeValues.call(this); }; /** * Clones this Sprite instance along with all it's children, while re-using the same * material, graphics and animation set. The returned result will be a copy of this sprite, * containing copies of all of it's children. * * Properties that are re-used (i.e. not cloned) by the new copy include name, * graphics, and material. Properties that are cloned or created anew for the copy * include subSpritees, children of the sprite, and the animator. * * If you want to copy just the sprite, reusing it's graphics and material while not * cloning it's children, the simplest way is to create a new sprite manually: * * <code> * var clone : Sprite = new Sprite(original.graphics, original.material); * </code> */ Sprite.prototype.clone = function () { var newInstance = Sprite.getNewSprite(); this.copyTo(newInstance); return newInstance; }; Sprite.prototype.copyTo = function (sprite, cloneShapes) { if (cloneShapes === void 0) { cloneShapes = false; } _super.prototype.copyTo.call(this, sprite); sprite._iSourcePrefab = this._iSourcePrefab; if (this._graphics) this._graphics.copyTo(sprite.graphics, cloneShapes); }; /** * */ Sprite.prototype.bakeTransformations = function () { this._graphics.applyTransformation(this.transform.matrix3D); this.transform.clearMatrix3D(); }; Sprite._sprites = []; Sprite.assetType = '[asset Sprite]'; return Sprite; }(DisplayObjectContainer)); export { Sprite };