/*
///////////////////////////////////////////////////////////////////////////////////////////////////
// Description : LinkBez.cpp
//   
//  Edit Bezier Link            
//
///////////////////////////////////////////////////////////////////////////////////////////////////
// inherit from  :	Link
///////////////////////////////////////////////////////////////////////////////////////////////////
// Creation date: 29 jan 1997                        Author: J Thénoz
///////////////////////////////////////////////////////////////////////////////////////////////////
// Modification date:			                      Author: 
// 
//
// 
//
///////////////////////////////////////////////////////////////////////////////////////////////////
*/
//ANNECY Shaitan Nettoyage (12/05/98) {
/*

#include "stdafx.h"
#include "acp_base.h"
#include "resource.h"
#include "incitf.h"
#include "incgam.h"
#define _ENG_BEZ_FRIEND_
	#include "incai.h"
#undef _ENG_BEZ_FRIEND_
#include "WpObj.hpp"
#include "WayObj.hpp"
#include "LinkObj.hpp"
#include "CurObj.hpp"
#include "Link2D.hpp"
#include "TanObj.hpp"
#include "LinkBez.hpp"
#include "LkBezDia.h"
#include "Inter.hpp"



//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 

char LinkBez::ms_cType = WP_ConnType_ucBezier;						
char LinkBez::ms_szName [10] = "Bezier";				

int iJustToLinkBezBidouille;
void fn_vJustToLinkBez (void) {   iJustToLinkBezBidouille=1;  }

void LinkBez::fn_vRegisterObject (void)
{
	tdst_LinkObject* pstLinkObject = new tdst_LinkObject;
	strcpy ( pstLinkObject->szName, ms_szName );
	pstLinkObject->cType = ms_cType;
	pstLinkObject->fn_pConstruct = fn_pConstruct;
	ms_oListLinkObject.AddTail (pstLinkObject);
	fn_vJustToLinkBez ();
}

LinkBez::LinkBez ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner, WP_tdhLink hLink )
	: Link ( p_oDLL, p_oOwner, hLink )
{
	MTH3D_tdstVector stNulVector = { 0, 0, 0 };
	hLink = (WP_tdhLink)GetEngineStruct();
	
	ACP_tdstDynaParam stDynaParam;
	WP_fnv_Link_GetDynamicParameter (hLink,&stDynaParam);
	
//-----------------------------------------------
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hBezierObject.CreateWithCurve
	(
		WP_fnv_Link_GetConnectionTypeBezierCurve (hLink),
		EDWAY_C_ucModeNoObject,
		NULL,
		&WP_M_hLinkGetEngineBezierObject(hLink)->m_dstListOfPoints
	);
	WP_fnv_Link_SetCurrentSample (hLink, 15);
	m_hBezierObject.ChangeSamplingRate(15);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
//-----------------------------------------------

	m_pstStartTangent = new Tangent ( p_oDLL, this, eVariableLenghtTangent, eFixOrientationTangent, eControlUpChangeNeighbour );				
	m_pstFinishTangent = new Tangent ( p_oDLL, this, eVariableLenghtTangent, eFixOrientationTangent, eControlUpChangeNeighbour );

	m_bFirstEdition = TRUE;
}



LinkBez::LinkBez ( CPA_ObjectDLLBase *p_oDLL, CPA_BaseObject *p_oOwner )
	: Link ( p_oDLL, p_oOwner )
{
	MTH3D_tdstVector stNulVector = { 0, 0, 0 };
	WP_tdhLink hLink = (WP_tdhLink)GetEngineStruct();
	
	// allocate bezier structure
	WP_fnv_Link_Allocate
	(
		hLink,
		WP_ConnType_ucBezier,
		WP_fneuc_Link_GetConnectionDynamicType (hLink)
	);

	
	ACP_tdstDynaParam stDynaParam;
	WP_fnv_Link_GetDynamicParameter (hLink,&stDynaParam);
	
//-------------------------------------------
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hBezierObject.CreateWithCurve
	( 
		WP_fnv_Link_GetConnectionTypeBezierCurve (hLink),
		EDWAY_C_ucModeNoObject,
		NULL,
		&WP_M_hLinkGetEngineBezierObject(hLink)->m_dstListOfPoints
	);
	// init sampling rate values
	WP_fnv_Link_SetCurrentSample (hLink, 15);
	m_hBezierObject.ChangeSamplingRate(15);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
//-------------------------------------------

	// c'est comme ça -> pour allouer m_dstListOfPoints j'ai pas trouvé autre chose
//	WP_fnv_Link_ChangeConnectionTypeBezier( hLink, EDWAY_fnh_BezierObject_GetBezierCurve(m_hBezierObject) );

	// TEST
//	ENG_fnv_BezierObject_BuildListOfPoints( WP_M_hLinkGetEngineBezierObject(hLink) );

//	((ENG_tdstBezierObject) WP_M_hLinkGetEngineBezierObject(hLink))->m_dstListOfPoints = NULL;

//	ENG_fnv_BezierObject_SetListOfPoints ( WP_M_hLinkGetEngineBezierObject(hLink), NULL );

//	EDWAY_fnv_BezierObject_Calculate (m_hBezierObject);

	m_pstStartTangent = new Tangent ( p_oDLL, this, eVariableLenghtTangent, eFixOrientationTangent, eControlUpChangeNeighbour );				
	m_pstFinishTangent = new Tangent ( p_oDLL, this, eVariableLenghtTangent, eFixOrientationTangent, eControlUpChangeNeighbour );
	
	m_bFirstEdition = TRUE;
}


	
void LinkBez::fn_vConstructGraphic (void)
{
	Link::fn_vConstructGraphic();
	m_pstStartTangent->GetSuperObject()->SetSuperObjectOwner (GetSuperObject());	
	m_pstFinishTangent->GetSuperObject()->SetSuperObjectOwner (GetSuperObject());	
}

void LinkBez::fn_vRotationGraphic (BOOL bRotation)
{
	Link::fn_vRotationGraphic (bRotation);
	if (bRotation)
	{
		GetInterface()->GetInterface()->fn_bDeleteObjectInHierarchy (m_pstStartTangent->GetSuperObject(), FALSE, FALSE, FALSE, FALSE );
		GetInterface()->GetInterface()->fn_bDeleteObjectInHierarchy (m_pstFinishTangent->GetSuperObject(), FALSE, FALSE, FALSE, FALSE );
	}
	else
	{
		GetInterface()->GetInterface()->fn_bInsertObjectInHierarchy (m_pstStartTangent->GetSuperObject(), GetSuperObject(),FALSE,FALSE,FALSE);
		GetInterface()->GetInterface()->fn_bInsertObjectInHierarchy (m_pstFinishTangent->GetSuperObject(), GetSuperObject(),FALSE,FALSE,FALSE);
	}
}


void LinkBez::fn_vInitGraphic (void)
{
	Link::fn_vInitGraphic ();
	
	if (!m_bFirstEdition) return;
	m_bFirstEdition = FALSE;
	
	// set the vertex of the Tangent object
	MTH3D_tdstVector stFirstFixVertex, stSecondFixVertex;
	MTH3D_tdstVector stFirstMoveVertex, stSecondMoveVertex;
	MTH3D_tdstVector u;
	MTH3D_tdstVector stStartPoint, stEndPoint;
	MTH3D_tdstVector stStartVector, stEndVector;

	
	WP_fnv_WayPoint_ComputeLocation (  (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stFirstFixVertex );
	WP_fnv_WayPoint_ComputeLocation (  (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stSecondFixVertex );

	stFirstMoveVertex = stFirstFixVertex;
	stSecondMoveVertex = stSecondFixVertex;
	
	
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//	EDWAY_fnv_BezierObject_GetParams (m_hBezierObject,&stStartPoint, &stEndPoint, &stStartVector, &stEndVector);
	m_hBezierObject.GetParams (&stStartPoint, &stEndPoint, &stStartVector, &stEndVector);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 


	
	if ( (MTH3D_M_xNormVector(&stStartVector)>1e-10) && (MTH3D_M_xNormVector(&stEndVector)>1e-10) )
	{
		// init with load values
		MTH3D_M_vAddVector(&stFirstMoveVertex,&stFirstMoveVertex,&stStartVector);    
		MTH3D_M_vAddVector(&stSecondMoveVertex,&stSecondMoveVertex,&stEndVector);    
	}
	else 
	{
		// set new init values
		u = stSecondFixVertex;
		MTH3D_M_vSubVector(&u, &u, &stFirstFixVertex );
		MTH3D_M_vMulScalarVector(&u,0.15f,&u);

		MTH_tdxReal xLenght = MTH3D_M_xNormVector (&u);
		if (xLenght<0.1f) MTH3D_M_vMulScalarVector ( &u, 0.1f/xLenght, &u);

		MTH3D_M_vAddVector(&stFirstMoveVertex,&stFirstMoveVertex,&u);    
		MTH3D_M_vMulScalarVector(&u,-1,&u);
		MTH3D_M_vAddVector(&stSecondMoveVertex,&stSecondMoveVertex,&u);
	}

	GetSuperObject()->AddTail ( m_pstStartTangent->GetSuperObject() );
	GetSuperObject()->AddTail ( m_pstFinishTangent->GetSuperObject() );
	
	
	GetInterface()->GetInterface()->fn_vCancelCurrentSelection (FALSE);

	m_pstStartTangent->fn_vSetAbsoluteVertex ( &stFirstFixVertex, &stFirstMoveVertex );
	m_pstFinishTangent->fn_vSetAbsoluteVertex ( &stSecondFixVertex, &stSecondMoveVertex );

}

void LinkBez::fn_vDrawNormal (void)
{
	fn_vComputeAxis ();
	m_pstStartTangent->fn_vDraw ();
	m_pstFinishTangent->fn_vDraw ();
	fn_vSetEngineStructure ();
}


void LinkBez::fn_vDrawOutHierarchy (GLD_tdstViewportAttributes *pViewportAttributes)
{
	fn_vSetEngineStructure ();

//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hBezierObject.SetViewPortAttributes( pViewportAttributes );
	m_hBezierObject.SetColor (0xFFFFFFFF );
	m_hBezierObject.Draw();
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 


}

CDialog* LinkBez::fn_pGetSpecificDialog (CWnd* pParentWnd)
{
	BezierDia* pDialog = new BezierDia (this);
	pDialog->Create ( IDD_BEZIER_DIALOG, pParentWnd );
	pDialog->fn_vTutRegister ();
	return (CDialog*) pDialog;
}

void LinkBez::fn_vFreeSpecificDialog (CDialog* pDialog)
{
	((BezierDia*)pDialog)->fn_vTutUnregister ();
}


void LinkBez::fn_vSetEngineStructure (void)
{
	MTH3D_tdstVector	stStartPoint;
	MTH3D_tdstVector	stEndPoint;
	MTH3D_tdstVector	stStartVector;
	MTH3D_tdstVector	stEndVector;

	m_pstStartTangent->fn_pGetAbsoluteTangent (&stStartVector);
	m_pstFinishTangent->fn_pGetAbsoluteTangent (&stEndVector);
	
	
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poFirstWP->GetEngineStruct()), &stStartPoint );
	WP_fnv_WayPoint_ComputeLocation ( (WP_tdhWayPoint)(m_poSecondWP->GetEngineStruct()), &stEndPoint );

//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
	m_hBezierObject.ChangeParams(&stStartPoint, &stEndPoint, &stStartVector, &stEndVector);
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 


}

void LinkBez::fn_vChangeNeighbourTangent (Tangent* pTangent)
{
	Link::fn_vChangeNeighbourTangent (pTangent);
	
	Link* pChangeLink;
	if (m_pstStartTangent==pTangent)
	{
		pChangeLink = m_poParent->fn_pPrevLink (this);
		if (pChangeLink) 
			if (pChangeLink->fn_cGetType()==WP_ConnType_ucBezier) 
		{
			MTH3D_tdstVector stTangent;
			pTangent->fn_pGetAbsoluteTangent(&stTangent);
			((LinkBez*)pChangeLink)->fn_vSetTangent ( 2, &stTangent );   
			pChangeLink->fn_vDraw ();
		}
	}
	else
	{
		pChangeLink = m_poParent->fn_pNextLink (this);
		if (pChangeLink) 
			if (pChangeLink->fn_cGetType()==WP_ConnType_ucBezier) 
		{
			MTH3D_tdstVector stTangent;
			pTangent->fn_pGetAbsoluteTangent(&stTangent);
			((LinkBez*)pChangeLink)->fn_vSetTangent ( 1, &stTangent );   
			pChangeLink->fn_vDraw ();
		}
	}
}

void LinkBez::fn_vSetTangent ( int iSide, MTH3D_tdstVector* pTangentVertex )
{
	switch (iSide)
	{
	case 1 : m_pstStartTangent->fn_vSetTangentDirection (pTangentVertex,TRUE); break;
	case 2 : m_pstFinishTangent->fn_vSetTangentDirection (pTangentVertex,TRUE); break;
	}
}

void LinkBez::fn_vGetSaveInfo ( char dszParam [20][20], long& lNbParam )
{
	unsigned char ucSamplingRate;
	MTH3D_tdstVector stFirstPoint, stSecondPoint, stFirstVector, stSecondVector;

	ucSamplingRate = WP_fnuc_Link_GetCurrentSample (GetStruct());
//ROMTEAM WorldEditor (Viorel Preoteasa 20/01/98) 
//	EDWAY_fnv_BezierObject_GetParams ( m_hBezierObject, &stFirstPoint, &stSecondPoint, &stFirstVector, &stSecondVector );
	m_hBezierObject.GetParams (&stFirstPoint, &stSecondPoint, &stFirstVector, &stSecondVector );
//ENDROMTEAM WorldEditor (Viorel Preoteasa) 
	
	strcpy ( dszParam [0], "Bezier" );
	lNbParam = 2;

	sprintf ( dszParam[lNbParam++], "%d", ucSamplingRate); 

	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetXofVector(&stFirstVector) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetYofVector(&stFirstVector) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetZofVector(&stFirstVector) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetXofVector(&stSecondVector) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetYofVector(&stSecondVector) );
	sprintf ( dszParam[lNbParam++], "%f", MTH3D_M_xGetZofVector(&stSecondVector) );
}


void LinkBez::fn_vInitEditorFromEngine (void)
{
}

*/
//ENDANNECY Shaitan Nettoyage }
