/as3/net/inctm/logger/LoggerLayout.as
ActionScript | 1 lines | 1 code | 0 blank | 0 comment | 0 complexity | 044f96b8ce1562e4a0ad092c83baee2c MD5 | raw file
- /**
* This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a copy of
* this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to Creative Commons,
* 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
* Copyright (c) 2010 Colin Duffy
*
*/
package net.inctm.logger
{
//_______________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------------------- IMPORTS
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.System;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.utils.getTimer;
import net.inctm.components.Style;
import net.inctm.components.VSlider;
import net.inctm.utils.StopWatch;
/**
* Controls the visual updates of the Logger class.
*
* @author Colin Duffy
* @modified 11-28-2010
* @version 1.0.3
*/
public class LoggerLayout extends Sprite
{
//____________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------- PUBLIC VARIABLES
//____________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------ PRIVATE VARIABLES
protected var g :Graphics;
protected var fieldMessage :TextField;
protected var fieldFPS :TextField;
protected var fieldMemory :TextField;
protected var draggable :Sprite;
protected var closeButton :Sprite;
protected var scroller :VSlider;
protected var watch :StopWatch;
protected var currentTime :uint;
protected var frameCount :int;
protected var totalCount :int;
protected var minMem :Number = Number.MAX_VALUE;
protected var maxMem :Number = Number.MIN_VALUE;
protected var minFps :Number = Number.MAX_VALUE;
protected var maxFps :Number = Number.MIN_VALUE;
protected var initTime :int;
protected var itvTime :int;
protected const spacerChar :String = "ť";
// Output colors
protected const COLOR_COPY :String = "CCFFAA";
protected const COLOR_LOG :String = "AADDFF";
protected const COLOR_WARN :String = "FFAAAA";
protected const COLOR_ERROR :String = "FF1133";
// Log colors
protected const COLOR_LOW :String = "666666";
protected const COLOR_NORMAL :String = "CCCCCC";
protected const COLOR_HIGH :String = "FFFFFF";
//____________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------------ CONSTRUCTOR
public function LoggerLayout():void
{
super();
g = this.graphics;
this.drawBackground();
this.drawStaticText();
this.drawCloseButton();
this.drawOutputText();
Style.BACKGROUND = 0x000000;
scroller = new VSlider( this, 585, 28, scrollHandler );
scroller.width = 15;
scroller.height = 244;
scroller.setSliderParams (0, 100, 0);
scroller.value = Math.abs (scroller.value - 100);
scroller.alpha = 0.89;
scroller.scrollerSize = 50;
scroller.enabled = false;
watch = new StopWatch( true );
initTime = itvTime = getTimer();
totalCount = frameCount = 0;
this.run();
this.addEventListener( Event.ENTER_FRAME, onAdded );
}
//____________________________________________________________________________________________________________
//--------------------------------------------------------------------------------------------- PUBLIC METHODS
/**
* Colorizes the output window.
* @param type The Log Type: trace/copy/warn.
* @param sender The name of the object that's sending the message.
* @param message The output to display.
*/
public function message( type:String, sender:*, messages:Array ):void
{
var total:int = messages.length;
var styled:String = "";
styled = "<font color=\"#" + getType( type ) + "\">" + getTypeName( type );
styled += " </font>";
styled += spacer;
styled += "<font color=\"#CCCCCC\"> " + sender.toString();
styled += " </font>";
styled += "\n";
styled += "<font color=\"#FFFFFF\">";
var i:int;
for( i = 0; i < total; i++ ) {
styled += spacerChar + " " + messages[i];
if( i < total-1 ) styled += "\n";
}
styled += " </font>";
this.result( styled );
if ( fieldMessage.numLines > 16 ) scroller.enabled = true;
if ( fieldMessage.numLines > 16 ) scroller.value = 0;
if ( Logger.TRACE ) {
trace( sender.toString() + " " + spacerChar + " " + this.getTypeName( type ) );
for( i = 0; i < messages.length; i++ ) trace( " " + spacerChar + " " + messages[i] );
}
}
/**
* Skips a line in the message field.
*/
public function skipLine():void
{
fieldMessage.htmlText += spacer + "\n";
fieldFPS.htmlText += "---";
fieldMemory.htmlText += "---";
this.textChange();
}
/**
* Disposes of all stored variables.
*/
public function dispose():void
{
this.removeEventListener( Event.ENTER_FRAME, run );
}
//____________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------ PROTECTED METHODS
/**
* Prepares the output window to receive more lines.
*/
protected function breakLine():void
{
fieldMessage.appendText("\n");
fieldFPS.appendText("\n");
fieldMemory.appendText("\n");
this.textChange();
}
/**
* Resets the textfields' formats.
*/
protected function textChange():void
{
fieldMessage.setTextFormat( LoggerLayout.outputFormat );
fieldFPS.setTextFormat( LoggerLayout.centerFormat );
fieldMemory.setTextFormat( LoggerLayout.centerFormat );
fieldMessage.scrollV = fieldMessage.maxScrollV;
fieldFPS.scrollV = fieldFPS.maxScrollV;
fieldMemory.scrollV = fieldMemory.maxScrollV;
}
/**
* Draws the background.
*/
protected function drawBackground():void
{
//Dividers
g.beginFill(0x000000, 0.9);
g.drawRect(0, 27, 421, 1);
g.drawRect(422, 27, 88, 1);
g.drawRect(511, 27, 73, 1);
g.drawRect(585, 27, 15, 1);
g.drawRect(0, 272, 421, 1);
g.drawRect(422, 272, 88, 1);
g.drawRect(511, 272, 74, 1);
g.drawRect(421, 0, 1, 300);
g.drawRect(510, 0, 1, 300);
g.drawRect(584, 0, 1, 272);
g.endFill();
//Fills top
g.beginFill(0x000000, 0.87);
g.drawRect(0, 0, 421, 27);
g.drawRect(422, 0, 88, 27);
g.drawRect(511, 0, 73, 27);
//g.drawRect(585, 0, 15, 27);
g.endFill();
//Fills Middle
g.beginFill(0x000000, 0.85);
g.drawRect(0, 28, 421, 244);
g.drawRect(422, 28, 88, 244);
g.drawRect(511, 28, 73, 244);
g.endFill();
g.beginFill(0x000000, 0.87);
g.drawRect(585, 28, 15, 272);
g.endFill();
//Fills bottom
g.beginFill(0x000000, 0.87);
g.drawRect(0, 273, 421, 27);
g.drawRect(422, 273, 88, 27);
g.drawRect(511, 273, 74, 27);
g.endFill();
}
protected function drawStaticText():void
{
var topMessage:TextField = new TextField();
topMessage.text = "Logged Messages";
topMessage.x = 10;
this.addChild( topMessage );
var topFPS:TextField = new TextField();
topFPS.text = "FPS";
topFPS.x = 424;
topFPS.width = 84;
this.addChild( topFPS );
var topMemory:TextField = new TextField();
topMemory.text = "Memory";
topMemory.x = 513;
topMemory.width = 69;
this.addChild( topMemory );
topMessage.y = topFPS.y = topMemory.y = 6;
topMessage.selectable = topFPS.selectable = topMemory.selectable = false;
topMessage.setTextFormat( LoggerLayout.outputFormat );
topMessage.defaultTextFormat = topMessage.getTextFormat();
topFPS.setTextFormat( LoggerLayout.centerFormat );
topFPS.defaultTextFormat = topFPS.getTextFormat();
topMemory.setTextFormat( LoggerLayout.centerFormat );
topMemory.defaultTextFormat = topMemory.getTextFormat();
topMessage.textColor = 0xFFFFFF;
topFPS.textColor = 0xFFFFFF;
topMemory.textColor = 0xFFFFFF;
draggable = new Sprite();
draggable.graphics.beginFill(0, 0);
draggable.graphics.drawRect(0, 0, 600, 27);
draggable.graphics.endFill();
addChild(draggable);
}
protected function drawOutputText():void
{
fieldMessage = new TextField();
fieldFPS = new TextField();
fieldMemory = new TextField();
fieldMessage.text = "";
fieldFPS.text = "";
fieldMemory.text = "";
this.addChild( fieldMessage );
this.addChild( fieldFPS );
this.addChild( fieldMemory );
fieldMessage.x = 10;
fieldFPS.x = 424;
fieldMemory.x = 513
fieldMessage.width = 408;
fieldFPS.width = 84;
fieldMemory.width = 69;
fieldMessage.height = fieldFPS.height = fieldMemory.height = 245;
fieldMessage.wordWrap = true;
fieldMessage.antiAliasType = fieldFPS.antiAliasType = fieldMemory.antiAliasType = "advanced";
fieldMessage.multiline = fieldFPS.multiline = fieldMemory.multiline = true;
fieldMessage.y = fieldFPS.y = fieldMemory.y = 32;
}
protected function drawCloseButton():void
{
closeButton = new Sprite();
var g:Graphics = closeButton.graphics;
closeButton.x = 585;
g.beginFill(0x000000, 0.87);
g.drawRect(0, 0, 15, 27);
g.endFill();
g.beginFill(0xFFFFFF, 1);
g.drawRect(5, 11, 1, 1);
g.drawRect(6, 12, 1, 1);
g.drawRect(7, 13, 1, 1);
g.drawRect(8, 14, 1, 1);
g.drawRect(9, 15, 1, 1);
g.drawRect(5, 15, 1, 1);
g.drawRect(6, 14, 1, 1);
g.drawRect(8, 12, 1, 1);
g.drawRect(9, 11, 1, 1);
g.endFill();
addChild(closeButton);
}
protected function result( message:String ):void
{
var before:int = fieldMessage.numLines;
fieldMessage.htmlText += message;
fieldFPS.htmlText += currentFps.toFixed(2) + " FPS";
fieldMemory.htmlText += currentMem.toFixed(2) + " mb";
var addLines:int = fieldMessage.numLines - before;
for( var i:int = 0; i < addLines; i++) {
fieldFPS.htmlText += "---";
fieldMemory.htmlText += "---";
}
breakLine();
}
protected function getType( type:String ):String
{
switch( type ) {
case Logger.LOG_TRACE:
return COLOR_LOG;
break;
case Logger.LOG_COPY:
return COLOR_COPY;
break;
case Logger.LOG_WARN:
return COLOR_WARN;
break;
case Logger.LOG_ERROR:
return COLOR_ERROR;
break;
}
return COLOR_LOG;
}
protected function getTypeName( type:String ):String
{
switch( type ) {
case Logger.LOG_TRACE:
return "Log ";
break;
case Logger.LOG_COPY:
return "Copy ";
break;
case Logger.LOG_WARN:
return "Warn ";
break;
case Logger.LOG_ERROR:
return "ERROR ";
break;
}
return "Log ";
}
protected function getLevelName( level:int ):String
{
switch( level ) {
case 1:
return "Low ";
break;
case 2:
return "Normal ";
break;
case 3:
return "High ";
break;
}
return "Low ";
}
protected function stripHTML( stringText:String ):String
{
var links:RegExp = new RegExp("<[^<]+?>", "gi");
stringText = stringText.replace(links, "");
return (stringText);
}
//____________________________________________________________________________________________________________
//--------------------------------------------------------------------------------------------- EVENT HANDLERS
protected function onAdded( evt:Event ):void
{
this.removeEventListener( Event.ENTER_FRAME, onAdded );
this.addEventListener( Event.ENTER_FRAME, run );
draggable.addEventListener( MouseEvent.MOUSE_DOWN, onDown );
closeButton.addEventListener( MouseEvent.CLICK, closeWindow );
closeButton.buttonMode = true;
draggable.buttonMode = true;
}
/**
* The Event.ENTER_FRAME handler to determine frame rate.
*/
protected function run( evt:Event = null ):void
{
currentTime = getTimer();
frameCount++;
totalCount++;
if(intervalTime >= 1) {
itvTime = currentTime;
frameCount = 0;
}
minMem = Math.min( currentMem, minMem );
maxMem = Math.max( currentMem, maxMem );
}
protected function scrollHandler( evt:Event ):void
{
scrollPosition = Math.abs( scroller.value - 100 ) * 0.01;
}
//____________________________________________________________________________________________________________
//--------------------------------------------------------------------------------------------- MOUSE HANDLERS
protected function closeWindow( evt:MouseEvent ):void
{
Logger.hide();
}
protected function onDown( evt:MouseEvent ):void
{
draggable.removeEventListener( MouseEvent.MOUSE_DOWN, onDown );
stage.addEventListener( MouseEvent.MOUSE_MOVE, onMove );
stage.addEventListener( MouseEvent.MOUSE_UP, onUp );
this.startDrag();
}
protected function onUp( evt:MouseEvent ):void
{
draggable.addEventListener( MouseEvent.MOUSE_DOWN, onDown );
stage.removeEventListener( MouseEvent.MOUSE_MOVE, onMove );
stage.removeEventListener( MouseEvent.MOUSE_UP, onUp );
this.stopDrag();
}
/**
* Smooths the drag.
*/
protected function onMove( evt:MouseEvent ):void
{
evt.updateAfterEvent();
}
//____________________________________________________________________________________________________________
//---------------------------------------------------------------------------------------------------- GETTERS
/**
* @private
* Returns the color string of the spacer character.
*/
protected function get spacer():String
{
return " <font color=\"#666666\">" + spacerChar + "</font> ";
}
/**
* Format used for the output text.
*/
public static function get outputFormat():TextFormat
{
var format:TextFormat = new TextFormat();
format.font = "Arial";
format.size = 9;
format.align = "left";
format.leading = 4;
return format;
}
/**
* Format used for the FPS/Memory panels.
*/
public static function get centerFormat():TextFormat
{
var format:TextFormat = new TextFormat();
format.font = "Arial";
format.size = 9;
format.align = "center";
format.color = 0xCCCCCC;
format.leading = 4;
return format;
}
/**
* Time since last Logger call.
*/
public function get benchmark():String
{
return Number(watch.lap() * 0.001).toFixed(2);
}
/**
* The current FPS.
*/
public function get currentFps():Number {
var fps:Number = Number( frameCount / intervalTime );
if( fps == Infinity ) return 0;
if( isNaN( fps ) ) return 0;
return fps;
}
/**
* The current global flashplayer memory usage.
*/
public function get currentMem():Number {
return (System.totalMemory / 1024) / 1000;
}
/**
* The average FPS.
*/
public function get averageFps():Number {
return totalCount / runningTime;
}
/**
* The average RAM.
*/
public function get averageMem():Number {
return (minMem + maxMem) / 2;
}
/**
* The amount of time the monitor has been running.
*/
public function get runningTime():Number {
return (currentTime - initTime) / 1000;
}
/**
* The interval at which the monitor is running.
*/
public function get intervalTime():Number {
return (currentTime - itvTime) / 1000;
}
//____________________________________________________________________________________________________________
//---------------------------------------------------------------------------------------------------- SETTERS
public function set scrollPosition( value:Number ):void
{
var scrollValue:int = int( value * this.fieldMessage.maxScrollV );
this.fieldMessage.scrollV = scrollValue;
this.fieldMemory.scrollV = scrollValue;
this.fieldFPS.scrollV = scrollValue;
}
//____________________________________________________________________________________________________________
//------------------------------------------------------------------------------------------ INHERITED METHODS
/**
* Returns "[LoggerLayout]" when you call the class name directly.
* @return String
*/
override public function toString():String {
return "[LoggerLayout]";
}
}
}