mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Simplify event handling code.
This commit is contained in:
parent
80d3763c39
commit
9061daa935
2 changed files with 24 additions and 20 deletions
|
@ -81,6 +81,23 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
doubleClick,
|
doubleClick,
|
||||||
clickTime;
|
clickTime;
|
||||||
|
|
||||||
|
function callEvent(type, point, target, event, bubble) {
|
||||||
|
var item = target,
|
||||||
|
mouseEvent,
|
||||||
|
called = false;
|
||||||
|
while (item) {
|
||||||
|
if (item.responds(type)) {
|
||||||
|
if (!mouseEvent)
|
||||||
|
mouseEvent = new MouseEvent(type, point, target, event);
|
||||||
|
called = item.fire(type, mouseEvent) || called;
|
||||||
|
if (called && (!bubble || mouseEvent._stopped))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
item = item.getParent();
|
||||||
|
}
|
||||||
|
return called;
|
||||||
|
}
|
||||||
|
|
||||||
function handleEvent(view, type, event, point) {
|
function handleEvent(view, type, event, point) {
|
||||||
if (view._eventCounters[type]) {
|
if (view._eventCounters[type]) {
|
||||||
var hit = view._project.hitTest(point, hitOptions),
|
var hit = view._project.hitTest(point, hitOptions),
|
||||||
|
@ -89,7 +106,7 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
// If we have a downItem with a mousedrag event, do not send
|
// If we have a downItem with a mousedrag event, do not send
|
||||||
// mousemove events to any item while we're dragging.
|
// mousemove events to any item while we're dragging.
|
||||||
if (type != 'mousemove' || !downItem)
|
if (type != 'mousemove' || !downItem)
|
||||||
new MouseEvent(type, point, item, event)._call();
|
callEvent(type, point, item, event);
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,11 +128,11 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
// If we had a mousedrag event locking mousemove events and are over
|
// If we had a mousedrag event locking mousemove events and are over
|
||||||
// another item, send it a mousemove event now
|
// another item, send it a mousemove event now
|
||||||
if (hasDrag && item != downItem)
|
if (hasDrag && item != downItem)
|
||||||
new MouseEvent('mousemove', point, item, event)._call();
|
callEvent('mousemove', point, item, event);
|
||||||
if (item == downItem) {
|
if (item == downItem) {
|
||||||
clickTime = Date.now();
|
clickTime = Date.now();
|
||||||
new MouseEvent(doubleClick ? 'doubleclick' : 'click', downPoint,
|
callEvent(doubleClick ? 'doubleclick' : 'click', downPoint,
|
||||||
overItem, event)._call();
|
overItem, event);
|
||||||
doubleClick = false;
|
doubleClick = false;
|
||||||
}
|
}
|
||||||
downItem = null;
|
downItem = null;
|
||||||
|
@ -125,12 +142,12 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
_onMouseMove: function(event, point) {
|
_onMouseMove: function(event, point) {
|
||||||
// Call the mousedrag event first if an item was clicked earlier
|
// Call the mousedrag event first if an item was clicked earlier
|
||||||
if (downItem)
|
if (downItem)
|
||||||
new MouseEvent('mousedrag', point, downItem, event)._call();
|
callEvent('mousedrag', point, downItem, event);
|
||||||
var item = handleEvent(this, 'mousemove', event, point);
|
var item = handleEvent(this, 'mousemove', event, point);
|
||||||
if (item != overItem) {
|
if (item != overItem) {
|
||||||
new MouseEvent('mouseleave', point, overItem, event)._call();
|
callEvent('mouseleave', point, overItem, event);
|
||||||
overItem = item;
|
overItem = item;
|
||||||
new MouseEvent('mouseenter', point, item, event)._call();
|
callEvent('mouseenter', point, item, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -36,18 +36,5 @@ var MouseEvent = this.MouseEvent = Event.extend(/** @lends MouseEvent# */{
|
||||||
+ ', target: ' + this.target
|
+ ', target: ' + this.target
|
||||||
+ ', modifiers: ' + this.getModifiers()
|
+ ', modifiers: ' + this.getModifiers()
|
||||||
+ ' }';
|
+ ' }';
|
||||||
},
|
|
||||||
|
|
||||||
// TODO: Move to Event perhaps?
|
|
||||||
_call: function(bubble) {
|
|
||||||
var item = this.target,
|
|
||||||
called = false;
|
|
||||||
while (item) {
|
|
||||||
called = item.fire(this.type, this) || called;
|
|
||||||
if (called && (!bubble || this._stopped))
|
|
||||||
break;
|
|
||||||
item = item.getParent();
|
|
||||||
}
|
|
||||||
return called;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue