Use #_set() in Rectangle#initialize() to simplify code and increase flexibility.

This commit is contained in:
Jürg Lehni 2013-03-01 14:37:27 -08:00
parent 8d2c2f5fda
commit 3f85dcdb77
2 changed files with 48 additions and 72 deletions

View file

@ -67,79 +67,56 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
this.x = this.y = this.width = this.height = 0; this.x = this.y = this.width = this.height = 0;
if (this._read) if (this._read)
this._read = arg0 === null ? 1 : 0; this._read = arg0 === null ? 1 : 0;
} else { } else if (arguments.length === 1) {
// Handle a couple of cases, with a fallback to reading // This can either be an array, or an object literal.
// (Point, Point) or (Point, Size) from the arguments list, in case if (Array.isArray(arg0)) {
// args is defined. this.x = arg0[0];
var args = arguments, this.y = arg0[1];
center = null; this.width = arg0[2];
if (arguments.length == 1) { this.height = arg0[3];
// This can either be an array, or an object literal. Clear args } else if (arg0.x !== undefined || arg0.width !== undefined) {
// since we don't want to read from arguments below, except for // Another rectangle or a simple object literal
// when an object literal is converted (e.g. {point, size}...) // describing one. Use duck typing, and 0 as defaults.
args = null; this.x = arg0.x || 0;
if (Array.isArray(arg0)) { this.y = arg0.y || 0;
this.x = arg0[0]; this.width = arg0.width || 0;
this.y = arg0[1]; this.height = arg0.height || 0;
this.width = arg0[2]; } else {
this.height = arg0[3]; // Use #_set to support whatever property the rectangle can
} else { // take.
// new Rectangle(obj) this.x = this.y = this.width = this.height = 0;
// See if we define the rectangle by {point, size}, this._set(arg0);
// or {center, size}
if (arg0.size) {
// We read point even if it's not defined in which case
// it's (0, 0), and set center so it can be changed at
// the end.
args = [Point.read([arg0.point]), Size.read([arg0.size])];
if (arg0.center)
center = Point.read([arg0.center]);
} else {
// Another rectangle or a simple object literal
// describing one. Use duck typing, and 0 as defaults.
this.x = arg0.x || 0;
this.y = arg0.y || 0;
this.width = arg0.width || 0;
this.height = arg0.height || 0;
}
}
if (this._read)
this._read = 1;
} }
// If args is defined, we either haven't been able to handle values if (this._read)
// above yet, or an object literal was converted to an arguments this._read = 1;
// list. } else if (arguments.length > 1) {
if (args && args.length > 1) { // Read a point argument and look at the next value to see wether
// Read a point argument and look at the next value to see // it's a size or a point, then read accordingly.
// wether it's a size or a point, then read accordingly. var point = Point.read(arguments),
var point = Point.read(args), next = Base.peek(arguments);
next = Base.peek(args); this.x = point.x;
this.x = point.x; this.y = point.y;
this.y = point.y; if (next && next.x !== undefined) {
if (next && next.x !== undefined) { // new Rectangle(point1, point2)
// new Rectangle(point1, point2) var point2 = Point.read(arguments);
var point2 = Point.read(args); this.width = point2.x - point.x;
this.width = point2.x - point.x; this.height = point2.y - point.y;
this.height = point2.y - point.y; if (this.width < 0) {
if (this.width < 0) { this.x = point2.x;
this.x = point2.x; this.width = -this.width;
this.width = -this.width;
}
if (this.height < 0) {
this.y = point2.y;
this.height = -this.height;
}
} else {
// new Rectangle(point, size)
var size = Size.read(args);
this.width = size.width;
this.height = size.height;
} }
if (this._read) if (this.height < 0) {
this._read = arguments._index; this.y = point2.y;
this.height = -this.height;
}
} else {
// new Rectangle(point, size)
var size = Size.read(arguments);
this.width = size.width;
this.height = size.height;
} }
if (center) if (this._read)
this.setCenter(center); this._read = arguments._index;
} }
}, },

View file

@ -20,8 +20,7 @@ Path.inject({ statics: new function() {
rect = new Rectangle(Point.readNamed(list, 'from'), rect = new Rectangle(Point.readNamed(list, 'from'),
Point.readNamed(list, 'to')); Point.readNamed(list, 'to'));
} else { } else {
rect = new Rectangle(); rect = new Rectangle(props);
rect._set(Base.getNamed(list));
} }
} else { } else {
rect = Rectangle.read(list); rect = Rectangle.read(list);