//////////////////////////////////////////////////////////////////////////////
//
// Tools_Object_LiftSplinePoint.cpp
//
// Copyright (c) 2006 Rebellion. All rights reserved.
//
// Description:
//
//     Tools control point for lift splines
//
//////////////////////////////////////////////////////////////////////////////

// I N C L U D E S ///////////////////////////////////////////////////////////

#include "Core/Asura.h"

// This file's header.
#include "Entity/Locations/Spline/LiftSpline/Tools_Object_LiftSplinePoint.h"

// asura
#include "Entity/Locations/OrientedNode/Asura_ServerNode_OrientedNode.h"
#include "Entity/Locations/Spline/LiftSpline/Asura_ServerNode_LiftSpline.h"
#include "Mesh/Asura_Mesh.h"

// tools
#include "Entity/Locations/Spline/LiftSpline/Tools_EntityNode_LiftSpline.h"

//////////////////////////////////////////////////////////////////////////////
//
// Tools_Object_LiftSplinePoint
//
//////////////////////////////////////////////////////////////////////////////

Tools_Object_LiftSplinePoint* Tools_Object_LiftSplinePoint::s_pxThisLiftSplinePoint = NULL;

Tools_Object_LiftSplinePoint::Tools_Object_LiftSplinePoint(
    Asura_ServerNode_LiftSpline* pxParentNode,
    Tools_EntityNode_LiftSpline* pxParentTools,
    int iIndex,
    bool bIsEndPoint)
: PARENT(pxParentNode, pxParentTools, iIndex, bIsEndPoint)
{
	Type = TOT_Entity_LiftSpline_Point;

    m_pxLiftParentNode  = pxParentNode;
    m_pxLiftParentTools = pxParentTools;
	m_pxLiftParentNode->GetPointOrientation(m_iIndex - 1).ToMatrix(Orientation);
	if (m_pxLiftParentNode->TakeOrientationFromPoints())
	{
		SetMesh( "ToolsShape_LiftSplinePoint" );
	}
	else
	{
		SetMesh( "ToolsShape_Attractor" );
	}
}

void Tools_Object_LiftSplinePoint::UpdatePositionInAsuraData(bool /*updateLinkedObjects*/)
{
	Asura_Quat xQuatOri;
	xQuatOri.FromMatrix(Orientation);
	// Note that the control points do not have an orientation, so there are NumPoints-2 orientations,
	// hence the m_iIndex-1 in all of the point orientation code.
	m_pxLiftParentNode->SetPointOrientation(m_iIndex - 1, xQuatOri);

	PARENT::UpdatePositionInAsuraData(true);
}

void Tools_Object_LiftSplinePoint::UpdatePositionFromAsuraData()
{
	m_pxLiftParentNode->GetPointOrientation(m_iIndex - 1).ToMatrix(Orientation);

	PARENT::UpdatePositionFromAsuraData();
}

void Tools_Object_LiftSplinePoint::OnDeselection()
{
	s_pxThisLiftSplinePoint = NULL;
	PARENT::OnDeselection();
}

void Tools_Object_LiftSplinePoint::OnSelection()
{
	s_pxThisLiftSplinePoint = this;
	PARENT::OnSelection();
}

void Tools_Object_LiftSplinePoint::Render()
{
	if ( m_pxLiftParentNode->TakeOrientationFromPoints() )
	{
		if ( !Mesh || strcmp( Mesh->Name.GetString(), "ToolsShape_CutsceneTrackPoint" ) != 0 )
		{
			SetMesh( "ToolsShape_LiftSplinePoint" );
		}
	}
	else
	{
		if ( !Mesh || strcmp( Mesh->Name.GetString(), "ToolsShape_Attractor" ) != 0 )
		{
			SetMesh( "ToolsShape_Attractor" );
		}
	}

	PARENT::Render();
}

void Tools_Object_LiftSplinePoint::Update()
{
	PARENT::Update();

	if ( m_pxLiftParentNode->TakeOrientationFromPoints() )
	{
		// If we're an end point we must make sure we orientate with the corresponding node.
		if ( m_bIsEndPoint )
		{
			const Asura_Guid uGuidWeMustRemainWith = (m_iIndex == 1) ? m_pxLiftParentNode->GetSourceGuid() : m_pxLiftParentNode->GetTargetGuid();
			const Asura_ServerNode_OrientedNode* pxNode = static_cast< Asura_ServerNode_OrientedNode* >( Asura_ServerNode_OrientedNode::FindEntity( uGuidWeMustRemainWith ) );
			if ( pxNode )
			{
				Asura_Quat xNodeOrientQ;
				pxNode->GetOrientation( xNodeOrientQ );
				if ( !m_pxLiftParentNode->GetPointOrientation( m_iIndex - 1 ).ApproximatelyEquals( xNodeOrientQ ) )
				{
					xNodeOrientQ.ToMatrix( Orientation );
					UpdatePositionInAsuraData( true );
				}
			}
		}
	}
	else if ( !m_pxLiftParentNode->GetPointOrientation( m_iIndex - 1 ).ApproximatelyEquals( Asura_Maths::IdentityQuat ) )
	{
		Orientation.Identity();
		UpdatePositionInAsuraData( true );
	}
}

//////////////////////////////////////////////////////////////////////////////
//
// 
//
//////////////////////////////////////////////////////////////////////////////
