Fix #981: Make sure event.delta is always calculated correctly.

The first mousemove / mousedrag events wrongly received the delta from the last mouseup event.
This commit is contained in:
Jürg Lehni 2016-02-17 20:53:51 +01:00
parent 623ec73c7e
commit b71e3a44d9
4 changed files with 31 additions and 40 deletions

View file

@ -25,7 +25,7 @@
function onMouseDrag(event) {
// If this is the first drag event,
// add the strokes at the start:
if (event.count == 1) {
if (event.count == 0) {
addStrokes(event.middlePoint, event.delta * -1);
} else {
var step = event.delta / 2;

View file

@ -53,9 +53,9 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
// DOCS: rewrite Tool constructor explanation
initialize: function Tool(props) {
PaperScopeItem.call(this);
this._firstMove = true;
this._count = 0;
this._downCount = -1; // So first is 0
// -1 so first event is 0:
this._moveCount = -1;
this._downCount = -1;
this._set(props);
},
@ -288,11 +288,10 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
paper = this._scope;
// If there is no mousedrag event installed, fall back to mousemove,
// with which we share the actual event handling code anyhow.
var move = mouse.move || mouse.drag && !this.responds(type);
// Make sure type is not 'mousedrag' if we fell back.
if (move)
if (mouse.drag && !this.responds(type))
type = 'mousemove';
var responds = this.responds(type),
var move = mouse.move || mouse.drag,
responds = this.responds(type),
minDistance = this.minDistance,
maxDistance = this.maxDistance,
called = false,
@ -302,14 +301,17 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
// to respect their settings, if necessary.
// Returns true as long as events should be fired, false when the target
// is reached.
function update(start, minDistance, maxDistance) {
var toolPoint = tool._point,
pt = point;
if (start) {
tool._count = 0;
} else {
if (pt.equals(toolPoint))
function update(minDistance, maxDistance) {
var pt = point,
// Set toolPoint to the previous point for moves or downPoint for
// clicks, so mouseup has a delta spanning over the full drag.
// Use the current point if this is the first mousedown, so
// there's always a delta.
toolPoint = move ? tool._point : tool._downPoint || pt;
if (move) {
if (tool._moveCount && pt.equals(toolPoint)) {
return false;
}
if (minDistance != null || maxDistance != null) {
var vector = pt.subtract(toolPoint),
distance = vector.getLength();
@ -322,44 +324,34 @@ var Tool = PaperScopeItem.extend(/** @lends Tool# */{
Math.min(distance, maxDistance)));
}
}
tool._count++;
tool._moveCount++;
}
if (responds) {
tool._point = pt;
tool._lastPoint = move || mouse.drag
// Make sure mousemove events have lastPoint set even for
// the first move so event.delta is always defined for them.
? start && move ? pt : toolPoint
// Set lastPoint to previous downPoint, or current point if
// this is the first mousedown, so there's always a delta.
// This way mouseup has a delta spanning over the full drag.
: tool._downPoint || pt;
}
// Keep track of downPoint regardless of the value of response
tool._lastPoint = toolPoint;
if (mouse.down) {
tool._moveCount = -1;
tool._downPoint = pt;
tool._downCount++;
}
return true;
}
function emit(firstMove) {
function emit() {
if (responds) {
called = tool.emit(type, new ToolEvent(tool, type, event))
|| called;
tool._firstMove = firstMove;
}
}
if (mouse.down) {
update(responds);
emit(false);
update();
emit();
} else if (mouse.up) {
update(false, null, maxDistance);
emit(true);
update(null, maxDistance);
emit();
} else if (responds) {
while (update(this._firstMove, minDistance, maxDistance))
emit(false);
while (update(minDistance, maxDistance))
emit();
}
return called;
}

View file

@ -158,9 +158,8 @@ var ToolEvent = Event.extend(/** @lends ToolEvent# */{
getCount: function() {
// 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;
return this.tool[/^mouse(down|up)$/.test(this.type)
? '_downCount' : '_moveCount'];
},
setCount: function(count) {

View file

@ -1349,7 +1349,7 @@ new function() { // Injection scope for event handling on the browser
// Only start dragging if the mousedown event has not
// prevented the default.
dragItem = !prevented && item;
downPoint = lastPoint = point;
downPoint = point;
} else if (mouse.up) {
// Emulate click / doubleclick, but only on item, not view
if (!prevented && item === downItem) {