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

#include "stdafx.h"
#include "acp_base.h"
#include "itf.h"
#include "lightobj.hpp"
#include "CurObj.hpp"
#include "Inter.hpp"

//----------- engine structure
static ACP_tdxHandleOfCursor fn_hConstructCursor (void)
{
	return NULL;
}
//----------- 

DeclareTemplateStatic(ACP_tdxHandleOfCursor);

void del(ACP_tdxHandleOfCursor h)
{
}

//------ static
void Cursor::fn_vInitObject (void)
{
	CPA_EdMot<ACP_tdxHandleOfCursor>::Init (fn_hConstructCursor, NULL, del);
}

					  
Cursor::Cursor(CPA_MainWorld *p_oMainWorld, CPA_ObjectDLLBase *p_oDLL, CPA_ReachableObject *p_oOwner ):
	CPA_Object(CPA_ProtectedObject, p_oMainWorld, p_oDLL, "LightCursor", p_oOwner, C_ucLightCursor, "LightCursor1", "", ""),
	CPA_EdMot<ACP_tdxHandleOfCursor>()
{

	CPA_SuperObject* poSuperCursor = new CPA_SuperObject (GetInterface()->GetInterface()->GetMainWorld(), FALSE, "", "", CPA_NoSave );
	poSuperCursor->SetTypeSO (C_Protected);
	poSuperCursor->SetObject(this);
	SetSuperObject (poSuperCursor);

	// Est -ce bien à moi de le faire !!!!
	POS_tdstCompletePosition *p_stMatrix;
	p_stMatrix = (POS_tdstCompletePosition *) malloc (sizeof (POS_tdstCompletePosition));
	POS_fn_vSetIdentityMatrix(p_stMatrix);

	HIE_fn_vSetSuperObjectMatrix(poSuperCursor->GetStruct(), p_stMatrix);
	POS_fn_vSetIdentityMatrix ( HIE_fn_hGetSuperObjectMatrix(poSuperCursor->GetStruct()) );
	poSuperCursor->GetStruct()->hGlobalMatrix = (POS_tdstCompletePosition *) malloc (sizeof(POS_tdstCompletePosition));
	POS_fn_vSetIdentityMatrix ( HIE_fn_hGetSuperObjectGlobalMatrix(poSuperCursor->GetStruct()) );

	// init
	MTH3D_M_vNullVector(&m_stVertex);
	m_xDim=(float)0.05;
	m_poParent = (CPA_SuperObject*)p_oOwner;

	// 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); 
}

Light_Interface* Cursor::GetInterface (void)
{
	return (Light_Interface*)GetDLL(); 
}

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) ;
}
	
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;
	fn_vGetAbsoluteVertex ( &stVertex );
	MTH3D_M_vAddVector(&stVertex, &stVertex, pstTranslation);
	fn_vSetAbsoluteVertex ( &stVertex );
	fn_vDraw ();
	GetInterface()->GetInterface()->fn_vUpdateAll (E_mc_JustDraw);
}
