/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : CurObj.cpp
//   
//  Definition of the cursor object            
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from  :	CPA_Object
//					CPA_EdMot<ACP_tdxHandleOfLink>
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 5 feb 1997                        Author: J Thénoz
///////////////////////////////////////////////////////////////////////////////////////////////////
// Modification date:			                      Author: 
// 
//
// 
//
///////////////////////////////////////////////////////////////////////////////////////////////////
*/

#include "stdafx.h"
#include "acp_base.h"
#include "incitf.h"
#include "incgam.h"
#include "incai.h"
#include "WPObj.hpp"
//ANNECY Shaitan Nettoyage (12/05/98) {
/*
#include "WayObj.hpp"
#include "LinkObj.hpp"
*/
//ENDANNECY Shaitan Nettoyage }
#include "CurObj.hpp"
#include "Inter.hpp"

//-------- static init
long Cursor::ms_lNbCursor=0;
Waypoint_Interface*	Cursor::ms_poInterface;

//------ static
void Cursor::fn_vInitObject (Waypoint_Interface* poInterface)
{
	ms_poInterface = poInterface;
}

					  
Cursor::Cursor( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner, eCursorMouseMoveLimit elimit ):
	CPA_BaseObject ( p_oDLL, "Cursor" )
{

	CPA_SuperObject* poSuperCursor = ms_poInterface->GetInterface()->GetNewSuperObject ( E_ss_NoSave, C_NoType );
	m_poSuperObject = poSuperCursor;

	poSuperCursor->SetTypeSO (C_Protected);
	poSuperCursor->SetObject(this);
	SetSuperObject (poSuperCursor);

	// name
	CString csName;
	csName.Format ("Cursor%d", ms_lNbCursor);
	ms_lNbCursor++;
	fn_eRename (csName);

	// init
	MTH3D_M_vNullVector(&m_stVertex);
	m_xDim=0.05f;
	m_poParent = (CPA_SuperObject*)p_oOwner;
	m_eMouseMoveLimit = elimit;
	m_bCursorMove = FALSE;

	// graphic object
	CPA_SuperObject* poGraphic = GetInterface()->fn_pGetNewCursorGraphicObject();
	poGraphic->SetSuperObjectOwner (GetSuperObject());	
	GetSuperObject()->AddTail (poGraphic);
	GEO_xComputeObjectNormals ( (ACP_tdxHandleOfObject)HIE_fn_hGetSuperObjectObject(poGraphic->GetStruct()));
	poGraphic->SetEditProtected (TRUE); 
}

long Cursor::GetDataType (void)
{
	return C_ucCursor;
}

void* Cursor::GetData (void)
{
	return NULL;
}


Waypoint_Interface* Cursor::GetInterface (void)
{
	return ms_poInterface; 
}

void Cursor::fn_vDraw (void)
{
	MTH3D_tdstVector stAbsoluteVertex;
	tdeLocalColor tdColor = GetSuperObject()->GetLocalColor ();

	CPA_SuperObject* psoObject = (CPA_SuperObject*)GetSuperObject()->GetHead();
	fn_vGetAbsoluteVertex ( &stAbsoluteVertex );

	ACP_tdxHandleOfObject hMot = (ACP_tdxHandleOfObject) HIE_fn_hGetSuperObjectObject(psoObject->GetStruct());

	MTH3D_tdstVector a8_stPoint [13];
	
	MTH3D_M_vSetVectorElements (a8_stPoint, m_xDim,m_xDim,-m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+1, m_xDim,-m_xDim,-m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+2, -m_xDim,-m_xDim,-m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+3, -m_xDim,m_xDim,-m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+4, m_xDim,m_xDim,m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+5, m_xDim,-m_xDim,m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+6, -m_xDim,-m_xDim,m_xDim);
	MTH3D_M_vSetVectorElements (a8_stPoint+7, -m_xDim,m_xDim,m_xDim);
	
	GEO_vSetListOfPointsOfObject 
	(
		hMot, 
        a8_stPoint, 
        8, 
        0
	);
	GEO_xComputeObjectNormals ( hMot );

	// change the local matrix
	GEO_tdxHandleToMatrix hAbsoluteObjectMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (psoObject->GetStruct());
	GEO_tdxHandleToMatrix hAbsoluteFatherMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poParent->GetStruct());

	POS_fn_vSetIdentityMatrix( hAbsoluteObjectMatrix );
	
	MTH3D_tdstVector stOx, stOy, stOz;

	POS_fn_vGetRotationMatrix( hAbsoluteFatherMatrix ,
								&stOx, &stOy, &stOz );

	POS_fn_vSetRotationMatrix( hAbsoluteObjectMatrix ,
								&stOx, &stOy, &stOz );

	POS_fn_vSetTranslationVector(  hAbsoluteObjectMatrix, &stAbsoluteVertex );
	GetInterface()->fn_vComputeNewRelativeMatrix (psoObject->GetStruct());
}

void Cursor::fn_vSetAbsoluteVertex ( MTH3D_tdstVector* pAbsoluteVertex )
{
	POS_tdstCompletePosition stInvMatrix;
	POS_fn_vSetIdentityMatrix(&stInvMatrix);
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poParent->GetStruct());
	POS_fn_vInvertMatrix( &stInvMatrix , hAbsoluteLineMatrix );
	POS_fn_vMulMatrixVertex(  &m_stVertex, &stInvMatrix, pAbsoluteVertex) ;
	m_stVertex.xZ=0.;
}
	
void Cursor::fn_vGetAbsoluteVertex ( MTH3D_tdstVector* pAbsoluteVertex )
{
	GEO_tdxHandleToMatrix hAbsoluteLineMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poParent->GetStruct());
	POS_fn_vMulMatrixVertex(  pAbsoluteVertex, hAbsoluteLineMatrix, &m_stVertex) ;
}


void Cursor::fn_vStartMove ( MTH3D_tdstVector* pstStartVertex )
{
	fn_vSetAbsoluteVertex (pstStartVertex);
	fn_vDraw ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}

void Cursor::fn_vMove ( MTH3D_tdstVector* pstTranslation )
{
	MTH3D_tdstVector stVertex, stInitialVertex;
	fn_vGetAbsoluteVertex ( &stVertex );
	stInitialVertex = stVertex;
	MTH3D_M_vAddVector(&stVertex, &stVertex, pstTranslation);
	if (fn_bCanMove(&stVertex))
	{
		fn_vSetAbsoluteVertex ( &stVertex );
		fn_vGetAbsoluteVertex ( pstTranslation );
		MTH3D_M_vSubVector(pstTranslation, pstTranslation, &stInitialVertex );
		fn_vDraw ();
		GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
	}
	else MTH3D_M_vSetVectorElements (pstTranslation, 0., 0., 0.);
}

void Cursor::fn_vEndMove (void)
{
	m_bCursorMove = TRUE;
}

BOOL Cursor::fn_bCanMove ( MTH3D_tdstVector* pstVertex )
{
	switch (m_eMouseMoveLimit)
	{
		case eCursorCircularLimit:
		{
			MTH3D_tdstVector stLocalA, stLocalC;
			MTH3D_tdstVector stAB, stBC, stAC;
			MTH_tdxReal lAB, lBC, lAC;
			
			MTH3D_M_vSetVectorElements (&stLocalA, 0., 0., 0.);
			MTH3D_M_vSetVectorElements (&stLocalC, 1., 0., 0.);
			GEO_tdxHandleToMatrix hAbsoluteMatrix = HIE_fn_hGetSuperObjectGlobalMatrix (m_poParent->GetStruct());
			MTH3D_tdstVector stFirstVertex, stSecondVertex;
		
			POS_fn_vMulMatrixVertex( &stFirstVertex, hAbsoluteMatrix, &stLocalA );
			POS_fn_vMulMatrixVertex( &stSecondVertex, hAbsoluteMatrix, &stLocalC );
	
			stAB=*pstVertex;
			MTH3D_M_vSubVector(&stAB,&stAB,&stFirstVertex);
			stBC=stSecondVertex;
			MTH3D_M_vSubVector(&stBC,&stBC,pstVertex);
			stAC=stSecondVertex;
			MTH3D_M_vSubVector(&stAC,&stAC,&stFirstVertex);
			lAB = MTH3D_M_xNormVector(&stAB);
			lBC = MTH3D_M_xNormVector(&stBC);
			lAC = MTH3D_M_xNormVector(&stAC);
			lAB *= lAB;
			lBC *= lBC;
			lAC *= lAC;
			return (lAB+lBC<=lAC);
		}
		default : return TRUE;
	}
}

BOOL Cursor::fn_bCursorMove (void)
{
	BOOL bReturnCursorMove = m_bCursorMove;
	m_bCursorMove = FALSE;
	return bReturnCursorMove;
}


