Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app
- {
- import app.utils.SystemUtil;
- import flash.display.NativeWindow;
- import flash.display.NativeWindowDisplayState;
- import flash.display.NativeWindowInitOptions;
- import flash.display.NativeWindowSystemChrome;
- import flash.display.NativeWindowType;
- import flash.display.Shape;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageQuality;
- import flash.display.StageScaleMode;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.ui.Keyboard;
- /**
- */
- final public class Console
- {
- //------------------------------------------------------------------------------------------
- //
- // STATIC PUBLIC PROPERTIES
- //
- //------------------------------------------------------------------------------------------
- static public const WIDTH:int = 800;
- static public const HEIGHT:int = 440;
- static public const PADDING:int = 20;
- static public const FONT:String = SystemUtil.isWindows() ? "Consolas" : "Monaco";
- static public const FONT_SIZE:int = 14;
- static public const FONT_COLOR:int = 0xF0F0F0;
- static public const LINE_HEIGHT:int = 20;
- static public const LINE_OFFSET:int = -2;
- static public const LINE_BUFFER:int = 100;
- static public const SCROLLER_WIDTH:int = 20;
- static public const SCROLLER_COLOR:int = 0xF0F0F0;
- static public const BACKGROUND_COLOR:int = 0x000000;
- //------------------------------------------------------------------------------------------
- //
- // PRIVATE PROPERTIES
- //
- //------------------------------------------------------------------------------------------
- private var m_window:NativeWindow;
- private var m_scroller:Sprite;
- private var m_scrollerIndex:int;
- private var m_scrollerPoint:int;
- private var m_textLines:Vector.<String>;
- private var m_textFields:Vector.<TextField>;
- private var m_isScrolling:Boolean;
- //------------------------------------------------------------------------------------------
- //
- // CONSTRUCTOR
- //
- //------------------------------------------------------------------------------------------
- /**
- */
- public function Console( title:String )
- {
- m_textLines = new Vector.<String>();
- m_textFields = new Vector.<TextField>();
- createWindow();
- createBackground();
- createScroller();
- createTextFields();
- m_window.title = title;
- m_window.stage.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
- m_window.stage.addEventListener( MouseEvent.MOUSE_UP, onMouseUp );
- m_window.stage.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove );
- m_window.stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyDown );
- }
- //------------------------------------------------------------------------------------------
- //
- // PUBLIC METHODS
- //
- //------------------------------------------------------------------------------------------
- /**
- */
- public function activate():void
- {
- if( m_window.displayState == NativeWindowDisplayState.MINIMIZED ) {
- m_window.restore();
- }
- m_window.activate();
- }
- /**
- */
- public function clear():void
- {
- m_textLines.length = 0;
- sync();
- }
- /**
- */
- public function write( ...lines ):void
- {
- var i:int = 0;
- var j:int = m_textLines.length;
- var n:int = lines.length;
- while( i < n ) {
- m_textLines[ j ] = String(lines[ i ]);
- i ++;
- j ++;
- }
- if( j > LINE_BUFFER ) {
- m_textLines.splice( 0, j - LINE_BUFFER );
- }
- if( m_isScrolling == false ) {
- m_scrollerIndex = j - m_textFields.length;
- }
- sync();
- }
- //------------------------------------------------------------------------------------------
- //
- // PRIVATE METHODS
- //
- //------------------------------------------------------------------------------------------
- /**
- */
- private function createWindow():void
- {
- var options:NativeWindowInitOptions = new NativeWindowInitOptions();
- options.type = NativeWindowType.NORMAL;
- options.systemChrome = NativeWindowSystemChrome.STANDARD;
- options.transparent = false;
- options.maximizable = false;
- options.minimizable = true;
- options.resizable = false;
- m_window = new NativeWindow( options );
- m_window.stage.align = StageAlign.TOP_LEFT;
- m_window.stage.quality = StageQuality.LOW;
- m_window.stage.scaleMode = StageScaleMode.NO_SCALE;
- m_window.stage.frameRate = 30;
- m_window.stage.stageWidth = WIDTH;
- m_window.stage.stageHeight = HEIGHT;
- }
- /**
- */
- private function createBackground():void
- {
- var background:Shape = new Shape();
- background.graphics.beginFill( BACKGROUND_COLOR );
- background.graphics.drawRect( 0, 0, WIDTH, HEIGHT );
- background.graphics.endFill();
- m_window.stage.addChild( background );
- }
- /**
- */
- private function createScroller():void
- {
- m_scroller = new Sprite();
- m_scroller.graphics.beginFill( SCROLLER_COLOR );
- m_scroller.graphics.drawRect( 0, 0, SCROLLER_WIDTH, HEIGHT );
- m_scroller.graphics.endFill();
- m_scroller.x = WIDTH - SCROLLER_WIDTH;
- m_scroller.mouseChildren = false;
- m_window.stage.addChild( m_scroller );
- }
- /**
- */
- private function createTextFields():void
- {
- var format:TextFormat = new TextFormat();
- format.font = FONT;
- format.size = FONT_SIZE;
- format.color = FONT_COLOR;
- format.kerning = 0;
- format.leading = 0;
- var i:int = 0;
- var n:int = ( HEIGHT - PADDING - PADDING ) / LINE_HEIGHT;
- var x:int = PADDING;
- var y:int = PADDING + LINE_OFFSET;
- var w:int = WIDTH - PADDING - PADDING - SCROLLER_WIDTH;
- var h:int = LINE_HEIGHT;
- while( i < n ) {
- var field:TextField = m_textFields[ i ] = new TextField();
- field.x = x;
- field.y = y;
- field.width = w;
- field.height = h;
- field.selectable = false;
- field.multiline = false;
- field.wordWrap = false;
- field.mouseEnabled = false;
- field.defaultTextFormat = format;
- m_window.stage.addChild( field );
- i ++;
- y += LINE_HEIGHT;
- }
- }
- /**
- */
- private function sync():void
- {
- var i:int = 0;
- var j:int = 0;
- var n:int = m_textFields.length;
- var m:int = m_textLines.length;
- if( m_scrollerIndex < 0 ) {
- m_scrollerIndex = 0;
- }
- else if( m > n && m_scrollerIndex > m - n ) {
- m_scrollerIndex = m - n;
- }
- j = m_scrollerIndex;
- while( i < n ) {
- m_textFields[ i ].text = j < m ? m_textLines[ j ] : "";
- i ++;
- j ++;
- }
- var h:int;
- if( m > n ) {
- h = Math.ceil( HEIGHT * ( n / m ) );
- }
- else {
- h = HEIGHT;
- }
- m_scroller.y = Math.ceil( ( HEIGHT / m ) * m_scrollerIndex );
- m_scroller.height = h;
- }
- //------------------------------------------------------------------------------------------
- //
- // PRIVATE METHODS - EVENT LISTENERS
- //
- //------------------------------------------------------------------------------------------
- /**
- */
- private function onMouseDown( event:MouseEvent ):void
- {
- if( event.target != m_scroller ) {
- return;
- }
- m_scroller.alpha = 0.5;
- m_scrollerPoint = m_scroller.y - event.stageY;
- m_isScrolling = true;
- }
- /**
- */
- private function onMouseUp( event:MouseEvent ):void
- {
- m_scroller.alpha = 1.0;
- m_isScrolling = false;
- }
- /**
- */
- private function onMouseMove( event:MouseEvent ):void
- {
- if( m_isScrolling == false ) {
- return;
- }
- var y:int = event.stageY + m_scrollerPoint;
- var h:int = m_scroller.height;
- if( y < 0 ) {
- y = 0;
- }
- else if( y > HEIGHT - h ) {
- y = HEIGHT - h;
- }
- var i:int = Math.round( ( m_textLines.length / HEIGHT ) * y );
- if( m_scrollerIndex != i ) {
- m_scrollerIndex = i;
- sync();
- }
- }
- /**
- */
- private function onKeyDown( event:KeyboardEvent ):void
- {
- if( m_isScrolling ) {
- return;
- }
- switch( event.keyCode ) {
- case Keyboard.UP: {
- m_scrollerIndex --;
- break;
- }
- case Keyboard.DOWN: {
- m_scrollerIndex ++;
- break;
- }
- case Keyboard.PAGE_UP: {
- m_scrollerIndex -= m_textFields.length;
- break;
- }
- case Keyboard.PAGE_DOWN: {
- m_scrollerIndex += m_textFields.length;
- break;
- }
- case Keyboard.HOME: {
- m_scrollerIndex = 0;
- break;
- }
- case Keyboard.END: {
- m_scrollerIndex = m_textLines.length - m_textFields.length;
- break;
- }
- default: return;
- }
- sync();
- }
- }// EOC
- }
Add Comment
Please, Sign In to add comment