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