var math = require('../math');
/**
* 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
* @namespace PIXI
*/
function DisplayObject()
{
/**
* The coordinate of the object relative to the local coordinates of the parent.
*
* @member {Point}
*/
this.position = new math.Point();
/**
* The scale factor of the object.
*
* @member {Point}
*/
this.scale = new math.Point(1, 1);
/**
* The pivot point of the displayObject that it rotates around
*
* @member {Point}
*/
this.pivot = new math.Point(0, 0);
/**
* The rotation of the object in radians.
*
* @member {number}
*/
this.rotation = 0;
/**
* 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.
*
* @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.
*
* @member {boolean}
*/
this.renderable = false;
/**
* The display object container that contains this display object.
*
* @member {DisplayObjectContainer}
* @readOnly
*/
this.parent = null;
/**
* The multiplied alpha of the displayObject
*
* @member {number}
* @readOnly
*/
this.worldAlpha = 1;
/**
* Current transform of the object based on world (parent) factors
*
* @member {Matrix}
* @readOnly
*/
this.worldTransform = new math.Matrix();
/**
* 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
*
* @member {Rectangle}
*/
this.filterArea = null;
/**
* cached sin rotation
*
* @member {number}
* @private
*/
this._sr = 0;
/**
* cached cos rotation
*
* @member {number}
* @private
*/
this._cr = 1;
/**
* The original, cached bounds of the object
*
* @member {Rectangle}
* @private
*/
this._bounds = new math.Rectangle(0, 0, 1, 1);
/**
* The most up-to-date bounds of the object
*
* @member {Rectangle}
* @private
*/
this._currentBounds = null;
/**
* The original, cached mask of the object
*
* @member {Rectangle}
* @private
*/
this._mask = null;
this._isMask = false;
/**
* Cached internal flag.
*
* @member {boolean}
* @private
*/
this._cacheIsDirty = false;
}
// constructor
DisplayObject.prototype.constructor = DisplayObject;
module.exports = DisplayObject;
Object.defineProperties(DisplayObject.prototype, {
/**
* The position of the displayObject on the x axis relative to the local coordinates of the parent.
*
* @member {number}
* @memberof DisplayObject#
*/
x: {
get: function ()
{
return this.position.x;
},
set: function (value)
{
this.position.x = value;
}
},
/**
* The position of the displayObject on the y axis relative to the local coordinates of the parent.
*
* @member {number}
* @memberof DisplayObject#
*/
y: {
get: function ()
{
return this.position.y;
},
set: function (value)
{
this.position.y = value;
}
},
/**
* Indicates if the sprite is globally visible.
*
* @member {boolean}
* @memberof DisplayObject#
* @readonly
*/
worldVisible: {
get: function ()
{
var 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 object. This allows for much faster masking in canvas as it utilises shape clipping.
* To remove a mask, set this property to null.
*
* @member {Graphics}
* @memberof DisplayObject#
*/
mask: {
get: function ()
{
return this._mask;
},
set: function (value)
{
if (this._mask)
{
this._mask.isMask = false;
}
this._mask = value;
if (this._mask)
{
this._mask.isMask = true;
}
}
},
/**
* 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 {Filter[]}
* @memberof DisplayObject#
*/
filters: {
get: function ()
{
return this._filters.slice();
},
set: function (value)
{
this._filters = value.slice();
//if(this._mask)
}
}
});
/*
* Updates the object transform for rendering
*
* TODO - Optimization pass!
*
* @private
*/
DisplayObject.prototype.updateTransform = function ()
{
// create some matrix refs for easy access
var pt = this.parent.worldTransform;
var wt = this.worldTransform;
// temporary matrix variables
var a, b, c, d, tx, ty;
// so if rotation is between 0 then we can simplify the multiplication process..
if (this.rotation % math.PI_2)
{
// check to see if the rotation is the same as the previous render. This means we only need to use sin and cos when rotation actually changes
if (this.rotation !== this.rotationCache)
{
this.rotationCache = this.rotation;
this._sr = Math.sin(this.rotation);
this._cr = Math.cos(this.rotation);
}
// get the matrix values of the displayobject based on its transform properties..
a = this._cr * this.scale.x;
b = this._sr * this.scale.x;
c = -this._sr * this.scale.y;
d = this._cr * this.scale.y;
tx = this.position.x;
ty = this.position.y;
// check for pivot.. not often used so geared towards that fact!
if (this.pivot.x || this.pivot.y)
{
tx -= this.pivot.x * a + this.pivot.y * c;
ty -= this.pivot.x * b + this.pivot.y * d;
}
// concat the parent matrix with the objects transform.
wt.a = a * pt.a + b * pt.c;
wt.b = a * pt.b + b * pt.d;
wt.c = c * pt.a + d * pt.c;
wt.d = c * pt.b + d * pt.d;
wt.tx = tx * pt.a + ty * pt.c + pt.tx;
wt.ty = tx * pt.b + ty * pt.d + pt.ty;
}
else
{
// lets do the fast version as we know there is no rotation..
a = this.scale.x;
d = this.scale.y;
tx = this.position.x - this.pivot.x * a;
ty = this.position.y - this.pivot.y * d;
wt.a = a * pt.a;
wt.b = a * pt.b;
wt.c = d * pt.c;
wt.d = d * pt.d;
wt.tx = tx * pt.a + ty * pt.c + pt.tx;
wt.ty = tx * pt.b + ty * pt.d + pt.ty;
}
// multiply the alphas..
this.worldAlpha = this.alpha * this.parent.worldAlpha;
};
// performance increase to avoid using call.. (10x faster)
DisplayObject.prototype.displayObjectUpdateTransform = DisplayObject.prototype.updateTransform;
/**
* Retrieves the bounds of the displayObject as a rectangle object
*
* @param matrix {Matrix}
* @return {Rectangle} the rectangular bounding area
*/
DisplayObject.prototype.getBounds = function (/* matrix */)
{
return math.Rectangle.EMPTY;
};
/**
* Retrieves the local bounds of the displayObject as a rectangle object
*
* @return {Rectangle} the rectangular bounding area
*/
DisplayObject.prototype.getLocalBounds = function ()
{
return this.getBounds(math.Matrix.IDENTITY);
};
/**
* Calculates the global position of the display object
*
* @param position {Point} The world origin to calculate from
* @return {Point} A point object representing the position of this object
*/
DisplayObject.prototype.toGlobal = function (position)
{
// don't need to u[date the lot
this.displayObjectUpdateTransform();
return this.worldTransform.apply(position);
};
/**
* Calculates the local position of the display object relative to another point
*
* @param position {Point} The world origin to calculate from
* @param [from] {DisplayObject} The DisplayObject to calculate the global position from
* @return {Point} A point object representing the position of this object
*/
DisplayObject.prototype.toLocal = function (position, from)
{
if (from)
{
position = from.toGlobal(position);
}
// don't need to update the lot
this.displayObjectUpdateTransform();
return this.worldTransform.applyInverse(position);
};
/**
* Renders the object using the WebGL renderer
*
* @param renderer {WebGLRenderer} The renderer
* @private
*/
DisplayObject.prototype.renderWebGL = function (/* renderer */)
{
// OVERWRITE;
};
/**
* Renders the object using the Canvas renderer
*
* @param renderer {CanvasRenderer} The renderer
* @private
*/
DisplayObject.prototype.renderCanvas = function (/* renderer */)
{
// OVERWRITE;
};