/*
 *  BasicMovieClip.java
 *  Cookbook
 *
 *  Copyright (c) 2001-2009 Flagstone Software Ltd. All rights reserved.
 *
 *  This code is distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 *  EXPRESS OR IMPLIED, AND Flagstone HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
 *  WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 *  PURPOSE, AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 */

import com.flagstone.transform.*;

import java.util.*;

/*
 * This example shows how to create a simple movie clip and control it from
 * the main movie.
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar BasicMovieClip file-out
 *
 * where
 *
 *     file-out is the path where the file will be written. If no output file 
 *     is specified then a file named after the example will be written to the 
 *     current directory.
*/
public class BasicMovieClip
{
	public static void main(String[] args)
	{
        try
        {
        	String out = args.length == 0 ? "BasicMovieClip.swf" : args[0];
        	BasicMovieClip example = new BasicMovieClip();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
	}
    
	void createMovie(FSMovie movie)
    {
        int xLower = 0;
        int yLower = 0;
        int xUpper = 8000;
        int yUpper = 8000;
        
        float framesPerSecond = 1.0f;

		movie.setFrameRate(framesPerSecond);
        movie.setFrameSize(new FSBounds(xLower, yLower, xUpper, yUpper));
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
        
        /*
         * Create the movie clip. A simple rectangle is drawn and then
         * moved across the screen.
         */
        FSDefineShape rectangle = createRectangle(movie);
        movie.add(rectangle);
        
        ArrayList clipMovements = new ArrayList();
        
        /*
         * The first frame of the movie clip displays the shape but 
         * does not start the animation.
         */
        FSDoAction clipActions = new FSDoAction(new ArrayList());
        clipActions.add(FSAction.Stop());
        clipMovements.add(clipActions);
        
        clipMovements.add(new FSPlaceObject2(rectangle.getIdentifier(), 1, 0, 0));
		clipMovements.add(new FSShowFrame());
        
        for (int i=0; i<10; i++) {
            clipMovements.add(new FSPlaceObject2(1, i*40, i*40));
    		clipMovements.add(new FSShowFrame());
        }
        
        FSDefineMovieClip clip = new FSDefineMovieClip(movie.newIdentifier(), clipMovements);
        FSPlaceObject2 place = new FSPlaceObject2(clip.getIdentifier(), 2, 4000, 4000);
        place.setName("clip");
        
        movie.add(clip);
 		movie.add(place);
		movie.add(new FSShowFrame());
		
        FSDoAction actions = new FSDoAction(new ArrayList());
        actions.add(new FSSetTarget("clip"));
        actions.add(new FSGotoFrame(2));
        actions.add(FSAction.Play());
        
        movie.add(clipActions);
		movie.add(new FSShowFrame());
    }
	
	private FSDefineShape createRectangle(FSMovie movie)
	{
        int identifier = movie.newIdentifier();
        
        int width = 4000;
        int height = 4000;
        
        FSBounds bounds = new FSBounds(-2000, -2000, 2000, 2000);
        
        ArrayList lineStyles = new ArrayList();
        ArrayList fillStyles = new ArrayList();
        
        lineStyles.add(new FSSolidLine(20, FSColorTable.black()));
        fillStyles.add(new FSSolidFill(FSColorTable.red()));
        
        /* 
         * Create the outline of the shape.
         */
        ArrayList shapeRecords = new ArrayList();
        
        shapeRecords.add(new FSShapeStyle(1, 1, 0, -width/2, -height/2));
        shapeRecords.add(new FSLine(width, 0));
        shapeRecords.add(new FSLine(0, height));
        shapeRecords.add(new FSLine(-width, 0));
        shapeRecords.add(new FSLine(0, -height));
        
        return new FSDefineShape(identifier, bounds, fillStyles, lineStyles, new FSShape(shapeRecords));
	}
}