mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Remove static create() methods from LinkedPoint/Size/Rectangle, and give them initialize() methods instead that also name the classes in debugging.
This commit is contained in:
parent
c6e50375df
commit
3d5ae373a8
6 changed files with 41 additions and 67 deletions
|
@ -917,12 +917,20 @@ var Point = Base.extend(/** @lends Point# */{
|
|||
*
|
||||
* @class An internal version of Point that notifies its owner of each change
|
||||
* through setting itself again on the setter that corresponds to the getter
|
||||
* that produced this LinkedPoint. See uses of LinkedPoint.create()
|
||||
* that produced this LinkedPoint.
|
||||
* Note: This prototype is not exported.
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
var LinkedPoint = Point.extend({
|
||||
// Have LinkedPoint appear as a normal Point in debugging
|
||||
initialize: function Point(x, y, owner, setter) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
this._owner = owner;
|
||||
this._setter = setter;
|
||||
},
|
||||
|
||||
set: function(x, y, dontNotify) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
|
@ -947,21 +955,5 @@ var LinkedPoint = Point.extend({
|
|||
setY: function(y) {
|
||||
this._y = y;
|
||||
this._owner[this._setter](this);
|
||||
},
|
||||
|
||||
statics: {
|
||||
create: function(owner, setter, x, y, dontLink) {
|
||||
// Support creation of normal Points rather than LinkedPoints
|
||||
// through an optional parameter that can be passed to the getters.
|
||||
// See e.g. Rectangle#getPoint(true).
|
||||
if (dontLink)
|
||||
return new Point(x, y);
|
||||
var point = Base.create(LinkedPoint);
|
||||
point._x = x;
|
||||
point._y = y;
|
||||
point._owner = owner;
|
||||
point._setter = setter;
|
||||
return point;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -238,9 +238,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @bean
|
||||
*/
|
||||
getPoint: function(/* dontLink */) {
|
||||
// Pass on the optional argument _dontLink which tells LinkedPoint to
|
||||
// produce a normal point instead. Used internally for speed reasons.
|
||||
return LinkedPoint.create(this, 'setPoint', this.x, this.y, arguments[0]);
|
||||
return new (arguments[0] ? Point : LinkedPoint)
|
||||
(this.x, this.y, this, 'setPoint');
|
||||
},
|
||||
|
||||
setPoint: function(point) {
|
||||
|
@ -257,8 +256,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @bean
|
||||
*/
|
||||
getSize: function(/* dontLink */) {
|
||||
return LinkedSize.create(this, 'setSize', this.width, this.height,
|
||||
arguments[0]);
|
||||
return new (arguments[0] ? Size : LinkedSize)
|
||||
(this.width, this.height, this, 'setSize');
|
||||
},
|
||||
|
||||
setSize: function(size) {
|
||||
|
@ -403,8 +402,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @bean
|
||||
*/
|
||||
getCenter: function(/* dontLink */) {
|
||||
return LinkedPoint.create(this, 'setCenter',
|
||||
this.getCenterX(), this.getCenterY(), arguments[0]);
|
||||
return new (arguments[0] ? Point : LinkedPoint)
|
||||
(this.getCenterX(), this.getCenterY(), this, 'setCenter');
|
||||
},
|
||||
|
||||
setCenter: function(point) {
|
||||
|
@ -801,8 +800,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
get = 'get' + part,
|
||||
set = 'set' + part;
|
||||
this[get] = function(/* dontLink */) {
|
||||
return LinkedPoint.create(this, set,
|
||||
this[getX](), this[getY](), arguments[0]);
|
||||
return new (arguments[0] ? Point : LinkedPoint)
|
||||
(this[getX](), this[getY](), this, set);
|
||||
};
|
||||
this[set] = function(point) {
|
||||
point = Point.read(arguments);
|
||||
|
@ -824,6 +823,13 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @private
|
||||
*/
|
||||
var LinkedRectangle = Rectangle.extend({
|
||||
// Have LinkedRectangle appear as a normal Rectangle in debugging
|
||||
initialize: function Rectangle(x, y, width, height, owner, setter) {
|
||||
this.set(x, y, width, height, true);
|
||||
this._owner = owner;
|
||||
this._setter = setter;
|
||||
},
|
||||
|
||||
set: function(x, y, width, height, dontNotify) {
|
||||
this._x = x;
|
||||
this._y = y;
|
||||
|
@ -832,22 +838,6 @@ var LinkedRectangle = Rectangle.extend({
|
|||
if (!dontNotify)
|
||||
this._owner[this._setter](this);
|
||||
return this;
|
||||
},
|
||||
|
||||
statics: {
|
||||
/**
|
||||
* Provide a faster creator for Points out of two coordinates that
|
||||
* does not rely on Point#initialize at all. This speeds up all math
|
||||
* operations a lot.
|
||||
*
|
||||
* @ignore
|
||||
*/
|
||||
create: function(owner, setter, x, y, width, height) {
|
||||
var rect = Base.create(LinkedRectangle).set(x, y, width, height, true);
|
||||
rect._owner = owner;
|
||||
rect._setter = setter;
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
}, new function() {
|
||||
var proto = Rectangle.prototype;
|
||||
|
|
|
@ -512,12 +512,20 @@ var Size = Base.extend(/** @lends Size# */{
|
|||
*
|
||||
* @class An internal version of Size that notifies its owner of each change
|
||||
* through setting itself again on the setter that corresponds to the getter
|
||||
* that produced this LinkedSize. See uses of LinkedSize.create()
|
||||
* that produced this LinkedSize.
|
||||
* Note: This prototype is not exported.
|
||||
*
|
||||
* @private
|
||||
*/
|
||||
var LinkedSize = Size.extend({
|
||||
// Have LinkedSize appear as a normal Size in debugging
|
||||
initialize: function Size(width, height, owner, setter) {
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
this._owner = owner;
|
||||
this._setter = setter;
|
||||
},
|
||||
|
||||
set: function(width, height, dontNotify) {
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
|
@ -542,19 +550,5 @@ var LinkedSize = Size.extend({
|
|||
setHeight: function(height) {
|
||||
this._height = height;
|
||||
this._owner[this._setter](this);
|
||||
},
|
||||
|
||||
statics: {
|
||||
create: function(owner, setter, width, height, dontLink) {
|
||||
// See LinkedPoint.create() for an explanation about dontLink.
|
||||
if (dontLink)
|
||||
return new Size(width, height);
|
||||
var size = Base.create(LinkedSize);
|
||||
size._width = width;
|
||||
size._height = height;
|
||||
size._owner = owner;
|
||||
size._setter = setter;
|
||||
return size;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -719,8 +719,8 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
// use them to calculate the difference in #setPosition, as when it is
|
||||
// modified, it would hold new values already and only then cause the
|
||||
// calling of #setPosition.
|
||||
return arguments[0] ? pos
|
||||
: LinkedPoint.create(this, 'setPosition', pos.x, pos.y);
|
||||
return new (arguments[0] ? Point : LinkedPoint)
|
||||
(pos.x, pos.y, this, 'setPosition');
|
||||
},
|
||||
|
||||
setPosition: function(/* point */) {
|
||||
|
@ -773,11 +773,9 @@ var Item = Base.extend(Callback, /** @lends Item# */{
|
|||
// If we're returning 'bounds', create a LinkedRectangle that uses
|
||||
// the setBounds() setter to update the Item whenever the bounds are
|
||||
// changed:
|
||||
return name == 'getBounds'
|
||||
? LinkedRectangle.create(this, 'setBounds',
|
||||
bounds.x, bounds.y, bounds.width, bounds.height)
|
||||
// Return a clone of the cahce, so modifications won't
|
||||
// affect it.
|
||||
return name === 'getBounds'
|
||||
? new LinkedRectangle(bounds.x, bounds.y, bounds.width,
|
||||
bounds.height, this, 'setBounds')
|
||||
: bounds;
|
||||
};
|
||||
},
|
||||
|
|
|
@ -61,7 +61,7 @@ var PointText = TextItem.extend(/** @lends PointText# */{
|
|||
// Se Item#getPosition for an explanation why we create new LinkedPoint
|
||||
// objects each time.
|
||||
var point = this._matrix.getTranslation();
|
||||
return LinkedPoint.create(this, 'setPoint', point.x, point.y);
|
||||
return new LinkedPoint(point.x, point.y, this, 'setPoint');
|
||||
},
|
||||
|
||||
setPoint: function(point) {
|
||||
|
|
|
@ -94,8 +94,8 @@ var View = Base.extend(Callback, /** @lends View# */{
|
|||
View._views.push(this);
|
||||
// Link this id to our view
|
||||
View._viewsById[this._id] = this;
|
||||
this._viewSize = LinkedSize.create(this, 'setViewSize',
|
||||
size.width, size.height);
|
||||
this._viewSize = new LinkedSize(size.width, size.height,
|
||||
this, 'setViewSize');
|
||||
this._matrix = new Matrix();
|
||||
this._zoom = 1;
|
||||
// Make sure the first view is focused for keyboard input straight away
|
||||
|
|
Loading…
Reference in a new issue