Merge branch 'master' of https://github.com/systemed/potlatch2
[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.FileBank;
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         public function Preloader()
25         {
26             super();
27         }
28
29         // This is called when the preloader has been created as a child on the stage.
30         //  Put all real initialization here.
31         override public function initialize():void
32         {
33             super.initialize();
34
35             clear();  // clear here, rather than in draw(), to speed up the drawing
36
37             var indent:int = 20;
38             var height:int = 20;
39
40             //creates all visual elements
41             createAssets();
42
43                         // request .zip files
44                         if (loaderInfo.parameters['assets']) {
45                                 for each (var file:String in loaderInfo.parameters['assets'].split(';')) {
46                                         var asset:Array=file.split('=');
47                     if (asset.length == 1) {
48                         FileBank.getInstance().addFromZip(asset[0]);
49                     } else {
50                         FileBank.getInstance().addFromZip(asset[0], asset[1]);
51                     }
52                                 }
53                         }
54         }
55                 
56         //this is our "animation" bit
57         override protected function draw():void
58         {
59             t.text = int(_fractionLoaded*100).toString()+"%";
60             //make objects below follow loading progress
61             //positions are completely arbitrary
62             //d tells us the x value of where the loading bar is at
63             var d:Number=barFrame.x + barFrame.width * _fractionLoaded
64             t.x = d - t.width - 25;
65             bar.graphics.beginFill(0xffffff,1)
66             bar.graphics.drawRoundRectComplex(0,0,bar.width * _fractionLoaded,15,12,0,0,12);
67             bar.graphics.endFill();
68         }
69
70         protected function createAssets():void
71         {
72             //create the logo
73             p2Logo = new Logo();
74             p2Logo.y = stageHeight/2 - p2Logo.height/2;
75             p2Logo.x = stageWidth/2 - p2Logo.width/2;
76             //p2Logo.filters = [f];
77             addChild(p2Logo);
78
79             //create bar
80             bar = new Sprite();
81              bar.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
82             bar.x = stageWidth/2 - bar.width/2;
83             bar.y = stageHeight/1.2 - bar.height/2;
84             bar.filters = [f];
85             addChild(bar);
86
87             //create bar frame
88             barFrame = new Sprite();
89             barFrame.graphics.lineStyle(2,0xFFFFFF,1)
90             barFrame.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
91             barFrame.graphics.endFill();
92             barFrame.x = stageWidth/2 - barFrame.width/2;
93             barFrame.y = stageHeight/1.2 - barFrame.height/2;
94             barFrame.filters = [f];
95             addChild(barFrame);
96
97             //create text field to show percentage of loading
98             t = new TextField()
99             t.y = barFrame.y-27;
100             t.filters=[f];
101             addChild(t);
102             //we can format our text
103             var s:TextFormat=new TextFormat("Verdana",null,0xFFFFFF,null,null,null,null,null,"right");
104             t.defaultTextFormat=s;
105         }
106
107         protected function clear():void
108         {
109             // Draw gradient background
110             var b:Sprite = new Sprite;
111              var matrix:Matrix =  new Matrix();
112             matrix.createGradientBox(stageWidth, stageHeight, Math.PI/2);
113             b.graphics.beginGradientFill(GradientType.LINEAR,
114                                         [mainColor, mainColor],
115                                         [1,1],
116                                         [0,255],
117                                         matrix
118                                         );
119             b.graphics.drawRect(0, 0, stageWidth, stageHeight);
120             b.graphics.endFill();
121             addChild(b);
122         }
123     }
124
125 }
126