Shift-R: paste relations from previously selected object, as per #3596.
[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                         // We don't let anything happen until the .zips are loaded
59                         // see http://stackoverflow.com/questions/2773617/how-to-preload-a-file-in-flex-before-the-application-initializes
60                         if (assetscount>0) {
61                                 queuedEvents.push(e);
62                                 e.stopImmediatePropagation();
63                         }
64                         draw();
65                 }
66                 
67                 private function zipLoaded(e:Event):void {
68                         assetscount--; if (assetscount>0) return;
69                         for each (var q:Event in queuedEvents) {
70                                 dispatchEvent(q);
71                         }
72                 }
73                 
74         //this is our "animation" bit
75         override protected function draw():void
76         {
77             t.text = int(_fractionLoaded*100).toString()+"%";
78             //make objects below follow loading progress
79             //positions are completely arbitrary
80             //d tells us the x value of where the loading bar is at
81             var d:Number=barFrame.x + barFrame.width * _fractionLoaded
82             t.x = d - t.width - 25;
83             bar.graphics.beginFill(0xffffff,1)
84             bar.graphics.drawRoundRectComplex(0,0,bar.width * _fractionLoaded,15,12,0,0,12);
85             bar.graphics.endFill();
86         }
87
88         protected function createAssets():void
89         {
90             //create the logo
91             p2Logo = new Logo();
92             p2Logo.y = stageHeight/2 - p2Logo.height/2;
93             p2Logo.x = stageWidth/2 - p2Logo.width/2;
94             //p2Logo.filters = [f];
95             addChild(p2Logo);
96
97             //create bar
98             bar = new Sprite();
99              bar.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
100             bar.x = stageWidth/2 - bar.width/2;
101             bar.y = stageHeight/1.2 - bar.height/2;
102             bar.filters = [f];
103             addChild(bar);
104
105             //create bar frame
106             barFrame = new Sprite();
107             barFrame.graphics.lineStyle(2,0xFFFFFF,1)
108             barFrame.graphics.drawRoundRectComplex(0,0,400,15,12,0,0,12);
109             barFrame.graphics.endFill();
110             barFrame.x = stageWidth/2 - barFrame.width/2;
111             barFrame.y = stageHeight/1.2 - barFrame.height/2;
112             barFrame.filters = [f];
113             addChild(barFrame);
114
115             //create text field to show percentage of loading
116             t = new TextField()
117             t.y = barFrame.y-27;
118             t.filters=[f];
119             addChild(t);
120             //we can format our text
121             var s:TextFormat=new TextFormat("Verdana",null,0xFFFFFF,null,null,null,null,null,"right");
122             t.defaultTextFormat=s;
123         }
124
125         protected function clear():void
126         {
127             // Draw gradient background
128             var b:Sprite = new Sprite;
129              var matrix:Matrix =  new Matrix();
130             matrix.createGradientBox(stageWidth, stageHeight, Math.PI/2);
131             b.graphics.beginGradientFill(GradientType.LINEAR,
132                                         [mainColor, mainColor],
133                                         [1,1],
134                                         [0,255],
135                                         matrix
136                                         );
137             b.graphics.drawRect(0, 0, stageWidth, stageHeight);
138             b.graphics.endFill();
139             addChild(b);
140         }
141     }
142
143 }
144