/**
* Timeline Manager
* @version: 01-29-2005
* @author Joseph 'Pcjoe' Florencio
*/

#include "fxsys.h"

// Constructor
CTimelineManager::CTimelineManager()
{	setFXObject(NULL);
	setTime(0);
	setPrevTime(0);
}

// Destructor
CTimelineManager::~CTimelineManager()
{	std::list<CTimeline*>::iterator	del;
	ForEachIn(del, m_Timelines)
	{	delete (*del);
	}
	m_Timelines.clear();
}

// Update Data
void CTimelineManager::updateData( void )
{	std::list<CTimeline*>::iterator	update;
	ForEachIn(update, m_Timelines)
	{	(*update)->updateData(getTime());
	}
}

// Add Timeline
void CTimelineManager::addTimeline( CTimeline *pTime )
{	// setup link to local timeline manager
	pTime->setTimelineManager(this);
	// Add timeline
	m_Timelines.push_back(pTime);
}

// Get timeline by ID
CTimeline *CTimelineManager::getTimeline( int iID )
{	std::list<CTimeline*>::iterator	iterFind;
	ForEachIn(iterFind, m_Timelines)
	{	if((*iterFind)->getID() == iID)
		{	return (*iterFind);
		}
	}
	return NULL;
}

// Reset Triggers
void CTimelineManager::resetTriggers( float flReleaseAfterTime )
{	CTimeline *pSound = getTimeline(TLID_SOUND);
	CTimeline *pSoundStream1 = getTimeline(TLID_SNDSTREAM1);
	CTimeline *pSoundStream2 = getTimeline(TLID_SNDSTREAM2);
	if(pSound)
		((CTimeSound*)pSound)->resetTriggers(flReleaseAfterTime);
	if(pSoundStream1)
		((CTimeSoundStream1*)pSoundStream1)->resetTriggers();
	if(pSoundStream2)
		((CTimeSoundStream2*)pSoundStream2)->resetTriggers();
}

// Read XML
void CTimelineManager::readXML( TiXmlElement *pElement )
{	TiXmlElement *pChildren=NULL;
	IterateXML(pChildren,pElement)
	{	int iID = atoi(pChildren->Attribute("tlid"));
		CTimeline *pTimeline = getTimeline(iID);
		if(pTimeline)
		{	pTimeline->readXML(pChildren);
		}
		else
		{	ConsolePrint("Error: Can't find timeline!\n");
		}
	}
}

// Write XML
TiXmlElement *CTimelineManager::writeXML( void )
{	TiXmlElement *pTimeManager = new TiXmlElement("timelines");

	std::list<CTimeline*>::iterator	iterCurrent;
	ForEachIn(iterCurrent, m_Timelines)
	{	pTimeManager->LinkEndChild((*iterCurrent)->writeXML());
	}
	return pTimeManager;
}