/**
* Positional Data
* @version: 01-29-2005
* @author Joseph 'Pcjoe' Florencio
*/

#include "fxsys.h"

//Get Position (dynamic or static)
vec3_t CFXDataPoint::getPosition( void )
{	if(getType()==DP_STATIC)
	{	if(getFXEvent())
			return getFXEvent()->makePosFinal(m_vecPosition);
		else
			return m_vecPosition;
	}
	else
	{
	}
	return vec3_t(0,0,0);
}

// Write XML
TiXmlElement *CFXDataPoint::writeXML( void )
{	TiXmlElement *pDP = new TiXmlElement("dp");
	char szFloat[16];

	switch(getType())
	{
	case DP_STATIC:
		pDP->SetAttribute("type","vector");
		// save vector
		sprintf(szFloat,"%f",m_vecPosition.x);
		pDP->SetAttribute("x",szFloat);
		sprintf(szFloat,"%f",m_vecPosition.y);
		pDP->SetAttribute("y",szFloat);
		sprintf(szFloat,"%f",m_vecPosition.z);
		pDP->SetAttribute("z",szFloat);
		break;
	case DP_BONE:
		pDP->SetAttribute("type","bone");
		pDP->SetAttribute("name",m_strBone.c_str());
		// save offset vector
		sprintf(szFloat,"%f",m_vecOffset.x);
		pDP->SetAttribute("x",szFloat);
		sprintf(szFloat,"%f",m_vecOffset.y);
		pDP->SetAttribute("y",szFloat);
		sprintf(szFloat,"%f",m_vecOffset.z);
		pDP->SetAttribute("z",szFloat);
		break;
	case DP_CAMERA:
		pDP->SetAttribute("type","camera");
		// save offset vector
		sprintf(szFloat,"%f",m_vecOffset.x);
		pDP->SetAttribute("x",szFloat);
		sprintf(szFloat,"%f",m_vecOffset.y);
		pDP->SetAttribute("y",szFloat);
		sprintf(szFloat,"%f",m_vecOffset.z);
		pDP->SetAttribute("z",szFloat);
		break;
	}

	return pDP;
}

// read XML
void CFXDataPoint::readXML( TiXmlElement *pElement )
{	if(!pElement)
	{	return;
	}
	string strType = pElement->Attribute("type");
	// Vector
	if(strType == "vector")
	{	vec3_t vecPos;
		if(pElement->Attribute("x"))
			vecPos.x = atof(pElement->Attribute("x"));
		if(pElement->Attribute("y"))
			vecPos.y = atof(pElement->Attribute("y"));
		if(pElement->Attribute("z"))
			vecPos.z = atof(pElement->Attribute("z"));

		setStaticPosition(vecPos);
	}
	else if(strType == "bone")
	{	std::string strBone;
		vec3_t vecOffset;
		if(pElement->Attribute("name"))
			strBone = pElement->Attribute("name");
		if(pElement->Attribute("x"))
			vecOffset.x = atof(pElement->Attribute("x"));
		if(pElement->Attribute("y"))
			vecOffset.y = atof(pElement->Attribute("y"));
		if(pElement->Attribute("z"))
			vecOffset.z = atof(pElement->Attribute("z"));
		setDynamicBone(strBone);
		setDynamicOffset(vecOffset);
	}
	else if(strType == "camera")
	{	vec3_t vecOffset;
		if(pElement->Attribute("x"))
			vecOffset.x = atof(pElement->Attribute("x"));
		if(pElement->Attribute("y"))
			vecOffset.y = atof(pElement->Attribute("y"));
		if(pElement->Attribute("z"))
			vecOffset.z = atof(pElement->Attribute("z"));
		setDynamicCamera();
		setDynamicOffset(vecOffset);
	}
}