
///////////////////////////////////////////////////////////////////////////////
//
// File   : Tools_FacialAnimation_AnimationHandler_HierarchyMode.cpp
// Author : Andy Tate
// Date   : 22-May-2008
//
// Copyright (C) 2008 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Include files...
////////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#if defined(ASURA_USE_FACIAL_ANIMATION_SYSTEM)

// This file's header...
#include "FacialAnimation/Tools_FacialAnimation_AnimationHandler_HierarchyMode.h"

#include "Animation/Asura_Animation.h"

// Tools header-files...
#include "FacialAnimation/Tools_FacialAnimation_Face.h"
#include "Hierarchy/Tools_Mode_Hierarchy.h"


////////////////////////////////////////////////////////////////////////////////
// Constant declarations...
////////////////////////////////////////////////////////////////////////////////

static const char*	g_szAnimationGroupName		= "FacialAnimation";
static const u_int	g_uAnimationGroupPriority	= 0x00008000;


////////////////////////////////////////////////////////////////////////////////
// Static variable declarations...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Private functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Protected functions...
////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
// Public functions...
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// Function     : Tools_FacialAnimation_AnimationHandler_HierarchyMode::Tools_FacialAnimation_AnimationHandler_HierarchyMode
// Author       : Andy Tate
// Date         : 22-May-2008
////////////////////////////////////////////////////////////////////////////////

Tools_FacialAnimation_AnimationHandler_HierarchyMode::Tools_FacialAnimation_AnimationHandler_HierarchyMode(const Asura_Hash_ID uFaceHash)
: PARENT(uFaceHash, g_szAnimationGroupName, g_uAnimationGroupPriority)
, m_uFaceHash(uFaceHash)
, m_uAnimHash(0)
{
	Asura_Animation*	pxAnimation	= Tools_Mode_Hierarchy::GetCurrentHierarchy();

	if(pxAnimation != NULL)
	{
		CreateAnimationGroup(*pxAnimation);
	}
	SetExternalEyeData(0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
}

/// End of function 'Tools_FacialAnimation_AnimationHandler_HierarchyMode::Tools_FacialAnimation_AnimationHandler_HierarchyMode'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Tools_FacialAnimation_AnimationHandler_HierarchyMode::~Tools_FacialAnimation_AnimationHandler_HierarchyMode
// Author       : Andy Tate
// Date         : 22-May-2008
////////////////////////////////////////////////////////////////////////////////

Tools_FacialAnimation_AnimationHandler_HierarchyMode::~Tools_FacialAnimation_AnimationHandler_HierarchyMode()
{
	Asura_Animation*	pxAnimation	= Tools_Mode_Hierarchy::GetCurrentHierarchy();

	if(pxAnimation != NULL)
	{
		DestroyAnimationGroup(*pxAnimation);
	}
}

/// End of function 'Tools_FacialAnimation_AnimationHandler_HierarchyMode::~Tools_FacialAnimation_AnimationHandler_HierarchyMode'. ///



////////////////////////////////////////////////////////////////////////////////
// Function     : Tools_FacialAnimation_AnimationHandler_HierarchyMode::Update
// Author       : Andy Tate
// Date         : 22-May-2008
////////////////////////////////////////////////////////////////////////////////

void Tools_FacialAnimation_AnimationHandler_HierarchyMode::Update(const float fTimeStep)
{
	Asura_Animation*	pxAnimation	= Tools_Mode_Hierarchy::GetCurrentHierarchy();

	if(pxAnimation != NULL)
	{
		if(m_uAnimHash)
		{
			Asura_Animation* pxAnim = Tools_Mode_Hierarchy::GetCurrentHierarchy();
			Asura_Hierarchy_Anim* pxHierAnim = Tools_Mode_Hierarchy::GetCurrentAnimation();
			if(pxAnim && pxHierAnim)
			{
				//Get the current primary animation timer in seconds
				float fTimer;
				if(Tools_Mode_Hierarchy::IsPrimaryAnimReversed())
				{
					fTimer = (1.0f-pxAnim->m_xPrimaryWrapper.GetPrimaryTimer()) * pxHierAnim->GetDuration();
				}
				else
				{
					fTimer = pxAnim->m_xPrimaryWrapper.GetPrimaryTimer() * pxHierAnim->GetDuration();
				}

				SetManualTimerUpdate(m_uAnimHash, true, fTimer);
			}
		}
		UpdateAnimation(fTimeStep, *pxAnimation);
	}
}

/// End of function 'Tools_FacialAnimation_AnimationHandler_HierarchyMode::Update'. ///


////////////////////////////////////////////////////////////////////////////////
// Function     : Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_PlayFacialAnimation
// Author       : Richard Rice
// Date         : 15-Dec-2008
////////////////////////////////////////////////////////////////////////////////

void Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_PlayFacialAnimation( Asura_Hash_ID uAnimHash, bool bLooped /*= false*/)
{
	//Keep just one facial animaton playing that is associated with a body animation to simplify things
	//RAR 15/12/2008
	if(m_uAnimHash)
	{
		StopFaceAnimation(m_uAnimHash);
		m_uAnimHash = 0;
	}

	if(PlayFaceAnimation(uAnimHash, bLooped))
	{
		m_uAnimHash = uAnimHash;
		SetManualTimerUpdate(uAnimHash, false, 0.0f);

	}
}
/// End of function 'Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_PlayFacialAnimation'. ///

////////////////////////////////////////////////////////////////////////////////
// Function     : Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_StopFacialAnimation
// Author       : Richard Rice
// Date         : 15-Dec-2008
////////////////////////////////////////////////////////////////////////////////

void Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_StopFacialAnimation()
{
	if(m_uAnimHash)
	{
		StopFaceAnimation(m_uAnimHash);
		m_uAnimHash = 0;
	}
}
/// End of function 'Tools_FacialAnimation_AnimationHandler_HierarchyMode::HierarchyMode_StopFacialAnimation'. ///



#endif	//defined(ASURA_USE_FACIAL_ANIMATION_SYSTEM)


/// End of file. ///
