diff --git a/src/core/display/ObservablePoint.js b/src/core/display/ObservablePoint.js index 4d65964..836fcdd 100644 --- a/src/core/display/ObservablePoint.js +++ b/src/core/display/ObservablePoint.js @@ -1,11 +1,12 @@ /** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. * An observable point is a point that triggers a callback when the point's position is changed. * * @class * @memberof PIXI - * - * @param cb {function} The function to be called when the point changes - * @param scope {*} The scope to be applied to the cb + * @param cb {Function} callback when changed + * @param scope {Object} owner of callback * @param [x=0] {number} position of the point on the x axis * @param [y=0] {number} position of the point on the y axis */ @@ -37,8 +38,10 @@ }, set: function (value) { - this._x = value; - this.cb.call(this.scope); + if (this._x !== value) { + this._x = value; + this.cb.call(this.scope); + } } }, /** @@ -54,8 +57,10 @@ }, set: function (value) { - this._y = value; - this.cb.call(this.scope); + if (this._y !== value) { + this._y = value; + this.cb.call(this.scope); + } } } }); @@ -69,8 +74,27 @@ */ ObservablePoint.prototype.set = function (x, y) { - this._x = x || 0; - this._y = y || ( (y !== 0) ? this._x : 0 ); + var _x = x || 0; + var _y = y || ( (y !== 0) ? _x : 0 ); + if (this._x !== _x || this._y !== _y) + { + this._x = _x; + this._y = _y; + this.cb.call(this.scope); + } +}; - this.transform._versionLocal++; // TODO: Pretty sure this doesn't exist. +/** + * Copies the data from another point + * + * @param point {PIXI.Point|{PIXI.ObservablePoint} point to copy from + */ +ObservablePoint.prototype.copy = function (point) +{ + if (this._x !== point.x || this._y !== point.y) + { + this._x = point.x; + this._y = point.y; + this.cb.call(this.scope); + } }; diff --git a/src/core/display/ObservablePoint.js b/src/core/display/ObservablePoint.js index 4d65964..836fcdd 100644 --- a/src/core/display/ObservablePoint.js +++ b/src/core/display/ObservablePoint.js @@ -1,11 +1,12 @@ /** + * The Point object represents a location in a two-dimensional coordinate system, where x represents + * the horizontal axis and y represents the vertical axis. * An observable point is a point that triggers a callback when the point's position is changed. * * @class * @memberof PIXI - * - * @param cb {function} The function to be called when the point changes - * @param scope {*} The scope to be applied to the cb + * @param cb {Function} callback when changed + * @param scope {Object} owner of callback * @param [x=0] {number} position of the point on the x axis * @param [y=0] {number} position of the point on the y axis */ @@ -37,8 +38,10 @@ }, set: function (value) { - this._x = value; - this.cb.call(this.scope); + if (this._x !== value) { + this._x = value; + this.cb.call(this.scope); + } } }, /** @@ -54,8 +57,10 @@ }, set: function (value) { - this._y = value; - this.cb.call(this.scope); + if (this._y !== value) { + this._y = value; + this.cb.call(this.scope); + } } } }); @@ -69,8 +74,27 @@ */ ObservablePoint.prototype.set = function (x, y) { - this._x = x || 0; - this._y = y || ( (y !== 0) ? this._x : 0 ); + var _x = x || 0; + var _y = y || ( (y !== 0) ? _x : 0 ); + if (this._x !== _x || this._y !== _y) + { + this._x = _x; + this._y = _y; + this.cb.call(this.scope); + } +}; - this.transform._versionLocal++; // TODO: Pretty sure this doesn't exist. +/** + * Copies the data from another point + * + * @param point {PIXI.Point|{PIXI.ObservablePoint} point to copy from + */ +ObservablePoint.prototype.copy = function (point) +{ + if (this._x !== point.x || this._y !== point.y) + { + this._x = point.x; + this._y = point.y; + this.cb.call(this.scope); + } }; diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 041e39c..6f1cdb9 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -1,4 +1,5 @@ -var core = require('../core'); +var core = require('../core'), + ObservablePoint = require('../core/display/ObservablePoint'); /** * A BitmapText object will create a line or multiple lines of text using bitmap font. To @@ -105,6 +106,14 @@ this.maxLineHeight = 0; /** + * Text anchor. read-only + * + * @member {PIXI.Point} + * @private + */ + this._anchor = new ObservablePoint(this.makeDirty, this, 0, 0); + + /** * The dirty state of this object. * * @member {boolean} @@ -160,6 +169,28 @@ }, /** + * The anchor sets the origin point of the text. + * The default is 0,0 this means the text's origin is the top left + * Setting the anchor to 0.5,0.5 means the text's origin is centered + * Setting the anchor to 1,1 would mean the text's origin point will be the bottom right corner + * + * @member {PIXI.Point | number} + */ + anchor: { + get : function() { + return this._anchor; + }, + set: function(value) { + if (typeof value === 'number'){ + this._anchor.set(value); + } + else { + this._anchor.copy(value); + } + } + }, + + /** * The font descriptor of the BitmapText object * * @member {string|object} @@ -343,6 +374,16 @@ this.textWidth = maxLineWidth * scale; this.textHeight = (pos.y + data.lineHeight) * scale; + + // apply anchor + if (this.anchor.x !== 0 || this.anchor.y !== 0) + { + for (i = 0; i < lenChars; i++) + { + this._glyphs[i].x -= this.textWidth * this.anchor.x; + this._glyphs[i].y -= this.textHeight * this.anchor.y; + } + } this.maxLineHeight = maxLineHeight * scale; }; @@ -383,4 +424,8 @@ } }; +BitmapText.prototype.makeDirty = function() { + this.dirty = true; +}; + BitmapText.fonts = {};