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


		#include "scene3d/mconstraint.h"
		#include "physic/physic_constraint.h"
		#include "physic/physic_world.h"
		#include "log/log.h"

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


//------------------------------------------------------------------------------
void		MConstraint::Setup(PhysicWorld *world)
{
	if	(world)
	{
		physic_data = world->NewConstraint();
		physic_data->SetupConstraint(desc);
	}
}
//------------------------------------------------------------------------------

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

	NMLTagForeach(pt, tag)
	{
				if	(pt->name == "MItem")
				MItem::FromMetaTag(*pt);
		else	if	(pt->name == "Constraint")
				desc.FromMetaTag(*pt);
		else	__LOG_W__ << "Unknown tag '" << pt->name << "' in <nMConstraint>.\n";
	}
	return true;
}
Tag			*MConstraint::AsMetaTag()
{
	Tag *root = new Tag("MConstraint");
	if	(!root)
		__ERR__(__LOG_E__ << "Could not create managed constraint tag to serialize.\n", NULL)
	
	root->AddChild(MItem::AsMetaTag());
	root->AddChild(desc.AsMetaTag());

	return root;
}
//------------------------------------------------------------------------------

MConstraint::MConstraint(PhysicConstraint *c) : physic_data(c)
{
	item_type = Type_Constraint;
}
