-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovieClipHelper.js
More file actions
402 lines (329 loc) · 11.4 KB
/
MovieClipHelper.js
File metadata and controls
402 lines (329 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
define(
[
'easeljs',
'CatLab/Easelbone/EaselJS/DisplayObjects/Placeholder',
'CatLab/Easelbone/Utilities/Deferred'
],
function(
createjs,
Placeholder,
Deferred
) {
"use strict";
var MovieClipHelper = function() {
};
var p = MovieClipHelper.prototype;
/**
* Find elements by name and turn them into placeholders.
* Returns an array with all placeholders.
* @param {string|array} name
* @param container
* @returns {Array}
*/
p.findPlaceholders = function (name, container) {
var out = [];
var placeholders = this.findFromNames(name, container);
placeholders.forEach(function (element) {
out.push(new Placeholder(element));
});
return out;
};
/**
* Find all named elements (generated by adobe animate) in the container
* or its children (for named elements), with dot notation support and fallback
* to deprecated/alternative names when providing an array.
* @param {string|Array} names
* @param containers
* @param {{ all: boolean }} options
* @returns {Array}
*/
p.findFromNames = function (names, containers, options) {
if (!Array.isArray(containers)) {
containers = [containers];
}
if (!Array.isArray(names)) {
names = [names];
}
var results = [];
var name;
var nameParts;
var rootName;
// Loop through the names and return the first resultset with matches
for (var i = 0; i < names.length; i ++) {
name = names[i];
// Check for dot notation
nameParts = name.split('.');
rootName = nameParts.shift();
// Go through all containers
containers.forEach(function (container) {
results = results.concat(this.findFromNameInContainer(container, rootName, options));
// Do we need to go further down the rabbithole?
if (nameParts.length > 0) {
results = this.findFromNames(nameParts.join('.'), results);
}
}.bind(this));
if (results.length > 0) {
return results;
}
}
return results;
};
p.buildNamedChildMap = function(container) {
if (typeof(container._mh_named_children_map) !== 'undefined') {
return;
}
// Create a map of named children.
// This is a simple 1-dimensional map: name -> child.
container._mh_named_children_map = {};
// Create a deep map of all descendants by name for O(1) lookups.
// This is a 1-dimensional map: name -> [child, child, ...]
container._mh_deep_named_children_map = {};
// Create a map of timeline labels.
// This is a 2-dimensional map: label -> [child, child, ...]
// A label can have multiple children; grandchildren are included as well
container._mh_timeline_label_map = {};
// Generate the timeline labels for this child.
if (container.timeline) {
var labels = container.timeline.getLabels();
for (var i = 0; i < labels.length; i++) {
if (typeof(container._mh_timeline_label_map[labels[i].label]) === 'undefined') {
container._mh_timeline_label_map[labels[i].label] = [];
}
container._mh_timeline_label_map[labels[i].label].push(container);
}
}
this.forEachNamedChild(container, function(child, name) {
container._mh_named_children_map[name] = child;
// Generate the map recursively.
this.buildNamedChildMap(child);
// Copy the timeline labels to the container.
for (var k in child._mh_timeline_label_map) {
if (child._mh_timeline_label_map.hasOwnProperty(k)) {
if (typeof(container._mh_timeline_label_map[k]) === 'undefined') {
container._mh_timeline_label_map[k] = [];
}
container._mh_timeline_label_map[k] = container._mh_timeline_label_map[k].concat(child._mh_timeline_label_map[k]);
}
}
// Build the deep map: add this child under its own name
if (typeof(container._mh_deep_named_children_map[name]) === 'undefined') {
container._mh_deep_named_children_map[name] = [];
}
container._mh_deep_named_children_map[name].push(child);
// Merge child's deep map into container's deep map
for (var dk in child._mh_deep_named_children_map) {
if (child._mh_deep_named_children_map.hasOwnProperty(dk)) {
if (typeof(container._mh_deep_named_children_map[dk]) === 'undefined') {
container._mh_deep_named_children_map[dk] = [];
}
container._mh_deep_named_children_map[dk] = container._mh_deep_named_children_map[dk].concat(child._mh_deep_named_children_map[dk]);
}
}
}.bind(this));
}
/**
* Find all named elements (generated by adobe animate) in the container
* and all child named elements and return them.
* @param container
* @param name
* @param {{ all: boolean }} options
* @param results
* @returns {Array}
*/
p.findFromNameInContainer = function (container, name, options, results) {
if (typeof (results) === 'undefined') {
results = [];
}
if (typeof (options) === 'undefined') {
options = {};
}
// Cache a container map for faster lookups.
this.buildNamedChildMap(container);
// Direct child lookup (O(1))
if (typeof(container._mh_named_children_map[name]) !== 'undefined') {
results.push(container._mh_named_children_map[name]);
}
// Deep lookup using pre-built map instead of recursive search
if (results.length === 0 || options.all) {
if (container._mh_deep_named_children_map && container._mh_deep_named_children_map[name]) {
var deepResults = container._mh_deep_named_children_map[name];
for (var i = 0; i < deepResults.length; i++) {
// Avoid duplicates from direct child lookup
if (results.indexOf(deepResults[i]) === -1) {
results.push(deepResults[i]);
}
}
}
}
return results;
};
/**
* Loop through all named children
* @param container
* @param callback
*/
p.forEachNamedChild = function(container, callback)
{
// Look for named properties (defined by adobe animate)
for (var key in container) {
if (!container.hasOwnProperty(key)) {
continue;
}
switch (key) {
case 'parent':
case 'mask':
continue;
}
if (
container[key] instanceof createjs.DisplayObject &&
!(container[key] instanceof createjs.Shape)
) {
if (callback(container[key], key, container) === false) {
return;
}
}
}
};
p.forAllNamedChildren = function(container, callback, level)
{
if (typeof(level) === 'undefined') {
level = 0;
}
this.forEachNamedChild(container, function(child, name) {
callback(child, name, container, level);
this.forAllNamedChildren(child, callback, level + 1);
}.bind(this));
};
/**
* Apply spritesheet filters and cache their result
* @param filters
* @param container
*/
p.applySpriteFilters = function(filters, container)
{
this.forAllNamedChildren(container, function(child, name, parent) {
if (!(child instanceof createjs.Sprite)) {
return;
}
var bounds = child.getBounds();
child.filters = filters;
child.cache(0, 0, bounds.width, bounds.height);
}.bind(this));
};
/**
* @param container
* @param label
* @returns {boolean}
*/
p.hasLabeledFrame = function(label, container) {
// Ensure the map is built
this.buildNamedChildMap(container);
// Fast lookup using the cached map
if (
container._mh_timeline_label_map &&
container._mh_timeline_label_map[label] &&
container._mh_timeline_label_map[label].length > 0
) {
return true;
}
return false;
};
p.pause = function(container)
{
if (container.timeline) {
container.timeline._was_paused = container.timeline.paused;
container.timeline.paused = true;
}
this.forEachNamedChild(container, function(child) {
this.pause(child);
}.bind(this));
};
p.resume = function(container)
{
if (container.timeline) {
container.timeline.paused = container.timeline._was_paused;
delete container.timeline._was_paused;
}
this.forEachNamedChild(container, function(child) {
this.resume(child);
}.bind(this));
}
/**
* @param container
* @param label
* @returns {number}
*/
p.countLabeledFrames = function(label, container) {
// Ensure the map is built
this.buildNamedChildMap(container);
var count = 0;
if (container._mh_timeline_label_map && container._mh_timeline_label_map[label]) {
count += container._mh_timeline_label_map[label].length;
}
return count;
};
/**
* @param label
* @param container
*/
p.jumpToFrame = function(label, container)
{
var promises = [];
if (
container._mh_timeline_label_map &&
container._mh_timeline_label_map[label]
) {
container._mh_timeline_label_map[label].forEach(function(child) {
promises.push(this._jumpToFrameUntilFinished(label, child));
}.bind(this));
}
return Deferred.when.apply(this, promises);
};
/**
* @param label
* @param container
* @returns {*}
* @private
*/
p._jumpToFrameUntilFinished = function(label, container)
{
// Don't jump to elements that are not visible.
var state = new Deferred();
if (container.parent) {
container.gotoAndPlay(label);
container.timeline.on('complete', function() {
state.resolve();
});
} else {
state.resolve();
}
return state.promise();
}
/**
* Attach helper methods to the createjs MovieClip prototype
*/
p.attach = function(createjs) {
var helper = this;
var p = createjs.MovieClip.prototype;
p.forEachNamedChild = function(callback) {
return helper.forEachNamedChild(this, callback);
};
p.findNamedChildren = function(names) {
return helper.findFromNames(names, this);
};
p.findPlaceholders = function(names) {
return helper.findPlaceholders(names, this);
};
p.applySpriteFilters = function(filters) {
return helper.applySpriteFilters(filters, this);
};
p.hasLabeledFrame = function(label) {
return helper.hasLabeledFrame(label, this);
};
p.jumpToFrame = function(label) {
return helper.jumpToFrame(label, this);
};
};
return new MovieClipHelper();
}
);