mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-22 07:19:57 -05:00
Implement Item#onMouseDrag evnets, and have them silence #onMouseMove events if both are present.
This commit is contained in:
parent
d066ad775d
commit
80d3763c39
1 changed files with 25 additions and 10 deletions
|
@ -74,22 +74,27 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
tolerance: 0
|
tolerance: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleEvent(view, type, event, point) {
|
|
||||||
if (view._eventCounters[type]) {
|
|
||||||
var hit = view._project.hitTest(point, hitOptions);
|
|
||||||
if (hit && hit.item) {
|
|
||||||
new MouseEvent(type, point, hit.item, event)._call();
|
|
||||||
return hit.item;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var downPoint,
|
var downPoint,
|
||||||
downItem,
|
downItem,
|
||||||
|
hasDrag,
|
||||||
overItem,
|
overItem,
|
||||||
doubleClick,
|
doubleClick,
|
||||||
clickTime;
|
clickTime;
|
||||||
|
|
||||||
|
function handleEvent(view, type, event, point) {
|
||||||
|
if (view._eventCounters[type]) {
|
||||||
|
var hit = view._project.hitTest(point, hitOptions),
|
||||||
|
item = hit && hit.item;
|
||||||
|
if (item) {
|
||||||
|
// If we have a downItem with a mousedrag event, do not send
|
||||||
|
// mousemove events to any item while we're dragging.
|
||||||
|
if (type != 'mousemove' || !downItem)
|
||||||
|
new MouseEvent(type, point, item, event)._call();
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
_onMouseDown: function(event, point) {
|
_onMouseDown: function(event, point) {
|
||||||
var item = handleEvent(this, 'mousedown', event, point);
|
var item = handleEvent(this, 'mousedown', event, point);
|
||||||
|
@ -98,19 +103,29 @@ var CanvasView = View.extend(/** @lends CanvasView# */{
|
||||||
doubleClick = downItem == item && Date.now() - clickTime < 300;
|
doubleClick = downItem == item && Date.now() - clickTime < 300;
|
||||||
downItem = item;
|
downItem = item;
|
||||||
downPoint = point;
|
downPoint = point;
|
||||||
|
hasDrag = downItem && downItem.responds('mousedrag');
|
||||||
},
|
},
|
||||||
|
|
||||||
_onMouseUp: function(event, point) {
|
_onMouseUp: function(event, point) {
|
||||||
var item = handleEvent(this, 'mouseup', event, point);
|
var item = handleEvent(this, 'mouseup', event, point);
|
||||||
|
// If we had a mousedrag event locking mousemove events and are over
|
||||||
|
// another item, send it a mousemove event now
|
||||||
|
if (hasDrag && item != downItem)
|
||||||
|
new MouseEvent('mousemove', point, item, event)._call();
|
||||||
if (item == downItem) {
|
if (item == downItem) {
|
||||||
clickTime = Date.now();
|
clickTime = Date.now();
|
||||||
new MouseEvent(doubleClick ? 'doubleclick' : 'click', downPoint,
|
new MouseEvent(doubleClick ? 'doubleclick' : 'click', downPoint,
|
||||||
overItem, event)._call();
|
overItem, event)._call();
|
||||||
doubleClick = false;
|
doubleClick = false;
|
||||||
}
|
}
|
||||||
|
downItem = null;
|
||||||
|
hasDrag = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
_onMouseMove: function(event, point) {
|
_onMouseMove: function(event, point) {
|
||||||
|
// Call the mousedrag event first if an item was clicked earlier
|
||||||
|
if (downItem)
|
||||||
|
new MouseEvent('mousedrag', point, downItem, event)._call();
|
||||||
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();
|
new MouseEvent('mouseleave', point, overItem, event)._call();
|
||||||
|
|
Loading…
Reference in a new issue