Begin implementing Path#curves list.

This commit is contained in:
Jürg Lehni 2011-03-06 12:24:15 +00:00
parent 474d79db17
commit 909b2a22ba
2 changed files with 135 additions and 3 deletions

View file

@ -1,4 +1,114 @@
var Curve = this.Curve = Base.extend({
initialize: function() {
initialize: function(arg0, arg1, arg2, arg3) {
if (arguments.length == 0) {
this._segment1 = new Segment();
this._segment2 = new Segment();
} else if (arguments.length == 1) {
// TODO: If beans are not activated, this won't copy from
// an existing segment. OK?
this._segment1 = new Segment(arg0.segment1);
this._segment2 = new Segment(arg0.segment2);
} else if (arguments.length == 2) {
if (arg0 instanceof Path) {
this._path = arg0;
this._index1 = arg1;
this._updateSegments();
} else {
this._segment1 = new Segment(arg0);
this._segment2 = new Segment(arg1);
}
} else if (arguments.length == 4) {
this._segment1 = new Segment(arg0, null, arg1);
this._segment2 = new Segment(arg3, arg2, null);
}
},
_updateSegments: function() {
if (this._path) {
this._index2 = this._index1 + 1;
// A closing curve?
var segments = this._path._segments;
if (this._index2 >= segments.length)
this._index2 = 0;
this._segment1 = segments[this._index1];
this._segment2 = segments[this._index2];
}
},
getPath: function() {
return this._path;
},
getIndex: function() {
return this._index1;
},
_setIndex: function(index) {
if (this._index1 != index) {
this._index1 = index;
this._updateSegments();
}
},
/**
* The first anchor point of the curve.
*/
getPoint1: function() {
return this._segment1._point;
},
setPoint1: function() {
var point = Point.read(arguments);
this._segment1._point.set(point.x, point.y);
},
/**
* The second anchor point of the curve.
*/
getPoint2: function() {
return this._segment2._point;
},
setPoint2: function() {
var point = Point.read(arguments);
this._segment2._point.set(point.x, point.y);
},
/**
* The handle point that describes the tangent in the first anchor point.
*/
getHandle1: function() {
return this._segment1._handleOut;
},
setHandle1: function() {
var point = Point.read(arguments);
this._segment1._handleOut.set(point.x, point.y);
},
/**
* The handle point that describes the tangent in the second anchor point.
*/
getHandle2: function() {
return this._segment2._handleIn;
},
setHandle2: function() {
var point = Point.read(arguments);
this._segment2._handleIn.set(point.x, point.y);
},
/**
* The first segment of the curve.
*/
getSegment1: function() {
return this._segment1;
},
/**
* The second segment of the curve.
*/
getSegment2: function() {
return this._segment2;
}
});

View file

@ -33,6 +33,28 @@ var Path = this.Path = PathItem.extend({
}
},
/**
* The curves contained within the path.
*/
getCurves: function() {
var length = this._segments.length;
// Reduce length by one if it's an open path:
if (!this.closed && length > 0)
length--;
var curves = this._curves = this._curves || new Array(length);
curves.length = length;
for (var i = 0; i < length; i++) {
var curve = curves[i];
if (!curve) {
curve = curves[i] = new Curve(this, i);
} else {
// Make sure index is kept up to date.
curve._setIndex(i);
}
}
return curves;
},
// TODO: Add back to Scriptographer:
getFirstSegment: function() {
@ -134,9 +156,9 @@ var Path = this.Path = PathItem.extend({
_add: function(segment, index) {
// If this segment belongs to another path already, clone it before
// adding.
if (segment.path)
if (segment._path)
segment = new Segment(segment);
segment.path = this;
segment._path = this;
if (index == undefined) {
this._segments.push(segment);
} else {