Imrove Point, Size and Rectangle constructors.

This commit is contained in:
Jürg Lehni 2011-03-06 12:15:15 +00:00
parent fd0d409d9e
commit cb2e4aaa04
3 changed files with 44 additions and 47 deletions

View file

@ -6,25 +6,24 @@
var Point = this.Point = Base.extend({ var Point = this.Point = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function(arg0, arg1) {
if (arguments.length == 2) { if (arguments.length == 2) {
this.x = arguments[0]; this.x = arg0;
this.y = arguments[1]; this.y = arg1;
} else if (arguments.length == 1) { } else if (arguments.length == 1) {
var arg = arguments[0]; if (arg0 == null) {
if (arg == null) {
this.x = this.y = 0; this.x = this.y = 0;
} else if (arg.x !== undefined) { } else if (arg0.x !== undefined) {
this.x = arg.x; this.x = arg0.x;
this.y = arg.y; this.y = arg0.y;
} else if (arg.width !== undefined) { } else if (arg0.width !== undefined) {
this.x = arg.width; this.x = arg0.width;
this.y = arg.height; this.y = arg0.height;
} else if (Array.isArray(arg)) { } else if (Array.isArray(arg0)) {
this.x = arg[0]; this.x = arg0[0];
this.y = arg.length > 1 ? arg[1] : arg[0]; this.y = arg0.length > 1 ? arg0[1] : arg0[0];
} else if (typeof arg === 'number') { } else if (typeof arg0 === 'number') {
this.x = this.y = arg; this.x = this.y = arg0;
} else { } else {
this.x = this.y = 0; this.x = this.y = 0;
} }

View file

@ -1,19 +1,18 @@
var Rectangle = this.Rectangle = Base.extend({ var Rectangle = this.Rectangle = Base.extend({
beans: true, beans: true,
initialize: function() { initialize: function(arg0, arg1, arg2, arg3) {
if (arguments.length == 1) { if (arguments.length == 1) {
var rect = arguments[0];
// Use 0 as defaults, in case we're reading from a Point or Size // Use 0 as defaults, in case we're reading from a Point or Size
this.x = rect.x || 0; this.x = arg0.x || 0;
this.y = rect.y || 0; this.y = arg0.y || 0;
this.width = rect.width || 0; this.width = arg0.width || 0;
this.height = rect.height || 0; this.height = arg0.height || 0;
} else if (arguments.length == 2) { } else if (arguments.length == 2) {
if (arguments[1].x !== undefined) { if (arg1.x !== undefined) {
// new Rectangle(point1, point2) // new Rectangle(point1, point2)
var point1 = new Point(arguments[0]); var point1 = Point.read(arguments, 0, 1);
var point2 = new Point(arguments[1]); var point2 = Point.read(arguments, 1, 1);
this.x = point1.x; this.x = point1.x;
this.y = point1.y; this.y = point1.y;
this.width = point2.x - point1.x; this.width = point2.x - point1.x;
@ -28,8 +27,8 @@ var Rectangle = this.Rectangle = Base.extend({
} }
} else { } else {
// new Rectangle(point, size) // new Rectangle(point, size)
var point = new Point(arguments[0]); var point = Point.read(arguments, 0, 1);
var size = new Size(arguments[1]); var size = Size.read(arguments, 1, 1);
this.x = point.x; this.x = point.x;
this.y = point.y; this.y = point.y;
this.width = size.width; this.width = size.width;
@ -37,10 +36,10 @@ var Rectangle = this.Rectangle = Base.extend({
} }
} else if (arguments.length == 4) { } else if (arguments.length == 4) {
// new Rectangle(x, y, width, height) // new Rectangle(x, y, width, height)
this.x = arguments[0]; this.x = arg0;
this.y = arguments[1]; this.y = arg1;
this.width = arguments[2]; this.width = arg2;
this.height = arguments[3]; this.height = arg3;
} else { } else {
// new Rectangle() // new Rectangle()
this.x = this.y = this.width = this.height = 0; this.x = this.y = this.width = this.height = 0;

View file

@ -1,23 +1,22 @@
var Size = this.Size = Base.extend({ var Size = this.Size = Base.extend({
initialize: function() { initialize: function(arg0, arg1) {
if (arguments.length == 2) { if (arguments.length == 2) {
this.width = arguments[0]; this.width = arg0;
this.height = arguments[1]; this.height = arg1;
} else if (arguments.length == 1) { } else if (arguments.length == 1) {
var arg = arguments[0]; if (arg0 == null) {
if (arg == null) {
this.width = this.height = 0; this.width = this.height = 0;
} else if (arg.width !== undefined) { } else if (arg0.width !== undefined) {
this.width = arg.width; this.width = arg0.width;
this.height = arg.height; this.height = arg0.height;
} else if (arg.x !== undefined) { } else if (arg0.x !== undefined) {
this.width = arg.x; this.width = arg0.x;
this.height = arg.y; this.height = arg0.y;
} else if (Array.isArray(arg)) { } else if (Array.isArray(arg0)) {
this.width = arg[0]; this.width = arg0[0];
this.height = arg.length > 1 ? arg[1] : arg[0]; this.height = arg0.length > 1 ? arg0[1] : arg0[0];
} else if (typeof arg === 'number') { } else if (typeof arg0 === 'number') {
this.width = this.height = arg; this.width = this.height = arg0;
} else { } else {
this.width = this.height = 0; this.width = this.height = 0;
} }