A whole lot of code clean-up in tool-event handling.

This commit is contained in:
Jürg Lehni 2016-01-13 17:27:45 +01:00
parent 35aabcc2b2
commit 6d768f559a

View file

@ -281,8 +281,9 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
_handleEvent: function(type, event, point) { _handleEvent: function(type, event, point) {
// Update global reference to this scope. // Update global reference to this scope.
paper = this._scope; paper = this._scope;
// Now handle event callbacks var minDistance = this.minDistance,
var // In order for idleInterval drag events to work, we need to not maxDistance = this.maxDistance,
// In order for idleInterval drag events to work, we need to not
// check the first call for a change of position. Subsequent calls // check the first call for a change of position. Subsequent calls
// required by min/maxDistance functionality will require it, // required by min/maxDistance functionality will require it,
// otherwise this might loop endlessly. // otherwise this might loop endlessly.
@ -293,22 +294,27 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
// results. matchMaxDistance controls this. // results. matchMaxDistance controls this.
matchMaxDistance = false, matchMaxDistance = false,
called = false, called = false,
drag = false, tool = this,
tool = this; mouse = {};
// Create a simple lookup object to quickly check for different
// mouse event types.
mouse[type.substr(5)] = true;
function update(start, minDistance, maxDistance) { function update(start, minDistance, maxDistance) {
var toolPoint = tool._point, var toolPoint = tool._point,
pt = point; pt = point;
if (!start) { if (start) {
tool._count = 0;
} else {
tool._count++;
if (minDistance != null || maxDistance != null) { if (minDistance != null || maxDistance != null) {
var minDist = minDistance != null ? minDistance : 0, var vector = pt.subtract(toolPoint),
vector = pt.subtract(toolPoint),
distance = vector.getLength(); distance = vector.getLength();
if (distance < minDist) if (distance < (minDistance || 0))
return false; return false;
// Produce a new point on the way to point if point is // Produce a new point on the way to point if point is
// further away than maxDistance // further away than maxDistance
if (maxDistance != null && maxDistance !== 0) { if (maxDistance) {
if (distance > maxDistance) { if (distance > maxDistance) {
pt = toolPoint.add(vector.normalize(maxDistance)); pt = toolPoint.add(vector.normalize(maxDistance));
} else if (matchMaxDistance) { } else if (matchMaxDistance) {
@ -322,21 +328,17 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
// Make sure mousemove events have lastPoint set even for the first // Make sure mousemove events have lastPoint set even for the first
// move so event.delta is always defined for them. // move so event.delta is always defined for them.
// TODO: Decide whether mousedown also should always have delta set. // TODO: Decide whether mousedown also should always have delta set.
tool._lastPoint = start && type === 'mousemove' ? pt : toolPoint; tool._lastPoint = start && mouse.move ? pt : toolPoint;
tool._point = pt; tool._point = pt;
switch (type) { if (mouse.down) {
case 'mousedown':
tool._lastPoint = tool._downPoint; tool._lastPoint = tool._downPoint;
tool._downPoint = pt; tool._downPoint = pt;
tool._downCount++; tool._downCount++;
break; } else if (mouse.up) {
case 'mouseup':
// Mouse up events return the down point for last point, so // Mouse up events return the down point for last point, so
// delta is spanning over the whole drag. // delta is spanning over the whole drag.
tool._lastPoint = tool._downPoint; tool._lastPoint = tool._downPoint;
break;
} }
tool._count = start ? 0 : tool._count + 1;
return true; return true;
} }
@ -345,29 +347,23 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
tool.emit(type, new ToolEvent(tool, type, event)) || called; tool.emit(type, new ToolEvent(tool, type, event)) || called;
} }
switch (type) { if (mouse.down) {
case 'mousedown':
update(true); update(true);
emit(); emit();
break; } else if (mouse.up) {
case 'mouseup': update(false, null, maxDistance);
update(false, null, this.maxDistance);
emit(); emit();
// Start with new values for 'mousemove' // Start with new values for 'mousemove'
update(true); update(true);
this._firstMove = true; this._firstMove = true;
break; } else {
case 'mousedrag':
// If there is no mousedrag event installed, fall back to mousemove, // If there is no mousedrag event installed, fall back to mousemove,
// with which we share the actual event handling code anyhow. // with which we share the actual event handling code anyhow.
if (!(drag = this.responds(type))) var drag = mouse.drag && this.responds(type);
if (!drag)
type = 'mousemove'; type = 'mousemove';
// Fall through to the shared event handling code below:
/* jshint -W086 */
case 'mousemove':
needsChange = !drag; needsChange = !drag;
while (update(!drag && this._firstMove, this.minDistance, while (update(!drag && this._firstMove, minDistance, maxDistance)) {
this.maxDistance)) {
emit(); emit();
if (drag) { if (drag) {
needsChange = matchMaxDistance = true; needsChange = matchMaxDistance = true;
@ -375,7 +371,6 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
this._firstMove = false; this._firstMove = false;
} }
} }
break;
} }
// Prevent default if mouse event was handled. // Prevent default if mouse event was handled.
if (called) if (called)