import EventEmitter from 'eventemitter3'; import { TRANSFORM_MODE } from '../const'; import TransformStatic from './TransformStatic'; import Transform from './Transform'; import Bounds from './Bounds'; import * as math from '../math'; // _tempDisplayObjectParent = new DisplayObject(); /** * The base class for all objects that are rendered on the screen. * This is an abstract class and should not be used on its own rather it should be extended. * * @class * @extends EventEmitter * @mixes PIXI.interaction.interactiveTarget * @memberof PIXI */ export default class DisplayObject extends EventEmitter { /** * */ constructor() { super(); const TransformClass = TRANSFORM_MODE.DEFAULT === TRANSFORM_MODE.STATIC ? TransformStatic : Transform; this.tempDisplayObjectParent = null; // TODO: need to create Transform from factory /** * World transform and local transform of this object. * This will become read-only later, please do not assign anything there unless you know what are you doing * * @member {PIXI.TransformBase} */ this.transform = new TransformClass(); /** * The opacity of the object. * * @member {number} */ this.alpha = 1; /** * The visibility of the object. If false the object will not be drawn, and * the updateTransform function will not be called. * * Only affects recursive calls from parent. You can ask for bounds or call updateTransform manually * * @member {boolean} */ this.visible = true; /** * Can this object be rendered, if false the object will not be drawn but the updateTransform * methods will still be called. * * Only affects recursive calls from parent. You can ask for bounds manually * * @member {boolean} */ this.renderable = true; /** * The display object container that contains this display object. * * @member {PIXI.Container} * @readonly */ this.parent = null; /** * The multiplied alpha of the displayObject * * @member {number} * @readonly */ this.worldAlpha = 1; /** * The area the filter is applied to. This is used as more of an optimisation * rather than figuring out the dimensions of the displayObject each frame you can set this rectangle * * Also works as an interaction mask * * @member {PIXI.Rectangle} */ this.filterArea = null; this._filters = null; this._enabledFilters = null; /** * The bounds object, this is used to calculate and store the bounds of the displayObject * * @member {PIXI.Rectangle} * @private */ this._bounds = new Bounds(); this._boundsID = 0; this._lastBoundsID = -1; this._boundsRect = null; this._localBoundsRect = null; /** * The original, cached mask of the object * * @member {PIXI.Rectangle} * @private */ this._mask = null; } /** * @private * @member {PIXI.DisplayObject} */ get _tempDisplayObjectParent() { if (this.tempDisplayObjectParent === null) { this.tempDisplayObjectParent = new DisplayObject(); } return this.tempDisplayObjectParent; } /** * Updates the object transform for rendering * * TODO - Optimization pass! */ updateTransform() { this.transform.updateTransform(this.parent.transform); // multiply the alphas.. this.worldAlpha = this.alpha * this.parent.worldAlpha; this._bounds.updateID++; } /** * recursively updates transform of all objects from the root to this one * internal function for toLocal() */ _recursivePostUpdateTransform() { if (this.parent) { this.parent._recursivePostUpdateTransform(); this.transform.updateTransform(this.parent.transform); } else { this.transform.updateTransform(this._tempDisplayObjectParent.transform); } } /** * Retrieves the bounds of the displayObject as a rectangle object. * * @param {boolean} skipUpdate - setting to true will stop the transforms of the scene graph from * being updated. This means the calculation returned MAY be out of date BUT will give you a * nice performance boost * @param {PIXI.Rectangle} rect - Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ getBounds(skipUpdate, rect) { if (!skipUpdate) { if (!this.parent) { this.parent = this._tempDisplayObjectParent; this.parent.transform._worldID++; this.updateTransform(); this.parent = null; } else { this._recursivePostUpdateTransform(); this.updateTransform(); } } if (this._boundsID !== this._lastBoundsID) { this.calculateBounds(); } if (!rect) { if (!this._boundsRect) { this._boundsRect = new math.Rectangle(); } rect = this._boundsRect; } return this._bounds.getRectangle(rect); } /** * Retrieves the local bounds of the displayObject as a rectangle object * * @param {PIXI.Rectangle} rect - Optional rectangle to store the result of the bounds calculation * @return {PIXI.Rectangle} the rectangular bounding area */ getLocalBounds(rect) { const transformRef = this.transform; const parentRef = this.parent; this.parent = null; this.transform = this._tempDisplayObjectParent.transform; if (!rect) { if (!this._localBoundsRect) { this._localBoundsRect = new math.Rectangle(); } rect = this._localBoundsRect; } const bounds = this.getBounds(false, rect); this.parent = parentRef; this.transform = transformRef; return bounds; } /** * Calculates the global position of the display object * * @param {PIXI.Point} position - The world origin to calculate from * @param {PIXI.Point} [point] - A Point object in which to store the value, optional * (otherwise will create a new Point) * @param {boolean} [skipUpdate=false] - Should we skip the update transform. * @return {PIXI.Point} A point object representing the position of this object */ toGlobal(position, point, skipUpdate = false) { if (!skipUpdate) { this._recursivePostUpdateTransform(); // this parent check is for just in case the item is a root object. // If it is we need to give it a temporary parent so that displayObjectUpdateTransform works correctly // this is mainly to avoid a parent check in the main loop. Every little helps for performance :) if (!this.parent) { this.parent = this._tempDisplayObjectParent; this.displayObjectUpdateTransform(); this.parent = null; } else { this.displayObjectUpdateTransform(); } } // don't need to update the lot return this.worldTransform.apply(position, point); } /** * Calculates the local position of the display object relative to another point * * @param {PIXI.Point} position - The world origin to calculate from * @param {PIXI.DisplayObject} [from] - The DisplayObject to calculate the global position from * @param {PIXI.Point} [point] - A Point object in which to store the value, optional * (otherwise will create a new Point) * @param {boolean} [skipUpdate=false] - Should we skip the update transform * @return {PIXI.Point} A point object representing the position of this object */ toLocal(position, from, point, skipUpdate) { if (from) { position = from.toGlobal(position, point, skipUpdate); } if (!skipUpdate) { this._recursivePostUpdateTransform(); // this parent check is for just in case the item is a root object. // If it is we need to give it a temporary parent so that displayObjectUpdateTransform works correctly // this is mainly to avoid a parent check in the main loop. Every little helps for performance :) if (!this.parent) { this.parent = this._tempDisplayObjectParent; this.displayObjectUpdateTransform(); this.parent = null; } else { this.displayObjectUpdateTransform(); } } // simply apply the matrix.. return this.worldTransform.applyInverse(position, point); } /** * Renders the object using the WebGL renderer * * @param {PIXI.WebGLRenderer} renderer - The renderer */ renderWebGL(renderer) // eslint-disable-line no-unused-vars { // OVERWRITE; } /** * Renders the object using the Canvas renderer * * @param {PIXI.CanvasRenderer} renderer - The renderer */ renderCanvas(renderer) // eslint-disable-line no-unused-vars { // OVERWRITE; } /** * Set the parent Container of this DisplayObject * * @param {PIXI.Container} container - The Container to add this DisplayObject to * @return {PIXI.Container} The Container that this DisplayObject was added to */ setParent(container) { if (!container || !container.addChild) { throw new Error('setParent: Argument must be a Container'); } container.addChild(this); return container; } /** * Convenience function to set the postion, scale, skew and pivot at once. * * @param {number} [x=0] - The X position * @param {number} [y=0] - The Y position * @param {number} [scaleX=1] - The X scale value * @param {number} [scaleY=1] - The Y scale value * @param {number} [rotation=0] - The rotation * @param {number} [skewX=0] - The X skew value * @param {number} [skewY=0] - The Y skew value * @param {number} [pivotX=0] - The X pivot value * @param {number} [pivotY=0] - The Y pivot value * @return {PIXI.DisplayObject} The DisplayObject instance */ setTransform(x = 0, y = 0, scaleX = 1, scaleY = 1, rotation = 0, skewX = 0, skewY = 0, pivotX = 0, pivotY = 0) { this.position.x = x; this.position.y = y; this.scale.x = !scaleX ? 1 : scaleX; this.scale.y = !scaleY ? 1 : scaleY; this.rotation = rotation; this.skew.x = skewX; this.skew.y = skewY; this.pivot.x = pivotX; this.pivot.y = pivotY; return this; } /** * Base destroy method for generic display objects. This will automatically * remove the display object from its parent Container as well as remove * all current event listeners and internal references. Do not use a DisplayObject * after calling `destroy`. * */ destroy() { this.removeAllListeners(); if (this.parent) { this.parent.removeChild(this); } this.transform = null; this.parent = null; this._bounds = null; this._currentBounds = null; this._mask = null; this.filterArea = null; this.interactive = false; this.interactiveChildren = false; } /** * The position of the displayObject on the x axis relative to the local coordinates of the parent. * An alias to position.x * * @member {number} * @memberof PIXI.DisplayObject# */ get x() { return this.position.x; } /** * Sets the X position of the object. * * @param {number} value - The value to set to. */ set x(value) { this.transform.position.x = value; } /** * The position of the displayObject on the y axis relative to the local coordinates of the parent. * An alias to position.y * * @member {number} * @memberof PIXI.DisplayObject# */ get y() { return this.position.y; } /** * Sets the Y position of the object. * * @param {number} value - The value to set to. */ set y(value) { this.transform.position.y = value; } /** * Current transform of the object based on world (parent) factors * * @member {PIXI.Matrix} * @memberof PIXI.DisplayObject# * @readonly */ get worldTransform() { return this.transform.worldTransform; } /** * Current transform of the object based on local factors: position, scale, other stuff * * @member {PIXI.Matrix} * @memberof PIXI.DisplayObject# * @readonly */ get localTransform() { return this.transform.localTransform; } /** * The coordinate of the object relative to the local coordinates of the parent. * Assignment by value since pixi-v4. * * @member {PIXI.Point|PIXI.ObservablePoint} * @memberof PIXI.DisplayObject# */ get position() { return this.transform.position; } /** * Copies the point to the position of the object. * * @param {PIXI.Point} value - The value to set to. */ set position(value) { this.transform.position.copy(value); } /** * The scale factor of the object. * Assignment by value since pixi-v4. * * @member {PIXI.Point|PIXI.ObservablePoint} * @memberof PIXI.DisplayObject# */ get scale() { return this.transform.scale; } /** * Copies the point to the scale of the object. * * @param {PIXI.Point} value - The value to set to. */ set scale(value) { this.transform.scale.copy(value); } /** * The pivot point of the displayObject that it rotates around * Assignment by value since pixi-v4. * * @member {PIXI.Point|PIXI.ObservablePoint} * @memberof PIXI.DisplayObject# */ get pivot() { return this.transform.pivot; } /** * Copies the point to the pivot of the object. * * @param {PIXI.Point} value - The value to set to. */ set pivot(value) { this.transform.pivot.copy(value); } /** * The skew factor for the object in radians. * Assignment by value since pixi-v4. * * @member {PIXI.ObservablePoint} * @memberof PIXI.DisplayObject# */ get skew() { return this.transform.skew; } /** * Copies the point to the skew of the object. * * @param {PIXI.Point} value - The value to set to. */ set skew(value) { this.transform.skew.copy(value); } /** * The rotation of the object in radians. * * @member {number} * @memberof PIXI.DisplayObject# */ get rotation() { return this.transform.rotation; } /** * Sets the rotation of the object. * * @param {number} value - The value to set to. */ set rotation(value) { this.transform.rotation = value; } /** * Indicates if the sprite is globally visible. * * @member {boolean} * @memberof PIXI.DisplayObject# * @readonly */ get worldVisible() { let item = this; do { if (!item.visible) { return false; } item = item.parent; } while (item); return true; } /** * Sets a mask for the displayObject. A mask is an object that limits the visibility of an * object to the shape of the mask applied to it. In PIXI a regular mask must be a * PIXI.Graphics or a PIXI.Sprite object. This allows for much faster masking in canvas as it * utilises shape clipping. To remove a mask, set this property to null. * * @todo For the moment, PIXI.CanvasRenderer doesn't support PIXI.Sprite as mask. * * @member {PIXI.Graphics|PIXI.Sprite} * @memberof PIXI.DisplayObject# */ get mask() { return this._mask; } /** * Sets the mask. * * @param {PIXI.Graphics|PIXI.Sprite} value - The value to set to. */ set mask(value) { if (this._mask) { this._mask.renderable = true; } this._mask = value; if (this._mask) { this._mask.renderable = false; } } /** * Sets the filters for the displayObject. * * IMPORTANT: This is a webGL only feature and will be ignored by the canvas renderer. * To remove filters simply set this property to 'null' * * @member {PIXI.AbstractFilter[]} * @memberof PIXI.DisplayObject# */ get filters() { return this._filters && this._filters.slice(); } /** * Shallow copies the array to the filters of the object. * * @param {PIXI.Filter[]} value - The filters to set. */ set filters(value) { this._filters = value && value.slice(); } } // performance increase to avoid using call.. (10x faster) DisplayObject.prototype.displayObjectUpdateTransform = DisplayObject.prototype.updateTransform;