package CharacterExport
{
	import flash.geom.*;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import djarts.core.*;

	public class AnimationPiece
	{
		public var Key:String;
		
		public var IsAnimated = false;
		
		// IsAnimated == false
		public var Offset:Point;		
		public var Data:BitmapData;
		//public var Node:RectangleNode;
		
		// IsAnimated == true
		public var Exporter:CharacterExporter = null;
		public var PieceNodes:Array = new Array();
		
		private var loadedCallback;
		
		public function AnimationPiece(key:String, clip:MovieClip, loadedCallback)
		{
			Key = key;
			this.loadedCallback = loadedCallback;
			
			if (clip.totalFrames > 1)
			{
				IsAnimated = true;
				
				Exporter = new CharacterExporter(null);
				Exporter.SingleSequenceClip = true;
				Exporter.ProcessFromClip(clip, loadedCallback, null);
			}
			else
			{
				IsAnimated = false;
				
				var bounds:Rectangle = CacheManager.Instance().GetAccurateBounds(clip);
				CharacterExporter.RoundRect(bounds);
				var bd:BitmapData = new BitmapData(Math.max(1, bounds.width), Math.max(1, bounds.height), true, 0x0);
				bd.draw(clip, new Matrix(1, 0, 0, 1, -bounds.left, -bounds.top));
				Offset = new Point(-bounds.left, -bounds.top);
				
				Data = bd;
				
				if (loadedCallback != null) loadedCallback();
			}
		}
	}
}