Move Tool#_updateEvent() to #_handleEvent() as private function.

This commit is contained in:
Jürg Lehni 2016-01-13 17:15:39 +01:00
parent be0f6e373f
commit 35aabcc2b2

View file

@ -278,56 +278,67 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
* } * }
*/ */
_updateEvent: function(type, point, minDistance, maxDistance, start, _handleEvent: function(type, event, point) {
needsChange, matchMaxDistance) { // Update global reference to this scope.
paper = this._scope;
// Now handle event callbacks
var // 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.
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,
called = false,
drag = false,
tool = this;
function update(start, minDistance, maxDistance) {
var toolPoint = tool._point,
pt = point;
if (!start) { if (!start) {
if (minDistance != null || maxDistance != null) { if (minDistance != null || maxDistance != null) {
var minDist = minDistance != null ? minDistance : 0, var minDist = minDistance != null ? minDistance : 0,
vector = point.subtract(this._point), vector = pt.subtract(toolPoint),
distance = vector.getLength(); distance = vector.getLength();
if (distance < minDist) if (distance < minDist)
return false; return false;
// Produce a new point on the way to point if point is further // Produce a new point on the way to point if point is
// away than maxDistance // further away than maxDistance
if (maxDistance != null && maxDistance !== 0) { if (maxDistance != null && maxDistance !== 0) {
if (distance > maxDistance) { if (distance > maxDistance) {
point = this._point.add(vector.normalize(maxDistance)); pt = toolPoint.add(vector.normalize(maxDistance));
} else if (matchMaxDistance) { } else if (matchMaxDistance) {
return false; return false;
} }
} }
} }
if (needsChange && point.equals(this._point)) if (needsChange && pt.equals(toolPoint))
return false; return false;
} }
// Make sure mousemove events have lastPoint set even for the first move // Make sure mousemove events have lastPoint set even for the first
// 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.
this._lastPoint = start && type == 'mousemove' ? point : this._point; tool._lastPoint = start && type === 'mousemove' ? pt : toolPoint;
this._point = point; tool._point = pt;
switch (type) { switch (type) {
case 'mousedown': case 'mousedown':
this._lastPoint = this._downPoint; tool._lastPoint = tool._downPoint;
this._downPoint = this._point; tool._downPoint = pt;
this._downCount++; tool._downCount++;
break; break;
case 'mouseup': case 'mouseup':
// Mouse up events return the down point for last point, so delta is // Mouse up events return the down point for last point, so
// spanning over the whole drag. // delta is spanning over the whole drag.
this._lastPoint = this._downPoint; tool._lastPoint = tool._downPoint;
break; break;
} }
this._count = start ? 0 : this._count + 1; tool._count = start ? 0 : tool._count + 1;
return true; return true;
}, }
_handleEvent: function(type, event, point) {
// Update global reference to this scope.
paper = this._scope;
// Now handle event callbacks
var tool = this,
called = false,
drag = false;
function emit() { function emit() {
called = tool.responds(type) && called = tool.responds(type) &&
@ -336,15 +347,14 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
switch (type) { switch (type) {
case 'mousedown': case 'mousedown':
this._updateEvent(type, point, null, null, true, false, false); update(true);
emit(); emit();
break; break;
case 'mouseup': case 'mouseup':
this._updateEvent(type, point, null, this.maxDistance, false, update(false, null, this.maxDistance);
false, false);
emit(); emit();
// Start with new values for 'mousemove' // Start with new values for 'mousemove'
this._updateEvent(type, point, null, null, true, false, false); update(true);
this._firstMove = true; this._firstMove = true;
break; break;
case 'mousedrag': case 'mousedrag':
@ -355,19 +365,9 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
// Fall through to the shared event handling code below: // Fall through to the shared event handling code below:
/* jshint -W086 */ /* jshint -W086 */
case 'mousemove': case 'mousemove':
// In order for idleInterval drag events to work, we need to not needsChange = !drag;
// check the first call for a change of position. Subsequent calls while (update(!drag && this._firstMove, this.minDistance,
// required by min/maxDistance functionality will require it, this.maxDistance)) {
// otherwise this might loop endlessly.
var needsChange = !drag,
// 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, point, this.minDistance,
this.maxDistance, !drag && this._firstMove, needsChange,
matchMaxDistance)) {
emit(); emit();
if (drag) { if (drag) {
needsChange = matchMaxDistance = true; needsChange = matchMaxDistance = true;