/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


		#include "physic/physic_constraint_desc.h"
		#include "metafile/nml.h"
		#include "log/log.h"

		using namespace GS::S3D;
		using GS::NML::Tag;


//------------------------------------------------------------------------------
bool				PhysicConstraintDesc::FromMetaTag(Tag &tag)
{
	if	(tag.name != "Constraint")
		__ERR__(__LOG_E__ << "Could not parse physic constraint, incorrect root tag (" << tag.name << ").\n", false)

	type = TypeNone;
	item_a = item_b = NULL;
	pivot_a = pivot_b = Matrix4::IdentityMatrix();

	NMLTagForeach(pt, tag)
	{
		if	(pt->name == "Type")
		{
			String _type(pt->GetString());

					if	(_type == "Point")
					type = TypePoint;
			else	if	(_type == "Hinge")
					type = TypeHinge;
		}

		else	if	(pt->name == "PivotA")
				pivot_a.FromMetaTag(*pt);
		else	if	(pt->name == "PivotB")
				pivot_b.FromMetaTag(*pt);

		else	__LOG_W__ << "Unknown tag '" << pt->name << "' in <PhysicConstraint>.\n";
	}
	return true;
}
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
Tag			*PhysicConstraintDesc::AsMetaTag() const
{
	Tag *root = new Tag("Constraint");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not create physic constraint root tag to serialize.\n", NULL)

	if	(type != TypeNone)
	{
		String	_type = "None";
		switch (type)
		{
			case TypePoint:	_type = "Point";	break;
			case TypeHinge:	_type = "Hinge";	break;

			default: break;
		}
		root->AddChild("Type", _type.c_str());
	}

	// Save pivot informations.
	root->AddChild(pivot_a.AsMetaTag("PivotA"));
	root->AddChild(pivot_b.AsMetaTag("PivotB"));
	return root;
}
//------------------------------------------------------------------------------
