PreloaderIMT.as 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.imt.intimamedia.views.preloader
  2. {
  3. import flash.display.DisplayObject;
  4. import flash.display.GradientType;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. import flash.events.ProgressEvent;
  8. import flash.events.TimerEvent;
  9. import flash.filters.DropShadowFilter;
  10. import flash.geom.Matrix;
  11. import flash.text.TextField;
  12. import flash.text.TextFormat;
  13. import flash.utils.Timer;
  14. import mx.events.FlexEvent;
  15. import mx.preloaders.IPreloaderDisplay;
  16. public class PreloaderIMT extends Sprite implements IPreloaderDisplay
  17. {
  18. [Embed("images/general/intimamedia2.png") ]
  19. [Bindable]
  20. public var LogoClass : Class;
  21. private var logo : DisplayObject;
  22. private var _IsInitComplete : Boolean = false;
  23. private var _timer : Timer;
  24. private var _bytesLoaded : uint = 0;
  25. private var _bytesExpected : uint = 1;
  26. private var _fractionLoaded : Number = 0;
  27. private var _preloader : Sprite;
  28. private var smallDropShadow : DropShadowFilter = new DropShadowFilter(2, 45, 0x000000,0.5)
  29. private var largeDropShadow : DropShadowFilter = new DropShadowFilter(6, 45, 0x333333, 0.9)
  30. // this is the border mainBox
  31. private var mainBox : Sprite;
  32. // the progress sprite
  33. private var bar : Sprite = new Sprite();
  34. // draws the border around the progress bar
  35. private var barFrame : Sprite;
  36. // the textfield for rendering the "Loading 0%" string
  37. private var loadingTextField : TextField;
  38. // the textfield for rendering the minimal screen resolution string
  39. private var configTextField : TextField;
  40. private var config2TextField : TextField;
  41. // the background color(s) - specify 1 or 2 colors
  42. private var bgColors : Array = [ 0xFFFFFF, 0xFFFFFF ];
  43. // the mainBox background gradient colors - specify 1 or 2 colors
  44. private var boxColors : Array = [ 0xe3e3e3, 0x620e0e ];
  45. // the progress bar color - specify either 1, 2, or 4 colors
  46. private var barColors : Array = [ 0xde4a33, 0xeea183, 0xb00b19, 0x620e0e ]; //0x0687d7;
  47. // the progress bar border color
  48. private var barBorderColor : uint = 0xdddddd;
  49. // the rounded corner radius for the progressbar
  50. private var barRadius : int = 0;
  51. // the width of the progressbar
  52. private var barWidth : int = 400;
  53. // the height of the progressbar
  54. private var barHeight : int = 30;
  55. // the loading text font
  56. private var textFont : String = "Tahoma"; // "Verdana";
  57. // the loading text color
  58. private var textColor : uint = 0xffffff;
  59. private var loading : String = "Loading ";
  60. private var config : String = "Minimal screen resolution: 1152 x 720";
  61. private var config2 : String = "This application is not available for human use.";
  62. public function PreloaderIMT()
  63. {
  64. super();
  65. }
  66. virtual public function initialize() : void
  67. {
  68. _timer = new Timer(1);
  69. _timer.addEventListener(TimerEvent.TIMER, timerHandler);
  70. _timer.start();
  71. // clear here, rather than in draw(), to speed up the drawing
  72. clear();
  73. //creates all visual elements
  74. createAssets();
  75. }
  76. private function clear() : void
  77. {
  78. // Draw background
  79. var bg : Sprite = new Sprite();
  80. if (bgColors.length == 2) {
  81. var matrix : Matrix = new Matrix();
  82. matrix.createGradientBox(stageWidth, stageHeight, Math.PI/2);
  83. bg.graphics.beginGradientFill(GradientType.LINEAR, bgColors, [1, 1], [0, 255], matrix);
  84. } else {
  85. bg.graphics.beginFill(uint(bgColors[0]));
  86. }
  87. bg.graphics.drawRect(0, 0, stageWidth, stageHeight);
  88. bg.graphics.endFill();
  89. addChild(bg);
  90. }
  91. private function createAssets() : void
  92. {
  93. // load the logo first so that we can get its dimensions
  94. logo = new LogoClass();
  95. var logoWidth : Number = logo.width;
  96. var logoHeight : Number = logo.height;
  97. // make the progress bar the same width as the logo if the logo is large
  98. barWidth = Math.max(barWidth, logoWidth);
  99. // calculate the box size & add some padding
  100. var boxWidth : Number = Math.max(logoWidth, barWidth) + 100;
  101. var boxHeight : Number = logoHeight + barHeight + 100;
  102. // create and position the main box (all other sprites are added to it)
  103. mainBox = new Sprite();
  104. mainBox.x = stageWidth/2 - boxWidth/2;
  105. mainBox.y = stageHeight/2 - boxHeight/2;
  106. mainBox.filters = [ largeDropShadow ];
  107. if (boxColors.length == 2)
  108. {
  109. var matrix : Matrix = new Matrix();
  110. matrix.createGradientBox(boxWidth, boxHeight, Math.PI/2);
  111. mainBox.graphics.beginGradientFill(GradientType.LINEAR, boxColors, [1, 1], [0, 255], matrix);
  112. }
  113. else
  114. {
  115. mainBox.graphics.beginFill(uint(boxColors[0]));
  116. }
  117. mainBox.graphics.drawRoundRectComplex(0, 0, boxWidth, boxHeight, 12, 12, 12, 12);
  118. mainBox.graphics.endFill();
  119. addChild(mainBox);
  120. // position the logo
  121. logo.y = 40;
  122. logo.x = 107;
  123. mainBox.addChild(logo);
  124. //create progress bar
  125. bar = new Sprite();
  126. bar.graphics.drawRoundRect(0, 0, barWidth, barHeight, barRadius, barRadius);
  127. bar.x = 50;
  128. bar.y = logo.y + logoHeight + 20;
  129. mainBox.addChild(bar);
  130. //create progress bar frame
  131. barFrame = new Sprite();
  132. barFrame.graphics.lineStyle(1, barBorderColor, 1)
  133. barFrame.graphics.drawRoundRect(0, 0, barWidth, barHeight, barRadius, barRadius);
  134. barFrame.graphics.endFill();
  135. barFrame.x = bar.x;
  136. barFrame.y = bar.y;
  137. barFrame.filters = [ smallDropShadow ];
  138. mainBox.addChild(barFrame);
  139. //create text field to show percentage of loading, centered over the progress bar
  140. loadingTextField = new TextField();
  141. loadingTextField.width = barWidth;
  142. // setup the loading text font, color, and center alignment
  143. var tf : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center");
  144. loadingTextField.defaultTextFormat = tf;
  145. // set the text AFTER the textformat has been set, otherwise the text sizes are wrong
  146. loadingTextField.text = loading + " 0%";
  147. // important - give the textfield a proper height
  148. loadingTextField.height = loadingTextField.textHeight + 8;
  149. loadingTextField.x = barFrame.x;
  150. // center the textfield vertically on the progress bar
  151. loadingTextField.y = barFrame.y + Math.round((barFrame.height - loadingTextField.height) / 2);
  152. mainBox.addChild(loadingTextField);
  153. configTextField = new TextField();
  154. configTextField.width = barWidth;
  155. var tf2 : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center");
  156. configTextField.defaultTextFormat = tf2;
  157. configTextField.height = 24;
  158. configTextField.text = config;
  159. configTextField.x = barFrame.x;
  160. configTextField.y = loadingTextField.y + 25 ;
  161. mainBox.addChild(configTextField);
  162. config2TextField = new TextField();
  163. config2TextField.width = barWidth;
  164. // var tf2 : TextFormat = new TextFormat(textFont, null, textColor, true, null, null, null, null, "center");
  165. config2TextField.defaultTextFormat = tf2;
  166. config2TextField.height = 24;
  167. config2TextField.text = config2;
  168. config2TextField.x = barFrame.x;
  169. config2TextField.y = loadingTextField.y + 40 ;
  170. mainBox.addChild(config2TextField);
  171. }
  172. // This function is called whenever the state of the preloader changes.
  173. // Use the _fractionLoaded variable to draw your progress bar.
  174. virtual protected function draw() : void
  175. {
  176. // update the % loaded string
  177. loadingTextField.text = loading + Math.round(_fractionLoaded * 100).toString() + "%";
  178. // draw a complex gradient progress bar
  179. var matrix : Matrix = new Matrix();
  180. matrix.createGradientBox(bar.width, bar.height, Math.PI/2);
  181. if (barColors.length == 2) {
  182. bar.graphics.beginGradientFill(GradientType.LINEAR, barColors, [1, 1], [0, 255], matrix);
  183. } else if (barColors.length == 4) {
  184. bar.graphics.beginGradientFill(GradientType.LINEAR, barColors, [1, 1, 1, 1], [0, 127, 128, 255], matrix);
  185. } else {
  186. bar.graphics.beginFill(uint(barColors[0]), 1);
  187. }
  188. bar.graphics.drawRoundRect(0, 0, bar.width * _fractionLoaded, bar.height, barRadius, barRadius);
  189. bar.graphics.endFill();
  190. }
  191. /**
  192. * The Preloader class passes in a reference to itself to the display class
  193. * so that it can listen for events from the preloader.
  194. * This code comes from DownloadProgressBar. I have modified it to remove some unused event handlers.
  195. */
  196. virtual public function set preloader(value : Sprite) : void
  197. {
  198. _preloader = value;
  199. value.addEventListener(ProgressEvent.PROGRESS, progressHandler);
  200. value.addEventListener(Event.COMPLETE, completeHandler);
  201. value.addEventListener(FlexEvent.INIT_PROGRESS, initProgressHandler);
  202. value.addEventListener(FlexEvent.INIT_COMPLETE, initCompleteHandler);
  203. }
  204. virtual public function set backgroundAlpha(alpha : Number) : void{}
  205. virtual public function get backgroundAlpha() : Number { return 1; }
  206. protected var _backgroundColor : uint = 0xffffffff;
  207. virtual public function set backgroundColor(color : uint) : void { _backgroundColor = color; }
  208. virtual public function get backgroundColor() : uint { return _backgroundColor; }
  209. virtual public function set backgroundImage(image : Object) : void {}
  210. virtual public function get backgroundImage() : Object { return null; }
  211. virtual public function set backgroundSize(size : String) : void {}
  212. virtual public function get backgroundSize() : String { return "auto"; }
  213. protected var _stageHeight : Number = 300;
  214. virtual public function set stageHeight(height : Number) : void { _stageHeight = height; }
  215. virtual public function get stageHeight() : Number { return _stageHeight; }
  216. protected var _stageWidth : Number = 400;
  217. virtual public function set stageWidth(width : Number) : void { _stageWidth = width; }
  218. virtual public function get stageWidth() : Number { return _stageWidth; }
  219. //--------------------------------------------------------------------------
  220. // Event handlers
  221. //--------------------------------------------------------------------------
  222. // Called from time to time as the download progresses.
  223. virtual protected function progressHandler(event : ProgressEvent) : void
  224. {
  225. _bytesLoaded = event.bytesLoaded;
  226. _bytesExpected = event.bytesTotal;
  227. _fractionLoaded = Number(_bytesLoaded) / Number(_bytesExpected);
  228. draw();
  229. }
  230. // Called when the download is complete, but initialization might not be done yet. (I *think*)
  231. // Note that there are two phases- download, and init
  232. virtual protected function completeHandler(event : Event) : void
  233. {
  234. }
  235. // Called from time to time as the initialization continues.
  236. virtual protected function initProgressHandler(event : Event) : void
  237. {
  238. draw();
  239. loadingTextField.text = "Initialization..."
  240. }
  241. // Called when both download and initialization are complete
  242. virtual protected function initCompleteHandler(event : Event) : void
  243. {
  244. _IsInitComplete = true;
  245. }
  246. // Called as often as possible
  247. virtual protected function timerHandler(event : Event) : void
  248. {
  249. if (_IsInitComplete)
  250. {
  251. _timer.stop();
  252. dispatchEvent(new Event(Event.COMPLETE));
  253. } else {
  254. draw();
  255. }
  256. }
  257. }
  258. }