paper.js/src/path/Segment.js

138 lines
3.5 KiB
JavaScript
Raw Normal View History

var Segment = this.Segment = Base.extend({
beans: true,
initialize: function(arg0, arg1, arg2, arg3, arg4, arg5) {
if (arguments.length == 0) {
this._point = new Point();
} else if (arguments.length == 1) {
// TODO: If beans are not activated, this won't copy from
// an existing segment. OK?
if (arg0.point) {
this._point = new Point(arg0.point);
this._handleIn = new Point(arg0.handleIn);
this._handleOut = new Point(arg0.handleOut);
2011-02-07 13:28:09 -05:00
} else {
this._point = new Point(arg0);
2011-02-07 13:28:09 -05:00
}
} else if (arguments.length < 6) {
if (arguments.length == 2 && !arg1.x) {
this._point = new Point(arg0, arg1);
2011-02-07 13:28:09 -05:00
} else {
this._point = new Point(arg0);
// Doesn't matter if these arguments exist, it creates 0, 0
// points otherwise
this._handleIn = new Point(arg1);
this._handleOut = new Point(arg2);
2011-02-07 13:28:09 -05:00
}
} else if (arguments.length == 6) {
this._point = new Point(arg0, arg1);
this._handleIn = new Point(arg2, arg3);
this._handleOut = new Point(arg4, arg5);
}
if (!this._handleIn)
this._handleIn = new Point();
if (!this._handleOut)
this._handleOut = new Point();
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
getPoint: function() {
return this._point;
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
setPoint: function() {
// Do not replace the internal object but update it instead, so
// references to it are kept alive.
var point = Point.read(arguments);
this._point.set(point.x, point.y);
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
getHandleIn: function() {
return this._handleIn;
},
2011-02-07 13:28:09 -05:00
setHandleIn: function() {
// See #setPoint:
var point = Point.read(arguments);
this._handleIn.set(point.x, point.y);
// Update corner accordingly
// this.corner = !this._handleIn.isParallel(this._handleOut);
2011-02-07 13:28:09 -05:00
},
getHandleInIfSet: function() {
return this._handleIn.x == 0 && this._handleIn.y == 0
? null : this._handleIn;
},
getHandleOut: function() {
return this._handleOut;
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
setHandleOut: function() {
// See #setPoint:
var point = Point.read(arguments);
this._handleOut.set(point.x, point.y);
// Update corner accordingly
// this.corner = !this._handleIn.isParallel(this._handleOut);
2011-02-07 13:28:09 -05:00
},
getHandleOutIfSet: function() {
return this._handleOut.x == 0 && this._handleOut.y == 0
? null : this._handleOut;
},
getPath: function() {
return this._path;
},
2011-02-07 13:28:09 -05:00
getIndex: function() {
// TODO: Cache and update indices instead of searching?
return this._path ? this._path._segments.indexOf(this) : -1;
2011-02-07 13:28:09 -05:00
},
// TODO:
2011-02-07 13:28:09 -05:00
// getCurve: function() {
// if (this._segments && this._segments.path) {
// var curves = this._segments.path.getCurves();
2011-02-07 13:28:09 -05:00
// // The curves list handles closing curves, so the curves.size
// // is adjusted accordingly. just check to be in the boundaries here:
// return index < curves.length ? curves[index] : null;
// }
// },
2011-02-07 13:28:09 -05:00
getNext: function() {
return this._path && this._path._segments[this.getIndex() + 1] || null;
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
getPrevious: function() {
return this._path && this._path._segments[this.getIndex() - 1] || null;
2011-02-07 13:28:09 -05:00
},
// TODO:
2011-02-07 13:28:09 -05:00
// isSelected: function() {
//
2011-02-07 13:28:09 -05:00
// }
//
// setSelected: function(pt, selected)
2011-02-07 13:28:09 -05:00
reverse: function() {
return new Segment(this._point, this._handleOut, this._handleIn);
2011-02-07 13:28:09 -05:00
},
2011-02-07 13:28:09 -05:00
clone: function() {
return new Segment(this);
},
2011-02-07 13:28:09 -05:00
remove: function() {
if (this._path && this._path._segments)
return !!this._path._segments.splice(this.getIndex(), 1).length;
2011-02-07 13:28:09 -05:00
return false;
},
2011-02-07 13:28:09 -05:00
toString: function() {
return '{ point: ' + this._point
+ (this._handleIn ? ', handleIn: ' + this._handleIn : '')
+ (this._handleOut ? ', handleOut: ' + this._handleOut : '')
2011-02-07 13:28:09 -05:00
+ ' }';
}
});