1 package net.systemeD.potlatch2
3 // As seen at: http://www.pathf.com/blogs/2008/08/custom-flex-3-lightweight-preloader-with-source-code/
4 // Can be "resued without restriction", so say the original authors
6 import flash.display.Sprite;
7 import flash.events.Event;
8 import flash.events.ProgressEvent;
9 import flash.events.TimerEvent;
10 import flash.utils.Timer;
12 import mx.events.FlexEvent;
13 import mx.preloaders.IPreloaderDisplay;
15 public class PreloaderDisplayBase extends Sprite implements IPreloaderDisplay
17 // Implementation variables, used to make everything work properly
18 protected var _IsInitComplete:Boolean = false;
19 protected var _timer:Timer; // we have a timer for animation
20 protected var _bytesLoaded:uint = 0;
21 protected var _bytesExpected:uint = 1; // we start at 1 to avoid division by zero errors.
22 protected var _fractionLoaded:Number = 0; // 0-1
24 public function PreloaderDisplayBase()
29 // This function is called whenever the state of the preloader changes. Use the _fractionLoaded variable to draw your progress bar.
30 virtual protected function draw():void
35 //// IPreloaderDisplay interface elements
36 //// check out the docs on IPreloaderDisplay to see more details.
38 // This function is called when the PreloaderDisplayBase has been created and is ready for action.
39 virtual public function initialize():void
41 _timer = new Timer(1);
42 _timer.addEventListener(TimerEvent.TIMER, timerHandler);
47 protected var _preloader:Sprite;
49 * The Preloader class passes in a reference to itself to the display class
50 * so that it can listen for events from the preloader.
52 // This code comes from DownloadProgressBar. I have modified it to remove some unused event handlers.
53 virtual public function set preloader(value:Sprite):void
57 value.addEventListener(ProgressEvent.PROGRESS, progressHandler);
58 value.addEventListener(Event.COMPLETE, completeHandler);
60 // value.addEventListener(RSLEvent.RSL_PROGRESS, rslProgressHandler);
61 // value.addEventListener(RSLEvent.RSL_COMPLETE, rslCompleteHandler);
62 // value.addEventListener(RSLEvent.RSL_ERROR, rslErrorHandler);
64 value.addEventListener(FlexEvent.INIT_PROGRESS, initProgressHandler);
65 value.addEventListener(FlexEvent.INIT_COMPLETE, initCompleteHandler);
68 virtual public function set backgroundAlpha(alpha:Number):void{}
69 virtual public function get backgroundAlpha():Number { return 1; }
71 protected var _backgroundColor:uint = 0xffffffff;
72 virtual public function set backgroundColor(color:uint):void { _backgroundColor = color; }
73 virtual public function get backgroundColor():uint { return _backgroundColor; }
75 virtual public function set backgroundImage(image:Object):void {}
76 virtual public function get backgroundImage():Object { return null; }
78 virtual public function set backgroundSize(size:String):void {}
79 virtual public function get backgroundSize():String { return "auto"; }
81 protected var _stageHeight:Number = 300;
82 virtual public function set stageHeight(height:Number):void { _stageHeight = height; }
83 virtual public function get stageHeight():Number { return _stageHeight; }
85 protected var _stageWidth:Number = 400;
86 virtual public function set stageWidth(width:Number):void { _stageWidth = width; }
87 virtual public function get stageWidth():Number { return _stageWidth; }
89 //--------------------------------------------------------------------------
93 //--------------------------------------------------------------------------
95 // Called from time to time as the download progresses.
96 virtual protected function progressHandler(event:ProgressEvent):void
98 _bytesLoaded = event.bytesLoaded;
99 _bytesExpected = event.bytesTotal;
100 _fractionLoaded = Number(_bytesLoaded) / Number(_bytesExpected);
105 // Called when the download is complete, but initialization might not be done yet. (I *think*)
106 // Note that there are two phases- download, and init
107 virtual protected function completeHandler(event:Event):void
112 // Called from time to time as the initialization continues.
113 virtual protected function initProgressHandler(event:Event):void
118 // Called when both download and initialization are complete
119 virtual protected function initCompleteHandler(event:Event):void
121 _IsInitComplete = true;
124 // Called as often as possible
125 virtual protected function timerHandler(event:Event):void
131 dispatchEvent(new Event(Event.COMPLETE));