Have Tool#onHandleEvent() report back if a callback was called or not, and use that to decide if View#draw() should be called, passing true as the to be implemented checkRedraw parameter.

This commit is contained in:
Jürg Lehni 2011-06-19 23:02:02 +01:00
parent 7a90f9260e
commit 2bf070415d
2 changed files with 27 additions and 18 deletions

View file

@ -313,12 +313,16 @@ var Tool = this.Tool = Base.extend({
},
onHandleEvent: function(type, pt, event) {
// Update global reference to this scope.
paper = this._scope;
var called = false;
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));
called = true;
}
break;
case 'mousedrag':
// In order for idleInterval drag events to work, we need to not
@ -333,8 +337,10 @@ var Tool = this.Tool = 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));
called = true;
}
needsChange = true;
matchMaxDistance = true;
}
@ -345,13 +351,17 @@ var Tool = this.Tool = Base.extend({
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));
called = true;
}
}
this.updateEvent(type, pt, null, this.maxDistance, false,
false, false);
if (this.onMouseUp)
if (this.onMouseUp) {
this.onMouseUp(new ToolEvent(this, type, event));
called = true;
}
// Start with new values for 'mousemove'
this.updateEvent(type, pt, null, null, true, false, false);
this._firstMove = true;
@ -359,11 +369,15 @@ var Tool = this.Tool = Base.extend({
case 'mousemove':
while (this.updateEvent(type, pt, this.minDistance,
this.maxDistance, this._firstMove, true, false)) {
if (this.onMouseMove)
if (this.onMouseMove) {
this.onMouseMove(new ToolEvent(this, type, event));
called = true;
}
this._firstMove = false;
}
break;
}
// Return if a callback was called or not.
return called;
}
});

View file

@ -366,9 +366,8 @@ var View = this.View = Base.extend({
if (!(tool = that._scope.tool))
return;
curPoint = viewToArtwork(event);
tool.onHandleEvent('mousedown', curPoint, event);
if (tool.onMouseDown)
that.draw();
if (tool.onHandleEvent('mousedown', curPoint, event))
that.draw(true);
if (tool.eventInterval != null)
timer = setInterval(mousemove, tool.eventInterval);
dragging = true;
@ -385,16 +384,13 @@ var View = this.View = Base.extend({
var onlyMove = !!(!tool.onMouseDrag && tool.onMouseMove);
if (dragging && !onlyMove) {
curPoint = point || curPoint;
if (curPoint)
tool.onHandleEvent('mousedrag', curPoint, event);
if (tool.onMouseDrag && !tool.onFrame)
that.draw();
if (curPoint && tool.onHandleEvent('mousedrag', curPoint, event))
that.draw(true);
// PORT: If there is only an onMouseMove handler, also call it when
// the user is dragging:
} else if (!dragging || onlyMove) {
tool.onHandleEvent('mousemove', point, event);
if (tool.onMouseMove && !tool.onFrame)
that.draw();
if (tool.onHandleEvent('mousemove', point, event))
that.draw(true);
}
}
@ -406,9 +402,8 @@ var View = this.View = Base.extend({
if (tool) {
if (tool.eventInterval != null)
timer = clearInterval(timer);
tool.onHandleEvent('mouseup', viewToArtwork(event), event);
if (tool.onMouseUp)
that.draw();
if (tool.onHandleEvent('mouseup', viewToArtwork(event), event))
that.draw(true);
}
}