mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2024-12-28 17:02:24 -05:00
Implement getters and setters
This commit is contained in:
parent
59a4c1183b
commit
0f21520800
7 changed files with 94 additions and 88 deletions
|
@ -36,6 +36,8 @@ PathItem = Item.extend(new function() {
|
|||
};
|
||||
|
||||
return {
|
||||
beans: true,
|
||||
|
||||
initialize: function() {
|
||||
this.closed = false;
|
||||
this.segments = [];//new SegmentList(this);
|
||||
|
@ -61,6 +63,16 @@ PathItem = Item.extend(new function() {
|
|||
this.segments.splice(index, 0, new Segment(segment));
|
||||
},
|
||||
|
||||
/**
|
||||
* Helper method that returns the current segment and checks if we need to
|
||||
* execute a moveTo() command first.
|
||||
*/
|
||||
getCurrentSegment: function() {
|
||||
if (this.segments.length == 0)
|
||||
throw('Use a moveTo() command first');
|
||||
return this.segments[this.segments.length - 1];
|
||||
},
|
||||
|
||||
moveTo: function() {
|
||||
var segment = Segment.read(arguments);
|
||||
if(segment && !this.segments.length)
|
||||
|
@ -79,7 +91,7 @@ PathItem = Item.extend(new function() {
|
|||
*/
|
||||
cubicCurveTo: function(handle1, handle2, to) {
|
||||
// First modify the current segment:
|
||||
var current = getCurrentSegment();
|
||||
var current = this.currentSegment;
|
||||
// Convert to relative values:
|
||||
current.handleOut.set(
|
||||
handle1.x - current.point.x,
|
||||
|
@ -321,7 +333,7 @@ PathItem = Item.extend(new function() {
|
|||
}
|
||||
}
|
||||
if (closed && handleIn != null) {
|
||||
var segment = get(0);
|
||||
var segment = this.segments[0];
|
||||
segment.handleIn = handleIn.subtract(segment.point);
|
||||
}
|
||||
},
|
||||
|
|
27
src/Point.js
27
src/Point.js
|
@ -1,4 +1,5 @@
|
|||
var Point = Base.extend({
|
||||
beans: true,
|
||||
initialize: function() {
|
||||
if(arguments.length == 2) {
|
||||
this.x = arguments[0];
|
||||
|
@ -10,17 +11,17 @@ var Point = Base.extend({
|
|||
} else if(first.x !== undefined) {
|
||||
this.x = first.x;
|
||||
this.y = first.y;
|
||||
this.angle = first.angle;
|
||||
this._angle = first.angle;
|
||||
} else if(first.width !== undefined) {
|
||||
this.x = first.width;
|
||||
this.y = first.height;
|
||||
this.angle = null;
|
||||
this._angle = null;
|
||||
} else if(first.length !== undefined) {
|
||||
this.x = first[0];
|
||||
this.y = first.length > 1 ? first[1] : first[0];
|
||||
} else if(typeof first === 'number') {
|
||||
this.x = this.y = first;
|
||||
this.angle = null;
|
||||
this._angle = null;
|
||||
} else {
|
||||
this.x = this.y = 0;
|
||||
}
|
||||
|
@ -88,8 +89,8 @@ var Point = Base.extend({
|
|||
|
||||
setLength: function(length) {
|
||||
if (this.isZero()) {
|
||||
if (this.angle != null) {
|
||||
var a = this.angle;
|
||||
if (this._angle != null) {
|
||||
var a = this._angle;
|
||||
this.x = Math.cos(a) * length;
|
||||
this.y = Math.sin(a) * length;
|
||||
} else {
|
||||
|
@ -98,7 +99,7 @@ var Point = Base.extend({
|
|||
// y is already 0
|
||||
}
|
||||
} else {
|
||||
var scale = length / this.getLength();
|
||||
var scale = length / this.length;
|
||||
if (scale == 0.0) {
|
||||
// Calculate angle now, so it will be preserved even when
|
||||
// x and y are 0
|
||||
|
@ -112,11 +113,11 @@ var Point = Base.extend({
|
|||
normalize: function(length) {
|
||||
if (length === null)
|
||||
length = 1;
|
||||
var len = this.getLength();
|
||||
var len = this.length;
|
||||
var scale = len != 0 ? length / len : 0;
|
||||
var res = new Point(this.x * scale, this.y * scale);
|
||||
// Preserve angle.
|
||||
res.angle = this.angle;
|
||||
res._angle = this._angle;
|
||||
return res;
|
||||
},
|
||||
|
||||
|
@ -146,9 +147,9 @@ var Point = Base.extend({
|
|||
},
|
||||
|
||||
setAngle: function(angle) {
|
||||
angle = this.angle = angle * Math.PI / 180;
|
||||
angle = this._angle = angle * Math.PI / 180;
|
||||
if(!this.isZero()) {
|
||||
var length = this.getLength();
|
||||
var length = this.length;
|
||||
this.x = Math.cos(angle) * length;
|
||||
this.y = Math.sin(angle) * length;
|
||||
}
|
||||
|
@ -158,21 +159,21 @@ var Point = Base.extend({
|
|||
var angle;
|
||||
if(arguments.length) {
|
||||
var point = Point.read(arguments);
|
||||
var div = this.getLength() * point.getLength();
|
||||
var div = this.length * point.length;
|
||||
if(div == 0) {
|
||||
return NaN;
|
||||
} else {
|
||||
angle = Math.acos(this.dot(point) / div);
|
||||
}
|
||||
} else {
|
||||
angle = this.angle = Math.atan2(this.y, this.x);
|
||||
angle = this._angle = Math.atan2(this.y, this.x);
|
||||
}
|
||||
return angle * 180 / Math.PI;
|
||||
},
|
||||
|
||||
getDirectedAngle: function() {
|
||||
var point = Point.read(arguments);
|
||||
var angle = this.getAngle() - point.getAngle();
|
||||
var angle = this.angle - point.angle;
|
||||
var bounds = 180;
|
||||
if(angle < - bounds) {
|
||||
return angle + bounds * 2;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
Rectangle = Base.extend({
|
||||
beans: true,
|
||||
initialize: function() {
|
||||
if(arguments.length == 1) {
|
||||
var rect = arguments[0];
|
||||
|
@ -126,83 +127,83 @@ Rectangle = Base.extend({
|
|||
},
|
||||
|
||||
getTopLeft: function() {
|
||||
return new Point(this.getLeft(), this.getTop());
|
||||
return new Point(this.left, this.top);
|
||||
},
|
||||
|
||||
setTopLeft: function() {
|
||||
var topLeft = Point.read(arguments);
|
||||
this.setLeft(topLeft.x);
|
||||
this.setTop(topLeft.y);
|
||||
this.left = topLeft.x;
|
||||
this.top = topLeft.y;
|
||||
},
|
||||
|
||||
getTopRight: function() {
|
||||
return new Point(this.getRight(), this.getTop());
|
||||
return new Point(this.right, this.top);
|
||||
},
|
||||
|
||||
setTopRight: function() {
|
||||
var topRight = Point.read(arguments);
|
||||
this.setRight(topRight.x);
|
||||
this.setTop(topRight.y);
|
||||
this.right = topRight.x;
|
||||
this.top = topRight.y;
|
||||
},
|
||||
|
||||
getBottomLeft: function() {
|
||||
return new Point(this.getLeft(), this.getBottom());
|
||||
return new Point(this.left, this.bottom);
|
||||
},
|
||||
|
||||
setBottomLeft: function() {
|
||||
var bottomLeft = Point.read(arguments);
|
||||
this.setLeft(bottomLeft.x);
|
||||
this.setBottom(bottomLeft.y);
|
||||
this.left = bottomLeft.x;
|
||||
this.bottom = bottomLeft.y;
|
||||
},
|
||||
|
||||
getBottomRight: function() {
|
||||
return new Point(this.getRight(), this.getBottom());
|
||||
return new Point(this.right, this.bottom);
|
||||
},
|
||||
|
||||
setBottomRight: function() {
|
||||
var bottomRight = Point.read(arguments);
|
||||
this.setBottom(bottomRight.y);
|
||||
this.setRight(bottomRight.x);
|
||||
this.bottom = bottomRight.y;
|
||||
this.right = bottomRight.x;
|
||||
},
|
||||
|
||||
getLeftCenter: function() {
|
||||
return new Point(this.getLeft(), this.getCenterY());
|
||||
return new Point(this.left, this.centerY);
|
||||
},
|
||||
|
||||
setLeftCenter: function() {
|
||||
var leftCenter = Point.read(arguments);
|
||||
this.setLeft(leftCenter.x);
|
||||
this.setCenterY(leftCenter.y);
|
||||
this.left = leftCenter.x;
|
||||
this.centerY = leftCenter.y;
|
||||
},
|
||||
|
||||
getTopCenter: function() {
|
||||
return new Point(this.getCenterX(), this.getTop());
|
||||
return new Point(this.centerX, this.top);
|
||||
},
|
||||
|
||||
setTopCenter: function() {
|
||||
var topCenter = Point.read(arguments);
|
||||
this.setCenterX(topCenter.x);
|
||||
this.setTop(topCenter.y);
|
||||
this.centerX = topCenter.x;
|
||||
this.top = topCenter.y;
|
||||
},
|
||||
|
||||
getRightCenter: function() {
|
||||
return new Point(this.getRight(), this.getCenterY());
|
||||
return new Point(this.right, this.centerY);
|
||||
},
|
||||
|
||||
setRightCenter: function() {
|
||||
var rightCenter = Point.read(arguments);
|
||||
this.setRight(rightCenter.x);
|
||||
this.setCenterY(rightCenter.y);
|
||||
this.right = rightCenter.x;
|
||||
this.centerY = rightCenter.y;
|
||||
},
|
||||
|
||||
getBottomCenter: function() {
|
||||
return new Point(this.getCenterX(), this.getBottom());
|
||||
return new Point(this.centerX, this.bottom);
|
||||
},
|
||||
|
||||
setBottomCenter: function() {
|
||||
var bottomCenter = Point.read(arguments);
|
||||
this.setBottom(bottomCenter.y);
|
||||
this.setCenterX(bottomCenter.x);
|
||||
this.bottom = bottomCenter.y;
|
||||
this.centerX = bottomCenter.x;
|
||||
},
|
||||
|
||||
clone: function() {
|
||||
|
|
|
@ -75,7 +75,7 @@ Segment = Base.extend({
|
|||
},
|
||||
|
||||
getPath: function() {
|
||||
return this.path;
|
||||
return this._path;
|
||||
},
|
||||
|
||||
// todo
|
||||
|
@ -89,13 +89,13 @@ Segment = Base.extend({
|
|||
// },
|
||||
|
||||
getNext: function() {
|
||||
var index = this.getIndex();
|
||||
var index = this.index;
|
||||
return this.path && index < this.path.segments.length - 1
|
||||
? this.path.segments[index + 1] : null;
|
||||
},
|
||||
|
||||
getPrevious: function() {
|
||||
return this.path != null && index > 0 ? this.segments[this.getIndex() - 1] : null;
|
||||
return this.path != null && index > 0 ? this.segments[this.index - 1] : null;
|
||||
},
|
||||
|
||||
// todo
|
||||
|
@ -115,7 +115,7 @@ Segment = Base.extend({
|
|||
|
||||
remove: function() {
|
||||
if(this.segments)
|
||||
return this.path.segments.unshift(this.getIndex(), 1);
|
||||
return this.path.segments.unshift(this.index, 1);
|
||||
return false;
|
||||
},
|
||||
|
||||
|
|
20
src/Tool.js
20
src/Tool.js
|
@ -1,30 +1,30 @@
|
|||
Tool = ToolHandler.extend({
|
||||
beans: true,
|
||||
initialize: function(handlers, doc) {
|
||||
this.base(handlers);
|
||||
this.setEventInterval(-1);
|
||||
},
|
||||
|
||||
setDocument: function(doc) {
|
||||
this.document = doc;
|
||||
this._document = doc;
|
||||
var that = this;
|
||||
$(doc.canvas).addEvents({
|
||||
mousedown: function(e) {
|
||||
that.onHandleEvent('MOUSE_DOWN', new Point(e.offset), null, null);
|
||||
that.document.redraw();
|
||||
that._document.redraw();
|
||||
},
|
||||
drag: function(e) {
|
||||
that.onHandleEvent('MOUSE_DRAG', new Point(e.offset), null, null);
|
||||
that.document.redraw();
|
||||
that._document.redraw();
|
||||
},
|
||||
mouseup: function(e) {
|
||||
that.onHandleEvent('MOUSE_UP', new Point(e.offset), null, null);
|
||||
that.document.redraw();
|
||||
that._document.redraw();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the fixed time delay between each call to the {@link #onMouseDrag}
|
||||
* The fixed time delay between each call to the {@link #onMouseDrag}
|
||||
* event. Setting this to an interval means the {@link #onMouseDrag} event
|
||||
* is called repeatedly after the initial {@link #onMouseDown} until the
|
||||
* user releases the mouse.
|
||||
|
@ -38,11 +38,9 @@ Tool = ToolHandler.extend({
|
|||
*
|
||||
* @return the interval time in milliseconds
|
||||
*/
|
||||
getEventInterval: function() {
|
||||
return this.eventInterval;
|
||||
},
|
||||
eventInterval: -1,
|
||||
|
||||
setEventInterval: function(interval) {
|
||||
this.eventInterval = interval;
|
||||
getDocument: function() {
|
||||
return this._document;
|
||||
}
|
||||
});
|
|
@ -45,6 +45,7 @@
|
|||
* @author lehni
|
||||
*/
|
||||
ToolEvent = Base.extend({
|
||||
beans: true,
|
||||
initialize: function(tool, type, modifiers) {
|
||||
// super(modifiers);
|
||||
this.tool = tool;
|
||||
|
@ -53,9 +54,9 @@ ToolEvent = Base.extend({
|
|||
|
||||
toString: function() {
|
||||
return '{ type: ' + type
|
||||
+ ', point: ' + this.getPoint()
|
||||
+ ', count: ' + this.getCount()
|
||||
+ ', modifiers: ' + this.getModifiers()
|
||||
+ ', point: ' + this.point
|
||||
+ ', count: ' + this.count
|
||||
+ ', modifiers: ' + this.modifiers
|
||||
+ ' }';
|
||||
},
|
||||
|
||||
|
@ -89,11 +90,11 @@ ToolEvent = Base.extend({
|
|||
* </code>
|
||||
*/
|
||||
getPoint: function() {
|
||||
return this.choosePoint(this.point, this.tool.point);
|
||||
return this.choosePoint(this._point, this.tool.point);
|
||||
},
|
||||
|
||||
setPoint: function(point) {
|
||||
this.point = point;
|
||||
this._point = point;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -101,11 +102,11 @@ ToolEvent = Base.extend({
|
|||
* event was fired.
|
||||
*/
|
||||
getLastPoint: function() {
|
||||
return this.choosePoint(this.lastPoint, this.tool.lastPoint);
|
||||
return this.choosePoint(this._lastPoint, this.tool.lastPoint);
|
||||
},
|
||||
|
||||
setLastPoint: function(lastPoint) {
|
||||
this.lastPoint = lastPoint;
|
||||
this._lastPoint = lastPoint;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -113,11 +114,11 @@ ToolEvent = Base.extend({
|
|||
* was last clicked.
|
||||
*/
|
||||
getDownPoint: function() {
|
||||
return this.choosePoint(this.downPoint, this.tool.downPoint);
|
||||
return this.choosePoint(this._downPoint, this.tool.downPoint);
|
||||
},
|
||||
|
||||
setDownPoint: function(downPoint) {
|
||||
this.downPoint = downPoint;
|
||||
this._downPoint = downPoint;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -128,7 +129,7 @@ ToolEvent = Base.extend({
|
|||
*/
|
||||
getMiddlePoint: function() {
|
||||
// For explanations, see getDelta()
|
||||
if (this.middlePoint == null && this.tool.lastPoint != null) {
|
||||
if (this._middlePoint == null && this.tool.lastPoint != null) {
|
||||
// (point + lastPoint) / 2
|
||||
return this.tool.point.add(this.tool.lastPoint).divide(2);
|
||||
}
|
||||
|
@ -136,7 +137,7 @@ ToolEvent = Base.extend({
|
|||
},
|
||||
|
||||
setMiddlePoint: function(middlePoint) {
|
||||
this.middlePoint = middlePoint;
|
||||
this._middlePoint = middlePoint;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -150,14 +151,14 @@ ToolEvent = Base.extend({
|
|||
// Instead, keep calculating the delta each time, so the result can be
|
||||
// directly modified by the script without changing the internal values.
|
||||
// We could cache this and use clone, but this is almost as fast...
|
||||
if (this.delta == null && this.tool.lastPoint != null) {
|
||||
if (this._delta == null && this.tool.lastPoint != null) {
|
||||
return this.tool.point.subtract(this.tool.lastPoint);
|
||||
}
|
||||
return this.delta;
|
||||
return this._delta;
|
||||
},
|
||||
|
||||
setDelta: function(delta) {
|
||||
this.delta = delta;
|
||||
this._delta = delta;
|
||||
},
|
||||
|
||||
/**
|
||||
|
@ -202,14 +203,6 @@ ToolEvent = Base.extend({
|
|||
}
|
||||
},
|
||||
|
||||
getType: function() {
|
||||
return this.type;
|
||||
},
|
||||
|
||||
setType: function(type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
// TODO: implement hitTest first
|
||||
// getItem: function() {
|
||||
// if (this.item == null) {
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
ToolHandler = Base.extend({
|
||||
beans: true,
|
||||
/**
|
||||
* Initializes the tool's settings, so a new tool can be assigned to it
|
||||
*/
|
||||
|
@ -24,30 +25,30 @@ ToolHandler = Base.extend({
|
|||
* </code>
|
||||
*/
|
||||
getMinDistance: function() {
|
||||
return this.minDistance;
|
||||
return this._minDistance;
|
||||
},
|
||||
|
||||
setMinDistance: function(minDistance) {
|
||||
this.minDistance = minDistance;
|
||||
if (this.minDistance != null && this.maxDistance != null
|
||||
&& this.minDistance > this.maxDistance)
|
||||
this.maxDistance = this.minDistance;
|
||||
this._minDistance = minDistance;
|
||||
if (this._minDistance != null && this._maxDistance != null
|
||||
&& this._minDistance > this._maxDistance)
|
||||
this._maxDistance = this._minDistance;
|
||||
},
|
||||
|
||||
getMaxDistance: function() {
|
||||
return this.maxDistance;
|
||||
return this._maxDistance;
|
||||
},
|
||||
|
||||
setMaxDistance: function(maxDistance) {
|
||||
this.maxDistance = maxDistance;
|
||||
if (this.minDistance != null && this.maxDistance != null
|
||||
&& this.maxDistance < this.minDistance)
|
||||
this.minDistance = maxDistance;
|
||||
this._maxDistance = maxDistance;
|
||||
if (this._minDistance != null && this._maxDistance != null
|
||||
&& this._maxDistance < this._minDistance)
|
||||
this._minDistance = maxDistance;
|
||||
},
|
||||
|
||||
getFixedDistance: function() {
|
||||
if (this.minDistance != null && this.minDistance.equals(this.maxDistance))
|
||||
return this.minDistance;
|
||||
if (this._minDistance != null && this._minDistance.equals(this._maxDistance))
|
||||
return this._minDistance;
|
||||
return null;
|
||||
},
|
||||
|
||||
|
|
Loading…
Reference in a new issue