Implement getters and setters

This commit is contained in:
Jonathan Puckey 2011-02-11 14:40:36 +01:00
parent 59a4c1183b
commit 0f21520800
7 changed files with 94 additions and 88 deletions

View file

@ -36,6 +36,8 @@ PathItem = Item.extend(new function() {
}; };
return { return {
beans: true,
initialize: function() { initialize: function() {
this.closed = false; this.closed = false;
this.segments = [];//new SegmentList(this); this.segments = [];//new SegmentList(this);
@ -61,6 +63,16 @@ PathItem = Item.extend(new function() {
this.segments.splice(index, 0, new Segment(segment)); 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() { moveTo: function() {
var segment = Segment.read(arguments); var segment = Segment.read(arguments);
if(segment && !this.segments.length) if(segment && !this.segments.length)
@ -79,7 +91,7 @@ PathItem = Item.extend(new function() {
*/ */
cubicCurveTo: function(handle1, handle2, to) { cubicCurveTo: function(handle1, handle2, to) {
// First modify the current segment: // First modify the current segment:
var current = getCurrentSegment(); var current = this.currentSegment;
// Convert to relative values: // Convert to relative values:
current.handleOut.set( current.handleOut.set(
handle1.x - current.point.x, handle1.x - current.point.x,
@ -321,7 +333,7 @@ PathItem = Item.extend(new function() {
} }
} }
if (closed && handleIn != null) { if (closed && handleIn != null) {
var segment = get(0); var segment = this.segments[0];
segment.handleIn = handleIn.subtract(segment.point); segment.handleIn = handleIn.subtract(segment.point);
} }
}, },

View file

@ -1,4 +1,5 @@
var Point = Base.extend({ var Point = Base.extend({
beans: true,
initialize: function() { initialize: function() {
if(arguments.length == 2) { if(arguments.length == 2) {
this.x = arguments[0]; this.x = arguments[0];
@ -10,17 +11,17 @@ var Point = Base.extend({
} else if(first.x !== undefined) { } else if(first.x !== undefined) {
this.x = first.x; this.x = first.x;
this.y = first.y; this.y = first.y;
this.angle = first.angle; this._angle = first.angle;
} else if(first.width !== undefined) { } else if(first.width !== undefined) {
this.x = first.width; this.x = first.width;
this.y = first.height; this.y = first.height;
this.angle = null; this._angle = null;
} else if(first.length !== undefined) { } else if(first.length !== undefined) {
this.x = first[0]; this.x = first[0];
this.y = first.length > 1 ? first[1] : first[0]; this.y = first.length > 1 ? first[1] : first[0];
} else if(typeof first === 'number') { } else if(typeof first === 'number') {
this.x = this.y = first; this.x = this.y = first;
this.angle = null; this._angle = null;
} else { } else {
this.x = this.y = 0; this.x = this.y = 0;
} }
@ -88,8 +89,8 @@ var Point = Base.extend({
setLength: function(length) { setLength: function(length) {
if (this.isZero()) { if (this.isZero()) {
if (this.angle != null) { if (this._angle != null) {
var a = this.angle; var a = this._angle;
this.x = Math.cos(a) * length; this.x = Math.cos(a) * length;
this.y = Math.sin(a) * length; this.y = Math.sin(a) * length;
} else { } else {
@ -98,7 +99,7 @@ var Point = Base.extend({
// y is already 0 // y is already 0
} }
} else { } else {
var scale = length / this.getLength(); var scale = length / this.length;
if (scale == 0.0) { if (scale == 0.0) {
// Calculate angle now, so it will be preserved even when // Calculate angle now, so it will be preserved even when
// x and y are 0 // x and y are 0
@ -112,11 +113,11 @@ var Point = Base.extend({
normalize: function(length) { normalize: function(length) {
if (length === null) if (length === null)
length = 1; length = 1;
var len = this.getLength(); var len = this.length;
var scale = len != 0 ? length / len : 0; var scale = len != 0 ? length / len : 0;
var res = new Point(this.x * scale, this.y * scale); var res = new Point(this.x * scale, this.y * scale);
// Preserve angle. // Preserve angle.
res.angle = this.angle; res._angle = this._angle;
return res; return res;
}, },
@ -146,9 +147,9 @@ var Point = Base.extend({
}, },
setAngle: function(angle) { setAngle: function(angle) {
angle = this.angle = angle * Math.PI / 180; angle = this._angle = angle * Math.PI / 180;
if(!this.isZero()) { if(!this.isZero()) {
var length = this.getLength(); var length = this.length;
this.x = Math.cos(angle) * length; this.x = Math.cos(angle) * length;
this.y = Math.sin(angle) * length; this.y = Math.sin(angle) * length;
} }
@ -158,21 +159,21 @@ var Point = Base.extend({
var angle; var angle;
if(arguments.length) { if(arguments.length) {
var point = Point.read(arguments); var point = Point.read(arguments);
var div = this.getLength() * point.getLength(); var div = this.length * point.length;
if(div == 0) { if(div == 0) {
return NaN; return NaN;
} else { } else {
angle = Math.acos(this.dot(point) / div); angle = Math.acos(this.dot(point) / div);
} }
} else { } else {
angle = this.angle = Math.atan2(this.y, this.x); angle = this._angle = Math.atan2(this.y, this.x);
} }
return angle * 180 / Math.PI; return angle * 180 / Math.PI;
}, },
getDirectedAngle: function() { getDirectedAngle: function() {
var point = Point.read(arguments); var point = Point.read(arguments);
var angle = this.getAngle() - point.getAngle(); var angle = this.angle - point.angle;
var bounds = 180; var bounds = 180;
if(angle < - bounds) { if(angle < - bounds) {
return angle + bounds * 2; return angle + bounds * 2;

View file

@ -1,4 +1,5 @@
Rectangle = Base.extend({ Rectangle = Base.extend({
beans: true,
initialize: function() { initialize: function() {
if(arguments.length == 1) { if(arguments.length == 1) {
var rect = arguments[0]; var rect = arguments[0];
@ -126,83 +127,83 @@ Rectangle = Base.extend({
}, },
getTopLeft: function() { getTopLeft: function() {
return new Point(this.getLeft(), this.getTop()); return new Point(this.left, this.top);
}, },
setTopLeft: function() { setTopLeft: function() {
var topLeft = Point.read(arguments); var topLeft = Point.read(arguments);
this.setLeft(topLeft.x); this.left = topLeft.x;
this.setTop(topLeft.y); this.top = topLeft.y;
}, },
getTopRight: function() { getTopRight: function() {
return new Point(this.getRight(), this.getTop()); return new Point(this.right, this.top);
}, },
setTopRight: function() { setTopRight: function() {
var topRight = Point.read(arguments); var topRight = Point.read(arguments);
this.setRight(topRight.x); this.right = topRight.x;
this.setTop(topRight.y); this.top = topRight.y;
}, },
getBottomLeft: function() { getBottomLeft: function() {
return new Point(this.getLeft(), this.getBottom()); return new Point(this.left, this.bottom);
}, },
setBottomLeft: function() { setBottomLeft: function() {
var bottomLeft = Point.read(arguments); var bottomLeft = Point.read(arguments);
this.setLeft(bottomLeft.x); this.left = bottomLeft.x;
this.setBottom(bottomLeft.y); this.bottom = bottomLeft.y;
}, },
getBottomRight: function() { getBottomRight: function() {
return new Point(this.getRight(), this.getBottom()); return new Point(this.right, this.bottom);
}, },
setBottomRight: function() { setBottomRight: function() {
var bottomRight = Point.read(arguments); var bottomRight = Point.read(arguments);
this.setBottom(bottomRight.y); this.bottom = bottomRight.y;
this.setRight(bottomRight.x); this.right = bottomRight.x;
}, },
getLeftCenter: function() { getLeftCenter: function() {
return new Point(this.getLeft(), this.getCenterY()); return new Point(this.left, this.centerY);
}, },
setLeftCenter: function() { setLeftCenter: function() {
var leftCenter = Point.read(arguments); var leftCenter = Point.read(arguments);
this.setLeft(leftCenter.x); this.left = leftCenter.x;
this.setCenterY(leftCenter.y); this.centerY = leftCenter.y;
}, },
getTopCenter: function() { getTopCenter: function() {
return new Point(this.getCenterX(), this.getTop()); return new Point(this.centerX, this.top);
}, },
setTopCenter: function() { setTopCenter: function() {
var topCenter = Point.read(arguments); var topCenter = Point.read(arguments);
this.setCenterX(topCenter.x); this.centerX = topCenter.x;
this.setTop(topCenter.y); this.top = topCenter.y;
}, },
getRightCenter: function() { getRightCenter: function() {
return new Point(this.getRight(), this.getCenterY()); return new Point(this.right, this.centerY);
}, },
setRightCenter: function() { setRightCenter: function() {
var rightCenter = Point.read(arguments); var rightCenter = Point.read(arguments);
this.setRight(rightCenter.x); this.right = rightCenter.x;
this.setCenterY(rightCenter.y); this.centerY = rightCenter.y;
}, },
getBottomCenter: function() { getBottomCenter: function() {
return new Point(this.getCenterX(), this.getBottom()); return new Point(this.centerX, this.bottom);
}, },
setBottomCenter: function() { setBottomCenter: function() {
var bottomCenter = Point.read(arguments); var bottomCenter = Point.read(arguments);
this.setBottom(bottomCenter.y); this.bottom = bottomCenter.y;
this.setCenterX(bottomCenter.x); this.centerX = bottomCenter.x;
}, },
clone: function() { clone: function() {

View file

@ -75,7 +75,7 @@ Segment = Base.extend({
}, },
getPath: function() { getPath: function() {
return this.path; return this._path;
}, },
// todo // todo
@ -89,13 +89,13 @@ Segment = Base.extend({
// }, // },
getNext: function() { getNext: function() {
var index = this.getIndex(); var index = this.index;
return this.path && index < this.path.segments.length - 1 return this.path && index < this.path.segments.length - 1
? this.path.segments[index + 1] : null; ? this.path.segments[index + 1] : null;
}, },
getPrevious: function() { 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 // todo
@ -115,7 +115,7 @@ Segment = Base.extend({
remove: function() { remove: function() {
if(this.segments) if(this.segments)
return this.path.segments.unshift(this.getIndex(), 1); return this.path.segments.unshift(this.index, 1);
return false; return false;
}, },

View file

@ -1,30 +1,30 @@
Tool = ToolHandler.extend({ Tool = ToolHandler.extend({
beans: true,
initialize: function(handlers, doc) { initialize: function(handlers, doc) {
this.base(handlers); this.base(handlers);
this.setEventInterval(-1);
}, },
setDocument: function(doc) { setDocument: function(doc) {
this.document = doc; this._document = doc;
var that = this; var that = this;
$(doc.canvas).addEvents({ $(doc.canvas).addEvents({
mousedown: function(e) { mousedown: function(e) {
that.onHandleEvent('MOUSE_DOWN', new Point(e.offset), null, null); that.onHandleEvent('MOUSE_DOWN', new Point(e.offset), null, null);
that.document.redraw(); that._document.redraw();
}, },
drag: function(e) { drag: function(e) {
that.onHandleEvent('MOUSE_DRAG', new Point(e.offset), null, null); that.onHandleEvent('MOUSE_DRAG', new Point(e.offset), null, null);
that.document.redraw(); that._document.redraw();
}, },
mouseup: function(e) { mouseup: function(e) {
that.onHandleEvent('MOUSE_UP', new Point(e.offset), null, null); 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 * event. Setting this to an interval means the {@link #onMouseDrag} event
* is called repeatedly after the initial {@link #onMouseDown} until the * is called repeatedly after the initial {@link #onMouseDown} until the
* user releases the mouse. * user releases the mouse.
@ -38,11 +38,9 @@ Tool = ToolHandler.extend({
* *
* @return the interval time in milliseconds * @return the interval time in milliseconds
*/ */
getEventInterval: function() { eventInterval: -1,
return this.eventInterval;
},
setEventInterval: function(interval) { getDocument: function() {
this.eventInterval = interval; return this._document;
} }
}); });

View file

@ -45,6 +45,7 @@
* @author lehni * @author lehni
*/ */
ToolEvent = Base.extend({ ToolEvent = Base.extend({
beans: true,
initialize: function(tool, type, modifiers) { initialize: function(tool, type, modifiers) {
// super(modifiers); // super(modifiers);
this.tool = tool; this.tool = tool;
@ -53,9 +54,9 @@ ToolEvent = Base.extend({
toString: function() { toString: function() {
return '{ type: ' + type return '{ type: ' + type
+ ', point: ' + this.getPoint() + ', point: ' + this.point
+ ', count: ' + this.getCount() + ', count: ' + this.count
+ ', modifiers: ' + this.getModifiers() + ', modifiers: ' + this.modifiers
+ ' }'; + ' }';
}, },
@ -89,11 +90,11 @@ ToolEvent = Base.extend({
* </code> * </code>
*/ */
getPoint: function() { getPoint: function() {
return this.choosePoint(this.point, this.tool.point); return this.choosePoint(this._point, this.tool.point);
}, },
setPoint: function(point) { setPoint: function(point) {
this.point = point; this._point = point;
}, },
/** /**
@ -101,11 +102,11 @@ ToolEvent = Base.extend({
* event was fired. * event was fired.
*/ */
getLastPoint: function() { getLastPoint: function() {
return this.choosePoint(this.lastPoint, this.tool.lastPoint); return this.choosePoint(this._lastPoint, this.tool.lastPoint);
}, },
setLastPoint: function(lastPoint) { setLastPoint: function(lastPoint) {
this.lastPoint = lastPoint; this._lastPoint = lastPoint;
}, },
/** /**
@ -113,11 +114,11 @@ ToolEvent = Base.extend({
* was last clicked. * was last clicked.
*/ */
getDownPoint: function() { getDownPoint: function() {
return this.choosePoint(this.downPoint, this.tool.downPoint); return this.choosePoint(this._downPoint, this.tool.downPoint);
}, },
setDownPoint: function(downPoint) { setDownPoint: function(downPoint) {
this.downPoint = downPoint; this._downPoint = downPoint;
}, },
/** /**
@ -128,7 +129,7 @@ ToolEvent = Base.extend({
*/ */
getMiddlePoint: function() { getMiddlePoint: function() {
// For explanations, see getDelta() // For explanations, see getDelta()
if (this.middlePoint == null && this.tool.lastPoint != null) { if (this._middlePoint == null && this.tool.lastPoint != null) {
// (point + lastPoint) / 2 // (point + lastPoint) / 2
return this.tool.point.add(this.tool.lastPoint).divide(2); return this.tool.point.add(this.tool.lastPoint).divide(2);
} }
@ -136,7 +137,7 @@ ToolEvent = Base.extend({
}, },
setMiddlePoint: function(middlePoint) { 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 // Instead, keep calculating the delta each time, so the result can be
// directly modified by the script without changing the internal values. // directly modified by the script without changing the internal values.
// We could cache this and use clone, but this is almost as fast... // 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.tool.point.subtract(this.tool.lastPoint);
} }
return this.delta; return this._delta;
}, },
setDelta: function(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 // TODO: implement hitTest first
// getItem: function() { // getItem: function() {
// if (this.item == null) { // if (this.item == null) {

View file

@ -1,4 +1,5 @@
ToolHandler = Base.extend({ ToolHandler = Base.extend({
beans: true,
/** /**
* Initializes the tool's settings, so a new tool can be assigned to it * Initializes the tool's settings, so a new tool can be assigned to it
*/ */
@ -24,30 +25,30 @@ ToolHandler = Base.extend({
* </code> * </code>
*/ */
getMinDistance: function() { getMinDistance: function() {
return this.minDistance; return this._minDistance;
}, },
setMinDistance: function(minDistance) { setMinDistance: function(minDistance) {
this.minDistance = minDistance; this._minDistance = minDistance;
if (this.minDistance != null && this.maxDistance != null if (this._minDistance != null && this._maxDistance != null
&& this.minDistance > this.maxDistance) && this._minDistance > this._maxDistance)
this.maxDistance = this.minDistance; this._maxDistance = this._minDistance;
}, },
getMaxDistance: function() { getMaxDistance: function() {
return this.maxDistance; return this._maxDistance;
}, },
setMaxDistance: function(maxDistance) { setMaxDistance: function(maxDistance) {
this.maxDistance = maxDistance; this._maxDistance = maxDistance;
if (this.minDistance != null && this.maxDistance != null if (this._minDistance != null && this._maxDistance != null
&& this.maxDistance < this.minDistance) && this._maxDistance < this._minDistance)
this.minDistance = maxDistance; this._minDistance = maxDistance;
}, },
getFixedDistance: function() { getFixedDistance: function() {
if (this.minDistance != null && this.minDistance.equals(this.maxDistance)) if (this._minDistance != null && this._minDistance.equals(this._maxDistance))
return this.minDistance; return this._minDistance;
return null; return null;
}, },