/*
 *  BasicLayers.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 some of the basic concepts used when displaying objects 
 * on display list layers.
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar BasicLayers 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 BasicLayers
{
    public static void main(String[] args)
    {      
        try
        {
           	String out = args.length == 0 ? "BasicLayers.swf" : args[0];
        	BasicLayers example = new BasicLayers();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
    }

    public void createMovie(FSMovie movie)
    {
    	FSDefineShape red = defineRectangle(movie.newIdentifier(), FSColorTable.red());
    	FSDefineShape green = defineRectangle(movie.newIdentifier(), FSColorTable.green());
    	FSDefineShape blue = defineRectangle(movie.newIdentifier(), FSColorTable.blue());

        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()));
        
        movie.add(red);
        movie.add(green);
        movie.add(blue);
        
        /*
         * Place each rectangle on a separate layer. The red rectangle is 
         * on the lowest layer so it will be displayed behind the green
         * one and the blue rectangle displayed in front.
         */
        movie.add(new FSPlaceObject2(red.getIdentifier(), 1, 1000, 1000));
        movie.add(new FSPlaceObject2(green.getIdentifier(), 2, 2000, 2000));
        movie.add(new FSPlaceObject2(blue.getIdentifier(), 3, 3000, 3000));      
        movie.add(new FSShowFrame());
        
        /*
         * Use a copy of the green rectangle to clip the blue rectangle that 
         * is displayed in front of it. The blue rectangle is removed and 
         * added to the display list after the clipping layer is defined to
         * ensure that it is displayed correctly.
         */
        movie.add(new FSRemoveObject2(3));
        
        movie.add(new FSPlaceObject2(green.getIdentifier(), 3, 4, 2000, 2000));
        movie.add(new FSPlaceObject2(blue.getIdentifier(), 4, 3000, 3000));      
        movie.add(new FSShowFrame());
        
        /*
         * Replace the green rectangle (and the one used for clipping) with a 
         * movie clip that in turn contains several layers to show that the layer 
         * numbers in the main movie are independent of the layer numbers used 
         * within a movie clip (or any other composite object such as a button).
         */
        FSDefineMovieClip clip = new FSDefineMovieClip(movie.newIdentifier());
        
        clip.add(new FSPlaceObject2(red.getIdentifier(), 1, 500, 2000));
        clip.add(new FSPlaceObject2(green.getIdentifier(), 2, 2000, 2000));
        clip.add(new FSPlaceObject2(blue.getIdentifier(), 3, 3500, 2000));
        clip.add(new FSShowFrame());

        movie.add(clip);
        
        movie.add(new FSRemoveObject2(2)); // remove green rectangle
        movie.add(new FSRemoveObject2(3)); // remove clipping rectangle
        movie.add(new FSRemoveObject2(4)); // remove blue rectangle so it gets redisplayed

        movie.add(new FSPlaceObject2(clip.getIdentifier(), 2, 0, 0));
        movie.add(new FSPlaceObject2(blue.getIdentifier(), 3, 3000, 3000));      
        movie.add(new FSShowFrame());
    }
    
    private FSDefineShape defineRectangle(int identifier, FSColor fillColor)
    {
        int width = 4000;
        int height = 4000;
        
        FSBounds bounds = new FSBounds(0, 0, 4000, 4000);
        
        ArrayList lineStyles = new ArrayList();
        ArrayList fillStyles = new ArrayList();
        
        lineStyles.add(new FSSolidLine(20, FSColorTable.black()));
        fillStyles.add(new FSSolidFill(fillColor));
        
        /* 
         * Create the outline of the shape.
         */
        ArrayList shapeRecords = new ArrayList();
        
        shapeRecords.add(new FSShapeStyle(1, 1, 0, 0, 0));
        shapeRecords.add(new FSLine(width, 0));
        shapeRecords.add(new FSLine(0, height));
        shapeRecords.add(new FSLine(-width, 0));
        shapeRecords.add(new FSLine(0, -height));
        
        FSDefineShape rectangle = new FSDefineShape(identifier, bounds, fillStyles, lineStyles, new FSShape(shapeRecords));

        return rectangle;
    }
}