/*
 *  BasicText.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 com.flagstone.transform.util.*;

import java.awt.Font;
import java.util.*;

/*
 * This example shows how to display dynamic text (a text field) in a movie.
 *
 * To run this example, type the following on a command line:
 *
 *     java -cp cookbook.jar BasicTextField string file-out
 *
 * where
 *
 *     string, the text that will be displayed - enclose in quotes if the string 
 *     contains spaces.
 *
 *     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 BasicTextField
{
	public static void main(String[] args)
	{
        try
        {
        	String str = args[0];
        	String out = args.length == 1 ? "BasicTextField.swf" : args[1];
        	BasicTextField example = new BasicTextField();         
            FSMovie movie = new FSMovie();            
            example.createMovie(movie, str);
            movie.encodeToFile(out);
        }
        catch (Exception e)
        {
        	e.printStackTrace();
        }
	}
    
	void createMovie(FSMovie movie, String str)
	{
		Font awtFont = new Font("Arial", Font.PLAIN, 12);
		
    	FSTextConstructor textGenerator = new FSTextConstructor(movie.newIdentifier(), awtFont);

        int layer = 1;                // Starting layer for objects in the display list.
        int fontSize = 280;           // Font size in twips 1 point = 20 twips
        
        /*
         * The movie will be sized to match the block of text generated.
         * The margins are defined so the text is displayed without touching
         * the edge of the Flash Player screen.
         */
        int leftMargin = fontSize;
        int rightMargin = fontSize;
        int topMargin = fontSize;
        int bottomMargin = fontSize;
        
        int fontId = movie.newIdentifier();

		/* 
		 * We are not using the FSTextGenerator to create the text objects
		 * we use the willDisplay() method to define which characters will be 
		 * included in the font definition. 
		 */
        textGenerator.willDisplay(str.toCharArray());
        
        FSDefineFont2 font = textGenerator.defineFont();

        FSDefineTextField field = new FSDefineTextField(movie.newIdentifier(), 
				textGenerator.boundsForText(str, fontSize));
		
        /*
         * FSDefineTextField has a large number of options to set the appearance
         * of the field and how the text is displayed inside it.
         */
		field.setFontHeight(fontSize);
		field.setFontIdentifier(font.getIdentifier());
		field.setUseFontGlyphs(false);
		field.setColor(FSColorTable.black());
		field.setInitialText(str);
		field.setReadOnly(true);
		field.setAutoSize(true);
		
		/* 
		 * Define the size of the Flash Player screen using the bounding 
		 * rectangle defined for the block of text plus a suitable margin so 
		 * the text does not touch the edge of the screen.
		 */
        int screenWidth = field.getBounds().getWidth() + leftMargin + rightMargin;
        int screenHeight = field.getBounds().getHeight() + topMargin + bottomMargin;
        
        /* 
         * Add all the objects together to create the movie. The origin of the 
         * block of text (0,0) is the top left corner as viewed on the Flash 
         * Player screen. The left and top margins offsets the text correctly
         * from the edge of the screen.
         */
        movie.setFrameSize(new FSBounds(0, 0, screenWidth, screenHeight));
        movie.setFrameRate(1.0f);
        movie.add(new FSSetBackgroundColor(FSColorTable.lightblue()));
        movie.add(font);
        movie.add(field);
        movie.add(new FSPlaceObject2(field.getIdentifier(), layer++, leftMargin , topMargin));
        movie.add(new FSShowFrame());
	}
}