PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/view/InfoBox.as

https://github.com/fogAndWhisky/TestCodeRepo
ActionScript | 295 lines | 181 code | 41 blank | 73 comment | 4 complexity | c090c8919d66dbf39ee60a93d5a67927 MD5 | raw file
  1. /**
  2. * Base class for infomation alerts
  3. */
  4. package view
  5. {
  6. import assetLib.ColorLib;
  7. import controller.InfoButton;
  8. import flash.display.Bitmap;
  9. import flash.display.SimpleButton;
  10. import flash.display.Sprite;
  11. import flash.events.Event;
  12. import flash.events.MouseEvent;
  13. import flash.events.TimerEvent;
  14. import flash.filters.BlurFilter;
  15. import flash.text.TextField;
  16. import flash.text.TextFormat;
  17. import flash.text.TextFormatAlign;
  18. import flash.utils.Timer;
  19. import gs.TweenMax;
  20. import gs.utils.tween.BlurFilterVars;
  21. import gs.utils.tween.TweenMaxVars;
  22. import gs.easing.Strong;
  23. public class InfoBox extends Sprite
  24. {
  25. /***********
  26. * Protected consts
  27. ***********/
  28. /** Spacer for clean layout */
  29. protected const SPACER:Number = 10;
  30. /** Larger corner radius */
  31. protected const bigRad:Number = 15;
  32. /** Smaller corner radius */
  33. protected const smallRad:Number = 5;
  34. /* Local references to colors */
  35. protected const bgColor:uint = ColorLib.BLACK;
  36. protected const lineColor:uint = ColorLib.LIGHT_TEXT;
  37. protected const textColor:uint = ColorLib.LIGHT_TEXT;
  38. /** Box keyline weight */
  39. protected const lineWieght:uint = 3;
  40. /***********
  41. * Protected members
  42. ***********/
  43. /** Close button */
  44. protected var closeBtn:SimpleButton;
  45. /** Textfield for display text */
  46. protected var textField:TextField;
  47. /** Format for the text if the textField */
  48. protected var textFormat:TextFormat;
  49. /** Background */
  50. protected var bg:Sprite;
  51. /** Timer for auto-dismiss */
  52. protected var dismissTimer:Timer;
  53. /** Optional list of buttons when user action required */
  54. protected var buttonBank:Sprite;
  55. /** Filter for cool fade in */
  56. protected var blurFilter:BlurFilter;
  57. public var blurValue:Number;
  58. /** Embedded PNG for the UP state of the close button */
  59. [Embed (source="images/close_up.png")]
  60. private var CloseUp:Class;
  61. /** Embedded PNG for the DOWN state of the close button */
  62. [Embed (source="images/close_over.png")]
  63. private var CloseDown:Class;
  64. /** Tween managing the blur effect */
  65. protected var blurTween:TweenMax;
  66. /** The controlling tween of a dismiss */
  67. protected var aTween:TweenMax;
  68. /**
  69. * Constructor
  70. *
  71. * @param w The width of the dialog
  72. * @param h The height of the dialog
  73. * @param msg The text to display
  74. * @param autoDismissMS (optional) Milliseconds until message auto-dismisses. Default 0,
  75. * which indicates no auto-dismiss.
  76. * @param buttonRank (optional) An array of InfoButtons to display in this view
  77. */
  78. public function InfoBox(w:Number, h:Number, msg:String,
  79. autoDismissMS:uint = 0, buttonRank:Array = null)
  80. {
  81. super();
  82. bg = new Sprite();
  83. bg.graphics.beginFill(bgColor, .5);
  84. bg.graphics.lineStyle(lineWieght, lineColor, .75, true);
  85. bg.graphics.drawRoundRectComplex(-w/2, -h/2, w, h,
  86. bigRad, smallRad, smallRad, bigRad);
  87. bg.graphics.endFill();
  88. addChild(bg);
  89. var closeUpSprite:Bitmap = new CloseUp();
  90. var closeDownSprite:Bitmap = new CloseDown();
  91. closeBtn = new SimpleButton(closeUpSprite, closeDownSprite, closeDownSprite, closeDownSprite);
  92. closeBtn.x = - (bg.width / 2) + SPACER;
  93. closeBtn.y = - (bg.height / 2) + SPACER;
  94. closeBtn.addEventListener(MouseEvent.CLICK, onCloseBtnClick);
  95. addChild(closeBtn);
  96. buildContent(w, h, msg, autoDismissMS, buttonRank);
  97. }
  98. /**
  99. * Fill in the contents of the window
  100. *
  101. * @param w Window width
  102. * @param h Window height
  103. * @param msg Text to display
  104. * @param autoDismiss (optional) Milliseconds until message auto-dismisses. Default 0.
  105. * @param buttonRank (optional) An array of InfoButtons to display in this view
  106. */
  107. protected function buildContent(w:Number, h:Number, msg:String,
  108. autoDismissMS:uint = 0, buttonRank:Array = null):void
  109. {
  110. textFormat = new TextFormat();
  111. textFormat.align = TextFormatAlign.CENTER;
  112. textFormat.color = textColor;
  113. textFormat.size = 18;
  114. textFormat.font = "_GameFont";
  115. textField = new TextField();
  116. textField.multiline = true;
  117. textField.wordWrap = true;
  118. textField.condenseWhite = true;
  119. textField.antiAliasType = "advanced";
  120. textField.defaultTextFormat = textFormat;
  121. textField.embedFonts = true;
  122. textField.htmlText = msg;
  123. textField.selectable = false;
  124. textField.width = bg.width - (SPACER * 2);
  125. textField.height = textField.textHeight +
  126. textField.getLineMetrics(textField.numLines-1).descent;
  127. textField.x = - (width/2) + SPACER;
  128. if (buttonRank)
  129. {
  130. closeBtn.visible = false;
  131. buttonBank = new Sprite();
  132. var len:uint = buttonRank.length;
  133. for (var a:uint = 0; a < len; a++)
  134. {
  135. var button:InfoButton = buttonRank[a] as InfoButton;
  136. button.y = (button.height + SPACER) * a;
  137. buttonBank.addChild(button);
  138. }
  139. buttonBank.x = -buttonBank.width / 2;
  140. buttonBank.y = (height / 2) - (buttonBank.height + SPACER);
  141. addChild(buttonBank);
  142. textField.y = closeBtn.y + closeBtn.height + SPACER;
  143. }
  144. else
  145. {
  146. textField.y = - (textField.textHeight/2);
  147. }
  148. addChild(textField);
  149. if (autoDismissMS)
  150. setAutoDismissTimer(autoDismissMS);
  151. summon();
  152. }
  153. /**
  154. * Animate in this infobox
  155. */
  156. public function summon():void
  157. {
  158. var startVars:TweenMaxVars = new TweenMaxVars();
  159. startVars.autoAlpha = 0;
  160. startVars.scaleX = 0;
  161. startVars.scaleY = 0;
  162. var tweenVars:TweenMaxVars = new TweenMaxVars();
  163. tweenVars.autoAlpha = 1;
  164. tweenVars.scaleX = 1;
  165. tweenVars.scaleY = 1;
  166. tweenVars.ease = Strong.easeInOut;
  167. tweenVars.startAt = startVars;
  168. var startBlurFilterVars:BlurFilterVars = new BlurFilterVars(255, 0, 1);
  169. var endBlurFilterVars:BlurFilterVars = new BlurFilterVars(0, 0, 1);
  170. var blurTweenVars:TweenMaxVars = new TweenMaxVars();
  171. blurTweenVars.blurFilter = endBlurFilterVars;
  172. blurTweenVars.ease = Strong.easeIn;
  173. blurTweenVars.startAt = new TweenMaxVars({blurFilter: startBlurFilterVars});
  174. aTween = TweenMax.to(this, 2, tweenVars);
  175. blurTween = TweenMax.to(this, 1, blurTweenVars);
  176. }
  177. /**
  178. * Dismiss this infobox
  179. */
  180. public function dismiss():void
  181. {
  182. if (dismissTimer)
  183. {
  184. dismissTimer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
  185. dismissTimer.stop();
  186. }
  187. var startVars:TweenMaxVars = new TweenMaxVars();
  188. startVars.autoAlpha = alpha;
  189. startVars.scaleX = scaleX;
  190. startVars.scaleY = scaleY;
  191. var tweenVars:TweenMaxVars = new TweenMaxVars();
  192. tweenVars.autoAlpha = 0;
  193. tweenVars.scaleX = 0;
  194. tweenVars.scaleY = 0;
  195. tweenVars.ease = Strong.easeInOut;
  196. tweenVars.startAt = startVars;
  197. var startBlurFilterVars:BlurFilterVars = new BlurFilterVars(0, 0, 1);
  198. var endBlurFilterVars:BlurFilterVars = new BlurFilterVars(255, 0, 1);
  199. var blurTweenVars:TweenMaxVars = new TweenMaxVars();
  200. blurTweenVars.blurFilter = endBlurFilterVars;
  201. blurTweenVars.ease = Strong.easeOut;
  202. blurTweenVars.startAt = new TweenMaxVars({blurFilter: startBlurFilterVars});
  203. aTween = TweenMax.to(this, 2, tweenVars);
  204. blurTween = TweenMax.to(this, 1, blurTweenVars);
  205. }
  206. /**
  207. * Deconstruct this alert
  208. */
  209. public function destroy():void
  210. {
  211. parent.removeChild(this);
  212. delete this;
  213. }
  214. /**
  215. * Event from aTween. Animation complete. Remove this alert.
  216. *
  217. * @param e The Tween event
  218. */
  219. protected function onDismissComplete():void
  220. {
  221. dispatchEvent(new Event(Event.COMPLETE));
  222. }
  223. /**
  224. * Event from close button. User has asked for close.
  225. *
  226. * @param e The MouseEvent.CLICK event
  227. */
  228. protected function onCloseBtnClick(e:MouseEvent):void
  229. {
  230. dispatchEvent(new Event(Event.CLOSE));
  231. }
  232. /**
  233. * Start the autoDismiss timer
  234. *
  235. * @param autoDismissMS The number of milliseconds until we dismiss
  236. */
  237. protected function setAutoDismissTimer(autoDismissMS:uint):void
  238. {
  239. dismissTimer = new Timer(autoDismissMS);
  240. dismissTimer.addEventListener(TimerEvent.TIMER, onTimerComplete);
  241. dismissTimer.start();
  242. }
  243. /**
  244. * Event from auto dismiss timer. Timeout complete. Time to dismiss.
  245. *
  246. * @param e The TimerEvent.TIMER_COMPLETE event
  247. */
  248. protected function onTimerComplete(e:TimerEvent):void
  249. {
  250. dismissTimer.removeEventListener(TimerEvent.TIMER, onTimerComplete);
  251. dismiss();
  252. }
  253. }
  254. }