From 0f21520800293018b4cc29f1938aa9a60222a6bd Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Fri, 11 Feb 2011 14:40:36 +0100 Subject: [PATCH] Implement getters and setters --- src/PathItem.js | 16 +++++++++++++-- src/Point.js | 27 +++++++++++++------------ src/Rectangle.js | 49 +++++++++++++++++++++++----------------------- src/Segment.js | 8 ++++---- src/Tool.js | 20 +++++++++---------- src/ToolEvent.js | 37 ++++++++++++++-------------------- src/ToolHandler.js | 25 +++++++++++------------ 7 files changed, 94 insertions(+), 88 deletions(-) diff --git a/src/PathItem.js b/src/PathItem.js index 0a970e15..6cbe6843 100644 --- a/src/PathItem.js +++ b/src/PathItem.js @@ -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); } }, diff --git a/src/Point.js b/src/Point.js index bbce6b1e..7a4ea7c2 100644 --- a/src/Point.js +++ b/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; diff --git a/src/Rectangle.js b/src/Rectangle.js index 5d348a62..ff27d31a 100644 --- a/src/Rectangle.js +++ b/src/Rectangle.js @@ -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() { diff --git a/src/Segment.js b/src/Segment.js index f95eca0e..2e642576 100644 --- a/src/Segment.js +++ b/src/Segment.js @@ -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; }, diff --git a/src/Tool.js b/src/Tool.js index aa51b2d1..5430f7f0 100644 --- a/src/Tool.js +++ b/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; } }); \ No newline at end of file diff --git a/src/ToolEvent.js b/src/ToolEvent.js index fa221d4d..a7227f33 100644 --- a/src/ToolEvent.js +++ b/src/ToolEvent.js @@ -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({ * */ 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) { diff --git a/src/ToolHandler.js b/src/ToolHandler.js index b9731527..733a64d5 100644 --- a/src/ToolHandler.js +++ b/src/ToolHandler.js @@ -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({ * */ 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; },