mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Merge Base.readValue() with Base.read() and rename Base.peekValue() to Base.peek().
When called directly on Base, read() reads values, otherwise the specific type of the subclass it is called on.
This commit is contained in:
parent
1b539301ad
commit
2abefee336
8 changed files with 30 additions and 29 deletions
|
@ -36,7 +36,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
|
||||||
// is automatially true, since we're describing an infinite line.
|
// is automatially true, since we're describing an infinite line.
|
||||||
var _point1 = Point.read(arguments),
|
var _point1 = Point.read(arguments),
|
||||||
_point2 = Point.read(arguments),
|
_point2 = Point.read(arguments),
|
||||||
_infinite = Base.readValue(arguments);
|
_infinite = Base.read(arguments);
|
||||||
if (_infinite !== undefined) {
|
if (_infinite !== undefined) {
|
||||||
this.point = _point1;
|
this.point = _point1;
|
||||||
this.vector = _point2.subtract(_point1);
|
this.vector = _point2.subtract(_point1);
|
||||||
|
|
|
@ -77,7 +77,7 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
|
||||||
// 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 wether
|
||||||
// it's a size or a point, then read accordingly
|
// it's a size or a point, then read accordingly
|
||||||
var point = Point.read(arguments),
|
var point = Point.read(arguments),
|
||||||
next = Base.peekValue(arguments);
|
next = Base.peek(arguments);
|
||||||
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) {
|
||||||
|
|
|
@ -110,17 +110,27 @@ this.Base = Base.inject(/** @lends Base# */{
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads arguments of the type of the class on which it is called on
|
* When called on a subclass of Base, it reads arguments of the type of
|
||||||
* from the passed arguments list or array, at the given index, up to
|
* the subclass from the passed arguments list or array, at the given
|
||||||
* the specified length. This is used in argument conversion, e.g. by
|
* index, up to the specified length.
|
||||||
* all basic types (Point, Size, Rectangle) and also higher classes such
|
* When called directly on Base, it reads any value without conversion
|
||||||
* as Color and Segment.
|
* from the apssed arguments list or array.
|
||||||
|
* This is used in argument conversion, e.g. by all basic types (Point,
|
||||||
|
* Size, Rectangle) and also higher classes such as Color and Segment.
|
||||||
* @param {Number} start the index at which to start reading in the list
|
* @param {Number} start the index at which to start reading in the list
|
||||||
* @param {Number} length the amount of elements that can be read
|
* @param {Number} length the amount of elements that can be read
|
||||||
* @param {Boolean} clone controls wether passed objects should be
|
* @param {Boolean} clone controls wether passed objects should be
|
||||||
* cloned if they are already provided in the required type
|
* cloned if they are already provided in the required type
|
||||||
*/
|
*/
|
||||||
read: function(list, start, length, clone, readNull) {
|
read: function(list, start, length, clone, readNull) {
|
||||||
|
// See if it's called directly on Base, and if so, read value and
|
||||||
|
// return without object conversion.
|
||||||
|
if (this === Base) {
|
||||||
|
var value = this.peek(list, start);
|
||||||
|
list._index++;
|
||||||
|
list._read = 1;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
var proto = this.prototype,
|
var proto = this.prototype,
|
||||||
readIndex = proto._readIndex,
|
readIndex = proto._readIndex,
|
||||||
index = start || readIndex && list._index || 0;
|
index = start || readIndex && list._index || 0;
|
||||||
|
@ -151,17 +161,10 @@ this.Base = Base.inject(/** @lends Base# */{
|
||||||
return obj;
|
return obj;
|
||||||
},
|
},
|
||||||
|
|
||||||
peekValue: function(list, start) {
|
peek: function(list, start) {
|
||||||
return list[list._index = start || list._index || 0];
|
return list[list._index = start || list._index || 0];
|
||||||
},
|
},
|
||||||
|
|
||||||
readValue: function(list, start) {
|
|
||||||
var value = this.peekValue(list, start);
|
|
||||||
list._index++;
|
|
||||||
list._read = 1;
|
|
||||||
return value;
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads all readable arguments from the list, handling nested arrays
|
* Reads all readable arguments from the list, handling nested arrays
|
||||||
* seperately.
|
* seperately.
|
||||||
|
|
|
@ -1119,7 +1119,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
|
||||||
*/
|
*/
|
||||||
hitTest: function(point, options) {
|
hitTest: function(point, options) {
|
||||||
point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
options = HitResult.getOptions(Base.readValue(arguments));
|
options = HitResult.getOptions(Base.read(arguments));
|
||||||
// Check if the point is withing roughBounds + tolerance, but only if
|
// Check if the point is withing roughBounds + tolerance, but only if
|
||||||
// this item does not have children, since we'd have to travel up the
|
// this item does not have children, since we'd have to travel up the
|
||||||
// chain already to determine the rough bounds.
|
// chain already to determine the rough bounds.
|
||||||
|
|
|
@ -463,8 +463,6 @@ statics: {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The normal is simply the rotated tangent:
|
// The normal is simply the rotated tangent:
|
||||||
// TODO: Rotate normals the other way in Scriptographer too?
|
|
||||||
// (Depending on orientation, I guess?)
|
|
||||||
return type == 2 ? new Point(y, -x) : new Point(x, y);
|
return type == 2 ? new Point(y, -x) : new Point(x, y);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -574,7 +572,7 @@ statics: {
|
||||||
},
|
},
|
||||||
|
|
||||||
getBounds: function(v) {
|
getBounds: function(v) {
|
||||||
var min = v.slice(0, 2),
|
var min = v.slice(0, 2), // Start with values of point1
|
||||||
max = min.slice(0), // clone
|
max = min.slice(0), // clone
|
||||||
roots = new Array(2);
|
roots = new Array(2);
|
||||||
for (var i = 0; i < 2; i++)
|
for (var i = 0; i < 2; i++)
|
||||||
|
|
|
@ -211,7 +211,7 @@ Path.inject({ statics: new function() {
|
||||||
*/
|
*/
|
||||||
Circle: function(center, radius) {
|
Circle: function(center, radius) {
|
||||||
var _center = Point.read(arguments),
|
var _center = Point.read(arguments),
|
||||||
_radius = Base.readValue(arguments);
|
_radius = Base.read(arguments);
|
||||||
return createEllipse(new Rectangle(_center.subtract(_radius),
|
return createEllipse(new Rectangle(_center.subtract(_radius),
|
||||||
Size.create(_radius * 2, _radius * 2)));
|
Size.create(_radius * 2, _radius * 2)));
|
||||||
},
|
},
|
||||||
|
@ -264,8 +264,8 @@ Path.inject({ statics: new function() {
|
||||||
*/
|
*/
|
||||||
RegularPolygon: function(center, numSides, radius) {
|
RegularPolygon: function(center, numSides, radius) {
|
||||||
var _center = Point.read(arguments),
|
var _center = Point.read(arguments),
|
||||||
_numSides = Base.readValue(arguments),
|
_numSides = Base.read(arguments),
|
||||||
_radius = Base.readValue(arguments),
|
_radius = Base.read(arguments),
|
||||||
path = new Path(),
|
path = new Path(),
|
||||||
step = 360 / _numSides,
|
step = 360 / _numSides,
|
||||||
three = !(_numSides % 3),
|
three = !(_numSides % 3),
|
||||||
|
@ -304,9 +304,9 @@ Path.inject({ statics: new function() {
|
||||||
*/
|
*/
|
||||||
Star: function(center, numPoints, radius1, radius2) {
|
Star: function(center, numPoints, radius1, radius2) {
|
||||||
var _center = Point.read(arguments),
|
var _center = Point.read(arguments),
|
||||||
_numPoints = Base.readValue(arguments) * 2,
|
_numPoints = Base.read(arguments) * 2,
|
||||||
_radius1 = Base.readValue(arguments),
|
_radius1 = Base.read(arguments),
|
||||||
_radius2 = Base.readValue(arguments),
|
_radius2 = Base.read(arguments),
|
||||||
path = new Path(),
|
path = new Path(),
|
||||||
step = 360 / _numPoints,
|
step = 360 / _numPoints,
|
||||||
vector = new Point(0, -1),
|
vector = new Point(0, -1),
|
||||||
|
|
|
@ -1725,7 +1725,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
|
||||||
curveTo: function(through, to, parameter) {
|
curveTo: function(through, to, parameter) {
|
||||||
var _through = Point.read(arguments),
|
var _through = Point.read(arguments),
|
||||||
_to = Point.read(arguments),
|
_to = Point.read(arguments),
|
||||||
t = Base.pick(Base.readValue(arguments), 0.5),
|
t = Base.pick(Base.read(arguments), 0.5),
|
||||||
t1 = 1 - t,
|
t1 = 1 - t,
|
||||||
current = getCurrentSegment(this)._point,
|
current = getCurrentSegment(this)._point,
|
||||||
// handle = (through - (1 - t)^2 * current - t^2 * to) /
|
// handle = (through - (1 - t)^2 * current - t^2 * to) /
|
||||||
|
@ -1746,7 +1746,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
|
||||||
point = Point.read(arguments),
|
point = Point.read(arguments),
|
||||||
// Peek at next value to see if it's clockwise,
|
// Peek at next value to see if it's clockwise,
|
||||||
// with true as default value.
|
// with true as default value.
|
||||||
next = Base.pick(Base.peekValue(arguments), true);
|
next = Base.pick(Base.peek(arguments), true);
|
||||||
if (typeof next === 'boolean') {
|
if (typeof next === 'boolean') {
|
||||||
// arcTo(to, clockwise)
|
// arcTo(to, clockwise)
|
||||||
to = point;
|
to = point;
|
||||||
|
@ -1872,7 +1872,7 @@ statics: {
|
||||||
var coords = new Array(6),
|
var coords = new Array(6),
|
||||||
// Make coordinates for first segment available in prevCoords.
|
// Make coordinates for first segment available in prevCoords.
|
||||||
prevCoords = first._transformCoordinates(matrix, new Array(6), false),
|
prevCoords = first._transformCoordinates(matrix, new Array(6), false),
|
||||||
min = prevCoords.slice(0, 2),
|
min = prevCoords.slice(0, 2), // Start with values of first point
|
||||||
max = min.slice(0), // clone
|
max = min.slice(0), // clone
|
||||||
roots = new Array(2);
|
roots = new Array(2);
|
||||||
|
|
||||||
|
|
|
@ -226,7 +226,7 @@ var Project = this.Project = PaperScopeItem.extend(/** @lends Project# */{
|
||||||
// We don't need to do this here, but it speeds up things since we won't
|
// We don't need to do this here, but it speeds up things since we won't
|
||||||
// repeatetly convert in Item#hitTest() then.
|
// repeatetly convert in Item#hitTest() then.
|
||||||
point = Point.read(arguments);
|
point = Point.read(arguments);
|
||||||
options = HitResult.getOptions(Base.readValue(arguments));
|
options = HitResult.getOptions(Base.read(arguments));
|
||||||
// Loop backwards, so layers that get drawn last are tested first
|
// Loop backwards, so layers that get drawn last are tested first
|
||||||
for (var i = this.layers.length - 1; i >= 0; i--) {
|
for (var i = this.layers.length - 1; i >= 0; i--) {
|
||||||
var res = this.layers[i].hitTest(point, options);
|
var res = this.layers[i].hitTest(point, options);
|
||||||
|
|
Loading…
Reference in a new issue