Quantcast
Channel: Keith Hair » JSFL
Viewing all articles
Browse latest Browse all 4

Resizing Flash Documents to Fit Contents in JSFL

$
0
0

I use Flex for the majority of my Flash related work, even for writing plain ActionScript. Lately I've barely even used the Flash authoring environment to create stuff. When I do use the Flash authoring environment, it's mostly for automation and prep work of content. If you are like me, you write JSFL to do that stuff.

I love automating redundant tasks. One thing that bugs me when importing graphical assets to Flash's stage is having to resize the document to what I imported. I like that I can do this by going to the Document Properties and setting the document to match the content size, but if I have to do this each time I import/export content it becomes a irksome chore. I write JSFL scripts that run tasks on multiple files, having a "match contents" function would be useful to me in my case...maybe to you as well. I do not see a JSFL way to do this. (Maybe there is and I had a brain fart?). Until then, here is a script I wrote to do it in JSFL...

/*****************************************************************************************
What:	Document_to_Content_Size.jsfl
		JSFL Command for Flash (8+) that will resize the document to fit the content
		that is on the stage in the authoring environment.

Why:		Yep, this action can also be done by going to the Document Properties
		and setting "match" to "contents". However, I have not
		found a way to do this same action within JSFL script		
		If you know a JSFL way to do this same action easier contact me.		


Date:		March 21, 2010
Author:	Keith Hair
Contact	khos2007@gmail.com
Web:		keith-hair.net		


INSTALL:
		If you deal with JSFL you probably know what to do, but anyway...
		Save this script with a ".jsfl" extension and put in:
		"Your Flash installation folder\en\First Run\Commands" folder
		Or just put it anywhere and double-click the file.

LEGAL STUFF:
		This script is free for whatever, just improve it or say hello! 		

NOTE:
		This is primarily for resizing the Flash
		document to fit content that is imported to the stage.		
		Resizing is buggy with Component instances.


******************************************************************************************/
fl.outputPanel.clear();
runApp();
function runApp()
{	
	var doc=fl.getDocumentDOM();
	var element;
	var docw=1;
	var doch=1;		
	var fn=0;
	var ln=0;
	var en=0;	
	var tframes;	
	var layer;
	var frame;
	var ox;
	var oy;
	var left=null;
	var right=null;
	var top=null;
	var bottom=null;
	var cf=doc.getTimeline().currentFrame;	
	while(ln < doc.getTimeline().layerCount)
	{
		layer=doc.getTimeline().layers[ln];
		tframes=layer.frameCount;
		fn=0;
		while(fn < tframes)
		{
			doc.getTimeline().currentFrame = fn;
			frame=layer.frames[fn];				
			en=0;
			while(en < frame.elements.length)
			{		
				element = frame.elements[en];	
				ox=element.x;
				oy=element.y;
				if(left == null){
					left=ox;
				}
				if(right == null){
					right=ox+element.width;
				}	
				if(top == null){
					top=oy;
				}
				if(bottom == null){
					bottom=oy+element.height;
				}

				left=Math.min(ox,left);
				right=Math.max(ox+element.width,right);
				top=Math.min(oy,top);
				bottom=Math.max(oy+element.height,bottom);					
				en++;
			}
			fn++;
		}
		ln++;
	}	
	fl.trace("CURRENT SIZE-->\t"+doc.width+" x "+doc.height);	
	docw=Math.round(right-left);
	doch=Math.round(bottom-top);
	moveAllElementsBy(-left,-top);
	doc.width=docw;
	doc.height=doch;
	fl.trace("NEW SIZE-->\t\t\t"+doc.width+" x "+doc.height);
	doc.getTimeline().currentFrame=cf;	
}

function moveAllElementsBy(tx,ty)
{	
	doc=fl.getDocumentDOM();
	var element;	
	var fn=0;
	var ln=0;
	var en=0;	
	var tframes;	
	var layer;
	var frame;
	var otp;			
	while(ln < doc.getTimeline().layerCount)
	{
		layer=doc.getTimeline().layers[ln];
		tframes=layer.frameCount;
		fn=0;
		while(fn < tframes)
		{
			doc.getTimeline().currentFrame = fn;
			frame=layer.frames[fn];			
			en=0;
			while(en < frame.elements.length)
			{		
				element=frame.elements[en];					
				element.x+=tx;
				element.y+=ty;
				if(element.elementType.search(/instance|text/mig) == -1){
					element.x+=element.width/2;
					element.y+=element.height/2;
				}
							
				en++;
			}
			fn++;
		}
		ln++;
	}
	
}


Viewing all articles
Browse latest Browse all 4

Trending Articles