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

/**
 * FSButtonSound defines the sounds that are played when an event occurs in a
 * button.
 * 
 * <p>
 * A sound is played for only a subset of the events that a button responds to:
 * </p>
 * 
 * <table>
 * <tr>
 * <td>rollOut</td>
 * <td>The cursor exits the active area of the button.</td>
 * </tr>
 * <tr>
 * <td>rollOver</td>
 * <td>The cursor enters the active area of the button.</td>
 * </tr>
 * <tr>
 * <td>press</td>
 * <td>The mouse button is clicked and the cursor is inside the active area of
 * the button.</td>
 * </tr>
 * <tr>
 * <td>release</td>
 * <td>The mouse button is released while the cursor is inside the active area
 * of the button.</td>
 * </tr>
 * </table>
 * 
 * <table>
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. The type attribute is
 * read-only and may be used when iterating through the objects in an FSMovie
 * object to identify the object class without using run-time type checking.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>the identifier of an FSDefineButton or FSDefineButton2 object that
 * define the button.</td>
 * </tr>
 * 
 * <tr>
 * <td>sound[rollOut]</a></td>
 * <td>An FSSound object that describes how the sound will be played for a
 * rollOver event.</td>
 * </tr>
 * 
 * <tr>
 * <td>sound[rollOver]</a></td>
 * <td>An FSSound object that describes how the sound will be played for a
 * rollOver event.</td>
 * </tr>
 * 
 * <tr>
 * <td>sound[press]</a></td>
 * <td>An FSSound object that describes how the sound will be played for a
 * press event.</td>
 * </tr>
 * 
 * <tr>
 * <td>sound[release]</a></td>
 * <td>An FSSound object that describes how the sound will be played for a
 * release event.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * <b>Important:</b> If the identifier in the FSSound object for an event is
 * set to zero then the corresponding FSSound object will not be encoded when
 * the object is encoded to a Flash file. This compresses the object when the
 * sounds for only a sub-set of the events are defined.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSButtonSound class represents the DefineButtonSound structure from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 2.
 * </p>
 */
public class FSButtonSound extends FSMovieObject
{
	private int identifier = 0;
	private FSSound[] sound = new FSSound[] {null, null, null, null };

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

	/**
	 * Constructs an FSButtonSound object that defines the sound played for a
	 * single button event.
	 * 
	 * @param anIdentifier
	 *            the identifier of the FSDefineButton or FSDefineButton2 object
	 *            that defines the button.
	 * @param eventCode
	 *            the event that identifies when the sound id played, must be
	 *            either FSButtonEvent.rollOver, FSButtonEvent.rollOut,
	 *            FSButtonEvent.press or FSButtonEvent.release.
	 * @param aSound
	 *            an FSSound object that identifies a sound and controls how it
	 *            is played.
	 */
	public FSButtonSound(int anIdentifier, int eventCode, FSSound aSound)
	{
		super(ButtonSound);

		setIdentifier(anIdentifier);
		setSoundForEvent(eventCode, aSound);
	}

	/**
	 * Constructs an FSButtonSound object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSButtonSound object.
	 */
	public FSButtonSound(FSButtonSound obj)
	{
		super(obj);

		identifier = obj.identifier;

		for (int i = 0; i < 4; i++)
		{
			if (obj.sound[i] != null)
				sound[i] = new FSSound(obj.sound[i]);
		}
	}

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

	/**
	 * Gets the FSSound object for the specified event.
	 * 
	 * @param eventCode
	 *            the code representing the button event, must be either
	 *            FSButtonEvent.RollOver, FSButtonEvent.RollOut,
	 *            FSButtonEvent.Press or FSButtonEvent.Release.
	 * @return the FSSound that identifies and controls the sound that will be
	 *         played for the event.
	 */
	public FSSound getSoundForEvent(int eventCode)
	{
		FSSound aSound = null;

		if (eventCode == FSButtonEvent.RollOut)
			aSound = sound[0];
		else if (eventCode == FSButtonEvent.RollOver)
			aSound = sound[1];
		else if (eventCode == FSButtonEvent.Press)
			aSound = sound[2];
		else
			aSound = sound[3];

		return aSound;
	}

	/**
	 * Sets the identifier of the button that this object applies to.
	 * 
	 * @param anIdentifier
	 *            the identifier of the button which this object applies to.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	/**
	 * Sets the FSSound object for the specified button event.
	 * 
	 * @param eventCode
	 *            the code representing the button event, must be either
	 *            FSButtonEvent.RollOver, FSButtonEvent.RollOut,
	 *            FSButtonEvent.Press or FSButtonEvent.Release.
	 * @param aSound
	 *            an FSSound object that identifies and controls how the sound
	 *            is played.
	 */
	public void setSoundForEvent(int eventCode, FSSound aSound)
	{
		if (eventCode == FSButtonEvent.RollOut)
			sound[0] = aSound;
		else if (eventCode == FSButtonEvent.RollOver)
			sound[1] = aSound;
		else if (eventCode == FSButtonEvent.Press)
			sound[2] = aSound;
		else
			sound[3] = aSound;
	}

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

		for (int i = 0; i < 4; i++)
		{
			anObject.sound[i] = (sound[i] != null) ? (FSSound) sound[i].clone()
							: null;
		}

		return anObject;
	}

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

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

			result = identifier == typedObject.identifier;

			for (int i = 0; i < 4; i++)
			{
				if (sound[i] != null)
					result = result && sound[i].equals(typedObject.sound[i]);
				else
					result = result && sound[i] == typedObject.sound[i];
			}
		}
		return result;
	}

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

		if (depth > 0)
		{
			buffer.append(name() + ": {");
			Transform.append(buffer, "identifier", identifier);

			if (depth - 1 > 0)
			{
				for (int i = 0; i < 4; i++)
				{
					buffer.append("sound[" + i + "] = ");

					if (sound[i] != null)
						sound[i].appendDescription(buffer, depth);
					else
						buffer.append("null; ");
				}
			} else
			{
				for (int i = 0; i < 4; i++)
				{
					buffer.append("sound[" + i + "] =");

					if (sound[i] != null)
						Transform.append(buffer, sound[i].name(), depth);
					else
						buffer.append("null");
				}
			}
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += 2;

		for (int i = 0; i < 4; i++)
		{
			if (sound[i] != null && sound[i].getIdentifier() != 0)
				length += sound[i].length(coder);
			else
				length += 2;
		}

		return length;
	}

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

		coder.writeWord(identifier, 2);

		for (int i = 0; i < 4; i++)
		{
			if (sound[i] != null && sound[i].getIdentifier() != 0)
				sound[i].encode(coder);
			else
				coder.writeWord(0, 2);
		}
		coder.endObject(name());
	}

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

		int start = coder.getPointer();

		identifier = coder.readWord(2, false);

		for (int i = 0; i < 4; i++)
		{
			if (coder.scanWord(2, false) > 0)
				sound[i] = new FSSound(coder);
			else
				coder.readWord(2, false);

			if (((coder.getPointer() - start) >>> 3) == length)
				break;
		}
		coder.endObject(name());
	}
}
