Add the ability to group image assets into a .zip file, removing the number of indivi...
[potlatch2.git] / net / systemeD / potlatch2 / Preloader.as
1 package net.systemeD.potlatch2 {
2     import flash.display.DisplayObject;
3     import flash.display.GradientType;
4     import flash.display.Sprite;
5     import flash.filters.DropShadowFilter;
6     import flash.geom.Matrix;
7     import flash.text.TextField;
8     import flash.text.TextFormat;
9     import net.systemeD.halcyon.ImageBank;
10
11
12     public class Preloader extends net.systemeD.potlatch2.PreloaderDisplayBase {
13
14        [Embed("../../../embedded/potlatch2logo.png") ]
15         [Bindable] public var Logo:Class;
16
17         private var t:TextField;
18         private var f:DropShadowFilter=new DropShadowFilter(2,45,0x000000,0.5)
19         private var p2Logo:DisplayObject;
20         private var bar:Sprite=new Sprite();
21         private var barFrame:Sprite;
22         private var mainColor:uint=0x045966;
23
24                 private var assetscount:uint=0;
25                 private var queuedEvents:Array=[];
26
27         public function Preloader()
28         {
29             super();
30         }
31
32         // This is called when the preloader has been created as a child on the stage.
33         //  Put all real initialization here.
34         override public function initialize():void
35         {
36             super.initialize();
37
38             clear();  // clear here, rather than in draw(), to speed up the drawing
39
40             var indent:int = 20;
41             var height:int = 20;
42
43             //creates all visual elements
44             createAssets();
45
46                         // request .zip files
47                         if (loaderInfo.parameters['assets']) {
48                                 ImageBank.getInstance().addEventListener(ImageBank.ZIP_LOADED, zipLoaded);
49                                 for each (var file:String in loaderInfo.parameters['assets'].split(';')) {
50                                         var asset:Array=file.split('=');
51                                         assetscount++;
52                                         ImageBank.getInstance().loadFromZip(asset[0],asset[1]);
53                                 }
54                         }
55         }
56
57                 override protected function initProgressHandler(e:Event):void {
58                         if (assetscount>0) {
59                                 queuedEvents.push(e);
60                                 e.stopImmediatePropagation();
61                         }
62                         draw();
63                 }
64                 
65                 private function zipLoaded(e:Event):void {
66                         assetscount--; if (assetscount>0) return;
67                         for each (var q:Event in queuedEvents) {
68                                 dispatchEvent(q);
69                         }
70                 }
71                 
72         //this is our "animation" bit
73         override protected function draw():void
74         {
75             t.text = int(_fractionLoaded*100).toString()+"%";
76             //make objects below follow loading progress
77             //positions are completely arbitrary
78             //d tells us the x value of where the loading bar is at
79             var d:Number=barFrame.x + barFrame.width * _fractionLoaded
80             t.x = d - t.width - 25;
81             bar.graphics.beginFill(0xffffff,1)
82             bar.graphics.drawRoundRectComplex(0,0,bar.width * _fractionLoaded,15,12,0,0,12);
83             bar.graphics.endFill();
84         }
85
86         protected function createAssets():void
87         {
88             //create the logo
89             p2Logo = new Logo();
90             p2Logo.y = stageHeight/2 - p2Logo.height/2;
91             p2Logo.x = stageWidth/2 - p2Logo.width/2;
92             //p2Logo.filters = [f];
93             addChild(p2Logo);
94
95             //create bar
96             bar = new Sprite();
97              bar.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
98             bar.x = stageWidth/2 - bar.width/2;
99             bar.y = stageHeight/1.2 - bar.height/2;
100             bar.filters = [f];
101             addChild(bar);
102
103             //create bar frame
104             barFrame = new Sprite();
105             barFrame.graphics.lineStyle(2,0xFFFFFF,1)
106             barFrame.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
107             barFrame.graphics.endFill();
108             barFrame.x = stageWidth/2 - barFrame.width/2;
109             barFrame.y = stageHeight/1.2 - barFrame.height/2;
110             barFrame.filters = [f];
111             addChild(barFrame);
112
113             //create text field to show percentage of loading
114             t = new TextField()
115             t.y = barFrame.y-27;
116             t.filters=[f];
117             addChild(t);
118             //we can format our text
119             var s:TextFormat=new TextFormat("Verdana",null,0xFFFFFF,null,null,null,null,null,"right");
120             t.defaultTextFormat=s;
121         }
122
123         protected function clear():void
124         {
125             // Draw gradient background
126             var b:Sprite = new Sprite;
127              var matrix:Matrix =  new Matrix();
128             matrix.createGradientBox(stageWidth, stageHeight, Math.PI/2);
129             b.graphics.beginGradientFill(GradientType.LINEAR,
130                                         [mainColor, mainColor],
131                                         [1,1],
132                                         [0,255],
133                                         matrix
134                                         );
135             b.graphics.drawRect(0, 0, stageWidth, stageHeight);
136             b.graphics.endFill();
137             addChild(b);
138         }
139     }
140
141 }
142