mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 23:39:59 -05:00
Merge ToolHandler with Tool and remove ToolHandler.
This commit is contained in:
parent
872fbe1574
commit
8c688bf83d
4 changed files with 152 additions and 171 deletions
|
@ -75,7 +75,6 @@ var sources = [
|
||||||
'src/ui/Key.js',
|
'src/ui/Key.js',
|
||||||
|
|
||||||
'src/tool/ToolEvent.js',
|
'src/tool/ToolEvent.js',
|
||||||
'src/tool/ToolHandler.js',
|
|
||||||
'src/tool/Tool.js',
|
'src/tool/Tool.js',
|
||||||
|
|
||||||
'src/util/CanvasProvider.js',
|
'src/util/CanvasProvider.js',
|
||||||
|
|
|
@ -94,7 +94,6 @@ var paper = new function() {
|
||||||
//#include "ui/Key.js"
|
//#include "ui/Key.js"
|
||||||
|
|
||||||
//#include "tool/ToolEvent.js"
|
//#include "tool/ToolEvent.js"
|
||||||
//#include "tool/ToolHandler.js"
|
|
||||||
//#include "tool/Tool.js"
|
//#include "tool/Tool.js"
|
||||||
|
|
||||||
//#endif // BROWSER
|
//#endif // BROWSER
|
||||||
|
|
154
src/tool/Tool.js
154
src/tool/Tool.js
|
@ -14,7 +14,23 @@
|
||||||
* All rights reserved.
|
* 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}
|
* The fixed time delay between each call to the {@link #onMouseDrag}
|
||||||
* event. Setting this to an interval means 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
|
* @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;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
Loading…
Reference in a new issue