Guest User

Untitled

a guest
Jan 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.43 KB | None | 0 0
  1. package app
  2. {
  3. import app.utils.SystemUtil;
  4.  
  5. import flash.display.NativeWindow;
  6. import flash.display.NativeWindowDisplayState;
  7. import flash.display.NativeWindowInitOptions;
  8. import flash.display.NativeWindowSystemChrome;
  9. import flash.display.NativeWindowType;
  10. import flash.display.Shape;
  11. import flash.display.Sprite;
  12. import flash.display.StageAlign;
  13. import flash.display.StageQuality;
  14. import flash.display.StageScaleMode;
  15. import flash.events.KeyboardEvent;
  16. import flash.events.MouseEvent;
  17. import flash.text.TextField;
  18. import flash.text.TextFormat;
  19. import flash.ui.Keyboard;
  20.  
  21. /**
  22. */
  23. final public class Console
  24. {
  25. //------------------------------------------------------------------------------------------
  26. //
  27. // STATIC PUBLIC PROPERTIES
  28. //
  29. //------------------------------------------------------------------------------------------
  30.  
  31. static public const WIDTH:int = 800;
  32. static public const HEIGHT:int = 440;
  33. static public const PADDING:int = 20;
  34. static public const FONT:String = SystemUtil.isWindows() ? "Consolas" : "Monaco";
  35. static public const FONT_SIZE:int = 14;
  36. static public const FONT_COLOR:int = 0xF0F0F0;
  37. static public const LINE_HEIGHT:int = 20;
  38. static public const LINE_OFFSET:int = -2;
  39. static public const LINE_BUFFER:int = 100;
  40. static public const SCROLLER_WIDTH:int = 20;
  41. static public const SCROLLER_COLOR:int = 0xF0F0F0;
  42. static public const BACKGROUND_COLOR:int = 0x000000;
  43.  
  44. //------------------------------------------------------------------------------------------
  45. //
  46. // PRIVATE PROPERTIES
  47. //
  48. //------------------------------------------------------------------------------------------
  49.  
  50. private var m_window:NativeWindow;
  51.  
  52. private var m_scroller:Sprite;
  53. private var m_scrollerIndex:int;
  54. private var m_scrollerPoint:int;
  55.  
  56. private var m_textLines:Vector.<String>;
  57. private var m_textFields:Vector.<TextField>;
  58.  
  59. private var m_isScrolling:Boolean;
  60.  
  61. //------------------------------------------------------------------------------------------
  62. //
  63. // CONSTRUCTOR
  64. //
  65. //------------------------------------------------------------------------------------------
  66.  
  67. /**
  68. */
  69. public function Console( title:String )
  70. {
  71. m_textLines = new Vector.<String>();
  72. m_textFields = new Vector.<TextField>();
  73.  
  74. createWindow();
  75. createBackground();
  76. createScroller();
  77. createTextFields();
  78.  
  79. m_window.title = title;
  80.  
  81. m_window.stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
  82. m_window.stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
  83. m_window.stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
  84. m_window.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
  85. }
  86.  
  87. //------------------------------------------------------------------------------------------
  88. //
  89. // PUBLIC METHODS
  90. //
  91. //------------------------------------------------------------------------------------------
  92.  
  93. /**
  94. */
  95. public function activate():void
  96. {
  97. if( m_window.displayState == NativeWindowDisplayState.MINIMIZED ) {
  98. m_window.restore();
  99. }
  100. m_window.activate();
  101. }
  102.  
  103. /**
  104. */
  105. public function clear():void
  106. {
  107. m_textLines.length = 0;
  108. sync();
  109. }
  110.  
  111. /**
  112. */
  113. public function write( ...lines ):void
  114. {
  115. var i:int = 0;
  116. var j:int = m_textLines.length;
  117. var n:int = lines.length;
  118.  
  119. while( i < n ) {
  120. m_textLines[ j ] = String(lines[ i ]);
  121. i ++;
  122. j ++;
  123. }
  124.  
  125. if( j > LINE_BUFFER ) {
  126. m_textLines.splice( 0, j - LINE_BUFFER );
  127. }
  128.  
  129. if( m_isScrolling == false ) {
  130. m_scrollerIndex = j - m_textFields.length;
  131. }
  132.  
  133. sync();
  134. }
  135.  
  136. //------------------------------------------------------------------------------------------
  137. //
  138. // PRIVATE METHODS
  139. //
  140. //------------------------------------------------------------------------------------------
  141.  
  142. /**
  143. */
  144. private function createWindow():void
  145. {
  146. var options:NativeWindowInitOptions = new NativeWindowInitOptions();
  147.  
  148. options.type = NativeWindowType.NORMAL;
  149. options.systemChrome = NativeWindowSystemChrome.STANDARD;
  150. options.transparent = false;
  151. options.maximizable = false;
  152. options.minimizable = true;
  153. options.resizable = false;
  154.  
  155. m_window = new NativeWindow( options );
  156.  
  157. m_window.stage.align = StageAlign.TOP_LEFT;
  158. m_window.stage.quality = StageQuality.LOW;
  159. m_window.stage.scaleMode = StageScaleMode.NO_SCALE;
  160. m_window.stage.frameRate = 30;
  161. m_window.stage.stageWidth = WIDTH;
  162. m_window.stage.stageHeight = HEIGHT;
  163. }
  164.  
  165. /**
  166. */
  167. private function createBackground():void
  168. {
  169. var background:Shape = new Shape();
  170.  
  171. background.graphics.beginFill( BACKGROUND_COLOR );
  172. background.graphics.drawRect( 0, 0, WIDTH, HEIGHT );
  173. background.graphics.endFill();
  174.  
  175. m_window.stage.addChild( background );
  176. }
  177.  
  178. /**
  179. */
  180. private function createScroller():void
  181. {
  182. m_scroller = new Sprite();
  183.  
  184. m_scroller.graphics.beginFill( SCROLLER_COLOR );
  185. m_scroller.graphics.drawRect( 0, 0, SCROLLER_WIDTH, HEIGHT );
  186. m_scroller.graphics.endFill();
  187.  
  188. m_scroller.x = WIDTH - SCROLLER_WIDTH;
  189. m_scroller.mouseChildren = false;
  190.  
  191. m_window.stage.addChild( m_scroller );
  192. }
  193.  
  194. /**
  195. */
  196. private function createTextFields():void
  197. {
  198. var format:TextFormat = new TextFormat();
  199.  
  200. format.font = FONT;
  201. format.size = FONT_SIZE;
  202. format.color = FONT_COLOR;
  203. format.kerning = 0;
  204. format.leading = 0;
  205.  
  206. var i:int = 0;
  207. var n:int = ( HEIGHT - PADDING - PADDING ) / LINE_HEIGHT;
  208.  
  209. var x:int = PADDING;
  210. var y:int = PADDING + LINE_OFFSET;
  211. var w:int = WIDTH - PADDING - PADDING - SCROLLER_WIDTH;
  212. var h:int = LINE_HEIGHT;
  213.  
  214. while( i < n ) {
  215. var field:TextField = m_textFields[ i ] = new TextField();
  216.  
  217. field.x = x;
  218. field.y = y;
  219. field.width = w;
  220. field.height = h;
  221. field.selectable = false;
  222. field.multiline = false;
  223. field.wordWrap = false;
  224. field.mouseEnabled = false;
  225. field.defaultTextFormat = format;
  226.  
  227. m_window.stage.addChild( field );
  228.  
  229. i ++;
  230. y += LINE_HEIGHT;
  231. }
  232. }
  233.  
  234. /**
  235. */
  236. private function sync():void
  237. {
  238. var i:int = 0;
  239. var j:int = 0;
  240. var n:int = m_textFields.length;
  241. var m:int = m_textLines.length;
  242.  
  243. if( m_scrollerIndex < 0 ) {
  244. m_scrollerIndex = 0;
  245. }
  246. else if( m > n && m_scrollerIndex > m - n ) {
  247. m_scrollerIndex = m - n;
  248. }
  249.  
  250. j = m_scrollerIndex;
  251.  
  252. while( i < n ) {
  253. m_textFields[ i ].text = j < m ? m_textLines[ j ] : "";
  254. i ++;
  255. j ++;
  256. }
  257.  
  258. var h:int;
  259.  
  260. if( m > n ) {
  261. h = Math.ceil( HEIGHT * ( n / m ) );
  262. }
  263. else {
  264. h = HEIGHT;
  265. }
  266.  
  267. m_scroller.y = Math.ceil( ( HEIGHT / m ) * m_scrollerIndex );
  268. m_scroller.height = h;
  269. }
  270.  
  271. //------------------------------------------------------------------------------------------
  272. //
  273. // PRIVATE METHODS - EVENT LISTENERS
  274. //
  275. //------------------------------------------------------------------------------------------
  276.  
  277. /**
  278. */
  279. private function onMouseDown( event:MouseEvent ):void
  280. {
  281. if( event.target != m_scroller ) {
  282. return;
  283. }
  284.  
  285. m_scroller.alpha = 0.5;
  286. m_scrollerPoint = m_scroller.y - event.stageY;
  287. m_isScrolling = true;
  288. }
  289.  
  290. /**
  291. */
  292. private function onMouseUp( event:MouseEvent ):void
  293. {
  294. m_scroller.alpha = 1.0;
  295. m_isScrolling = false;
  296. }
  297.  
  298. /**
  299. */
  300. private function onMouseMove( event:MouseEvent ):void
  301. {
  302. if( m_isScrolling == false ) {
  303. return;
  304. }
  305.  
  306. var y:int = event.stageY + m_scrollerPoint;
  307. var h:int = m_scroller.height;
  308.  
  309. if( y < 0 ) {
  310. y = 0;
  311. }
  312. else if( y > HEIGHT - h ) {
  313. y = HEIGHT - h;
  314. }
  315.  
  316. var i:int = Math.round( ( m_textLines.length / HEIGHT ) * y );
  317.  
  318. if( m_scrollerIndex != i ) {
  319. m_scrollerIndex = i;
  320. sync();
  321. }
  322. }
  323.  
  324. /**
  325. */
  326. private function onKeyDown( event:KeyboardEvent ):void
  327. {
  328. if( m_isScrolling ) {
  329. return;
  330. }
  331.  
  332. switch( event.keyCode ) {
  333. case Keyboard.UP: {
  334. m_scrollerIndex --;
  335. break;
  336. }
  337.  
  338. case Keyboard.DOWN: {
  339. m_scrollerIndex ++;
  340. break;
  341. }
  342.  
  343. case Keyboard.PAGE_UP: {
  344. m_scrollerIndex -= m_textFields.length;
  345. break;
  346. }
  347.  
  348. case Keyboard.PAGE_DOWN: {
  349. m_scrollerIndex += m_textFields.length;
  350. break;
  351. }
  352.  
  353. case Keyboard.HOME: {
  354. m_scrollerIndex = 0;
  355. break;
  356. }
  357.  
  358. case Keyboard.END: {
  359. m_scrollerIndex = m_textLines.length - m_textFields.length;
  360. break;
  361. }
  362.  
  363. default: return;
  364. }
  365.  
  366. sync();
  367. }
  368.  
  369. }// EOC
  370. }
Add Comment
Please, Sign In to add comment