This repository has been archived by the owner on Sep 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 512
/
ScratchObj.as
717 lines (625 loc) · 22.4 KB
/
ScratchObj.as
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// ScratchObj.as
// John Maloney, April 2010
//
// This is the superclass for both ScratchStage and ScratchSprite,
// containing the variables and methods common to both.
package scratch {
import blocks.*;
import filters.FilterPack;
import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.ColorTransform;
import flash.utils.*;
import interpreter.*;
import scratch.ScratchComment;
import scratch.ScratchSprite;
import translation.Translator;
import util.*;
import watchers.*;
public class ScratchObj extends Sprite {
[Embed(source='../assets/pop.wav', mimeType='application/octet-stream')] protected static var Pop:Class;
public static const STAGEW:int = 480;
public static const STAGEH:int = 360;
public var objName:String = 'no name';
public var isStage:Boolean = false;
public var variables:Array = [];
public var lists:Array = [];
public var scripts:Array = [];
public var scriptComments:Array = [];
public var sounds:Array = [];
public var costumes:Array = [];
public var currentCostumeIndex:Number;
public var volume:Number = 100;
public var instrument:int = 0;
public var filterPack:FilterPack;
public var isClone:Boolean;
public var img:Sprite; // holds a bitmap or svg object, after applying image filters, scale, and rotation
private var lastCostume:ScratchCostume;
// Caches used by the interpreter:
public var listCache:Object = {};
public var procCache:Object = {};
public var varCache:Object = {};
public function clearCaches():void {
// Clear the list, procedure, and variable caches for this object.
listCache = {};
procCache = {};
varCache = {};
}
public function allObjects():Array { return [this] }
public function deleteCostume(c:ScratchCostume):void {
if (costumes.length < 2) return; // a sprite must have at least one costume
var i:int = costumes.indexOf(c);
if (i < 0) return;
costumes.splice(i, 1);
if (currentCostumeIndex >= i) showCostume(currentCostumeIndex - 1);
if (Scratch.app) Scratch.app.setSaveNeeded();
}
public function deleteSound(snd:ScratchSound):void {
var i:int = sounds.indexOf(snd);
if (i < 0) return;
sounds.splice(i, 1);
if (Scratch.app) Scratch.app.setSaveNeeded();
}
public function showCostumeNamed(n:String):void {
var i:int = indexOfCostumeNamed(n);
if (i >= 0) showCostume(i);
}
public function indexOfCostumeNamed(n:String):int {
for (var i:int = 0; i < costumes.length; i++) {
if (ScratchCostume(costumes[i]).costumeName == n) return i;
}
return -1;
}
public function showCostume(costumeIndex:Number):void {
if (isNaNOrInfinity(costumeIndex)) costumeIndex = 0;
currentCostumeIndex = costumeIndex % costumes.length;
if (currentCostumeIndex < 0) currentCostumeIndex += costumes.length;
var c:ScratchCostume = currentCostume();
if (c == lastCostume) return; // optimization: already showing that costume
lastCostume = c.isBitmap() ? c : null; // cache only bitmap costumes for now
updateImage();
}
public function updateCostume():void { updateImage(); }
public function currentCostume():ScratchCostume {
return costumes[Math.round(currentCostumeIndex) % costumes.length];
}
public function costumeNumber():int {
// One-based costume number as seen by user (currentCostumeIndex is 0-based)
return currentCostumeIndex + 1;
}
public function unusedCostumeName(baseName:String = ''):String {
// Create a unique costume name by appending a number if necessary.
if (baseName == '') baseName = Translator.map(isStage ? 'backdrop1' : 'costume1');
var existingNames:Array = [];
for each (var c:ScratchCostume in costumes) {
existingNames.push(c.costumeName.toLowerCase());
}
var lcBaseName:String = baseName.toLowerCase();
if (existingNames.indexOf(lcBaseName) < 0) return baseName; // basename is not already used
lcBaseName = withoutTrailingDigits(lcBaseName);
var i:int = 2;
while (existingNames.indexOf(lcBaseName + i) >= 0) { i++ } // find an unused name
return withoutTrailingDigits(baseName) + i;
}
public function unusedSoundName(baseName:String = ''):String {
// Create a unique sound name by appending a number if necessary.
if (baseName == '') baseName = 'sound';
var existingNames:Array = [];
for each (var snd:ScratchSound in sounds) {
existingNames.push(snd.soundName.toLowerCase());
}
var lcBaseName:String = baseName.toLowerCase();
if (existingNames.indexOf(lcBaseName) < 0) return baseName; // basename is not already used
lcBaseName = withoutTrailingDigits(lcBaseName);
var i:int = 2;
while (existingNames.indexOf(lcBaseName + i) >= 0) { i++ } // find an unused name
return withoutTrailingDigits(baseName) + i;
}
protected function withoutTrailingDigits(s:String):String {
var i:int = s.length - 1;
while ((i >= 0) && ('0123456789'.indexOf(s.charAt(i)) > -1)) i--;
return s.slice(0, i + 1);
}
protected function updateImage():void {
var currChild:DisplayObject = (img.numChildren == 1 ? img.getChildAt(0) : null);
var currDispObj:DisplayObject = currentCostume().displayObj();
var change:Boolean = (currChild != currDispObj);
if(change) {
while (img.numChildren > 0) img.removeChildAt(0);
img.addChild(currDispObj);
}
clearCachedBitmap();
adjustForRotationCenter();
updateRenderDetails(0);
}
protected function updateRenderDetails(reason:uint):void {
SCRATCH::allow3d {
if(this is ScratchStage || this is ScratchSprite || (parent && parent is ScratchStage)) {
var renderOpts:Object = {};
var costume:ScratchCostume = currentCostume();
// 0 - costume change, 1 - rotation style change
if(reason == 0) {
if(costume && costume.baseLayerID == ScratchCostume.WasEdited)
costume.prepareToSave();
var id:String = (costume ? costume.baseLayerMD5 : null);
if(!id) id = objName + (costume ? costume.costumeName : '_' + currentCostumeIndex);
else if(costume && costume.textLayerMD5) id += costume.textLayerMD5;
renderOpts.bitmap = (costume && costume.bitmap ? costume.bitmap : null);
}
// TODO: Clip original bitmap to match visible bounds?
if(reason == 1)
renderOpts.costumeFlipped = (this is ScratchSprite ? (this as ScratchSprite).isCostumeFlipped() : false);
if(reason == 0) {
if(this is ScratchSprite) {
renderOpts.bounds = (this as ScratchSprite).getVisibleBounds(this);
renderOpts.raw_bounds = getBounds(this);
}
else
renderOpts.bounds = getBounds(this);
}
if (Scratch.app.isIn3D) Scratch.app.render3D.updateRender((this is ScratchStage ? img : this), id, renderOpts);
}
}
}
protected function adjustForRotationCenter():void {
// Adjust the offset of img relative to it's parent. If this object is a
// ScratchSprite, then img is adjusted based on the costume's rotation center.
// If it is a ScratchStage, img is centered on the stage.
var costumeObj:DisplayObject = img.getChildAt(0);
if (isStage) {
if (costumeObj is Bitmap) {
img.x = (STAGEW - costumeObj.width) / 2;
img.y = (STAGEH - costumeObj.height) / 2;
} else {
// SVG costume; don't center for now
img.x = img.y = 0;
}
} else {
var c:ScratchCostume = currentCostume();
costumeObj.scaleX = 1 / c.bitmapResolution; // don't flip
img.x = -c.rotationCenterX / c.bitmapResolution;
img.y = -c.rotationCenterY / c.bitmapResolution;
if ((this as ScratchSprite).isCostumeFlipped()) {
costumeObj.scaleX = -1 / c.bitmapResolution; // flip
img.x = -img.x;
}
}
}
public function clearCachedBitmap():void {
// Does nothing here, but overridden in ScratchSprite
}
static private var cTrans:ColorTransform = new ColorTransform();
public function applyFilters(forDragging:Boolean = false):void {
img.filters = filterPack.buildFilters(forDragging);
clearCachedBitmap();
if(!Scratch.app.isIn3D || forDragging) {
var n:Number = Math.max(0, Math.min(filterPack.getFilterSetting('ghost'), 100));
cTrans.alphaMultiplier = 1.0 - (n / 100.0);
n = 255 * Math.max(-100, Math.min(filterPack.getFilterSetting('brightness'), 100)) / 100;
cTrans.redOffset = cTrans.greenOffset = cTrans.blueOffset = n;
img.transform.colorTransform = cTrans;
}
else {
updateEffectsFor3D();
}
}
public function updateEffectsFor3D():void {
SCRATCH::allow3d {
if((parent && parent is ScratchStage) || this is ScratchStage) {
if(parent is ScratchStage)
(parent as ScratchStage).updateSpriteEffects(this, filterPack.getAllSettings());
else {
(this as ScratchStage).updateSpriteEffects(img, filterPack.getAllSettings());
// if((this as ScratchStage).videoImage)
// (this as ScratchStage).updateSpriteEffects((this as ScratchStage).videoImage, filterPack.getAllSettings());
}
}
}
}
protected function shapeChangedByFilter():Boolean {
var filters:Object = filterPack.getAllSettings();
return (filters['fisheye'] !== 0 || filters['whirl'] !== 0 || filters['mosaic'] !== 0);
}
static public const clearColorTrans:ColorTransform = new ColorTransform();
public function clearFilters():void {
filterPack.resetAllFilters();
img.filters = [];
img.transform.colorTransform = clearColorTrans;
clearCachedBitmap();
SCRATCH::allow3d {
if (parent && parent is ScratchStage) {
(parent as ScratchStage).updateSpriteEffects(this, null);
}
}
}
public function setMedia(media:Array, currentCostume:ScratchCostume):void {
var newCostumes:Array = [];
sounds = [];
for each (var m:* in media) {
if (m is ScratchSound) sounds.push(m);
if (m is ScratchCostume) newCostumes.push(m);
}
if (newCostumes.length > 0) costumes = newCostumes;
var i:int = costumes.indexOf(currentCostume);
currentCostumeIndex = (i < 0) ? 0 : i;
showCostume(i);
}
public function defaultArgsFor(op:String, specDefaults:Array):Array {
// Return an array of default parameter values for the given operation (primitive name).
// For most ops, this will simply return the array of default arg values from the command spec.
var sprites:Array;
if ((['broadcast:', 'doBroadcastAndWait', 'whenIReceive'].indexOf(op)) > -1) {
var msgs:Array = Scratch.app.runtime.collectBroadcasts();
return [msgs[0]];
}
if ((['lookLike:', 'startScene', 'startSceneAndWait', 'whenSceneStarts'].indexOf(op)) > -1) {
return [costumes[costumes.length - 1].costumeName];
}
if ((['playSound:', 'doPlaySoundAndWait'].indexOf(op)) > -1) {
return (sounds.length > 0) ? [sounds[sounds.length - 1].soundName] : [''];
}
if ('createCloneOf' == op) {
if (!isStage) return ['_myself_'];
sprites = Scratch.app.stagePane.sprites();
return (sprites.length > 0) ? [sprites[sprites.length - 1].objName] : [''];
}
if ('getAttribute:of:' == op) {
sprites = Scratch.app.stagePane.sprites();
return (sprites.length > 0) ? ['x position', sprites[sprites.length - 1].objName] : ['volume', '_stage_'];
}
if ('setVar:to:' == op) return [defaultVarName(), 0];
if ('changeVar:by:' == op) return [defaultVarName(), 1];
if ('showVariable:' == op) return [defaultVarName()];
if ('hideVariable:' == op) return [defaultVarName()];
if ('append:toList:' == op) return ['thing', defaultListName()];
if ('deleteLine:ofList:' == op) return [1, defaultListName()];
if ('insert:at:ofList:' == op) return ['thing', 1, defaultListName()];
if ('setLine:ofList:to:' == op) return [1, defaultListName(), 'thing'];
if ('getLine:ofList:' == op) return [1, defaultListName()];
if ('lineCountOfList:' == op) return [defaultListName()];
if ('list:contains:' == op) return [defaultListName(), 'thing'];
if ('showList:' == op) return [defaultListName()];
if ('hideList:' == op) return [defaultListName()];
return specDefaults;
}
public function defaultVarName():String {
if (variables.length > 0) return variables[variables.length - 1].name; // local var
return isStage ? '' : Scratch.app.stagePane.defaultVarName(); // global var, if any
}
public function defaultListName():String {
if (lists.length > 0) return lists[lists.length - 1].listName; // local list
return isStage ? '' : Scratch.app.stagePane.defaultListName(); // global list, if any
}
/* Scripts */
public function allBlocks():Array {
var result:Array = [];
for each (var script:Block in scripts) {
script.allBlocksDo(function(b:Block):void { result.push(b) });
}
return result;
}
/* Sounds */
public function findSound(arg:*):ScratchSound {
// Return a sound describe by arg, which can be a string (sound name),
// a number (sound index), or a string representing a number (sound index).
if (sounds.length == 0) return null;
if (typeof(arg) == 'number') {
var i:int = Math.round(arg - 1) % sounds.length;
if (i < 0) i += sounds.length; // ensure positive
return sounds[i];
} else if (typeof(arg) == 'string') {
for each (var snd:ScratchSound in sounds) {
if (snd.soundName == arg) return snd; // arg matches a sound name
}
// try converting string arg to a number
var n:Number = Number(arg);
if (isNaN(n)) return null;
return findSound(n);
}
return null;
}
public function setVolume(vol:Number):void {
volume = Math.max(0, Math.min(vol, 100));
}
public function setInstrument(instr:Number):void {
instrument = Math.max(1, Math.min(Math.round(instr), 128));
}
/* Procedures */
public function procedureDefinitions():Array {
var result:Array = [];
for (var i:int = 0; i < scripts.length; i++) {
var b:Block = scripts[i] as Block;
if (b && (b.op == Specs.PROCEDURE_DEF)) result.push(b);
}
return result;
}
public function lookupProcedure(procName:String):Block {
for (var i:int = 0; i < scripts.length; i++) {
var b:Block = scripts[i] as Block;
if (b && (b.op == Specs.PROCEDURE_DEF) && (b.spec == procName)) return b;
}
return null;
}
/* Variables */
public function varNames():Array {
var varList:Array = [];
for each (var v:Variable in variables) varList.push(v.name);
return varList;
}
public function setVarTo(varName:String, value:*):void {
var v:Variable = lookupOrCreateVar(varName);
v.value = value;
Scratch.app.runtime.updateVariable(v);
}
public function ownsVar(varName:String):Boolean {
// Return true if this object owns a variable of the given name.
for each (var v:Variable in variables) {
if (v.name == varName) return true;
}
return false;
}
public function hasName(varName:String):Boolean {
var p:ScratchObj = parent as ScratchObj;
return ownsVar(varName) || ownsList(varName) || p && (p.ownsVar(varName) || p.ownsList(varName));
}
public function lookupOrCreateVar(varName:String):Variable {
// Lookup and return a variable. If lookup fails, create the variable in this object.
var v:Variable = lookupVar(varName);
if (v == null) { // not found; create it
v = new Variable(varName, 0);
variables.push(v);
Scratch.app.updatePalette(false);
}
return v;
}
public function lookupVar(varName:String):Variable {
// Look for variable first in sprite (local), then stage (global).
// Return null if not found.
var v:Variable;
for each (v in variables) {
if (v.name == varName) return v;
}
for each (v in Scratch.app.stagePane.variables) {
if (v.name == varName) return v;
}
return null;
}
public function deleteVar(varToDelete:String):void {
var newVars:Array = [];
for each (var v:Variable in variables) {
if (v.name == varToDelete) {
if ((v.watcher != null) && (v.watcher.parent != null)) {
v.watcher.parent.removeChild(v.watcher);
}
v.watcher = v.value = null;
}
else newVars.push(v);
}
variables = newVars;
}
/* Lists */
public function listNames():Array {
var result:Array = [];
for each (var list:ListWatcher in lists) result.push(list.listName);
return result;
}
public function ownsList(listName:String):Boolean {
// Return true if this object owns a list of the given name.
for each (var w:ListWatcher in lists) {
if (w.listName == listName) return true;
}
return false;
}
public function lookupOrCreateList(listName:String):ListWatcher {
// Look and return a list. If lookup fails, create the list in this object.
var list:ListWatcher = lookupList(listName);
if (list == null) { // not found; create it
list = new ListWatcher(listName, [], this);
lists.push(list);
Scratch.app.updatePalette(false);
}
return list;
}
public function lookupList(listName:String):ListWatcher {
// Look for list first in this sprite (local), then stage (global).
// Return null if not found.
var list:ListWatcher;
for each (list in lists) {
if (list.listName == listName) return list;
}
for each (list in Scratch.app.stagePane.lists) {
if (list.listName == listName) return list;
}
return null;
}
public function deleteList(listName:String):void {
var newLists:Array = [];
for each (var w:ListWatcher in lists) {
if (w.listName == listName) {
if (w.parent) w.parent.removeChild(w);
} else {
newLists.push(w);
}
}
lists = newLists;
}
/* Events */
private const DOUBLE_CLICK_MSECS:int = 300;
private var lastClickTime:uint;
public function click(evt:MouseEvent):void {
var app:Scratch = root as Scratch;
if (!app) return;
var now:uint = getTimer();
app.runtime.startClickedHats(this);
if ((now - lastClickTime) < DOUBLE_CLICK_MSECS) {
if (isStage || ScratchSprite(this).isClone) return;
app.selectSprite(this);
lastClickTime = 0;
} else {
lastClickTime = now;
}
}
/* Translation */
public function updateScriptsAfterTranslation():void {
// Update the scripts of this object after switching languages.
var newScripts:Array = [];
for each (var b:Block in scripts) {
var newStack:Block = BlockIO.arrayToStack(BlockIO.stackToArray(b), isStage);
newStack.x = b.x;
newStack.y = b.y;
newScripts.push(newStack);
if (b.parent) { // stack in the scripts pane; replace it
b.parent.addChild(newStack);
b.parent.removeChild(b);
}
}
scripts = newScripts;
var blockList:Array = allBlocks();
for each (var c:ScratchComment in scriptComments) {
c.updateBlockRef(blockList);
}
}
/* Saving */
public function writeJSON(json:util.JSON):void {
var allScripts:Array = [];
for each (var b:Block in scripts) {
allScripts.push([b.x, b.y, BlockIO.stackToArray(b)]);
}
var allComments:Array = [];
for each (var c:ScratchComment in scriptComments) {
allComments.push(c.toArray());
}
json.writeKeyValue('objName', objName);
if (variables.length > 0) json.writeKeyValue('variables', variables);
if (lists.length > 0) json.writeKeyValue('lists', lists);
if (scripts.length > 0) json.writeKeyValue('scripts', allScripts);
if (scriptComments.length > 0) json.writeKeyValue('scriptComments', allComments);
if (sounds.length > 0) json.writeKeyValue('sounds', sounds);
json.writeKeyValue('costumes', costumes);
json.writeKeyValue('currentCostumeIndex', currentCostumeIndex);
}
public function readJSON(jsonObj:Object):void {
objName = jsonObj.objName;
variables = jsonObj.variables || [];
for (var i:int = 0; i < variables.length; i++) {
var varObj:Object = variables[i];
variables[i] = Scratch.app.runtime.makeVariable(varObj);
}
lists = jsonObj.lists || [];
scripts = jsonObj.scripts || [];
scriptComments = jsonObj.scriptComments || [];
sounds = jsonObj.sounds || [];
costumes = jsonObj.costumes || [];
currentCostumeIndex = jsonObj.currentCostumeIndex;
if (isNaNOrInfinity(currentCostumeIndex)) currentCostumeIndex = 0;
}
private function isNaNOrInfinity(n:Number):Boolean {
if (n != n) return true; // NaN
if (n == Number.POSITIVE_INFINITY) return true;
if (n == Number.NEGATIVE_INFINITY) return true;
return false;
}
public function instantiateFromJSON(newStage:ScratchStage):void {
var i:int, jsonObj:Object;
// lists
for (i = 0; i < lists.length; i++) {
jsonObj = lists[i];
var newList:ListWatcher = new ListWatcher();
newList.readJSON(jsonObj);
newList.target = this;
newStage.addChild(newList);
newList.updateTitleAndContents();
lists[i] = newList;
}
// scripts
for (i = 0; i < scripts.length; i++) {
// entries are of the form: [x y stack]
var entry:Array = scripts[i];
var b:Block = BlockIO.arrayToStack(entry[2], isStage);
b.x = entry[0];
b.y = entry[1];
scripts[i] = b;
}
// script comments
for (i = 0; i < scriptComments.length; i++) {
scriptComments[i] = ScratchComment.fromArray(scriptComments[i]);
}
// sounds
for (i = 0; i < sounds.length; i++) {
jsonObj = sounds[i];
sounds[i] = new ScratchSound('json temp', null);
sounds[i].readJSON(jsonObj);
}
// costumes
for (i = 0; i < costumes.length; i++) {
jsonObj = costumes[i];
costumes[i] = new ScratchCostume('json temp', null);
costumes[i].readJSON(jsonObj);
}
}
public function getSummary():String {
var s:Array = [];
s.push(h1(objName));
if (variables.length) {
s.push(h2(Translator.map("Variables")));
for each (var v:Variable in variables) {
s.push("- " + v.name + " = " + v.value);
}
s.push("");
}
if (lists.length) {
s.push(h2(Translator.map("Lists")));
for each (var list:ListWatcher in lists) {
s.push("- " + list.listName + (list.contents.length ? ":" : ""));
for each (var item:* in list.contents) {
s.push(" - " + item);
}
}
s.push("");
}
s.push(h2(Translator.map(isStage ? "Backdrops" : "Costumes")));
for each (var costume:ScratchCostume in costumes) {
s.push("- " + costume.costumeName);
}
s.push("");
if (sounds.length) {
s.push(h2(Translator.map("Sounds")));
for each (var sound:ScratchSound in sounds) {
s.push("- " + sound.soundName);
}
s.push("");
}
if (scripts.length) {
s.push(h2(Translator.map("Scripts")));
for each (var script:Block in scripts) {
s.push(script.getSummary());
s.push("")
}
}
return s.join("\n");
}
protected static function h1(s:String, ch:String = "="):String {
return s + "\n" + new Array(s.length + 1).join(ch) + "\n";
}
protected static function h2(s:String):String {
return h1(s, "-");
}
}}