#include "xmltexture.h"

// ----------------------------------------------------------------------------
CXmlTexCoords::CXmlTexCoords()
{
	m_left = m_top = m_right = m_bottom = 0.0f;
}

// ----------------------------------------------------------------------------
bool CXmlTexCoords::LoadFromDOMNode(const DOMNode* node)
{
	if ( node == NULL || node->getNodeType() != DOMNode::ELEMENT_NODE )
		return false;

	// Attribute
	DOMNamedNodeMap* nodeMap = node->getAttributes();
	if ( nodeMap )
	{
		const XMLCh* nodeValue;
		if ( (nodeValue = GetNodeAttribute(nodeMap, "left")) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_left = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute(nodeMap, "top")) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_top = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute(nodeMap, "right")) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_right = (float)atof(tmp);
			XMLString::release(&tmp);
		}
		if ( (nodeValue = GetNodeAttribute(nodeMap, "bottom")) != NULL )
		{
			char* tmp = XMLString::transcode(nodeValue);
			m_bottom = (float)atof(tmp);
			XMLString::release(&tmp);
		}
	}

	return true;
}

// ----------------------------------------------------------------------------
CXmlTexture::CXmlTexture(CXmlUi* pXmlUi, CXmlLayout* pParent)
	: CXmlLayout(pXmlUi, pParent)
{
	m_type				= eXmlUiType_Texture;

	m_file.clear();
	m_texCoords.clear();
	m_alphaMode			= eAlphaMode_Blend;
	m_animationSpeed	= 0.0f;
}

// ----------------------------------------------------------------------------
CXmlTexture::~CXmlTexture()
{
}

// ----------------------------------------------------------------------------
int	CXmlTexture::XmlValueToAlphaMode( const char* valueStr )
{
	if ( valueStr )
	{
		if ( XMLString::compareString(valueStr, "DISABLE") == 0 )
			return eAlphaMode_Disable;
		else if ( XMLString::compareString(valueStr, "BLEND") == 0 )
			return eAlphaMode_Blend;
		else if ( XMLString::compareString(valueStr, "ADD") == 0 )
			return eAlphaMode_Add;
	}
	return eAlphaMode_Blend;
}

// ----------------------------------------------------------------------------
void CXmlTexture::LoadNodeAttribute(const DOMNamedNodeMap* nodeMap)
{
	CXmlLayout::LoadNodeAttribute(nodeMap);

	const XMLCh* nodeValue;
	if ( (nodeValue = GetNodeAttribute(nodeMap, "file")) != NULL )
	{
		char* tmp = XMLString::transcode(nodeValue);
		m_file = tmp;
		XMLString::release(&tmp);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "alphaMode")) != NULL )
	{
		char* tmp = XMLString::transcode(nodeValue);
		m_alphaMode = XmlValueToAlphaMode(tmp);
		XMLString::release(&tmp);
	}
	if ( (nodeValue = GetNodeAttribute(nodeMap, "animationSpeed")) != NULL )
	{
		char* tmp = XMLString::transcode(nodeValue);
		m_animationSpeed = (float)atof(tmp);
		XMLString::release(&tmp);
	}		
}

// ----------------------------------------------------------------------------
void CXmlTexture::LoadNodeElement(const DOMNode* node, const char* nodeName)
{
	CXmlLayout::LoadNodeElement(node, nodeName);

	// Texture Coordinates
	if ( XMLString::compareString(nodeName, "TexCoords") == 0 )
	{
		CXmlTexCoords texCoords;
		if ( texCoords.LoadFromDOMNode(node) )
		{
			m_texCoords.push_back(texCoords);
		}
	}
	// Color
	else if ( XMLString::compareString(nodeName, "Color") == 0 )
	{
		m_color.LoadFromDOMNode(node);
	}
}

// ----------------------------------------------------------------------------
void CXmlTexture::Inherit(CXmlLayout* pXmlLayout)
{
	CXmlLayout::Inherit(pXmlLayout);
	CXmlTexture* pXmlTexture = dynamic_cast<CXmlTexture*>(pXmlLayout);
	if (pXmlTexture)
	{
		m_file				= pXmlTexture->m_file;
		m_texCoords			= pXmlTexture->m_texCoords;
		m_color				= pXmlTexture->m_color;
		m_alphaMode			= pXmlTexture->m_alphaMode;
		m_animationSpeed	= pXmlTexture->m_animationSpeed;
	}
}