Rename ObservedRectangle to LinkedRectangle, ObservedPoint to LinkedPoint, and add more comments about what it is they are doing.

This commit is contained in:
Jürg Lehni 2011-03-22 17:27:46 +00:00
parent dbb947b7aa
commit b5fdecf3d1
6 changed files with 38 additions and 39 deletions

View file

@ -159,7 +159,7 @@ var Point = this.Point = Base.extend({
if (this.isZero()) { if (this.isZero()) {
if (this._angle != null) { if (this._angle != null) {
var a = this._angle; var a = this._angle;
// Use #set() instead of direct assignment, so ObservedPoint // Use #set() instead of direct assignment, so LinkedPoint
// can optimise // can optimise
this.set( this.set(
Math.cos(a) * length, Math.cos(a) * length,
@ -177,7 +177,7 @@ var Point = this.Point = Base.extend({
// x and y are 0 // x and y are 0
this.getAngle(); this.getAngle();
} }
// Use #set() instead of direct assignment, so ObservedPoint // Use #set() instead of direct assignment, so LinkedPoint
// can optimise // can optimise
this.set( this.set(
this.x * scale, this.x * scale,
@ -237,7 +237,7 @@ var Point = this.Point = Base.extend({
angle = this._angle = angle * Math.PI / 180; angle = this._angle = angle * Math.PI / 180;
if (!this.isZero()) { if (!this.isZero()) {
var length = this.getLength(); var length = this.getLength();
// Use #set() instead of direct assignment, so ObservedPoint // Use #set() instead of direct assignment, so LinkedPoint
// can optimise // can optimise
this.set( this.set(
Math.cos(angle) * length, Math.cos(angle) * length,
@ -573,13 +573,19 @@ var Point = this.Point = Base.extend({
} }
}); });
var ObservedPoint = Point.extend({ /**
* 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()
* Note: This prototype is not exported.
*/
var LinkedPoint = Point.extend({
beans: true, beans: true,
set: function(x, y) { set: function(x, y) {
this._x = x; this._x = x;
this._y = y; this._y = y;
this._observer[this._set](this); this._owner[this._set](this);
return this; return this;
}, },
@ -589,7 +595,7 @@ var ObservedPoint = Point.extend({
setX: function(x) { setX: function(x) {
this._x = x; this._x = x;
this._observer[this._set](this); this._owner[this._set](this);
}, },
getY: function() { getY: function() {
@ -598,24 +604,15 @@ var ObservedPoint = Point.extend({
setY: function(y) { setY: function(y) {
this._y = y; this._y = y;
this._observer[this._set](this); this._owner[this._set](this);
}, },
statics: { statics: {
/** create: function(owner, set, x, y) {
* Provide a faster creator for Points out of two coordinates that var point = new LinkedPoint(LinkedPoint.dont);
* does not rely on Point#initialize at all. This speeds up all math
* operations a lot.
*/
create: function(observer, set, x, y) {
// Don't use the shorter form as we want absolute maximum
// performance here:
// return new Point(Point.dont).set(x, y);
// TODO: Benchmark and decide
var point = new ObservedPoint(ObservedPoint.dont);
point._x = x; point._x = x;
point._y = y; point._y = y;
point._observer = observer; point._owner = owner;
point._set = set; point._set = set;
return point; return point;
} }

View file

@ -71,7 +71,7 @@ var Rectangle = this.Rectangle = Base.extend({
}, },
getPoint: function() { getPoint: function() {
return ObservedPoint.create(this, 'setPoint', this.x, this.y); return LinkedPoint.create(this, 'setPoint', this.x, this.y);
}, },
setPoint: function(point) { setPoint: function(point) {
@ -149,7 +149,7 @@ var Rectangle = this.Rectangle = Base.extend({
}, },
getCenter: function() { getCenter: function() {
return ObservedPoint.create(this, 'setCenter', return LinkedPoint.create(this, 'setCenter',
this.getCenterX(), this.getCenterY()); this.getCenterX(), this.getCenterY());
}, },
@ -258,7 +258,7 @@ var Rectangle = this.Rectangle = Base.extend({
get = 'get' + part, get = 'get' + part,
set = 'set' + part; set = 'set' + part;
this[get] = function() { this[get] = function() {
return ObservedPoint.create(this, set, return LinkedPoint.create(this, set,
this[getX](), this[getY]()); this[getX](), this[getY]());
}; };
this[set] = function(point) { this[set] = function(point) {
@ -269,7 +269,13 @@ var Rectangle = this.Rectangle = Base.extend({
}, { beans: true }); }, { beans: true });
}); });
var ObservedRectangle = Rectangle.extend({ /**
* An internal version of Rectangle that notifies its owner of each change
* through setting itself again on the setter that corresponds to the getter
* that produced this LinkedRectangle. See uses of LinkedRectangle.create()
* Note: This prototype is not exported.
*/
var LinkedRectangle = Rectangle.extend({
beans: true, beans: true,
set: function(x, y, width, height) { set: function(x, y, width, height) {
@ -277,8 +283,8 @@ var ObservedRectangle = Rectangle.extend({
this._y = y; this._y = y;
this._width = width; this._width = width;
this._height = height; this._height = height;
if (this._observer) if (this._owner)
this._observer[this._set](this); this._owner[this._set](this);
return this; return this;
}, },
@ -288,14 +294,10 @@ var ObservedRectangle = Rectangle.extend({
* does not rely on Point#initialize at all. This speeds up all math * does not rely on Point#initialize at all. This speeds up all math
* operations a lot. * operations a lot.
*/ */
create: function(observer, set, x, y, width, height) { create: function(owner, set, x, y, width, height) {
// Don't use the shorter form as we want absolute maximum var rect = new LinkedRectangle(LinkedRectangle.dont).set(x, y,
// performance here:
// return new Point(Point.dont).set(x, y);
// TODO: Benchmark and decide
var rect = new ObservedRectangle(ObservedRectangle.dont).set(x, y,
width, height); width, height);
rect._observer = observer; rect._owner = owner;
rect._set = set; rect._set = set;
return rect; return rect;
} }
@ -315,7 +317,7 @@ var ObservedRectangle = Rectangle.extend({
// Check if this setter is called from another one which sets // Check if this setter is called from another one which sets
// _dontNotify, as it will notify itself // _dontNotify, as it will notify itself
if (!this._dontNotify) if (!this._dontNotify)
this._observer[this._set](this); this._owner[this._set](this);
} }
}, Base.each(['Point', 'Size', 'Center', }, Base.each(['Point', 'Size', 'Center',
'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY', 'Left', 'Top', 'Right', 'Bottom', 'CenterX', 'CenterY',
@ -325,12 +327,12 @@ var ObservedRectangle = Rectangle.extend({
var name = 'set' + key; var name = 'set' + key;
this[name] = function(value) { this[name] = function(value) {
// Make sure the above setters of x, y, width, height do not // Make sure the above setters of x, y, width, height do not
// each notify the observer, as we're going to take care of this // each notify the owner, as we're going to take care of this
// afterwards here, only once per change. // afterwards here, only once per change.
this._dontNotify = true; this._dontNotify = true;
proto[name].apply(this, arguments); proto[name].apply(this, arguments);
delete this._dontNotify; delete this._dontNotify;
this._observer[this._set](this); this._owner[this._set](this);
return this; return this;
} }
}, { beans: true }) }, { beans: true })

View file

@ -43,7 +43,7 @@ var Group = this.Group = Item.extend({
y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1); y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1);
} }
} }
return ObservedRectangle.create(this, 'setBounds', return LinkedRectangle.create(this, 'setBounds',
x1, y1, x2 - x1, y2 - y1); x1, y1, x2 - x1, y2 - y1);
}, },

View file

@ -44,7 +44,7 @@ var PlacedSymbol = this.PlacedSymbol = Item.extend({
getBounds: function() { getBounds: function() {
var bounds = this.symbol._definition.getStrokeBounds(this.matrix, true); var bounds = this.symbol._definition.getStrokeBounds(this.matrix, true);
return ObservedRectangle.create(this, 'setBounds', return LinkedRectangle.create(this, 'setBounds',
bounds.x, bounds.y, bounds.width, bounds.height); bounds.x, bounds.y, bounds.width, bounds.height);
}, },

View file

@ -42,7 +42,7 @@ var CompoundPath = this.CompoundPath = PathItem.extend({
y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1); y2 = Math.max(rect2.y + rect2.height, y1 + y2 - y1);
} }
} }
return ObservedRectangle.create(this, 'setBounds', return LinkedRectangle.create(this, 'setBounds',
x1, y1, x2 - x1, y2 - y1); x1, y1, x2 - x1, y2 - y1);
}, },

View file

@ -701,7 +701,7 @@ var Path = this.Path = PathItem.extend({
// Pass the matrix hidden from Bootstrap, so it still inject // Pass the matrix hidden from Bootstrap, so it still inject
// getBounds as bean too. // getBounds as bean too.
var bounds = getBounds(this, arguments[0]); var bounds = getBounds(this, arguments[0]);
return ObservedRectangle.create(this, 'setBounds', return LinkedRectangle.create(this, 'setBounds',
bounds.x, bounds.y, bounds.width, bounds.height); bounds.x, bounds.y, bounds.width, bounds.height);
}, },