/*
 * FSPlaceObject.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSPlaceObject is used to add an object (shape, button, etc.) to the Flash
 * Player's display list.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>The unique identifier, in the range 1..65535, of the object to be
 * displayed.</td>
 * </tr>
 * 
 * <tr>
 * <td>layer</td>
 * <td>The Display List layer on which the object is drawn.</td>
 * </tr>
 * 
 * <tr>
 * <td>coordTransform</td>
 * <td>An FSCoordTransform which defines the location and appearance of the
 * object.</td>
 * </tr>
 * 
 * <tr>
 * <td>colorTransform</td>
 * <td>An FSColorTransform which defines any changes to be made to the object's
 * original colour. Optional. May be set to null if an object's colour is not
 * being changed.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * Every class that defines a shape, button etc. is assigned a unique
 * identifier. This is an integer in the range 1..65535 and is used to refer to
 * objects when performing actions such as adding or removing them from the
 * display list.
 * </p>
 * 
 * <p>
 * The display list contains all the objects that are currently visible on the
 * Flash Player's screen. The display list is ordered in layers, with one (and
 * only one) object displayed on each layer. The Layer defines the order in
 * which objects are displayed. Objects with a higher layer number are displayed
 * in front of objects on a lower layer.
 * </p>
 * 
 * <p>
 * The coordinate transform is principally used to specify the location of the
 * object when it is drawn on the screen however more complex coordinate
 * transforms can also be specified such as rotating or scaling the object
 * without changing the original definition.
 * </p>
 * 
 * <p>
 * Similarly the color transform allows the color of the object to be changed
 * when it is displayed without changing the original definition. The
 * FSPlaceObject class only supports opaque colours so although the
 * FSColorTransform supports transparent colours this information is ignored by
 * the Flash Player. The colour transform is optional and may be set to the null
 * object.
 * </p>
 * 
 * <h1 class="datasheet">Examples</h1>
 * 
 * <p>
 * The following simplified code fragments illustrate how the FSPlaceObject
 * class can be used.
 * </p>
 * 
 * <p>
 * 1. Display an object.<br/> Display an object on layer 1 at (400, 400).
 * FSPlaceObject provides a constructor which specifies just the x and y
 * coordinates, the FSCoordTransform object is constructed internally.
 * </p>
 * 
 * <pre>
 *  FSDefineShape shape = new FSDefineShape(movie.newIdentifier(), ......)
 * 
 *  movie.add(new FSPlaceObject(shape.getIdentifier(), 1, 400, 400));
 *  movie.add(new FSShowFrame());
 * </pre>
 * 
 * <p>
 * 2. Scale an object.<br/> Scale an object to twice its original size by
 * specifying a more complex coordinate transformation. Complex transforms can
 * be created by compositing individual steps.
 * </p>
 * 
 * <pre>
 * FSCoordTransform location = FSCoordTransform.translate(200, 200);
 * FSCoordTransform scale = FSCoordTransform.scale(2.0, 2.0);
 * FSCoordTransform transform = FSCoordTransform.composite(location, scale);
 * 
 * movie.add(new FSPlaceObject(shape.getIdentifier(), 1, transform));
 * movie.add(new FSShowFrame());
 * </pre>
 * 
 * <p>
 * 3. Move an object.<br/> To move an object the FSPlaceObject class must be
 * used in conjunction with the FSRemoveObject class to first remove the object
 * from its existing position before being placed at the new location.
 * </p>
 * 
 * <pre>
 * // Add the shape to the display list.
 * movie.add(new FSPlaceObject(shape.getIdentifier(), 1, 400, 400));
 * movie.add(new FSShowFrame());
 * 
 * // Move shape to a new location, removing the original so it does not get displayed twice.
 * movie.add(new FSRemoveObject(shape.getIdentifier(), 1));
 * movie.add(new FSPlaceObject(shape.getIdentifier(), 1, 250, 300));
 * movie.add(new FSShowFrame());
 * </pre>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSPlaceObject class represents the PlaceObject tag from the Macromedia
 * Flash (SWF) File Format Specification. It was introduced in Flash 1 and is
 * superseded by the PlaceObject2 tag which was added in Flash 3.
 * </p>
 */
public class FSPlaceObject extends FSMovieObject
{
	private int identifier = 0;
	private int layer = 0;
	private FSCoordTransform transform = null;
	private FSColorTransform colorTransform = null;

	/**
	 * Construct an FSPlaceObject object, initalising it with values decoded
	 * from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSPlaceObject(FSCoder coder)
	{
		super(FSMovieObject.PlaceObject);
		decode(coder);
	}

	/**
	 * Constructs an FSPlaceObject object that places an object with the
	 * identifier into the display list layer at the specified coordinates
	 * (x,y).
	 * 
	 * @param anIdentifier
	 *            the unique identifier for the object to the placed on the
	 *            display list.
	 * @param aLayer
	 *            the layer in the display list where the object will be placed.
	 * @param xLocation
	 *            the x-coordinate where the object will be drawn.
	 * @param yLocation
	 *            the y-coordinate where the object will be drawn.
	 */
	public FSPlaceObject(int anIdentifier, int aLayer, int xLocation,
							int yLocation)
	{
		super(FSMovieObject.PlaceObject);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(new FSCoordTransform(xLocation, yLocation));
	}

	/**
	 * Constructs an FSPlaceObject object that places the object with the
	 * identifier at the specified layer with the coordinate transform.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for the object to the placed on the
	 *            display list.
	 * @param aLayer
	 *            the layer in the display list where the object will be placed.
	 * @param aTransform
	 *            an FSCoordTransform object that defines the orientation, size
	 *            and location of the object when it is drawn.
	 */
	public FSPlaceObject(int anIdentifier, int aLayer,
							FSCoordTransform aTransform)
	{
		super(FSMovieObject.PlaceObject);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(aTransform);
	}

	/**
	 * Constructs an FSPlaceObject object that places the the object with the
	 * identifier at the specified layer, coordinate transform and colour
	 * transform.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for the object to the placed on the
	 *            display list.
	 * @param aLayer
	 *            the layer in the display list where the object will be placed.
	 * @param aTransform
	 *            an FSCoordTransform object that defines the orientation, size
	 *            and location of the object when it is drawn.
	 * @param aColorTransform
	 *            an FSColorTransform object that defines the colour of the
	 *            object when it is drawn.
	 */
	public FSPlaceObject(int anIdentifier, int aLayer,
							FSCoordTransform aTransform,
							FSColorTransform aColorTransform)
	{
		super(FSMovieObject.PlaceObject);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(aTransform);
		setColorTransform(aColorTransform);
	}

	/**
	 * Constructs an FSPlaceObject object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSPlaceObject object.
	 */
	public FSPlaceObject(FSPlaceObject obj)
	{
		super(obj);
		identifier = obj.identifier;
		layer = obj.layer;
		transform = new FSCoordTransform(obj.transform);

		if (obj.colorTransform != null)
			colorTransform = new FSColorTransform(obj.colorTransform);
	}

	/**
	 * Gets the identifier of the object.
	 * 
	 * @return the identifier of the object being placed.
	 */
	public int getIdentifier()
	{
		return identifier;
	}

	/**
	 * Gets the layer that defines the order in which objects are displayed.
	 * 
	 * @return the layer on which the object will be displayed.
	 */
	public int getLayer()
	{
		return layer;
	}

	/**
	 * Gets the transform that defines the position where the object is
	 * displayed.
	 * 
	 * @return the FSCoordTransform applied to the object when it is drawn.
	 */
	public FSCoordTransform getTransform()
	{
		return transform;
	}

	/**
	 * Gets the colour transform that defines any colour effects applied when
	 * the object is displayed.
	 * 
	 * @return the FSColorTransform applied to the object when it is drawn.
	 */
	public FSColorTransform getColorTransform()
	{
		return colorTransform;
	}

	/**
	 * Sets the identifier of the object.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for the object to the placed on the
	 *            display list.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	/**
	 * Sets the layer that defines the order in which objects are displayed.
	 * 
	 * @param aNumber
	 *            the layer in the display list where the object will be placed.
	 */
	public void setLayer(int aNumber)
	{
		layer = aNumber;
	}

	/**
	 * Sets the transform that defines the position where the object is
	 * displayed.
	 * 
	 * @param aTransform
	 *            an FSCoordTransform object that defines the orientation, size
	 *            and location of the object when it is drawn.
	 */
	public void setTransform(FSCoordTransform aTransform)
	{
		transform = aTransform;
	}

	/**
	 * Sets the colour transform that defines any colour effects applied when
	 * the object is displayed.
	 * 
	 * @param aColorTransform
	 *            an FSColorTransform object that defines the colour of the
	 *            object when it is drawn.
	 */
	public void setColorTransform(FSColorTransform aColorTransform)
	{
		colorTransform = aColorTransform;
	}

	public Object clone()
	{
		FSPlaceObject anObject = (FSPlaceObject) super.clone();

		anObject.transform = (transform != null) ? (FSCoordTransform) transform
						.clone() : null;
		anObject.colorTransform = (colorTransform != null) ? (FSColorTransform) colorTransform
						.clone()
						: null;

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSPlaceObject typedObject = (FSPlaceObject) anObject;

			result = identifier == typedObject.identifier;
			result = result && layer == typedObject.layer;

			if (transform != null)
				result = result && transform.equals(typedObject.transform);
			else
				result = result && transform == typedObject.transform;

			if (colorTransform == null)
				result = result && (typedObject.getColorTransform() == null);
			else
				result = result
								&& colorTransform
												.equals(typedObject.colorTransform);
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "identifier", identifier);
			Transform.append(buffer, "layer", layer);
			Transform.append(buffer, "transform", transform, depth);
			Transform.append(buffer, "colorTransform", colorTransform, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);
		length += 4;
		length += transform.length(coder);
		length += (colorTransform != null) ? colorTransform.length(coder) : 0;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeWord(identifier, 2);
		coder.writeWord(layer, 2);
		transform.encode(coder);

		if (colorTransform != null)
			colorTransform.encode(coder);

		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		int lengthRead = coder.getPointer();

		super.decode(coder);

		identifier = coder.readWord(2, false);
		layer = coder.readWord(2, false);
		transform = new FSCoordTransform(coder);

		lengthRead = (coder.getPointer() - lengthRead) >> 3;

		if (length > lengthRead)
			colorTransform = new FSColorTransform(coder);

		coder.endObject(name());
	}
}
