Clean up Path.* constructors and make sure they handle Base.read() returning null.

This commit is contained in:
Jürg Lehni 2011-03-08 17:20:30 +00:00
parent 484b2b0c94
commit 195b089424

View file

@ -26,32 +26,29 @@ Path.inject({ statics: new function() {
return { return {
Line: function() { Line: function() {
var path = new Path(); if (arguments.length >= 2) {
if (arguments.length == 2) { var step = Math.floor(arguments.length / 2);
path._add(new Segment(arguments[0])); return new Path(
path._add(new Segment(arguments[1])); Segment.read(arguments, 0, step),
} else if (arguments.length == 4) { Segment.read(arguments, step, step)
path._add(new Segment(arguments[0], arguments[1])); );
path._add(new Segment(arguments[2], arguments[3]));
} }
return path;
}, },
Rectangle: function() { Rectangle: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
var path = new Path(), var path = new Path(),
rectangle = Rectangle.read(arguments),
corners = ['getBottomLeft', 'getTopLeft', 'getTopRight', corners = ['getBottomLeft', 'getTopLeft', 'getTopRight',
'getBottomRight']; 'getBottomRight'];
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
path.add(rectangle[corners[i]]()); path.add(rect[corners[i]]());
} }
path.closed = true; path.closed = true;
return path; return path;
}, },
RoundRectangle: function() { RoundRectangle: function(rect, size) {
var path = new Path(),
rect, size;
if (arguments.length == 2) { if (arguments.length == 2) {
rect = Rectangle.read(arguments, 0, 1); rect = Rectangle.read(arguments, 0, 1);
size = Size.read(arguments, 1, 1); size = Size.read(arguments, 1, 1);
@ -59,8 +56,11 @@ Path.inject({ statics: new function() {
rect = Rectangle.read(arguments, 0, 4); rect = Rectangle.read(arguments, 0, 4);
size = Size.read(arguments, 4, 2); size = Size.read(arguments, 4, 2);
} }
if (!rect || !size)
return null;
size = Size.min(size, rect.getSize().divide(2)); size = Size.min(size, rect.getSize().divide(2));
var uSize = size.multiply(kappa * 2), var path = new Path(),
uSize = size.multiply(kappa * 2),
bl = rect.getBottomLeft(), bl = rect.getBottomLeft(),
tl = rect.getTopLeft(), tl = rect.getTopLeft(),
@ -83,9 +83,10 @@ Path.inject({ statics: new function() {
return path; return path;
}, },
Oval: function() { Oval: function(rect) {
if (!(rect = Rectangle.read(arguments)))
return null;
var path = new Path(), var path = new Path(),
rect = Rectangle.read(arguments),
topLeft = rect.getTopLeft(), topLeft = rect.getTopLeft(),
size = new Size(rect.width, rect.height); size = new Size(rect.width, rect.height);
for (var i = 0; i < 4; i++) { for (var i = 0; i < 4; i++) {
@ -100,15 +101,15 @@ Path.inject({ statics: new function() {
return path; return path;
}, },
Circle: function() { Circle: function(center, radius) {
var center, radius;
if (arguments.length == 3) { if (arguments.length == 3) {
center = new Point(arguments[0], arguments[1]); center = Point.read(arguments, 0, 2);
radius = arguments[2]; radius = arguments[2];
} else { } else {
center = new Point(arguments[0]); center = Point.read(arguments, 0, 1);
radius = arguments[1];
} }
if (!center || !radius)
return null;
return Path.Oval(new Rectangle(center.subtract(radius), return Path.Oval(new Rectangle(center.subtract(radius),
new Size(radius * 2, radius * 2))); new Size(radius * 2, radius * 2)));
}, },
@ -121,7 +122,8 @@ Path.inject({ statics: new function() {
}, },
RegularPolygon: function(center, numSides, radius) { RegularPolygon: function(center, numSides, radius) {
center = new Point(center); if (!(center = Point.read(arguments, 0)))
return null;
var path = new Path(), var path = new Path(),
three = !(numSides % 3), three = !(numSides % 3),
vector = new Point(0, three ? -radius : radius), vector = new Point(0, three ? -radius : radius),