1  /**
     2   * com.sekati.log.ConsoleStyle
     3   * @version 1.3.5
     4   * @author jason m horwitz | sekati.com
     5   * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
     6   * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
     7   */
     8  
     9  import com.sekati.display.BaseClip;
    10  import com.sekati.draw.Rectangle;
    11  import com.sekati.draw.Triangle;
    12  import com.sekati.geom.Point;
    13  import com.sekati.utils.ClassUtils;
    14  import com.sekati.utils.TextUtils; 
    15  
    16  /**
    17   * Singleton class defining {@link com.sekati.log.Console} style and layout.
    18   */
    19  class com.sekati.log.ConsoleStyle {
    20  
    21  	private static var _instance:ConsoleStyle;
    22  	public var CW:Number = 900; 
    23  	// console width
    24  	public var CH:Number = 500; 
    25  	// console height
    26  	public var IW:Number = 889; 
    27  	// item width
    28  	public var IH:Number = 12; 
    29  	// item height
    30  	public var TH:Number = 14; 
    31  	// text height
    32  	
    33  	// style definition
    34  	public var CSS:Object = {
    35  		console:{
    36  			n:"___Console", x:0, y:0, w:CW, h:CH, minW:450, minH:150, maxW:Stage.width, maxH:Stage.height, bg:{n:"bgMc", x:0, y:0, w:CW, h:CH, c:0x1D1D1D, a:95}, head:{
    37  				n:"headMc", x:0, y:0, w:CW, h:IH, isDraggable:false, bg:{n:"bcMc", x:0, y:0, w:CW, h:IH, c:0xFFFFFF, a:100}, textfields:{
    38  					head:{n:"headTf", t:"com.sekati.log.Console", url:"http://sasapi.googlecode.com/", x:5, y:-1, w:500, h:TH, c:0x6C8297, a:100}
    39  				}, fps:{n:"fpsMc", x:CW - 120, y:0, w:110, h:TH, updateRate:10, bg:{n:"bgMc", x:0, y:0, w:110, h:TH - 2, c:0xFFFFFF, a:0}, textfields:{
    40  						trend_colors:{up:"#009900", down:"#CC0000"}, current_label:{n:"currentLabelTf", t:"current", x:0, y:-1, w:42, h:TH, c:0x6C8297, a:100}, current_fps:{n:"currentFpsTf", t:"00", html:true, x:32, y:-1, w:25, h:TH, c:0x6C8297, a:100}, average_label:{n:"averageLabelTf", t:"average", x:55, y:-1, w:42, h:TH, c:0x6C8297, a:100}, average_fps:{n:"averageFpsTf", t:"00", html:true, x:91, y:-1, w:25, h:TH, c:0x6C8297, a:100}
    41  					}
    42  				}				
    43  			}, holder:{
    44  				n:"holderMc", x:0, y:IH, w:0, h:0, list:{n:"listMc", x:0, y:0, w:0, h:0}, mask:{n:"maskMc", x:0, y:0, w:IW, h:CH - IH, c:0x00ffff, a:100}, gutter:{n:"gutterMc", x:CW - 10, y:0, w:10, h:CH - IH, c:0x000000, a:100}, bar:{n:"barMc", x:CW - 10, y:0, w:10, h:165, c:0x6C8297, a:100} // c:0xCC3300, a:30
    45  			}, resizer:{n:"resizerMc", x:CW - 10, y:CH - 10, w:10, h:10, c:0xFFFFFF, a:100}
    46  		}, item:{
    47  			n:"itemMc", x:0, y:0, w:IW, h:IH, bg:{n:"bgMc", x:0, y:0, w:IW, h:IH, c:[ 0x202020, 0x000000 ], cindex:1, a:50}, line:{n:"lineMc", x:0, y:0, w:IW, h:1, c:0xFFFFFF, a:50}, textfields:{
    48  				id:{n:"idTf", t:"id", x:5, y:0, w:30, h:TH, c:0x999999, a:100}, type:{n:"typeTf", t:"type", x:40, y:0, w:40, h:TH, a:100, c:{
    49  					trace:0xFF00FF, info:0x00FFFF, status:0x99FF00, warn:0xFFFF00, error:0xFF9900, fatal:0xFF0000, object:0x9900FF, custom:0x0066FF}
    50  				}, origin:{n:"originTf", t:"origin", selectable:false, x:85, y:0, w:90, h:TH, c:0x6C8297, a:100}, message:{n:"messageTf", t:"message", selectable:true, x:180, y:0, w:650, h:TH, c:0xF7F7F7, a:100}, benchmark:{n:"benchmarkTf", t:"benchmark", x:835, y:0, w:51, h:TH, c:0x999999, a:100}					
    51  			}
    52  		}, meta_item:{
    53  			n:"metaItemMc", x:0, y:0, w:IW, h:IH, bg:{n:"bgMc", x:0, y:0, w:IW, h:IH, c:0x000000, a:95}, line:{n:"lineMc", x:0, y:0, w:IW, h:1, c:0xFFFFFF, a:50}, textfields:{
    54  				id:{n:"idTf", t:"id", x:5, y:0, w:30, h:TH, c:0x666666, a:100}, type:{n:"typeTf", t:"type", x:40, y:0, w:40, h:TH, c:0x666666, a:100}, origin:{n:"originTf", t:"origin", x:85, y:0, w:90, h:TH, c:0x666666, a:100}, message:{n:"messageTf", t:"message", x:180, y:0, w:650, h:TH, c:0x666666, a:100}, benchmark:{n:"benchmarkTf", t:"benchmark", x:835, y:0, w:51, h:TH, c:0x666666, a:100}					
    55  			}
    56  		}
    57  	};	
    58  
    59  	/**
    60  	 * Singleton Private Constructor
    61  	 */
    62  	private function ConsoleStyle() {
    63  	}
    64  
    65  	/**
    66  	 * Singleton Accessor
    67  	 * @return ConsoleStyle
    68  	 */	
    69  	public static function getInstance():ConsoleStyle {
    70  		if (!_instance) _instance = new ConsoleStyle( );
    71  		return _instance;
    72  	}
    73  
    74  	/**
    75  	 * Shorthand Singleton Accessor Getter
    76  	 */
    77  	public static function get $():ConsoleStyle {
    78  		return ConsoleStyle.getInstance( );	
    79  	}	
    80  
    81  	/**
    82  	 * Create an empty BaseClip - but do not inherit x,y,w,h from 'style' arg.
    83  	 * @param target (MovieClip)
    84  	 * @param style (Object) ConsoleStyle.CSS reference obj
    85  	 * @return MovieClip
    86  	 * {@code Usage:
    87  	 * 	ar e:MovieClip = ConsoleStyle.$.createClip(_root, ConsoleStyle.$.CSS.console.item.bg);
    88  	 * }
    89  	 */
    90  	public function createClip(target:MovieClip, style:Object):MovieClip {
    91  		return ClassUtils.createEmptyMovieClip( com.sekati.display.BaseClip, target, style.n );
    92  	}
    93  
    94  	/**
    95  	 * Create an empty BaseClip which inherits x,y from 'style' arg.
    96  	 * @param target (MovieClip)
    97  	 * @param style (Object) ConsoleStyle.CSS reference obj
    98  	 * @return MovieClip
    99  	 * {@code Usage:
   100  	 * 	ar e:MovieClip = ConsoleStyle.$.createPositionClip(_root, ConsoleStyle.$.CSS.console.container);
   101  	 * }
   102  	 */
   103  	public function createPositionClip(target:MovieClip, style:Object):MovieClip {
   104  		return ClassUtils.createEmptyMovieClip( com.sekati.display.BaseClip, target, style.n, {_x:style.x, _y:style.y, _alpha:style.a} );
   105  	}
   106  
   107  	/**
   108  	 * Create a styled rectangle BaseClip
   109  	 * @param target (MovieClip)
   110  	 * @param style (Object) ConsoleStyle.CSS reference obj
   111  	 * @return MovieClip
   112  	 * {@code Usage:
   113  	 * 	var r:MovieClip = ConsoleStyle.$.createStyledRectangle (_this, ConsoleStyle.$.CSS.item.bg);
   114  	 * }
   115  	 */
   116  	public function createStyledRectangle(target:MovieClip, style:Object):MovieClip {
   117  		// create the shape container clip
   118  		var r:MovieClip = createClip( target, style );	
   119  		// sanitize style.c - if it is an array (instead of a number) it means we need to alternate colors with style.cindex
   120  		var c:Number = (style.c instanceof Array) ? alternateItemBgColor( ) : style.c;
   121  		//trace("drawing rect: "+style.n+": "+c);
   122  		Rectangle.draw( r, new Point( 0, 0 ), new Point( style.w, style.h ), c, style.a );
   123  		r._x = style.x;
   124  		r._y = style.y;
   125  		return r;
   126  	}
   127  
   128  	/**
   129  	 * Create a styled triangle BaseClip
   130  	 * @param target (MovieClip)
   131  	 * @param style (Object) ConsoleStyle.CSS reference obj
   132  	 * @return MovieClip
   133  	 */
   134  	public function createStyledTriangle(target:MovieClip, style:Object):MovieClip {
   135  		var t:MovieClip = createClip( target, style );
   136  		Triangle.draw( t, new Point( style.w, style.h ), new Point( 0, style.h ), new Point( style.w, 0 ), style.c, style.a, 0 );
   137  		t._x = style.x;
   138  		t._y = style.y;
   139  		return t;	
   140  	}
   141  
   142  	/**
   143  	 * Alternate the item bg color to create "zebra striped" item entries.
   144  	 * @return Number - item bg color
   145  	 */
   146  	private function alternateItemBgColor():Number {
   147  		CSS.item.bg.cindex = (CSS.item.bg.cindex == 1) ? 0 : 1;
   148  		//trace("alternateColor ["+CSS.item.bg.cindex+"] called: "+CSS.item.bg.c[CSS.item.bg.cindex]);
   149  		return CSS.item.bg.c[CSS.item.bg.cindex];
   150  	}	
   151  
   152  	/**
   153  	 * Create a styled textfield
   154  	 * @param target (MovieClip)
   155  	 * @param style (Object) ConsoleStyle.CSS reference obj
   156  	 * @param str (String) contents of textfield [i.e. "trace"]
   157  	 * @return TextField
   158  	 * {@code Usage:
   159  	 * 	var tf:TextField = ConsoleStyle.$.createStyledTextField(_this, ConsoleStyle.$.CSS.item.textfields.origin, "hello world");
   160  	 * }
   161  	 */
   162  	public function createStyledTextField(target:MovieClip, style:Object, str:String):TextField {
   163  		// if no 'str' arg was passed use default style.t text
   164  		var s:String = (str != undefined) ? str : style.t;
   165  		// if style.c is an Object (instead of number) assume we are a message type and resolve the color based on 'str' arg.
   166  		var c:Number = (!isNaN( style.c )) ? style.c : resolveTypeColor( str );
   167  		// if 'messageTf' || 'originTf' then make autoSize, multiLine, wordWrap, selectable true; else default to false.
   168  		var isSizable:Boolean = (style.n == CSS.item.textfields.message.n || style.n == CSS.item.textfields.origin.n);
   169  		// see if we want to be an html textfield
   170  		var isHtml:Boolean = (style.html == true) ? true : false;
   171  		// see if we want a selectable textfield
   172  		var isSelectable:Boolean = (style.selectable == true) ? true : false;
   173  		// build properties and format objects
   174  		var props:Object = {type:"dynamic", html:isHtml, autoSize:isSizable, wordWrap:isSizable, multiline:isSizable, selectable:isSelectable, mouseWheelEnabled:false, embedFonts:false, _alpha:style.a, htmlText:s};		
   175  		var format:Object = {font:"Arial", size:9, color:c};
   176  		// create the TextField now that we have good properties, formats and string.
   177  		var t:TextField = TextUtils.create( target, style.n, style.x, style.y, style.w, style.h, props, format );
   178  		return t;
   179  	}		
   180  
   181  	/**
   182  	 * Compare the type string argument to those existing in the ConsoleStyle.COLOR.item.textfields.type style,
   183  	 * if one matches return it, else default to the 'custom' style.
   184  	 * @param type (String) type as string
   185  	 * @return Number
   186  	 * {@code Usage:
   187  	 * 	var c:Number = resolveTypeColor("trace"); // returns CSS.item.textfields.type.c.trace
   188  	 * 	var c:Number = resolveTypeColor("sekati"); // returns CSS.item.textfields.type.c.custom
   189  	 * }
   190  	 */
   191  	private function resolveTypeColor(type:String):Number {
   192  		var t:String = type.toLowerCase( );
   193  		var c:Number = (!CSS.item.textfields.type.c[t]) ? CSS.item.textfields.type.c.custom : CSS.item.textfields.type.c[t];
   194  		//trace("resolveTypeColor: "+t+": "+ c);
   195  		return c;
   196  	}
   197  }