forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScratch.as
More file actions
1105 lines (966 loc) · 34.5 KB
/
Scratch.as
File metadata and controls
1105 lines (966 loc) · 34.5 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
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.
*/
// Scratch.as
// John Maloney, September 2009
//
// This is the top-level application.
package {
import extensions.ExtensionManager;
import flash.display.*;
import flash.events.*;
import flash.external.ExternalInterface;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.net.LocalConnection;
import flash.system.*;
import flash.text.*;
import flash.utils.*;
import interpreter.*;
import blocks.*;
import scratch.*;
import watchers.ListWatcher;
import translation.*;
import ui.*;
import ui.media.*;
import ui.parts.*;
import uiwidgets.*;
import util.*;
public class Scratch extends Sprite {
// Version
public static const versionString:String = 'v417';
public static var app:Scratch; // static reference to the app, used for debugging
// Display modes
public var editMode:Boolean; // true when project editor showing, false when only the player is showing
public var isOffline:Boolean; // true when running as an offline (i.e. stand-alone) app
public var isSmallPlayer:Boolean; // true when displaying as a scaled-down player (e.g. in search results)
public var stageIsContracted:Boolean; // true when the stage is half size to give more space on small screens
public var isIn3D:Boolean;
public var render3D:IRenderIn3D;
public var jsEnabled:Boolean = false; // true when the SWF can talk to the webpage
// Runtime
public var runtime:ScratchRuntime;
public var interp:Interpreter;
public var extensionManager:ExtensionManager;
public var server:Server;
public var gh:GestureHandler;
public var projectID:String = '';
public var projectOwner:String = '';
public var projectIsPrivate:Boolean;
public var oldWebsiteURL:String = '';
public var loadInProgress:Boolean;
public var debugOps:Boolean = false;
public var debugOpCmd:String = '';
protected var autostart:Boolean;
private var viewedObject:ScratchObj;
private var lastTab:String = 'scripts';
protected var wasEdited:Boolean; // true if the project was edited and autosaved
private var _usesUserNameBlock:Boolean = false;
protected var languageChanged:Boolean; // set when language changed
// UI Elements
public var playerBG:Shape;
public var palette:BlockPalette;
public var scriptsPane:ScriptsPane;
public var stagePane:ScratchStage;
public var mediaLibrary:MediaLibrary;
public var lp:LoadProgress;
public var cameraDialog:CameraDialog;
// UI Parts
public var libraryPart:LibraryPart;
protected var topBarPart:TopBarPart;
protected var stagePart:StagePart;
private var tabsPart:TabsPart;
protected var scriptsPart:ScriptsPart;
public var imagesPart:ImagesPart;
protected var soundsPart:SoundsPart;
public function Scratch() {
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
isOffline = loaderInfo.url.indexOf('http:') == -1;
checkFlashVersion();
initServer();
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.frameRate = 30;
Block.setFonts(10, 9, true, 0); // default font sizes
Block.MenuHandlerFunction = BlockMenus.BlockMenuHandler;
CursorTool.init(this);
app = this;
stagePane = new ScratchStage();
gh = new GestureHandler(this, (loaderInfo.parameters['inIE'] == 'true'));
initInterpreter();
initRuntime();
extensionManager = new ExtensionManager(this);
Translator.initializeLanguageList();
playerBG = new Shape(); // create, but don't add
addParts();
stage.addEventListener(MouseEvent.MOUSE_DOWN, gh.mouseDown);
stage.addEventListener(MouseEvent.MOUSE_MOVE, gh.mouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, gh.mouseUp);
stage.addEventListener('rightClick', gh.rightMouseClick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, runtime.keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, runtime.keyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); // to handle escape key
stage.addEventListener(Event.ENTER_FRAME, step);
stage.addEventListener(Event.RESIZE, onResize);
setEditMode(startInEditMode());
// install project before calling fixLayout()
if (editMode) runtime.installNewProject();
else runtime.installEmptyProject();
fixLayout();
//Analyze.collectAssets(0, 119110);
//Analyze.checkProjects(56086, 64220);
//Analyze.countMissingAssets();
}
protected function initTopBarPart():void {
topBarPart = new TopBarPart(this);
}
protected function initInterpreter():void {
interp = new Interpreter(this);
}
protected function initRuntime():void {
runtime = new ScratchRuntime(this, interp);
}
protected function initServer():void {
server = new Server();
}
public function showTip(tipName:String):void {}
public function closeTips():void {}
public function reopenTips():void {}
protected function startInEditMode():Boolean {
return isOffline;
}
public function getMediaLibrary(app:Scratch, type:String, whenDone:Function):MediaLibrary {
return new MediaLibrary(app, type, whenDone);
}
public function getMediaPane(app:Scratch, type:String):MediaPane {
return new MediaPane(app, type);
}
public function getScratchStage():ScratchStage {
return new ScratchStage();
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
if (event.error is Error)
{
var error:Error = event.error as Error;
logException(error);
}
else if (event.error is ErrorEvent)
{
var errorEvent:ErrorEvent = event.error as ErrorEvent;
logMessage(errorEvent.toString());
}
}
public function log(s:String):void {
trace(s);
}
public function logException(e:Error):void {}
public function logMessage(msg:String, extra_data:Object=null):void {}
public function loadProjectFailed():void {}
[Embed(source='../libs/RenderIn3D.swf', mimeType='application/octet-stream')]
public static const MySwfData:Class;
protected function checkFlashVersion():void {
if(Capabilities.playerType != "Desktop" || Capabilities.version.indexOf('IOS') === 0) {
var isArmCPU:Boolean = (jsEnabled && ExternalInterface.call("window.navigator.userAgent.toString").indexOf('CrOS arm') > -1);
var versionString:String = Capabilities.version.substr(Capabilities.version.indexOf(' ')+1);
var versionParts:Array = versionString.split(',');
var majorVersion:int = parseInt(versionParts[0]);
var minorVersion:int = parseInt(versionParts[1]);
if((majorVersion > 11 || (majorVersion == 11 && minorVersion >=1)) && !isArmCPU && Capabilities.cpuArchitecture == 'x86') {
loadRenderLibrary();
return;
}
}
render3D = null;
}
protected var loading3DLib:Boolean = false;
protected function loadRenderLibrary():void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoaded);
// we need the loaded code to be in the same (main) application domain
var ctx:LoaderContext = new LoaderContext(false, loaderInfo.applicationDomain);
ctx.allowCodeImport = true;
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
loader.loadBytes(new MySwfData() as ByteArray, ctx);
loading3DLib = true;
}
protected function handleRenderCallback(enabled:Boolean):void {
loading3DLib = false;
if(!enabled) {
go2D();
render3D = null;
}
else {
for(var i:int=0; i<stagePane.numChildren; ++i) {
var spr:ScratchSprite = (stagePane.getChildAt(i) as ScratchSprite);
if(spr) {
spr.clearCachedBitmap();
spr.updateCostume();
spr.applyFilters();
}
}
stagePane.clearCachedBitmap();
stagePane.updateCostume();
stagePane.applyFilters();
}
}
protected function onSwfLoaded(e:Event):void {
var info:LoaderInfo = LoaderInfo(e.target);
var r3dClass:Class = info.applicationDomain.getDefinition("DisplayObjectContainerIn3D") as Class;
render3D = (new r3dClass() as IRenderIn3D);
render3D.setStatusCallback(handleRenderCallback);
}
public function clearCachedBitmaps():void {
for(var i:int=0; i<stagePane.numChildren; ++i) {
var spr:ScratchSprite = (stagePane.getChildAt(i) as ScratchSprite);
if(spr) spr.clearCachedBitmap();
}
stagePane.clearCachedBitmap();
// unsupported technique that seems to force garbage collection
try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
} catch (e:Error) {}
}
public function go3D():void {
if(!render3D || isIn3D) return;
var i:int = stagePart.getChildIndex(stagePane);
stagePart.removeChild(stagePane);
render3D.setStage(stagePane, stagePane.penLayer);
stagePart.addChildAt(stagePane, i);
isIn3D = true;
}
public function go2D():void {
if(!render3D || !isIn3D) return;
var i:int = stagePart.getChildIndex(stagePane);
stagePart.removeChild(stagePane);
render3D.setStage(null, null);
stagePart.addChildAt(stagePane, i);
isIn3D = false;
for(i=0; i<stagePane.numChildren; ++i) {
var spr:ScratchSprite = (stagePane.getChildAt(i) as ScratchSprite);
if(spr) {
spr.clearCachedBitmap();
spr.updateCostume();
spr.applyFilters();
}
}
stagePane.clearCachedBitmap();
stagePane.updateCostume();
stagePane.applyFilters();
}
private var debugRect:Shape;
public function showDebugRect(r:Rectangle):void {
// Used during debugging...
var p:Point = stagePane.localToGlobal(new Point(0, 0));
if (!debugRect) debugRect = new Shape();
var g:Graphics = debugRect.graphics;
g.clear();
if (r) {
g.lineStyle(2, 0xFFFF00);
g.drawRect(p.x + r.x, p.y + r.y, r.width, r.height);
addChild(debugRect);
}
}
public function strings():Array {
return [
'a copy of the project file on your computer.',
'Project not saved!', 'Save now', 'Not saved; project did not load.',
'Save now', 'Saved',
'Revert', 'Undo Revert', 'Reverting...',
'Throw away all changes since opening this project?',
];
}
public function viewedObj():ScratchObj { return viewedObject; }
public function stageObj():ScratchStage { return stagePane; }
public function projectName():String { return stagePart.projectName(); }
public function highlightSprites(sprites:Array):void { libraryPart.highlight(sprites); }
public function refreshImageTab(fromEditor:Boolean):void { imagesPart.refresh(fromEditor); }
public function refreshSoundTab():void { soundsPart.refresh(); }
public function selectCostume():void { imagesPart.selectCostume(); }
public function selectSound(snd:ScratchSound):void { soundsPart.selectSound(snd); }
public function clearTool():void { CursorTool.setTool(null); topBarPart.clearToolButtons(); }
public function tabsRight():int { return tabsPart.x + tabsPart.w; }
public function enableEditorTools(flag:Boolean):void { imagesPart.editor.enableTools(flag); }
public function get usesUserNameBlock():Boolean {
return _usesUserNameBlock;
}
public function set usesUserNameBlock(value:Boolean):void {
_usesUserNameBlock = value;
stagePart.refresh();
}
public function updatePalette(clearCaches:Boolean = true):void {
// Note: updatePalette() is called after changing variable, list, or procedure
// definitions, so this is a convenient place to clear the interpreter's caches.
if (isShowing(scriptsPart)) scriptsPart.updatePalette();
if (clearCaches) runtime.clearAllCaches();
}
public function setProjectName(s:String):void {
if (s.slice(-3) == '.sb') s = s.slice(0, -3);
if (s.slice(-4) == '.sb2') s = s.slice(0, -4);
stagePart.setProjectName(s);
}
protected var wasEditing:Boolean;
public function setPresentationMode(enterPresentation:Boolean):void {
if (enterPresentation) {
wasEditing = editMode;
if (wasEditing) {
setEditMode(false);
if(jsEnabled) ExternalInterface.call('tip_bar_api.hide');
}
} else {
if (wasEditing) {
setEditMode(true);
if(jsEnabled) ExternalInterface.call('tip_bar_api.show');
}
}
if (isOffline) {
stage.displayState = enterPresentation ? StageDisplayState.FULL_SCREEN_INTERACTIVE : StageDisplayState.NORMAL;
}
for each (var o:ScratchObj in stagePane.allObjects()) o.applyFilters();
if (lp) fixLoadProgressLayout();
stagePane.updateCostume();
if(isIn3D) render3D.onStageResize();
}
private function keyDown(evt:KeyboardEvent):void {
// Escape exists presentation mode.
if ((evt.charCode == 27) && stagePart.isInPresentationMode()) {
setPresentationMode(false);
stagePart.exitPresentationMode();
}
// Handle enter key
// else if(evt.keyCode == 13 && !stage.focus) {
// stagePart.playButtonPressed(null);
// evt.preventDefault();
// evt.stopImmediatePropagation();
// }
// Handle ctrl-m and toggle 2d/3d mode
else if(evt.ctrlKey && evt.charCode == 109) {
isIn3D ? go2D() : go3D();
evt.preventDefault();
evt.stopImmediatePropagation();
}
}
private function setSmallStageMode(flag:Boolean):void {
stageIsContracted = flag;
stagePart.refresh();
fixLayout();
libraryPart.refresh();
tabsPart.refresh();
stagePane.applyFilters();
stagePane.updateCostume();
}
public function projectLoaded():void {
removeLoadProgressBox();
System.gc();
if (autostart) runtime.startGreenFlags(true);
saveNeeded = false;
// translate the blocks of the newly loaded project
for each (var o:ScratchObj in stagePane.allObjects()) {
o.updateScriptsAfterTranslation();
}
}
protected function step(e:Event):void {
// Step the runtime system and all UI components.
gh.step();
runtime.stepRuntime();
Transition.step(null);
stagePart.step();
libraryPart.step();
scriptsPart.step();
imagesPart.step();
}
public function updateSpriteLibrary(sortByIndex:Boolean = false):void { libraryPart.refresh() }
public function threadStarted():void { stagePart.threadStarted() }
public function selectSprite(obj:ScratchObj):void {
if (isShowing(imagesPart)) imagesPart.editor.shutdown();
if (isShowing(soundsPart)) soundsPart.editor.shutdown();
viewedObject = obj;
libraryPart.refresh();
tabsPart.refresh();
if (isShowing(imagesPart)) {
imagesPart.refresh();
}
if (isShowing(soundsPart)) {
soundsPart.currentIndex = 0;
soundsPart.refresh();
}
if (isShowing(scriptsPart)) {
scriptsPart.updatePalette();
scriptsPane.viewScriptsFor(obj);
scriptsPart.updateSpriteWatermark();
}
}
public function setTab(tabName:String):void {
if (isShowing(imagesPart)) imagesPart.editor.shutdown();
if (isShowing(soundsPart)) soundsPart.editor.shutdown();
hide(scriptsPart);
hide(imagesPart);
hide(soundsPart);
if (!editMode) return;
if (tabName == 'images') {
show(imagesPart);
imagesPart.refresh();
} else if (tabName == 'sounds') {
soundsPart.refresh();
show(soundsPart);
} else if (tabName && (tabName.length > 0)) {
tabName = 'scripts';
scriptsPart.updatePalette();
scriptsPane.viewScriptsFor(viewedObject);
scriptsPart.updateSpriteWatermark();
show(scriptsPart);
}
show(tabsPart);
show(stagePart); // put stage in front
tabsPart.selectTab(tabName);
lastTab = tabName;
if (saveNeeded) setSaveNeeded(true); // save project when switching tabs, if needed (but NOT while loading!)
}
public function installStage(newStage:ScratchStage):void {
var showGreenflagOverlay:Boolean = shouldShowGreenFlag();
stagePart.installStage(newStage, showGreenflagOverlay);
selectSprite(newStage);
libraryPart.refresh();
setTab('scripts');
scriptsPart.resetCategory();
wasEdited = false;
}
protected function shouldShowGreenFlag():Boolean {
return !(autostart || editMode);
}
protected function addParts():void {
initTopBarPart();
stagePart = getStagePart();
libraryPart = getLibraryPart();
tabsPart = new TabsPart(this);
scriptsPart = new ScriptsPart(this);
imagesPart = new ImagesPart(this);
soundsPart = new SoundsPart(this);
addChild(topBarPart);
addChild(stagePart);
addChild(libraryPart);
addChild(tabsPart);
}
protected function getStagePart():StagePart {
return new StagePart(this);
}
protected function getLibraryPart():LibraryPart {
return new LibraryPart(this);
}
// -----------------------------
// UI Modes and Resizing
//------------------------------
public function setEditMode(newMode:Boolean):void {
Menu.removeMenusFrom(stage);
editMode = newMode;
if (editMode) {
hide(playerBG);
show(topBarPart);
show(libraryPart);
show(tabsPart);
setTab(lastTab);
stagePart.hidePlayButton();
runtime.edgeTriggersEnabled = true;
} else {
addChildAt(playerBG, 0); // behind everything
playerBG.visible = false;
hide(topBarPart);
hide(libraryPart);
hide(tabsPart);
setTab(null); // hides scripts, images, and sounds
}
show(stagePart); // put stage in front
fixLayout();
stagePart.refresh();
}
protected function hide(obj:DisplayObject):void { if (obj.parent) obj.parent.removeChild(obj) }
protected function show(obj:DisplayObject):void { addChild(obj) }
protected function isShowing(obj:DisplayObject):Boolean { return obj.parent != null }
public function onResize(e:Event):void {
fixLayout();
}
public function fixLayout():void {
var w:int = stage.stageWidth;
var h:int = stage.stageHeight - 1; // fix to show bottom border...
w = Math.ceil(w / scaleX);
h = Math.ceil(h / scaleY);
updateLayout(w, h);
}
protected function updateLayout(w:int, h:int):void {
topBarPart.x = 0;
topBarPart.y = 0;
topBarPart.setWidthHeight(w, 28);
var extraW:int = 2;
var extraH:int = stagePart.computeTopBarHeight() + 1;
if (editMode) {
// adjust for global scale (from browser zoom)
if (stageIsContracted) {
stagePart.setWidthHeight(240 + extraW, 180 + extraH, 0.5);
} else {
stagePart.setWidthHeight(480 + extraW, 360 + extraH, 1);
}
stagePart.x = 5;
stagePart.y = topBarPart.bottom() + 5;
} else {
drawBG();
var pad:int = (w > 550) ? 16 : 0; // add padding for full-screen mode
var scale:Number = Math.min((w - extraW - pad) / 480, (h - extraH - pad) / 360);
scale = Math.max(0.01, scale);
var scaledW:int = Math.floor((scale * 480) / 4) * 4; // round down to a multiple of 4
scale = scaledW / 480;
var playerW:Number = (scale * 480) + extraW;
var playerH:Number = (scale * 360) + extraH;
stagePart.setWidthHeight(playerW, playerH, scale);
stagePart.x = int((w - playerW) / 2);
stagePart.y = int((h - playerH) / 2);
fixLoadProgressLayout();
return;
}
libraryPart.x = stagePart.x;
libraryPart.y = stagePart.bottom() + 18;
libraryPart.setWidthHeight(stagePart.w, h - libraryPart.y);
tabsPart.x = stagePart.right() + 5;
tabsPart.y = topBarPart.bottom() + 5;
tabsPart.fixLayout();
// the content area shows the part associated with the currently selected tab:
var contentY:int = tabsPart.y + 27;
updateContentArea(tabsPart.x, contentY, w - tabsPart.x - 6, h - contentY - 5, h);
}
protected function updateContentArea(contentX:int, contentY:int, contentW:int, contentH:int, fullH:int):void {
imagesPart.x = soundsPart.x = scriptsPart.x = contentX;
imagesPart.y = soundsPart.y = scriptsPart.y = contentY;
imagesPart.setWidthHeight(contentW, contentH);
soundsPart.setWidthHeight(contentW, contentH);
scriptsPart.setWidthHeight(contentW, contentH);
if (mediaLibrary) mediaLibrary.setWidthHeight(topBarPart.w, fullH);
if (frameRateGraph) {
frameRateGraph.y = stage.stageHeight - frameRateGraphH;
addChild(frameRateGraph); // put in front
}
if(isIn3D) render3D.onStageResize();
}
private function drawBG():void {
var g:Graphics = playerBG.graphics;
g.clear();
g.beginFill(0);
g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
}
// -----------------------------
// Translations utilities
//------------------------------
public function translationChanged():void {
// The translation has changed. Fix scripts and update the UI.
// directionChanged is true if the writing direction (e.g. left-to-right) has changed.
for each (var o:ScratchObj in stagePane.allObjects()) {
o.updateScriptsAfterTranslation();
}
var uiLayer:Sprite = app.stagePane.getUILayer();
for (var i:int = 0; i < uiLayer.numChildren; ++i) {
var lw:ListWatcher = uiLayer.getChildAt(i) as ListWatcher;
if (lw) lw.updateTranslation();
}
topBarPart.updateTranslation();
stagePart.updateTranslation();
libraryPart.updateTranslation();
tabsPart.updateTranslation();
updatePalette(false);
imagesPart.updateTranslation();
soundsPart.updateTranslation();
}
// -----------------------------
// Menus
//------------------------------
public function showFileMenu(b:*):void {
var m:Menu = new Menu(null, 'File', CSS.topBarColor, 28);
m.addItem('New', createNewProject);
m.addLine();
// Derived class will handle this
addFileMenuItems(b, m);
m.showOnStage(stage, b.x, topBarPart.bottom() - 1);
}
protected function addFileMenuItems(b:*, m:Menu):void {
m.addItem('Load Project', runtime.selectProjectFile);
m.addItem('Save Project', exportProjectToFile);
if (canUndoRevert()) {
m.addLine();
m.addItem('Undo Revert', undoRevert);
} else if (canRevert()) {
m.addLine();
m.addItem('Revert', revertToOriginalProject);
}
if (b.lastEvent.shiftKey && jsEnabled) {
m.addLine();
m.addItem('Import experimental extension', function():void {
function loadJSExtension(dialog:DialogBox):void {
var url:String = dialog.fields['URL'].text.replace(/^\s+|\s+$/g, '');
if (url.length == 0) return;
ExternalInterface.call('ScratchExtensions.loadExternalJS', url);
}
var d:DialogBox = new DialogBox(loadJSExtension);
d.addTitle('Load Javascript Scratch Extension');
d.addField('URL', 120);
d.addAcceptCancelButtons('Load');
d.showOnStage(app.stage);
});
}
}
public function showEditMenu(b:*):void {
var m:Menu = new Menu(null, 'More', CSS.topBarColor, 28);
m.addItem('Undelete', runtime.undelete, runtime.canUndelete());
m.addLine();
m.addItem('Small stage layout', toggleSmallStage, true, stageIsContracted);
m.addItem('Turbo mode', toggleTurboMode, true, interp.turboMode);
addEditMenuItems(b, m);
var p:Point = b.localToGlobal(new Point(0, 0));
m.showOnStage(stage, b.x, topBarPart.bottom() - 1);
}
protected function addEditMenuItems(b:*, m:Menu):void {}
protected function canExportInternals():Boolean {
return false;
}
private function showAboutDialog():void {
DialogBox.notify(
'Scratch 2.0 ' + versionString,
'\n\nCopyright © 2012 MIT Media Laboratory' +
'\nAll rights reserved.' +
'\n\nPlease do not distribute!', stage);
}
protected function createNewProject(ignore:* = null):void {
function clearProject():void {
startNewProject('', '');
setProjectName('Untitled');
topBarPart.refresh();
stagePart.refresh();
}
saveProjectAndThen(clearProject);
}
protected function saveProjectAndThen(postSaveAction:Function = null):void {
// Give the user a chance to save their project, if needed, then call postSaveAction.
function doNothing():void {}
function cancel():void { d.cancel(); }
function proceedWithoutSaving():void { d.cancel(); postSaveAction() }
function save():void {
d.cancel();
exportProjectToFile(); // if this succeeds, saveNeeded will become false
if (!saveNeeded) postSaveAction();
}
if (postSaveAction == null) postSaveAction = doNothing;
if (!saveNeeded) {
postSaveAction();
return;
}
var d:DialogBox = new DialogBox();
d.addTitle(Translator.map('Save project') + '?');
d.addButton('Save', save);
d.addButton('Don\'t save', proceedWithoutSaving);
d.addButton('Cancel', cancel);
d.showOnStage(stage);
}
protected function exportProjectToFile(fromJS:Boolean = false):void {
function squeakSoundsConverted():void {
scriptsPane.saveScripts(false);
var defaultName:String = (projectName().length > 0) ? projectName() + '.sb2' : 'project.sb2';
var zipData:ByteArray = projIO.encodeProjectAsZipFile(stagePane);
var file:FileReference = new FileReference();
file.addEventListener(Event.COMPLETE, fileSaved);
file.save(zipData, fixFileName(defaultName));
}
function fileSaved(e:Event):void {
if (!fromJS) setProjectName(e.target.name);
}
if (loadInProgress) return;
var projIO:ProjectIO = new ProjectIO(this);
projIO.convertSqueakSounds(stagePane, squeakSoundsConverted);
}
public static function fixFileName(s:String):String {
// Replace illegal characters in the given string with dashes.
const illegal:String = '\\/:*?"<>|%';
var result:String = '';
for (var i:int = 0; i < s.length; i++) {
var ch:String = s.charAt(i);
if ((i == 0) && ('.' == ch)) ch = '-'; // don't allow leading period
result += (illegal.indexOf(ch) > -1) ? '-' : ch;
}
return result;
}
public function toggleSmallStage():void {
setSmallStageMode(!stageIsContracted);
}
public function toggleTurboMode():void {
interp.turboMode = !interp.turboMode;
stagePart.refresh();
}
public function handleTool(tool:String, evt:MouseEvent):void { }
// -----------------------------
// Project Management and Sign in
//------------------------------
public function setLanguagePressed(b:IconButton):void {
function setLanguage(lang:String):void {
Translator.setLanguage(lang);
languageChanged = true;
}
if (Translator.languages.length == 0) return; // empty language list
var m:Menu = new Menu(setLanguage, 'Language', CSS.topBarColor, 28);
if (b.lastEvent.shiftKey) {
m.addItem('import translation file');
m.addItem('set font size');
m.addLine();
}
for each (var entry:Array in Translator.languages) {
m.addItem(entry[1], entry[0]);
}
var p:Point = b.localToGlobal(new Point(0, 0));
m.showOnStage(stage, b.x, topBarPart.bottom() - 1);
}
public function startNewProject(newOwner:String, newID:String):void {
runtime.installNewProject();
projectOwner = newOwner;
projectID = newID;
projectIsPrivate = true;
loadInProgress = false;
}
// -----------------------------
// Save status
//------------------------------
public var saveNeeded:Boolean;
public function setSaveNeeded(saveNow:Boolean = false):void {
saveNow = false;
// Set saveNeeded flag and update the status string.
saveNeeded = true;
if (!wasEdited) saveNow = true; // force a save on first change
clearRevertUndo();
}
protected function clearSaveNeeded():void {
// Clear saveNeeded flag and update the status string.
function twoDigits(n:int):String { return ((n < 10) ? '0' : '') + n }
saveNeeded = false;
wasEdited = true;
}
// -----------------------------
// Project Reverting
//------------------------------
protected var originalProj:ByteArray;
private var revertUndo:ByteArray;
public function saveForRevert(projData:ByteArray, isNew:Boolean, onServer:Boolean = false):void {
originalProj = projData;
revertUndo = null;
}
protected function doRevert():void {
runtime.installProjectFromData(originalProj, false);
}
protected function revertToOriginalProject():void {
function preDoRevert():void {
revertUndo = new ProjectIO(Scratch.app).encodeProjectAsZipFile(stagePane);
doRevert();
}
if (!originalProj) return;
DialogBox.confirm('Throw away all changes since opening this project?', stage, preDoRevert);
}
protected function undoRevert():void {
if (!revertUndo) return;
runtime.installProjectFromData(revertUndo, false);
revertUndo = null;
}
protected function canRevert():Boolean { return originalProj != null }
protected function canUndoRevert():Boolean { return revertUndo != null }
private function clearRevertUndo():void { revertUndo = null }
public function addNewSprite(spr:ScratchSprite, showImages:Boolean = false, atMouse:Boolean = false):void {
var c:ScratchCostume, byteCount:int;
for each (c in spr.costumes) byteCount + c.baseLayerData.length;
if (!okayToAdd(byteCount)) return; // not enough room
spr.objName = stagePane.unusedSpriteName(spr.objName);
spr.indexInLibrary = 1000000; // add at end of library
spr.setScratchXY(int(200 * Math.random() - 100), int(100 * Math.random() - 50));
if (atMouse) spr.setScratchXY(stagePane.scratchMouseX(), stagePane.scratchMouseY());
stagePane.addChild(spr);
selectSprite(spr);
setTab(showImages ? 'images' : 'scripts');
setSaveNeeded(true);
libraryPart.refresh();
for each (c in spr.costumes) {
if (ScratchCostume.isSVGData(c.baseLayerData)) c.setSVGData(c.baseLayerData, false);
}
}
public function addSound(snd:ScratchSound, targetObj:ScratchObj = null):void {
if (snd.soundData && !okayToAdd(snd.soundData.length)) return; // not enough room
if (!targetObj) targetObj = viewedObj();
snd.soundName = targetObj.unusedSoundName(snd.soundName);
targetObj.sounds.push(snd);
setSaveNeeded(true);
if (targetObj == viewedObj()) {
soundsPart.selectSound(snd);
setTab('sounds');
}
}
public function addCostume(c:ScratchCostume, targetObj:ScratchObj = null):void {
if (!c.baseLayerData) c.prepareToSave();
if (!okayToAdd(c.baseLayerData.length)) return; // not enough room
if (!targetObj) targetObj = viewedObj();
c.costumeName = targetObj.unusedCostumeName(c.costumeName);
targetObj.costumes.push(c);
targetObj.showCostumeNamed(c.costumeName);
setSaveNeeded(true);
if (targetObj == viewedObj()) setTab('images');
}
public function okayToAdd(newAssetBytes:int):Boolean {
// Return true if there is room to add an asset of the given size.
// Otherwise, return false and display a warning dialog.
const assetByteLimit:int = 50 * 1024 * 1024; // 50 megabytes
var assetByteCount:int = newAssetBytes;
for each (var obj:ScratchObj in stagePane.allObjects()) {
for each (var c:ScratchCostume in obj.costumes) {
if (!c.baseLayerData) c.prepareToSave();
assetByteCount += c.baseLayerData.length;
}
for each (var snd:ScratchSound in obj.sounds) assetByteCount += snd.soundData.length;
}
if (assetByteCount > assetByteLimit) {
var overBy:int = Math.max(1, (assetByteCount - assetByteLimit) / 1024);
DialogBox.notify(
'Sorry!',
'Adding that media asset would put this project over the size limit by ' + overBy + ' KB\n' +
'Please remove some costumes, backdrops, or sounds before adding additional media.',
stage);
return false;
}
return true;
}
// -----------------------------
// Flash sprite (helps connect a sprite on thestage with a sprite library entry)
//------------------------------
public function flashSprite(spr:ScratchSprite):void {
function doFade(alpha:Number):void { box.alpha = alpha }
function deleteBox():void { if (box.parent) { box.parent.removeChild(box) }}
var r:Rectangle = spr.getVisibleBounds(this);
var box:Shape = new Shape();
box.graphics.lineStyle(3, CSS.overColor, 1, true);
box.graphics.beginFill(0x808080);
box.graphics.drawRoundRect(0, 0, r.width, r.height, 12, 12);
box.x = r.x;
box.y = r.y;
addChild(box);
Transition.cubic(doFade, 1, 0, 0.5, deleteBox);
}
// -----------------------------
// Download Progress
//------------------------------
public function addLoadProgressBox(title:String):void {
removeLoadProgressBox();
lp = new LoadProgress();
lp.setTitle(title);
stage.addChild(lp);
fixLoadProgressLayout();
}
public function removeLoadProgressBox():void {
if (lp && lp.parent) lp.parent.removeChild(lp);
lp = null;
}