mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-01 02:38:43 -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
8d1abf7ee2
commit
eb2f7e293a
3 changed files with 91 additions and 89 deletions
|
@ -31,7 +31,7 @@ var Emitter = {
|
|||
handlers.push(func);
|
||||
// See if this is the first handler that we're attaching,
|
||||
// and call install if defined.
|
||||
if (entry && entry.install && handlers.length == 1)
|
||||
if (entry && entry.install && handlers.length === 1)
|
||||
entry.install.call(this, type);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,79 +125,33 @@ var Item = Base.extend(Emitter, /** @lends Item# */{
|
|||
return hasProps;
|
||||
},
|
||||
|
||||
_events: new function() {
|
||||
|
||||
// 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',
|
||||
_events: Base.each(['onMouseDown', 'onMouseUp', 'onMouseDrag', 'onClick',
|
||||
'onDoubleClick', 'onMouseMove', 'onMouseEnter', 'onMouseLeave'],
|
||||
function(name) {
|
||||
this[name] = mouseEvent;
|
||||
}, {
|
||||
onFrame: {
|
||||
install: function() {
|
||||
this._animateItem(true);
|
||||
},
|
||||
uninstall: function() {
|
||||
this._animateItem(false);
|
||||
}
|
||||
function(name) {
|
||||
this[name] = {
|
||||
install: function(type) {
|
||||
this.getView()._installEvent(type);
|
||||
},
|
||||
|
||||
// Only for external sources, e.g. Raster
|
||||
onLoad: {}
|
||||
}
|
||||
);
|
||||
},
|
||||
uninstall: function(type) {
|
||||
this.getView()._uninstallEvent(type);
|
||||
}
|
||||
};
|
||||
}, {
|
||||
onFrame: {
|
||||
install: function() {
|
||||
this.getView()._animateItem(this, true);
|
||||
},
|
||||
|
||||
_animateItem: function(animate) {
|
||||
this.getView()._animateItem(this, animate);
|
||||
},
|
||||
uninstall: function() {
|
||||
this.getView()._animateItem(this, false);
|
||||
}
|
||||
},
|
||||
|
||||
// Only for external sources, e.g. Raster
|
||||
onLoad: {}
|
||||
}
|
||||
),
|
||||
|
||||
_serialize: function(options, dictionary) {
|
||||
var props = {},
|
||||
|
|
|
@ -149,27 +149,29 @@ var View = Base.extend(Emitter, /** @lends View# */{
|
|||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* @namespace
|
||||
* @ignore
|
||||
*/
|
||||
_events: {
|
||||
/**
|
||||
* @namespace
|
||||
* @ignore
|
||||
*/
|
||||
onFrame: {
|
||||
install: function() {
|
||||
this.play();
|
||||
},
|
||||
_events: Base.each(['onResize', 'onMouseDown', 'onMouseUp', 'onMouseMove'],
|
||||
function(name) {
|
||||
this[name] = {
|
||||
install: function(type) {
|
||||
this._installEvent(type);
|
||||
},
|
||||
|
||||
uninstall: function() {
|
||||
this.pause();
|
||||
uninstall: function(type) {
|
||||
this._uninstallEvent(type);
|
||||
}
|
||||
};
|
||||
}, {
|
||||
onFrame: {
|
||||
install: function() {
|
||||
this.play();
|
||||
},
|
||||
|
||||
uninstall: function() {
|
||||
this.pause();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
onResize: {}
|
||||
},
|
||||
}
|
||||
),
|
||||
|
||||
// These are default values for event related properties on the prototype.
|
||||
// Writing item._count++ does not change the defaults, it creates / updates
|
||||
|
@ -828,12 +830,58 @@ var View = Base.extend(Emitter, /** @lends View# */{
|
|||
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 {
|
||||
_viewEvents: viewEvents,
|
||||
|
||||
// To be defined in subclasses
|
||||
_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: {
|
||||
/**
|
||||
* Loops through all views and sets the focus on the first
|
||||
|
|
Loading…
Reference in a new issue