From 8c688bf83d38cb9c2db609a7c68053e5ff3fd112 Mon Sep 17 00:00:00 2001 From: Jonathan Puckey Date: Mon, 23 May 2011 16:30:18 +0200 Subject: [PATCH] Merge ToolHandler with Tool and remove ToolHandler. --- src/load.js | 1 - src/paper.js | 1 - src/tool/Tool.js | 154 +++++++++++++++++++++++++++++++++++- src/tool/ToolHandler.js | 167 ---------------------------------------- 4 files changed, 152 insertions(+), 171 deletions(-) delete mode 100644 src/tool/ToolHandler.js diff --git a/src/load.js b/src/load.js index f87a61d2..b14b32e6 100644 --- a/src/load.js +++ b/src/load.js @@ -75,7 +75,6 @@ var sources = [ 'src/ui/Key.js', 'src/tool/ToolEvent.js', - 'src/tool/ToolHandler.js', 'src/tool/Tool.js', 'src/util/CanvasProvider.js', diff --git a/src/paper.js b/src/paper.js index cf775d52..ca0d4d0b 100644 --- a/src/paper.js +++ b/src/paper.js @@ -94,7 +94,6 @@ var paper = new function() { //#include "ui/Key.js" //#include "tool/ToolEvent.js" -//#include "tool/ToolHandler.js" //#include "tool/Tool.js" //#endif // BROWSER diff --git a/src/tool/Tool.js b/src/tool/Tool.js index 1b617e1f..ddac2cae 100644 --- a/src/tool/Tool.js +++ b/src/tool/Tool.js @@ -14,7 +14,23 @@ * All rights reserved. */ -var Tool = this.Tool = ToolHandler.extend({ +var Tool = this.Tool = Base.extend({ + /** @lends Tool# */ + + beans: true, + + /** + * Initializes the tool's settings, so a new tool can be assigned to it + */ + initialize: function(handlers, scope) { + this._scope = scope; + this._firstMove = true; + this._count = 0; + this._downCount = 0; + for (var i in handlers) + this[i] = handlers[i]; + }, + /** * The fixed time delay between each call to the {@link #onMouseDrag} * event. Setting this to an interval means the {@link #onMouseDrag} @@ -23,5 +39,139 @@ var Tool = this.Tool = ToolHandler.extend({ * * @return the interval time in milliseconds */ - eventInterval: null + eventInterval: null, + + /** + * The minimum distance the mouse has to drag before firing the onMouseDrag + * event, since the last onMouseDrag event. + */ + getMinDistance: function() { + return this._minDistance; + }, + + setMinDistance: function(minDistance) { + this._minDistance = minDistance; + if (this._minDistance != null && this._maxDistance != null + && this._minDistance > this._maxDistance) { + this._maxDistance = this._minDistance; + } + }, + + getMaxDistance: function() { + return this._maxDistance; + }, + + setMaxDistance: function(maxDistance) { + this._maxDistance = maxDistance; + if (this._minDistance != null && this._maxDistance != null + && this._maxDistance < this._minDistance) { + this._minDistance = maxDistance; + } + }, + + getFixedDistance: function() { + return this._minDistance == this._maxDistance + ? this._minDistance : null; + }, + + setFixedDistance: function(distance) { + this._minDistance = distance; + this._maxDistance = distance; + }, + + updateEvent: function(type, pt, minDistance, maxDistance, start, + needsChange, matchMaxDistance) { + if (!start) { + if (minDistance != null || maxDistance != null) { + var minDist = minDistance != null ? minDistance : 0; + var vector = pt.subtract(this._point); + var distance = vector.getLength(); + if (distance < minDist) + return false; + // Produce a new point on the way to pt if pt is further away + // than maxDistance + var maxDist = maxDistance != null ? maxDistance : 0; + if (maxDist != 0) { + if (distance > maxDist) { + pt = this._point.add(vector.normalize(maxDist)); + } else if (matchMaxDistance) { + return false; + } + } + } + if (needsChange && pt.equals(this._point)) + return false; + } + this._lastPoint = this._point; + this._point = pt; + switch (type) { + case 'mousedown': + this._lastPoint = this._downPoint; + this._downPoint = this._point; + this._downCount++; + break; + case 'mouseup': + // Mouse up events return the down point for last point, so delta is + // spanning over the whole drag. + this._lastPoint = this._downPoint; + break; + } + this._count = start ? 0 : this._count + 1; + return true; + }, + + onHandleEvent: function(type, pt, event) { + paper = this._scope; + switch (type) { + case 'mousedown': + this.updateEvent(type, pt, null, null, true, false, false); + if (this.onMouseDown) + this.onMouseDown(new ToolEvent(this, type, event)); + break; + case 'mousedrag': + // In order for idleInterval drag events to work, we need to not + // check the first call for a change of position. Subsequent calls + // required by min/maxDistance functionality will require it, + // otherwise this might loop endlessly. + var needsChange = false, + // If the mouse is moving faster than maxDistance, do not produce + // events for what is left after the first event is generated in + // case it is shorter than maxDistance, as this would produce weird + // results. matchMaxDistance controls this. + matchMaxDistance = false; + while (this.updateEvent(type, pt, this.minDistance, + this.maxDistance, false, needsChange, matchMaxDistance)) { + if (this.onMouseDrag) + this.onMouseDrag(new ToolEvent(this, type, event)); + needsChange = true; + matchMaxDistance = true; + } + break; + case 'mouseup': + // If the last mouse drag happened in a different place, call mouse + // drag first, then mouse up. + if ((this._point.x != pt.x || this._point.y != pt.y) + && this.updateEvent('mousedrag', pt, this.minDistance, + this.maxDistance, false, false, false)) { + if (this.onMouseDrag) + this.onMouseDrag(new ToolEvent(this, type, event)); + } + this.updateEvent(type, pt, null, this.maxDistance, false, + false, false); + if (this.onMouseUp) + this.onMouseUp(new ToolEvent(this, type, event)); + // Start with new values for 'mousemove' + this.updateEvent(type, pt, null, null, true, false, false); + this._firstMove = true; + break; + case 'mousemove': + while (this.updateEvent(type, pt, this.minDistance, + this.maxDistance, this._firstMove, true, false)) { + if (this.onMouseMove) + this.onMouseMove(new ToolEvent(this, type, event)); + this._firstMove = false; + } + break; + } + } }); diff --git a/src/tool/ToolHandler.js b/src/tool/ToolHandler.js deleted file mode 100644 index c19ecc7f..00000000 --- a/src/tool/ToolHandler.js +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Paper.js - * - * This file is part of Paper.js, a JavaScript Vector Graphics Library, - * based on Scriptographer.org and designed to be largely API compatible. - * http://paperjs.org/ - * http://scriptographer.org/ - * - * Distributed under the MIT license. See LICENSE file for details. - * - * Copyright (c) 2011, Juerg Lehni & Jonathan Puckey - * http://lehni.org/ & http://jonathanpuckey.com/ - * - * All rights reserved. - */ - -var ToolHandler = this.ToolHandler = Base.extend({ - /** @lends ToolHandler# */ - - beans: true, - - /** - * Initializes the tool's settings, so a new tool can be assigned to it - */ - initialize: function(handlers, scope) { - this._scope = scope; - this._firstMove = true; - this._count = 0; - this._downCount = 0; - for (var i in handlers) - this[i] = handlers[i]; - }, - - /** - * The minimum distance the mouse has to drag before firing the onMouseDrag - * event, since the last onMouseDrag event. - */ - getMinDistance: function() { - return this._minDistance; - }, - - setMinDistance: function(minDistance) { - this._minDistance = minDistance; - if (this._minDistance != null && this._maxDistance != null - && this._minDistance > this._maxDistance) { - this._maxDistance = this._minDistance; - } - }, - - getMaxDistance: function() { - return this._maxDistance; - }, - - setMaxDistance: function(maxDistance) { - this._maxDistance = maxDistance; - if (this._minDistance != null && this._maxDistance != null - && this._maxDistance < this._minDistance) { - this._minDistance = maxDistance; - } - }, - - getFixedDistance: function() { - return this._minDistance == this._maxDistance - ? this._minDistance : null; - }, - - setFixedDistance: function(distance) { - this._minDistance = distance; - this._maxDistance = distance; - }, - - updateEvent: function(type, pt, minDistance, maxDistance, start, - needsChange, matchMaxDistance) { - if (!start) { - if (minDistance != null || maxDistance != null) { - var minDist = minDistance != null ? minDistance : 0; - var vector = pt.subtract(this._point); - var distance = vector.getLength(); - if (distance < minDist) - return false; - // Produce a new point on the way to pt if pt is further away - // than maxDistance - var maxDist = maxDistance != null ? maxDistance : 0; - if (maxDist != 0) { - if (distance > maxDist) { - pt = this._point.add(vector.normalize(maxDist)); - } else if (matchMaxDistance) { - return false; - } - } - } - if (needsChange && pt.equals(this._point)) - return false; - } - this._lastPoint = this._point; - this._point = pt; - switch (type) { - case 'mousedown': - this._lastPoint = this._downPoint; - this._downPoint = this._point; - this._downCount++; - break; - case 'mouseup': - // Mouse up events return the down point for last point, so delta is - // spanning over the whole drag. - this._lastPoint = this._downPoint; - break; - } - this._count = start ? 0 : this._count + 1; - return true; - }, - - onHandleEvent: function(type, pt, event) { - paper = this._scope; - switch (type) { - case 'mousedown': - this.updateEvent(type, pt, null, null, true, false, false); - if (this.onMouseDown) - this.onMouseDown(new ToolEvent(this, type, event)); - break; - case 'mousedrag': - // In order for idleInterval drag events to work, we need to not - // check the first call for a change of position. Subsequent calls - // required by min/maxDistance functionality will require it, - // otherwise this might loop endlessly. - var needsChange = false, - // If the mouse is moving faster than maxDistance, do not produce - // events for what is left after the first event is generated in - // case it is shorter than maxDistance, as this would produce weird - // results. matchMaxDistance controls this. - matchMaxDistance = false; - while (this.updateEvent(type, pt, this.minDistance, - this.maxDistance, false, needsChange, matchMaxDistance)) { - if (this.onMouseDrag) - this.onMouseDrag(new ToolEvent(this, type, event)); - needsChange = true; - matchMaxDistance = true; - } - break; - case 'mouseup': - // If the last mouse drag happened in a different place, call mouse - // drag first, then mouse up. - if ((this._point.x != pt.x || this._point.y != pt.y) - && this.updateEvent('mousedrag', pt, this.minDistance, - this.maxDistance, false, false, false)) { - if (this.onMouseDrag) - this.onMouseDrag(new ToolEvent(this, type, event)); - } - this.updateEvent(type, pt, null, this.maxDistance, false, - false, false); - if (this.onMouseUp) - this.onMouseUp(new ToolEvent(this, type, event)); - // Start with new values for 'mousemove' - this.updateEvent(type, pt, null, null, true, false, false); - this._firstMove = true; - break; - case 'mousemove': - while (this.updateEvent(type, pt, this.minDistance, - this.maxDistance, this._firstMove, true, false)) { - if (this.onMouseMove) - this.onMouseMove(new ToolEvent(this, type, event)); - this._firstMove = false; - } - break; - } - } -});