/* libsswf_tag_sprite.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagSprite class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;



/////////////////////////////////////////// TagSprite

/** \class sswf::TagSprite
 *
 * \brief Group shapes, sprites, texts, buttons, sound, etc.
 *
 * The TagSprite is the SWF object used to group other objects
 * in one single entity. The sprite is given an ID which can
 * later be used to display the sprite.
 *
 * A sprite can be viewed as a group, though it only includes
 * a set of control tags used to define a display list.
 *
 * For instance, to include a TagSprite inside another TagSprite
 * you actually need to use a TagPlace which reference the
 * TagSprite to be inserted.
 *
 * There is one special case sprite: the empty sprite. This is
 * required if you want to use the CreateEmptyMovie() function
 * in some ActionScript!
 *
 * Since version 1.8.0, a Sprite derives from the sswf::TagScalingGrid.
 * This means you can define a scaling rectangle with a call
 * to the
 * void sswf::TagScalingGrid::SetGrid(const SRectangle& rect)
 * function.
 *
 * \sa <a href="../SWFalexref.html#tag_definesprite">SWF Alexis' Reference&mdash;Define Sprite</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \brief Initialize a sprite
 *
 * By default, a TagSprite is created empty. You can
 * add objects using the Sprite object as the parent
 * of other objects (instead of the TagHeader.)
 */
TagSprite::TagSprite(TagBase *parent)
	: TagBaseID("sprite", parent)
{
}



/** \brief Returns the flags defining a TagSprite object
 *
 * A Sprite is a definition, it includes references to
 * other objects, it accept scripts, it is a sprite
 * and it has an identifier.
 *
 * \return The set of flags that the TagSprite support
 */
TagBase::swf_type_t TagSprite::TypeFlags(void) const
{
	return SWF_TYPE_DEFINE | SWF_TYPE_REFERENCE | SWF_TYPE_SCRIPT | SWF_TYPE_SPRITE | SWF_TYPE_HAS_ID;
}


/** \brief Check that the child is valid for a TagSprite
 *
 * A sswf::TagSprite accepts only a few control tags as defined in
 * <a href="../SWFalexref.html#tag_definesprite">my reference</a>.
 *
 * This function returns an error if the child is not one of these
 * control tags:
 *
 *	\li TagDoAction
 *	\li TagEnd
 *	\li TagFrameLabel
 *	\li TagPlace
 *	\li TagRemove
 *	\li TagShowFrame
 *	\li TagSoundStream [not implemented]
 *	\li TagStartSound
 *
 * \param child_name The name of the child to be added to this parent
 *
 * \return An error code: ErrorManager::ERROR_CODE_NONE or
 *		ErrorManager::ERROR_CODE_INCOMPATIBLE_CHILD
 *
 * \sa sswf::TagBase::OnNewChild(const char *child)
 * \sa sswf::TagSprite::OnNewChild(const char *child)
 */
ErrorManager::error_code_t TagSprite::OnNewChild(const char *child_name) const
{
	if(strcmp(child_name, "doaction") == 0
	|| strcmp(child_name, "end") == 0
	|| strcmp(child_name, "framelabel") == 0
	|| strcmp(child_name, "place") == 0
	|| strcmp(child_name, "remove") == 0
	|| strcmp(child_name, "showframe") == 0
	|| strcmp(child_name, "soundstream") == 0	// Not yet implemented
	|| strcmp(child_name, "startsound") == 0) {
		// valid for a sprite!
		return ErrorManager::ERROR_CODE_NONE;
	}

	// a sprite doesn't like those!
	return ErrorManager::ERROR_CODE_INCOMPATIBLE_CHILD;
}



/** \brief Defines the minimum version necessary to save this sprite
 *
 * Sprites appeared in version 3 of SWF.
 *
 * Since version 8 of SWF, it is possible to attached a scaling grid
 * to a sprite (by referencing the sprite in the scaling grid.)
 *
 * When no grid is defined, then a version 3 movie is sufficient.
 *
 * When a grid is defined, then a version 8 or better is required.
 */
ErrorManager::error_code_t TagSprite::PreSave(void)
{
	ErrorManager::error_code_t	r;

	MinimumVersion(3);

	r = GridPreSave();
	if(r != 0) {
		return r;
	}

	return TagBase::PreSave();
}


/** \brief Save a Sprite control list
 *
 * This function saves all the children defined in this sprite,
 * the Sprite header and when the user did not specify them, the
 * TagShowFrame and TagEnd at the end of the list.
 *
 * \warning
 * The size of the sub-list of tags within a sprite is limited
 * to 64Kb.
 *
 * \param data The Data buffer in which the sprite is to be saved
 *
 * \return An error manager code
 */
ErrorManager::error_code_t TagSprite::Save(Data& data)
{
	TagBase				*child, *last;
	Data				sub_data;
	ErrorManager::error_code_t	ec;

// reset the counter in case the user saves multiple times
	ResetFrames();

	SaveID(sub_data);
	sub_data.PutShort(1);		// the frame counter (undefined at this point)
		// Note: I use a default of 1 so that way I do not have to fix this
		//	 number is case you are creating an empty sprite!

// save all the children first
	last = 0;
	child = Children();
	while(child != 0) {
		ec = child->Save(sub_data);
		if(ec != ErrorManager::ERROR_CODE_NONE) {
			return ec;
		}
		last = child;
		child = child->Next();
	}

// there is nothing in there?!?
	if(last == 0) {
		// this is a valid case to create objects in your
		// actionscripts (see the assembler)
		sub_data.PutShort(SWF_TAG_END);
		SaveTag(data, SWF_TAG_DEFINE_SPRITE, sub_data.ByteSize());
		data.Append(sub_data);
		return GridSave(data, Identification());
	}

// add the SHOW FRAME and END tags so it works properly with any player
	// NOTE: if the list ends with and "end" but not "show frame" then there
	//       simply won't be any "show frame".
	if(strcmp(last->Name(), "end") != 0) {
		if(strcmp(last->Name(), "showframe") != 0) {
			ShowFrame();
			SaveTag(sub_data, SWF_TAG_SHOW_FRAME, 0);
		}
		sub_data.PutShort(SWF_TAG_END);
	}

	// write the frame count after the sprite ID
	sub_data.OverwriteShort(sizeof(sswf_id_t), FrameCount());

	SaveTag(data, SWF_TAG_DEFINE_SPRITE, sub_data.ByteSize());
	data.Append(sub_data);

// now we can save our grid if any
	return GridSave(data, Identification());
}




/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
