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:
Jürg Lehni 2012-12-30 16:07:20 +01:00
parent 1b539301ad
commit 2abefee336
8 changed files with 30 additions and 29 deletions

View file

@ -36,7 +36,7 @@ var Line = this.Line = Base.extend(/** @lends Line# */{
// is automatially true, since we're describing an infinite line.
var _point1 = Point.read(arguments),
_point2 = Point.read(arguments),
_infinite = Base.readValue(arguments);
_infinite = Base.read(arguments);
if (_infinite !== undefined) {
this.point = _point1;
this.vector = _point2.subtract(_point1);

View file

@ -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
// it's a size or a point, then read accordingly
var point = Point.read(arguments),
next = Base.peekValue(arguments);
next = Base.peek(arguments);
this.x = point.x;
this.y = point.y;
if (next && next.x !== undefined) {

View file

@ -110,17 +110,27 @@ this.Base = Base.inject(/** @lends Base# */{
},
/**
* Reads arguments of the type of the class on which it is called on
* from the passed arguments list or array, at the given index, up to
* the specified length. This is used in argument conversion, e.g. by
* all basic types (Point, Size, Rectangle) and also higher classes such
* as Color and Segment.
* When called on a subclass of Base, it reads arguments of the type of
* the subclass from the passed arguments list or array, at the given
* index, up to the specified length.
* When called directly on Base, it reads any value without conversion
* 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} length the amount of elements that can be read
* @param {Boolean} clone controls wether passed objects should be
* cloned if they are already provided in the required type
*/
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,
readIndex = proto._readIndex,
index = start || readIndex && list._index || 0;
@ -151,17 +161,10 @@ this.Base = Base.inject(/** @lends Base# */{
return obj;
},
peekValue: function(list, start) {
peek: function(list, start) {
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
* seperately.

View file

@ -1119,7 +1119,7 @@ var Item = this.Item = Base.extend(Callback, /** @lends Item# */{
*/
hitTest: function(point, options) {
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
// this item does not have children, since we'd have to travel up the
// chain already to determine the rough bounds.

View file

@ -463,8 +463,6 @@ statics: {
}
}
// 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);
},
@ -574,7 +572,7 @@ statics: {
},
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
roots = new Array(2);
for (var i = 0; i < 2; i++)

View file

@ -211,7 +211,7 @@ Path.inject({ statics: new function() {
*/
Circle: function(center, radius) {
var _center = Point.read(arguments),
_radius = Base.readValue(arguments);
_radius = Base.read(arguments);
return createEllipse(new Rectangle(_center.subtract(_radius),
Size.create(_radius * 2, _radius * 2)));
},
@ -264,8 +264,8 @@ Path.inject({ statics: new function() {
*/
RegularPolygon: function(center, numSides, radius) {
var _center = Point.read(arguments),
_numSides = Base.readValue(arguments),
_radius = Base.readValue(arguments),
_numSides = Base.read(arguments),
_radius = Base.read(arguments),
path = new Path(),
step = 360 / _numSides,
three = !(_numSides % 3),
@ -304,9 +304,9 @@ Path.inject({ statics: new function() {
*/
Star: function(center, numPoints, radius1, radius2) {
var _center = Point.read(arguments),
_numPoints = Base.readValue(arguments) * 2,
_radius1 = Base.readValue(arguments),
_radius2 = Base.readValue(arguments),
_numPoints = Base.read(arguments) * 2,
_radius1 = Base.read(arguments),
_radius2 = Base.read(arguments),
path = new Path(),
step = 360 / _numPoints,
vector = new Point(0, -1),

View file

@ -1725,7 +1725,7 @@ var Path = this.Path = PathItem.extend(/** @lends Path# */{
curveTo: function(through, to, parameter) {
var _through = 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,
current = getCurrentSegment(this)._point,
// 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),
// Peek at next value to see if it's clockwise,
// with true as default value.
next = Base.pick(Base.peekValue(arguments), true);
next = Base.pick(Base.peek(arguments), true);
if (typeof next === 'boolean') {
// arcTo(to, clockwise)
to = point;
@ -1872,7 +1872,7 @@ statics: {
var coords = new Array(6),
// Make coordinates for first segment available in prevCoords.
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
roots = new Array(2);

View file

@ -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
// repeatetly convert in Item#hitTest() then.
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
for (var i = this.layers.length - 1; i >= 0; i--) {
var res = this.layers[i].hitTest(point, options);