Change item based onFrame handlers to have their own time and frame counters.

This commit is contained in:
Jürg Lehni 2012-12-03 10:04:10 -08:00
parent cf5853c8cc
commit 92e9bb2a6a

View file

@ -24,7 +24,6 @@
* screen. * screen.
*/ */
var View = this.View = Base.extend(Callback, /** @lends View# */{ var View = this.View = Base.extend(Callback, /** @lends View# */{
initialize: function(element) { initialize: function(element) {
// Store reference to the currently active global paper scope, and the // Store reference to the currently active global paper scope, and the
// active project, which will be represented by this view // active project, which will be represented by this view
@ -104,7 +103,8 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
if (!View._focused) if (!View._focused)
View._focused = this; View._focused = this;
// Items that need the onFrame handler called on them // Items that need the onFrame handler called on them
this._frameItems = []; this._frameItems = {};
this._frameItemCount = 0;
}, },
/** /**
@ -129,7 +129,7 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
// Removing all onFrame handlers makes the onFrame handler stop // Removing all onFrame handlers makes the onFrame handler stop
// automatically through its uninstall method. // automatically through its uninstall method.
this.detach('frame'); this.detach('frame');
this._frameItems = []; this._frameItems = {};
return true; return true;
}, },
@ -197,18 +197,19 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
_animateItem: function(item, animate) { _animateItem: function(item, animate) {
var items = this._frameItems; var items = this._frameItems;
if (animate) { if (animate) {
if (!items.length) items[item._id] = {
item: item,
// Additional information for the event callback
time: 0,
count: 0
};
if (++this._frameItemCount == 1)
this.attach('frame', this._handleFrameItems); this.attach('frame', this._handleFrameItems);
items.push(item);
} else { } else {
// Mark for deletion, but do not remove it yet, since delete items[item._id];
// removing handlers from inside handlers would mess up if (--this._frameItemCount == 0) {
// onFrame loop in the view otherwise.
items[items.indexOf(this)] = null;
if (items.length == 1) {
// If this is the last one, just stop animating straight away. // If this is the last one, just stop animating straight away.
this.detach('frame', this._handleFrameItems); this.detach('frame', this._handleFrameItems);
this._frameItems = [];
} }
} }
}, },
@ -217,15 +218,13 @@ var View = this.View = Base.extend(Callback, /** @lends View# */{
// through the onFrame callback framework that automatically starts and // through the onFrame callback framework that automatically starts and
// stops the animation for us whenever there's one or more frame handlers // stops the animation for us whenever there's one or more frame handlers
_handleFrameItems: function(event) { _handleFrameItems: function(event) {
var items = this._frameItems; for (var i in this._frameItems) {
// Note: Do not optimaize onFrameItems.length since it may change! var entry = this._frameItems[i];
for (var i = 0; i < items.length; i++) { entry.item.fire('frame', Base.merge(event, {
var item = items[i]; // Time since first call of frame() in seconds:
if (item) time: entry.time += event.delta,
item.fire('frame', event); count: entry.count++
else }));
// item was marked for delition. Remove it, and reduce index
items.splice(i--, 1);
} }
}, },