/*
 * FSButton.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;

/**
 * FSButton identifies the shape that is drawn when a button is in a particular
 * state.
 * 
 * <p>
 * Shapes can be drawn for each of three button states, Over, Up and Down.
 * Creating a button with more than one FSButton object each referencing a
 * different shape allows the button to be animated as it is clicked.
 * </p>
 * 
 * <p>
 * A shape is also used to define active area of the button. When defining the
 * active area the outline of the shape defines the boundary of the area. The
 * shape is not drawn when the button is displayed. The button will only respond
 * to mouse events when the cursor is placed inside the active area.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSButton_0">state</a></td>
 * <td>the state(s) the button is in when the shape will be drawn, either Up,
 * Over or Down. Active is used to denote when a shape defined the active area
 * of a button.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSButton_1">identifier<a/></td>
 * <td>The identifier, in the range 1..65535, of the object that contains the
 * definition of the shape that is drawn.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSButton_2">layer</a></td>
 * <td>The layer number defines the order in which shapes are displayed. Shapes
 * with a higher layer number are displayed in front of those on a lower layer.
 * Complex buttons can be created by displaying more than one shape for a given
 * button state. The layer number may be in the range 1..65535 - the range is
 * determined by the size of the field when the value is encoded to a Flash file -
 * however the vast majority of buttons will typically only use layer numbers
 * less than 10.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSButton_3">transform</a></td>
 * <td>an FSCoordTransform object that defines a coordinate transform that will
 * be applied to the shape when it is displayed. The transform is most commonly
 * used to offset shapes to create a 3-D appearance. The size may also be
 * changed allowing a single shape definition to be used to display buttons in a
 * range of sizes while still maintaining the same look and feel.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSButton_3">colorTransform</a></td>
 * <td>an optional FSColorTransform object that can be used to change the
 * shape's colour when it is drawn.</td>
 * </tr>
 * 
 * 
 * </table>
 * 
 * <p>
 * An FSButton can define the appearance of the button for more than one state.
 * Multiple states can be defined by bitwise Or-ing individual state codes
 * together:
 * </p>
 * 
 * <pre>
 * int buttonState = FSButton.Up | FSButton.Over;
 * </pre>
 * 
 * <p>
 * The purpose of the layer attribute is to define the order in which shapes are
 * displayed for a given button state. This is analogous to the layer number in
 * the Flash Player's display list - specified using objects such as
 * FSPlaceObject. When a button is displayed it is placed on a single layer in
 * the display list and the layer numbers assigned to shapes in a FSButton
 * object do not interfere with shapes assigned to the same layer in the display
 * list.
 * </p>
 * 
 * <p>
 * The coordinate transform is used to change the appearance of the button
 * without changing the original shape definitions. If it is omitted then a
 * unity transform will be encoded which does not change the way the shape is
 * drawn.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSButton class represents the ButtonRecord structure from the Macromedia
 * Flash (SWF) File Format Specification. It was introduced in Flash 1.
 * </p>
 * 
 */
public class FSButton extends FSTransformObject
{
	/** Code for the button in the up state. */
	public static final int Up = 1;

	/** Code for the state of the button when the mouse is over the active area. */
	public static final int Over = 2;

	/**
	 * Code for the state of the button when the mouse is clicked the active
	 * area.
	 */
	public static final int Down = 4;

	/** Code for the active area of the button. */
	public static final int Active = 8;

	private int state = 0;

	private int identifier = 0;

	private int layer = 0;

	private FSCoordTransform transform = null;

	private FSColorTransform colorTransform = null;

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

	/**
	 * Constructs an FSButton object without a coordinate or colour transform
	 * that will be applied to the shape drawn for the button states. The
	 * transforms default to unity transforms which do not change the location
	 * of colour of the shape for the button.
	 * 
	 * @param aState
	 *            the state of the button when the shape is drawn.
	 * @param anIdentifier
	 *            the identifier of an FSDefineShape, FSDefineShape2 or
	 *            FSDefineShape3 object.
	 * @param aLayer
	 *            the layer in the display list on which the shape is drawn.
	 */
	public FSButton(int aState, int anIdentifier, int aLayer)
	{
		setState(aState);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(new FSCoordTransform(0, 0));
		setColorTransform(new FSColorTransform());
	}

	/**
	 * Constructs an FSButton object with a coordinate transform that will be
	 * applied to the shape drawn for the button states.
	 * 
	 * @param aState
	 *            the state of the button when the shape is drawn.
	 * @param anIdentifier
	 *            the identifier of an FSDefineShape, FSDefineShape2 or
	 *            FSDefineShape3 object.
	 * @param aLayer
	 *            the layer in the display list on which the shape is drawn.
	 * @param aTransform
	 *            an FSCoordTransform object that changes the appearance of the
	 *            shape when it is drawn.
	 */
	public FSButton(int aState, int anIdentifier, int aLayer,
					FSCoordTransform aTransform)
	{
		setState(aState);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(aTransform);
		setColorTransform(new FSColorTransform());
	}

	/**
	 * Constructs an FSButton object with a coordinate and colour transform that
	 * will be applied to the shape drawn for the button states.
	 * 
	 * @param aState
	 *            the state of the button when the shape is drawn.
	 * @param anIdentifier
	 *            the identifier of an FSDefineShape, FSDefineShape2 or
	 *            FSDefineShape3 object.
	 * @param aLayer
	 *            the layer in the display list on which the shape is drawn.
	 * @param aTransform
	 *            an FSCoordTransform object that changes the appearance of the
	 *            shape when it is drawn.
	 * @param aColorTransform
	 *            an FSColorTransform object that changes the colour of the
	 *            shape when it is drawn.
	 */
	public FSButton(int aState, int anIdentifier, int aLayer,
					FSCoordTransform aTransform,
					FSColorTransform aColorTransform)
	{
		setState(aState);
		setIdentifier(anIdentifier);
		setLayer(aLayer);
		setTransform(aTransform);
		setColorTransform(aColorTransform);
	}

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

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

	/**
	 * Get the state(s) of the button when the shape is drawn.
	 * 
	 * @return the state of the button when the shape is drawn.
	 */
	public int getState()
	{
		return state;
	}

	/**
	 * Does the Button Record defines a shape for the button's active area.
	 * 
	 * @return a boolean flag indicating whether the button record defines the
	 *         button's active area.
	 */
	public boolean isActive()
	{
		return ((state & Active) != 0) ? true : false;
	}

	/**
	 * Does the Button Record define a shape for the button's 'down' state.
	 * 
	 * @return a boolean flag indicating whether the button record defines the
	 *         shape for the button's 'down' state.
	 */
	public boolean isDown()
	{
		return ((state & Down) != 0) ? true : false;
	}

	/**
	 * Does the Button Record define a shape for the button's 'over' state.
	 * 
	 * @return a boolean flag indicating whether the button record defines the
	 *         shape for the button's 'over' state.
	 */
	public boolean isOver()
	{
		return ((state & Over) != 0) ? true : false;
	}

	/**
	 * Does the Button Record define a shape for the button's 'up' state.
	 * 
	 * @return a boolean flag indicating whether the button record defines the
	 *         shape for the button's 'up' state.
	 */
	public boolean isUp()
	{
		return ((state & Up) != 0) ? true : false;
	}

	/**
	 * Gets the identifier of the shape that this FSButton applies to.
	 * 
	 * @return the identifier of the shape.
	 */
	public int getIdentifier()
	{
		return identifier;
	}

	/**
	 * Gets the layer that the button will be displayed on.
	 * 
	 * @return the layer on which the shape is displayed.
	 */
	public int getLayer()
	{
		return layer;
	}

	/**
	 * Gets the coordinate transform that will be applied to the button.
	 * 
	 * @return the coordinate transform that is applied to the shape.
	 */
	public FSCoordTransform getTransform()
	{
		return transform;
	}

	/**
	 * Gets the colour transform that will be applied to the button.
	 * 
	 * @return the colour transform that is applied to the shape.
	 */
	public FSColorTransform getColorTransform()
	{
		return colorTransform;
	}

	/**
	 * Set the state(s) of the button when the shape is drawn. The shape may be
	 * drawn for more than one state. Multiple states can be defined by
	 * bitwise-OR'ing individual states together, e.g. Up | Over.
	 * 
	 * @param aNumber
	 *            the state of the button when the shape is drawn.
	 */
	public void setState(int aNumber)
	{
		state = aNumber;
	}

	/**
	 * Sets the identifier of the FSDefineShape, FSDefineShape2 or
	 * FSDefineShape3 object that defines the appearance of the button when it
	 * is in the specified state(s).
	 * 
	 * @param anIdentifier
	 *            the identifier of the shape object that defines the shape's
	 *            appearance.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	/**
	 * Sets the layer in the display list that the shape will be displayed on.
	 * 
	 * @param aNumber
	 *            the number of the layer in the display list where the shape is
	 *            drawn.
	 */
	public void setLayer(int aNumber)
	{
		layer = aNumber;
	}

	/**
	 * Sets the coordinate transform that will be applied to the shape to change
	 * it's appearance.
	 * 
	 * @param aTransform
	 *            an FSCoordTransform object that will be applied to the shape.
	 */
	public void setTransform(FSCoordTransform aTransform)
	{
		transform = aTransform;
	}

	/**
	 * Sets the colour transform that will be applied to the shape to change
	 * it's colour.
	 * 
	 * @param aTransform
	 *            an FSColorTransform object that will be applied to the shape.
	 */
	public void setColorTransform(FSColorTransform aTransform)
	{
		colorTransform = aTransform;
	}

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

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

		return anObject;
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

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

			result = state == typedObject.state;
			result = 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
								&& colorTransform
												.equals(typedObject.colorTransform);
			else
				result = result && colorTransform == typedObject.colorTransform;
		}
		return result;
	}

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "state", state);
			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)
	{
		int length = 0;

		length += 5;
		length += transform.length(coder);
		// Flash 3
		if (coder.context[FSCoder.Type] == FSMovieObject.DefineButton2)
			length += colorTransform.length(coder);
		// End Flash 3
		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.writeBits(0, 4);
		coder.writeBits(state, 4);
		coder.writeWord(identifier, 2);
		coder.writeWord(layer, 2);
		transform.encode(coder);
		// Flash 3
		if (coder.context[FSCoder.Type] == FSMovieObject.DefineButton2)
			colorTransform.encode(coder);
		// End Flash 3
	}

	public void decode(FSCoder coder)
	{
		coder.readBits(4, false);

		state = coder.readBits(4, false);
		identifier = coder.readWord(2, false);
		layer = coder.readWord(2, false);

		transform = new FSCoordTransform(coder);
		// Flash 3
		if (coder.context[FSCoder.Type] == FSMovieObject.DefineButton2)
			colorTransform = new FSColorTransform(coder);
		// End Flash 3
	}
}
