package com.imt.intimamedia.views.preloader { import flash.display.DisplayObject; import flash.display.GradientType; import flash.display.Sprite; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.TimerEvent; import flash.filters.DropShadowFilter; import flash.geom.Matrix; import flash.text.TextField; import flash.text.TextFormat; import flash.utils.Timer; import mx.events.FlexEvent; import mx.preloaders.IPreloaderDisplay; public class PreloaderIMT extends Sprite implements IPreloaderDisplay { [Embed("images/general/intimamedia2.png") ] [Bindable] public var LogoClass : Class; private var logo : DisplayObject; private var _IsInitComplete : Boolean = false; private var _timer : Timer; private var _bytesLoaded : uint = 0; private var _bytesExpected : uint = 1; private var _fractionLoaded : Number = 0; private var _preloader : Sprite; private var smallDropShadow : DropShadowFilter = new DropShadowFilter(2, 45, 0x000000,0.5) private var largeDropShadow : DropShadowFilter = new DropShadowFilter(6, 45, 0x333333, 0.9) // this is the border mainBox private var mainBox : Sprite; // the progress sprite private var bar : Sprite = new Sprite(); // draws the border around the progress bar private var barFrame : Sprite; // the textfield for rendering the "Loading 0%" string private var loadingTextField : TextField; // the textfield for rendering the minimal screen resolution string private var configTextField : TextField; private var config2TextField : TextField; // the background color(s) - specify 1 or 2 colors private var bgColors : Array = [ 0xFFFFFF, 0xFFFFFF ]; // the mainBox background gradient colors - specify 1 or 2 colors private var boxColors : Array = [ 0xe3e3e3, 0x620e0e ]; // the progress bar color - specify either 1, 2, or 4 colors private var barColors : Array = [ 0xde4a33, 0xeea183, 0xb00b19, 0x620e0e ]; //0x0687d7; // the progress bar border color private var barBorderColor : uint = 0xdddddd; // the rounded corner radius for the progressbar private var barRadius : int = 0; // the width of the progressbar private var barWidth : int = 400; // the height of the progressbar private var barHeight : int = 30; // the loading text font private var textFont : String = "Tahoma"; // "Verdana"; // the loading text color private var textColor : uint = 0xffffff; private var loading : String = "Loading "; private var config : String = "Minimal screen resolution: 1152 x 720"; private var config2 : String = "This application is not available for human use."; public function PreloaderIMT() { super(); } virtual public function initialize() : void { _timer = new Timer(1); _timer.addEventListener(TimerEvent.TIMER, timerHandler); _timer.start(); // clear here, rather than in draw(), to speed up the drawing clear(); //creates all visual elements createAssets(); } private function clear() : void { // Draw background var bg : Sprite = new Sprite(); if (bgColors.length == 2) { var matrix : Matrix = new Matrix(); matrix.createGradientBox(stageWidth, stageHeight, Math.PI/2); bg.graphics.beginGradientFill(GradientType.LINEAR, bgColors, [1, 1], [0, 255], matrix); } else { bg.graphics.beginFill(uint(bgColors[0])); } bg.graphics.drawRect(0, 0, stageWidth, stageHeight); bg.graphics.endFill(); addChild(bg); } private function createAssets() : void { // load the logo first so that we can get its dimensions logo = new LogoClass(); var logoWidth : Number = logo.width; var logoHeight : Number = logo.height; // make the progress bar the same width as the logo if the logo is large barWidth = Math.max(barWidth, logoWidth); // calculate the box size & add some padding var boxWidth : Number = Math.max(logoWidth, barWidth) + 100; var boxHeight : Number = logoHeight + barHeight + 100; // create and position the main box (all other sprites are added to it) mainBox = new Sprite(); mainBox.x = stageWidth/2 - boxWidth/2; mainBox.y = stageHeight/2 - boxHeight/2; mainBox.filters = [ largeDropShadow ]; if (boxColors.length == 2) { var matrix : Matrix = new Matrix(); matrix.createGradientBox(boxWidth, boxHeight, Math.PI/2); mainBox.graphics.beginGradientFill(GradientType.LINEAR, boxColors, [1, 1], [0, 255], matrix); } else { mainBox.graphics.beginFill(uint(boxColors[0])); } mainBox.graphics.drawRoundRectComplex(0, 0, boxWidth, boxHeight, 12, 12, 12, 12); mainBox.graphics.endFill(); addChild(mainBox); // position the logo logo.y = 40; logo.x = 107; mainBox.addChild(logo); //create progress bar bar = new Sprite(); bar.graphics.drawRoundRect(0, 0, barWidth, barHeight, barRadius, barRadius); bar.x = 50; bar.y = logo.y + logoHeight + 20; mainBox.addChild(bar); //create progress bar frame barFrame = new Sprite(); barFrame.graphics.lineStyle(1, barBorderColor, 1) barFrame.graphics.drawRoundRect(0, 0, barWidth, barHeight, barRadius, barRadius); barFrame.graphics.endFill(); barFrame.x = bar.x; barFrame.y = bar.y; barFrame.filters = [ smallDropShadow ]; mainBox.addChild(barFrame); //create text field to show percentage of loading, centered over the progress bar loadingTextField = new TextField(); loadingTextField.width = barWidth; // setup the loading text font, color, and center alignment var tf : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center"); loadingTextField.defaultTextFormat = tf; // set the text AFTER the textformat has been set, otherwise the text sizes are wrong loadingTextField.text = loading + " 0%"; // important - give the textfield a proper height loadingTextField.height = loadingTextField.textHeight + 8; loadingTextField.x = barFrame.x; // center the textfield vertically on the progress bar loadingTextField.y = barFrame.y + Math.round((barFrame.height - loadingTextField.height) / 2); mainBox.addChild(loadingTextField); configTextField = new TextField(); configTextField.width = barWidth; var tf2 : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center"); configTextField.defaultTextFormat = tf2; configTextField.height = 24; configTextField.text = config; configTextField.x = barFrame.x; configTextField.y = loadingTextField.y + 25 ; mainBox.addChild(configTextField); config2TextField = new TextField(); config2TextField.width = barWidth; // var tf2 : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center"); config2TextField.defaultTextFormat = tf2; config2TextField.height = 24; config2TextField.text = config2; config2TextField.x = barFrame.x; config2TextField.y = loadingTextField.y + 40 ; mainBox.addChild(config2TextField); } // This function is called whenever the state of the preloader changes. // Use the _fractionLoaded variable to draw your progress bar. virtual protected function draw() : void { // update the % loaded string loadingTextField.text = loading + Math.round(_fractionLoaded * 100).toString() + "%"; // draw a complex gradient progress bar var matrix : Matrix = new Matrix(); matrix.createGradientBox(bar.width, bar.height, Math.PI/2); if (barColors.length == 2) { bar.graphics.beginGradientFill(GradientType.LINEAR, barColors, [1, 1], [0, 255], matrix); } else if (barColors.length == 4) { bar.graphics.beginGradientFill(GradientType.LINEAR, barColors, [1, 1, 1, 1], [0, 127, 128, 255], matrix); } else { bar.graphics.beginFill(uint(barColors[0]), 1); } bar.graphics.drawRoundRect(0, 0, bar.width * _fractionLoaded, bar.height, barRadius, barRadius); bar.graphics.endFill(); } /** * The Preloader class passes in a reference to itself to the display class * so that it can listen for events from the preloader. * This code comes from DownloadProgressBar. I have modified it to remove some unused event handlers. */ virtual public function set preloader(value : Sprite) : void { _preloader = value; value.addEventListener(ProgressEvent.PROGRESS, progressHandler); value.addEventListener(Event.COMPLETE, completeHandler); value.addEventListener(FlexEvent.INIT_PROGRESS, initProgressHandler); value.addEventListener(FlexEvent.INIT_COMPLETE, initCompleteHandler); } virtual public function set backgroundAlpha(alpha : Number) : void{} virtual public function get backgroundAlpha() : Number { return 1; } protected var _backgroundColor : uint = 0xffffffff; virtual public function set backgroundColor(color : uint) : void { _backgroundColor = color; } virtual public function get backgroundColor() : uint { return _backgroundColor; } virtual public function set backgroundImage(image : Object) : void {} virtual public function get backgroundImage() : Object { return null; } virtual public function set backgroundSize(size : String) : void {} virtual public function get backgroundSize() : String { return "auto"; } protected var _stageHeight : Number = 300; virtual public function set stageHeight(height : Number) : void { _stageHeight = height; } virtual public function get stageHeight() : Number { return _stageHeight; } protected var _stageWidth : Number = 400; virtual public function set stageWidth(width : Number) : void { _stageWidth = width; } virtual public function get stageWidth() : Number { return _stageWidth; } //-------------------------------------------------------------------------- // Event handlers //-------------------------------------------------------------------------- // Called from time to time as the download progresses. virtual protected function progressHandler(event : ProgressEvent) : void { _bytesLoaded = event.bytesLoaded; _bytesExpected = event.bytesTotal; _fractionLoaded = Number(_bytesLoaded) / Number(_bytesExpected); draw(); } // Called when the download is complete, but initialization might not be done yet. (I *think*) // Note that there are two phases- download, and init virtual protected function completeHandler(event : Event) : void { } // Called from time to time as the initialization continues. virtual protected function initProgressHandler(event : Event) : void { draw(); loadingTextField.text = "Initialization..." } // Called when both download and initialization are complete virtual protected function initCompleteHandler(event : Event) : void { _IsInitComplete = true; } // Called as often as possible virtual protected function timerHandler(event : Event) : void { if (_IsInitComplete) { _timer.stop(); dispatchEvent(new Event(Event.COMPLETE)); } else { draw(); } } } }