mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-20 22:39:50 -05:00
Clean up ToolHandler code by making many fields private, to better reflect the Java version.
This commit is contained in:
parent
edaaaa4df5
commit
3072eed91d
2 changed files with 51 additions and 40 deletions
|
@ -66,7 +66,7 @@ var ToolEvent = this.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) {
|
||||
|
@ -78,7 +78,7 @@ var ToolEvent = this.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) {
|
||||
|
@ -90,7 +90,7 @@ var ToolEvent = this.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) {
|
||||
|
@ -105,9 +105,9 @@ var ToolEvent = this.ToolEvent = Base.extend({
|
|||
*/
|
||||
getMiddlePoint: function() {
|
||||
// For explanations, see getDelta()
|
||||
if (!this._middlePoint && this.tool.lastPoint) {
|
||||
if (!this._middlePoint && this.tool._lastPoint) {
|
||||
// (point + lastPoint) / 2
|
||||
return this.tool.point.add(this.tool.lastPoint).divide(2);
|
||||
return this.tool._point.add(this.tool._lastPoint).divide(2);
|
||||
}
|
||||
return this.middlePoint;
|
||||
},
|
||||
|
@ -127,8 +127,8 @@ var ToolEvent = this.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...
|
||||
return !this._delta && this.tool.lastPoint
|
||||
? this.tool.point.subtract(this.tool.lastPoint)
|
||||
return !this._delta && this.tool._lastPoint
|
||||
? this.tool._point.subtract(this.tool._lastPoint)
|
||||
: this._delta;
|
||||
},
|
||||
|
||||
|
@ -158,8 +158,8 @@ var ToolEvent = this.ToolEvent = Base.extend({
|
|||
// Return downCount for both mouse down and up, since
|
||||
// the count is the same.
|
||||
return /^mouse(down|up)$/.test(this.type)
|
||||
? this.tool.downCount
|
||||
: this.tool.count;
|
||||
? this.tool._downCount
|
||||
: this.tool._count;
|
||||
},
|
||||
|
||||
setCount: function(count) {
|
||||
|
|
|
@ -20,10 +20,10 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
/**
|
||||
* Initializes the tool's settings, so a new tool can be assigned to it
|
||||
*/
|
||||
initialize: function(handlers) {
|
||||
this.firstMove = true;
|
||||
this.count = 0;
|
||||
this.downCount = 0;
|
||||
initialize: function(handlers, scope) {
|
||||
this._firstMove = true;
|
||||
this._count = 0;
|
||||
this._downCount = 0;
|
||||
for (var i in handlers) {
|
||||
this[i] = handlers[i];
|
||||
}
|
||||
|
@ -47,8 +47,9 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
setMinDistance: function(minDistance) {
|
||||
this._minDistance = minDistance;
|
||||
if (this._minDistance != null && this._maxDistance != null
|
||||
&& this._minDistance > this._maxDistance)
|
||||
&& this._minDistance > this._maxDistance) {
|
||||
this._maxDistance = this._minDistance;
|
||||
}
|
||||
},
|
||||
|
||||
getMaxDistance: function() {
|
||||
|
@ -58,19 +59,21 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
setMaxDistance: function(maxDistance) {
|
||||
this._maxDistance = maxDistance;
|
||||
if (this._minDistance != null && this._maxDistance != null
|
||||
&& this._maxDistance < this._minDistance)
|
||||
&& this._maxDistance < this._minDistance) {
|
||||
this._minDistance = maxDistance;
|
||||
}
|
||||
},
|
||||
|
||||
getFixedDistance: function() {
|
||||
if (this._minDistance == this._maxDistance)
|
||||
if (this._minDistance == this._maxDistance) {
|
||||
return this._minDistance;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
setFixedDistance: function(distance) {
|
||||
this.minDistance = distance;
|
||||
this.maxDistance = distance;
|
||||
this._minDistance = distance;
|
||||
this._maxDistance = distance;
|
||||
},
|
||||
|
||||
updateEvent: function(type, pt, minDistance, maxDistance, start,
|
||||
|
@ -78,38 +81,41 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
if (!start) {
|
||||
if (minDistance != null || maxDistance != null) {
|
||||
var minDist = minDistance != null ? minDistance : 0;
|
||||
var vector = pt.subtract(this.point);
|
||||
var vector = pt.subtract(this._point);
|
||||
var distance = vector.getLength();
|
||||
if (distance < minDist)
|
||||
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)
|
||||
if (distance > maxDist) {
|
||||
pt = this._point.add(vector.normalize(maxDist));
|
||||
} else if (matchMaxDistance) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (needsChange && pt.equals(this.point))
|
||||
if (needsChange && pt.equals(this._point)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.lastPoint = this.point;
|
||||
this.point = pt;
|
||||
this._lastPoint = this._point;
|
||||
this._point = pt;
|
||||
switch (type) {
|
||||
case 'mousedown':
|
||||
this.lastPoint = this.downPoint;
|
||||
this.downPoint = this.point;
|
||||
this.downCount++;
|
||||
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;
|
||||
this._lastPoint = this._downPoint;
|
||||
break;
|
||||
}
|
||||
this.count = start ? 0 : this.count + 1;
|
||||
this._count = start ? 0 : this._count + 1;
|
||||
return true;
|
||||
},
|
||||
|
||||
|
@ -117,8 +123,9 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
switch (type) {
|
||||
case 'mousedown':
|
||||
this.updateEvent(type, pt, null, null, true, false, false);
|
||||
if (this.onMouseDown)
|
||||
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
|
||||
|
@ -133,8 +140,9 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
matchMaxDistance = false;
|
||||
while (this.updateEvent(type, pt, this.minDistance,
|
||||
this.maxDistance, false, needsChange, matchMaxDistance)) {
|
||||
if (this.onMouseDrag)
|
||||
if (this.onMouseDrag) {
|
||||
this.onMouseDrag(new ToolEvent(this, type, event));
|
||||
}
|
||||
needsChange = true;
|
||||
matchMaxDistance = true;
|
||||
}
|
||||
|
@ -142,26 +150,29 @@ var ToolHandler = this.ToolHandler = Base.extend({
|
|||
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)
|
||||
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)
|
||||
if (this.onMouseDrag) {
|
||||
this.onMouseDrag(new ToolEvent(this, type, event));
|
||||
}
|
||||
}
|
||||
this.updateEvent(type, pt, null, this.maxDistance, false,
|
||||
false, false);
|
||||
if (this.onMouseUp)
|
||||
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;
|
||||
this._firstMove = true;
|
||||
break;
|
||||
case 'mousemove':
|
||||
while (this.updateEvent(type, pt, this.minDistance,
|
||||
this.maxDistance, this.firstMove, true, false)) {
|
||||
if (this.onMouseMove)
|
||||
this.maxDistance, this._firstMove, true, false)) {
|
||||
if (this.onMouseMove) {
|
||||
this.onMouseMove(new ToolEvent(this, type, event));
|
||||
this.firstMove = false;
|
||||
}
|
||||
this._firstMove = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue