
/************************************************************************************************************************************
///																																  ///
///		FICHIER :	ZooNode.cpp																									  ///
///																																  ///
///		NATURE	:	Defines the base class of the  node of the 3D scene															  ///
///																																  ///
**************************************************************************************************************************************/

#include	"..\Basic\ZooNode.h"


/************************************************************************************************************************************************
/// ZNode Class
///
///		- Node Class -Derivate for ZObject, ZLight,ZBone......
///		- Method for the hierarchy of the scene
///		- PRS ( Position,Rotation,Scale of an object of the scene
///
**************************************************************************************************************************************************/

// Constucteur : init de la hiérarchie à NULL						
ZNode::ZNode() : ZData()
{
	prev = next = father = son = NULL;
	posX = posY = posZ = rotX = rotY = rotZ = scale = 0.0f ;
	isBone = false ;
}



/// Insert as a child of the designed father							
bool ZNode::InsertAsChildOf(ZNode *newParent)
{
	if (newParent==NULL)	return false;

	// deselect the current father
	if(father)
	{
		if(father->son == this)
		{ 
			father->son = next;
			if (next)		next->prev = prev;
		} 
		else
		{
			if (!next)		father->son->prev = prev;						// If the node is the last, fix the prevlink of the first child
			prev->next = next;
			if (next)		next->prev = prev;
		}
		prev = next = NULL;													// Brother links are free, but the attached tree is available
	}
	
	// put the new father
	if (newParent->son==NULL)
	{
		father = newParent;
		newParent->son = prev = this;
		next = NULL;
	}
	else
	{
		next = newParent->son;
		prev = newParent->son->prev;
		newParent->son->prev = this;
		newParent->son = this;
		father = newParent;
	}

	return true;
}

//Insert the node at the last of the node Parent
bool ZNode::InsertAsLastOf(ZNode *newParent)
{
	if (newParent==NULL)	return false;

	// deselect the current father
	if(father)
	{
		if(father->son == this)
		{ 
			father->son = next;
			if (next)		next->prev = prev;
		} 
		else
		{
			if (!next)		father->son->prev = prev;						// If the node is the last, fix the prevlink of the first child
			prev->next = next;
			if (next)		next->prev = prev;
		}
		prev = next = NULL;													// Brother links are free, but the attached tree is available
	}
	
	// put the new father
	if (newParent->son==NULL)
	{
		father = newParent;
		newParent->son = prev = this;
		next = NULL;
	}
	else
	{
		next = NULL;
		prev = newParent->son->prev;
		newParent->son->prev->next = this;
		newParent->son->prev = this;
		father = newParent;
	}

	return true;
}