package com.terry.engine {
	import flash.display.*;
	import flash.geom.*;
  import flash.events.*;
  import flash.net.*;
	import flash.text.*;
	
	public class font{
		public static function init():void {
			initfont();
    }
		
		//Text Functions
		public static function initfont():void {
			var i:int;
			for (i = 0; i < 5; i++){
				font.fontsize.push(0); 
				font.tf.push(new TextField);
			}
			
			fontsize[0] = 8;
			fontsize[1] = 16;
			fontsize[2] = 24;
			fontsize[3] = 32;
			fontsize[4] = 48;
			fontname = getfontname();
			
			for (i = 0; i < 5; i++){
				tf[i].embedFonts = true;
				tf[i].defaultTextFormat = new TextFormat(fontname, fontsize[i], 0, true);
				tf[i].width = gfx.screenwidth; tf[i].height = 48;
				tf[i].antiAliasType = AntiAliasType.NORMAL;
			}
		}
		
		//Text functions
		public static function rprint(x:int, y:int, t:String, r:int, g:int, b:int):void {
			x = x - len(t);
			print(x, y, t, r, g, b, false);
		}
		
		public static function print(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false):void {
			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
			
			tf[0].textColor = gfx.RGB(r, g, b);
			tf[0].text = t;
			if (cen) x = gfx.screenwidthmid - (tf[0].textWidth / 2) + x;
			
			gfx.shapematrix.translate(x, y);
			tf[0].textColor = gfx.RGB(r, g, b);
			gfx.backbuffer.draw(tf[0], gfx.shapematrix);
			
			gfx.shapematrix.translate(-x, -y);
		}
		
		public static function len(t:String, sz:int = 1):int {
			if (sz > 0 && sz <= 5) {
				tf[sz-1].text = t;
				return tf[sz - 1].textWidth;
			}
			
			tf[0].text = t;
			return tf[0].textWidth;
		}
		
		public static function hig(t:String, sz:int = 1):int {
			if (sz > 0 && sz <= 5) {
				tf[sz-1].text = t;
				return tf[sz-1].textHeight;
			}
			
			tf[0].text = t;
			return tf[0].textHeight;
		}
		
		public static function rbigprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false, sc:Number = 2):void {
			x = x - len(t, sc);
			bigprint(x, y, t, r, g, b, cen, sc);
		}
		
		public static function bigprint(x:int, y:int, t:String, r:int, g:int, b:int, cen:Boolean = false, sc:Number = 2):void {
			if (r < 0) r = 0; if (g < 0) g = 0; if (b < 0) b = 0;
			if (r > 255) r = 255; if (g > 255) g = 255; if (b > 255) b = 255;
			
			tf[sc-1].text = t;
			if (cen) x = (gfx.screenwidthmid - (tf[sc - 1].textWidth / 2)) + x;
			
			gfx.shapematrix.translate(x, y);
			tf[sc-1].textColor = gfx.RGB(r, g, b);
			gfx.backbuffer.draw(tf[sc-1], gfx.shapematrix);
			
			gfx.shapematrix.translate(-x, -y);
		}
		
		include "../../../config/loadfont.as";
  	
		public static var tf:Vector.<TextField> = new Vector.<TextField>;
		public static var fontsize:Vector.<int> = new Vector.<int>;
		public static var fontname:String;
	}
}