/flex/client/src/main/flex/org/ow2/kerneos/core/view/preloader/KerneosPreloader.as

https://github.com/diorcety/kerneos · ActionScript · 181 lines · 69 code · 29 blank · 83 comment · 0 complexity · 8dd10298b92c819548e884b4dedc4d51 MD5 · raw file

  1. /**
  2. * Kerneos
  3. * Copyright (C) 2009 Bull S.A.S.
  4. * Contact: jasmine AT ow2.org
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. * USA
  20. *
  21. * --------------------------------------------------------------------------
  22. * $Id$
  23. * --------------------------------------------------------------------------
  24. */
  25. package org.ow2.kerneos.core.view.preloader
  26. {
  27. import flash.display.GradientType;
  28. import flash.display.Sprite;
  29. import flash.filters.DropShadowFilter;
  30. import flash.geom.Matrix;
  31. import flash.text.TextField;
  32. import flash.text.TextFormat;
  33. /**
  34. * A custom preloader displayed while the user downloads the application.
  35. *
  36. * Massively inspired from
  37. * http://www.pathf.com/blogs/2008/08/custom-flex-3-lightweight-preloader-with-source-code/ .
  38. *
  39. * @author Julien Nicoulaud
  40. * @internal The styles and positions are made to fit the application's ones and make sure a smooth
  41. * graphical transition is made.
  42. * @see org.ow2.kerneos.core.view.LoadingView
  43. */
  44. public class KerneosPreloader extends PreloaderDisplayBase
  45. {
  46. // =========================================================================
  47. // Properties
  48. // =========================================================================
  49. /**
  50. * The label text displayed the status.
  51. */
  52. private var label : TextField;
  53. /**
  54. * Drop shadow filter.
  55. */
  56. private var shadowFilter : DropShadowFilter = new DropShadowFilter(2, 45, 0x000000, 0.5);
  57. /**
  58. * The loading bar.
  59. */
  60. private var bar : Sprite = new Sprite();
  61. /**
  62. * The loading bar frame.
  63. */
  64. private var barFrame : Sprite;
  65. /**
  66. * The main color of the UI.
  67. */
  68. private var mainColor : uint = 0x666666;
  69. // =========================================================================
  70. // Initialization
  71. // =========================================================================
  72. /**
  73. * Build a new KerneosPreloader.
  74. */
  75. public function KerneosPreloader()
  76. {
  77. super();
  78. }
  79. /**
  80. * @inheritDoc
  81. */
  82. override public function initialize() : void
  83. {
  84. // Call super class method.
  85. super.initialize();
  86. // Create the background.
  87. createBackground();
  88. // Create the UI children.
  89. createUIChildren();
  90. }
  91. /**
  92. * Create the progress bar and the displayed label.
  93. */
  94. protected function createUIChildren() : void
  95. {
  96. // Create the progress bar.
  97. bar = new Sprite();
  98. bar.graphics.drawRoundRectComplex(0, 0, 298, 18, 12, 0, 0, 12);
  99. bar.x = stageWidth / 2 - bar.width / 2 + 1;
  100. bar.y = stageHeight / 2 - bar.height / 2 + 14;
  101. bar.filters = [shadowFilter];
  102. addChild(bar);
  103. barFrame = new Sprite();
  104. barFrame.graphics.lineStyle(2, 0xFFFFFF, 1)
  105. barFrame.graphics.drawRoundRectComplex(0, 0, 298, 18, 12, 0, 0, 12);
  106. barFrame.graphics.endFill();
  107. barFrame.x = stageWidth / 2 - barFrame.width / 2 + 1;
  108. barFrame.y = stageHeight / 2 - barFrame.height / 2 + 14;
  109. barFrame.filters = [shadowFilter];
  110. addChild(barFrame);
  111. // Create the displayed label.
  112. label = new TextField();
  113. label.width = 400;
  114. label.x = barFrame.x;
  115. label.y = barFrame.y - 25;
  116. label.filters = [shadowFilter];
  117. addChild(label);
  118. // Format the label.
  119. var s : TextFormat = new TextFormat("Verdana", "12", 0xEFEFEF, true, false, null, null, null, "left");
  120. label.defaultTextFormat = s;
  121. }
  122. /**
  123. * Create the background.
  124. */
  125. protected function createBackground() : void
  126. {
  127. // Create a gradient background (white => mainColor)
  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, [0xFFFFFF, mainColor], [1, 1], [0, 255], matrix);
  132. b.graphics.drawRect(0, 0, stageWidth, stageHeight);
  133. b.graphics.endFill();
  134. addChild(b);
  135. }
  136. // =========================================================================
  137. // UI updates
  138. // =========================================================================
  139. /**
  140. * @inheritDoc
  141. */
  142. override protected function draw() : void
  143. {
  144. // Update the displayed label.
  145. label.text = "Downloading... " + int(_fractionLoaded * 100).toString() + "% (" + Math.ceil((_bytesLoaded / 1024)).toString() + "/" + Math.ceil((_bytesExpected / 1024)).toString() + "kb)";
  146. // Update the progress bar.
  147. bar.graphics.beginFill(mainColor, 1)
  148. bar.graphics.drawRoundRectComplex(0, 0, bar.width * _fractionLoaded, 20, 12, 0, 0, 12);
  149. bar.graphics.endFill();
  150. }
  151. }
  152. }