/build/game/Console.as

https://github.com/vener91/Rhythm · ActionScript · 184 lines · 107 code · 23 blank · 54 comment · 10 complexity · 2b8679af1f47d3147c862e6e16da8aa6 MD5 · raw file

  1. /**
  2. * Console
  3. *
  4. * Creates a pseudo Console panel at the bottom of your swf
  5. * use Console.trace() to send messages to this Console
  6. */
  7. package {
  8. import flash.display.Shape;
  9. import flash.display.Sprite;
  10. import flash.display.Stage;
  11. import flash.display.GradientType;
  12. import flash.events.Event;
  13. import flash.events.MouseEvent;
  14. import flash.geom.Matrix;
  15. import flash.text.TextField;
  16. import flash.text.TextFieldType;
  17. import flash.text.TextFormat;
  18. import flash.text.TextFormatAlign;
  19. import flash.text.TextFieldAutoSize;
  20. public class Console extends Sprite {
  21. private var Console_txt:TextField; // text field containiner Console text
  22. private var titleBar:Sprite; // sprite for titleBar
  23. private var mainStage:Stage; // reference to the main stage (for events)
  24. private static var instance:Console; // singleton Console instance reference
  25. private static var _enabled:Boolean = true; // enabled - when false Console messages do not appear
  26. private static var maxChars:int = 10000; // maximum characters allowed in Console, when reached oldest Console text is deleted
  27. public static var autoExpand:Boolean = true; // when true, Console panel automatically opens when trace is used
  28. // public enabled
  29. public static function get enabled():Boolean {
  30. return _enabled;
  31. }
  32. public static function set enabled(b:Boolean):void {
  33. _enabled = b;
  34. }
  35. /**
  36. * Constructor
  37. */
  38. public function Console(targetStage:Stage = null, ConsoleHeight:uint = 155){
  39. // if instance already exists, remove it from the display
  40. // list and create a new one
  41. if (instance && instance.parent){
  42. instance.parent.removeChild(instance);
  43. }
  44. instance = this;
  45. // start hidden
  46. visible = false;
  47. // assign the passed stage to mainStage
  48. mainStage = (targetStage) ? targetStage : stage;
  49. // create and add Console and titleBar
  50. addChild(newConsoleField(ConsoleHeight));
  51. addChild(newTitleBar());
  52. // listen for click and resize events for
  53. // panel toggling and resizing
  54. titleBar.addEventListener(MouseEvent.CLICK, toggleCollapse);
  55. mainStage.addEventListener(Event.RESIZE, fitToStage);
  56. // start off fit to stage and collapsed
  57. fitToStage();
  58. toggleCollapse();
  59. }
  60. /**
  61. * trace
  62. * sends arguments to the Console text field for display
  63. */
  64. public static function trace(...args):void {
  65. // if not enabled, exit
  66. if (!instance || !_enabled) return;
  67. // add text, keeping within maxChars
  68. instance.Console_txt.appendText(args.toString() + "\n");
  69. if (instance.Console_txt.text.length > maxChars) {
  70. instance.Console_txt.text = instance.Console_txt.text.slice(-maxChars);
  71. }
  72. // scroll to bottom of text field
  73. instance.Console_txt.scrollV = instance.Console_txt.maxScrollV;
  74. // Make visible and expand if not already
  75. if (!instance.visible) instance.visible = true;
  76. if (autoExpand && !instance.Console_txt.visible) toggleCollapse();
  77. }
  78. /**
  79. * clear
  80. * clears Console text field text
  81. */
  82. public static function clear():void {
  83. if (!instance) return;
  84. instance.Console_txt.text = "";
  85. }
  86. /**
  87. * toggleCollapse
  88. * either collapses or expands the Console panel
  89. * depending on its current state
  90. */
  91. public static function toggleCollapse(evt:Event = null):void {
  92. if (!instance) return;
  93. instance.Console_txt.visible = !instance.Console_txt.visible;
  94. // fit to stage will handle visual expand or collapse
  95. instance.fitToStage();
  96. }
  97. /**
  98. * newConsoleField
  99. * creates the text field used for traced Console messages
  100. */
  101. private function newConsoleField(ConsoleHeight:uint):TextField {
  102. Console_txt = new TextField();
  103. Console_txt.type = TextFieldType.INPUT;
  104. Console_txt.border = true;
  105. Console_txt.borderColor = 0;
  106. Console_txt.background = true;
  107. Console_txt.backgroundColor = 0xFFFFFF;
  108. Console_txt.height = ConsoleHeight;
  109. Console_txt.multiline = true;
  110. Console_txt.wordWrap = true;
  111. var format:TextFormat = Console_txt.getTextFormat();
  112. format.font = "_typewriter";
  113. Console_txt.setTextFormat(format);
  114. Console_txt.defaultTextFormat = format;
  115. return Console_txt;
  116. }
  117. /**
  118. * newTitleBar
  119. * creates the titleBar sprite
  120. */
  121. private function newTitleBar():Sprite {
  122. // create a new shape for the gradient background
  123. var barGraphics:Shape = new Shape();
  124. barGraphics.name = "bar";
  125. var colors:Array = new Array(0xE0E0F0, 0xB0C0D0, 0xE0E0F0);
  126. var alphas:Array = new Array(1, 1, 1);
  127. var ratios:Array = new Array(0, 50, 255);
  128. var gradientMatrix:Matrix = new Matrix();
  129. gradientMatrix.createGradientBox(18, 18, Math.PI/2, 0, 0);
  130. barGraphics.graphics.lineStyle(0);
  131. barGraphics.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, gradientMatrix);
  132. barGraphics.graphics.drawRect(0, 0, 18, 18);
  133. // label for the panel title "Console"
  134. var barLabel:TextField = new TextField();
  135. barLabel.autoSize = TextFieldAutoSize.LEFT;
  136. barLabel.selectable = false;
  137. barLabel.text = "Console";
  138. var format:TextFormat = barLabel.getTextFormat();
  139. format.font = "_sans";
  140. barLabel.setTextFormat(format);
  141. // Sprite to contain both the gradient and the title
  142. titleBar = new Sprite();
  143. titleBar.addChild(barGraphics);
  144. titleBar.addChild(barLabel);
  145. return titleBar;
  146. }
  147. /**
  148. * fitToStage - event handler
  149. * when the stage resizes, stretch to fit horizontally
  150. * and position at the bottom of the screen
  151. */
  152. private function fitToStage(evt:Event = null):void {
  153. Console_txt.width = mainStage.stageWidth;
  154. Console_txt.y = mainStage.stageHeight - Console_txt.height;
  155. // position titleBar at the top of the Console text field
  156. // if its visible or, if not, at the bottom of the screen
  157. titleBar.y = (Console_txt.visible) ? Console_txt.y - titleBar.height : mainStage.stageHeight - titleBar.height;
  158. titleBar.getChildByName("bar").width = mainStage.stageWidth;
  159. }
  160. }
  161. }