mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-07 13:22:07 -05:00
Streamline mouse event handling between View and Item.
Consolidating code and making View#onMouseDown/Up/Move/... events work.
This commit is contained in:
parent
9762d2c9e6
commit
3314668a0c
3 changed files with 91 additions and 89 deletions
|
@ -31,7 +31,7 @@ var Emitter = {
|
||||||
handlers.push(func);
|
handlers.push(func);
|
||||||
// See if this is the first handler that we're attaching,
|
// See if this is the first handler that we're attaching,
|
||||||
// and call install if defined.
|
// and call install if defined.
|
||||||
if (entry && entry.install && handlers.length == 1)
|
if (entry && entry.install && handlers.length === 1)
|
||||||
entry.install.call(this, type);
|
entry.install.call(this, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -125,79 +125,33 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
|
||||||
return hasProps;
|
return hasProps;
|
||||||
},
|
},
|
||||||
|
|
||||||
_events: new function() {
|
_events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
|
||||||
|
|
||||||
// Flags defining which native events are required by which Paper events
|
|
||||||
// as required for counting amount of necessary natives events.
|
|
||||||
// The mapping is native -> virtual
|
|
||||||
var mouseFlags = {
|
|
||||||
mousedown: {
|
|
||||||
mousedown: 1,
|
|
||||||
mousedrag: 1,
|
|
||||||
click: 1,
|
|
||||||
doubleclick: 1
|
|
||||||
},
|
|
||||||
mouseup: {
|
|
||||||
mouseup: 1,
|
|
||||||
mousedrag: 1,
|
|
||||||
click: 1,
|
|
||||||
doubleclick: 1
|
|
||||||
},
|
|
||||||
mousemove: {
|
|
||||||
mousedrag: 1,
|
|
||||||
mousemove: 1,
|
|
||||||
mouseenter: 1,
|
|
||||||
mouseleave: 1
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Entry for all mouse events in the _events list
|
|
||||||
var mouseEvent = {
|
|
||||||
install: function(type) {
|
|
||||||
// If the view requires counting of installed mouse events,
|
|
||||||
// increase the counters now according to mouseFlags
|
|
||||||
var counters = this.getView()._eventCounters;
|
|
||||||
if (counters) {
|
|
||||||
for (var key in mouseFlags) {
|
|
||||||
counters[key] = (counters[key] || 0)
|
|
||||||
+ (mouseFlags[key][type] || 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
uninstall: function(type) {
|
|
||||||
// If the view requires counting of installed mouse events,
|
|
||||||
// decrease the counters now according to mouseFlags
|
|
||||||
var counters = this.getView()._eventCounters;
|
|
||||||
if (counters) {
|
|
||||||
for (var key in mouseFlags)
|
|
||||||
counters[key] -= mouseFlags[key][type] || 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
|
|
||||||
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'],
|
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'],
|
||||||
function(name) {
|
function(name) {
|
||||||
this[name] = mouseEvent;
|
this[name] = {
|
||||||
}, {
|
install: function(type) {
|
||||||
onFrame: {
|
this.getView()._installEvent(type);
|
||||||
install: function() {
|
|
||||||
this._animateItem(true);
|
|
||||||
},
|
|
||||||
uninstall: function() {
|
|
||||||
this._animateItem(false);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
// Only for external sources, e.g. Raster
|
uninstall: function(type) {
|
||||||
onLoad: {}
|
this.getView()._uninstallEvent(type);
|
||||||
}
|
}
|
||||||
);
|
};
|
||||||
},
|
}, {
|
||||||
|
onFrame: {
|
||||||
|
install: function() {
|
||||||
|
this.getView()._animateItem(this, true);
|
||||||
|
},
|
||||||
|
|
||||||
_animateItem: function(animate) {
|
uninstall: function() {
|
||||||
this.getView()._animateItem(this, animate);
|
this.getView()._animateItem(this, false);
|
||||||
},
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Only for external sources, e.g. Raster
|
||||||
|
onLoad: {}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
|
||||||
_serialize: function(options, dictionary) {
|
_serialize: function(options, dictionary) {
|
||||||
var props = {},
|
var props = {},
|
||||||
|
|
|
@ -149,27 +149,29 @@ var View = Base.extend(Emitter, /** @lends View# */{
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
_events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove'],
|
||||||
* @namespace
|
function(name) {
|
||||||
* @ignore
|
this[name] = {
|
||||||
*/
|
install: function(type) {
|
||||||
_events: {
|
this._installEvent(type);
|
||||||
/**
|
},
|
||||||
* @namespace
|
|
||||||
* @ignore
|
|
||||||
*/
|
|
||||||
onFrame: {
|
|
||||||
install: function() {
|
|
||||||
this.play();
|
|
||||||
},
|
|
||||||
|
|
||||||
uninstall: function() {
|
uninstall: function(type) {
|
||||||
this.pause();
|
this._uninstallEvent(type);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, {
|
||||||
|
onFrame: {
|
||||||
|
install: function() {
|
||||||
|
this.play();
|
||||||
|
},
|
||||||
|
|
||||||
|
uninstall: function() {
|
||||||
|
this.pause();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
),
|
||||||
onResize: {}
|
|
||||||
},
|
|
||||||
|
|
||||||
// These are default values for event related properties on the prototype.
|
// These are default values for event related properties on the prototype.
|
||||||
// Writing item._count++ does not change the defaults, it creates / updates
|
// Writing item._count++ does not change the defaults, it creates / updates
|
||||||
|
@ -828,12 +830,58 @@ new function() { // Injection scope for mouse events on the browser
|
||||||
load: updateFocus
|
load: updateFocus
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Flags defining which native events are required by which Paper events
|
||||||
|
// as required for counting amount of necessary natives events.
|
||||||
|
// The mapping is native -> virtual
|
||||||
|
var mouseFlags = {
|
||||||
|
mousedown: {
|
||||||
|
mousedown: 1,
|
||||||
|
mousedrag: 1,
|
||||||
|
click: 1,
|
||||||
|
doubleclick: 1
|
||||||
|
},
|
||||||
|
mouseup: {
|
||||||
|
mouseup: 1,
|
||||||
|
mousedrag: 1,
|
||||||
|
click: 1,
|
||||||
|
doubleclick: 1
|
||||||
|
},
|
||||||
|
mousemove: {
|
||||||
|
mousedrag: 1,
|
||||||
|
mousemove: 1,
|
||||||
|
mouseenter: 1,
|
||||||
|
mouseleave: 1
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
_viewEvents: viewEvents,
|
_viewEvents: viewEvents,
|
||||||
|
|
||||||
// To be defined in subclasses
|
// To be defined in subclasses
|
||||||
_handleEvent: function(/* type, point, event */) {},
|
_handleEvent: function(/* type, point, event */) {},
|
||||||
|
|
||||||
|
_installEvent: function(type) {
|
||||||
|
// If the view requires counting of installed mouse events,
|
||||||
|
// increase the counters now according to mouseFlags
|
||||||
|
var counters = this._eventCounters;
|
||||||
|
if (counters) {
|
||||||
|
for (var key in mouseFlags) {
|
||||||
|
counters[key] = (counters[key] || 0)
|
||||||
|
+ (mouseFlags[key][type] || 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_uninstallEvent: function(type) {
|
||||||
|
// If the view requires counting of installed mouse events,
|
||||||
|
// decrease the counters now according to mouseFlags
|
||||||
|
var counters = this._eventCounters;
|
||||||
|
if (counters) {
|
||||||
|
for (var key in mouseFlags)
|
||||||
|
counters[key] -= mouseFlags[key][type] || 0;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
statics: {
|
statics: {
|
||||||
/**
|
/**
|
||||||
* Loops through all views and sets the focus on the first
|
* Loops through all views and sets the focus on the first
|
||||||
|
|
Loading…
Reference in a new issue