// Implementation file for the definition of an integer characteristic
///////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "Data\CTL_DatV.hpp"

#include "Others\CTL_Pri.hpp"

//External Modules
#include "CTL_ErO.hpp"
#include "MTH.h"
//End of External Modules

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//Constructor / Destructor
//************************************************************************************
CTL_Editor_VectorData::CTL_Editor_VectorData(char cDataSize,
																						 CString csScriptName,
																						 CTL_Editor_DataList *_pclParentList,
																						 CTL_Editor_Data *pclCurrentBaseData /*= NULL*/)
											: CTL_Editor_Data(CTL_DATA_TYPE__VECTOR,
																				csScriptName,
																				_pclParentList,
																				pclCurrentBaseData)
{
	m_cDataSize = cDataSize;

	m_ldCurrentValueX = 0;  
	m_ldCurrentValueY = 0;  
	m_ldCurrentValueZ = 0;  
}

//************************************************************************************
CTL_Editor_VectorData::~CTL_Editor_VectorData()
{

}

//Member functions

//**************************************************************************************
//Function called to Init the data
void CTL_Editor_VectorData::m_fn_vInitData()
{

}

//************************************************************************************
//Function called to read associated motor's data
void CTL_Editor_VectorData::m_fn_vGetMotorData()
{
	m_ldCurrentValueX = 0.0;  
	m_ldCurrentValueY = 0.0;  
	m_ldCurrentValueZ = 0.0;  

	BOOL bMustReadData =	TRUE;
	if ( m_pro_td_p_fn_bDataMustBeRead != NULL )
		bMustReadData &=	m_pro_td_p_fn_bDataMustBeRead(this);

	if ( bMustReadData )
	{
		MTH3D_tdstVector *p_tdstVector = (MTH3D_tdstVector *)m_pub_fn_pvGetMotorData();

		if(p_tdstVector)
		{
			CTL_fn_vGetMemoryForFloat(&(p_tdstVector->xX), m_ldCurrentValueX, m_cDataSize);
			CTL_fn_vGetMemoryForFloat(&(p_tdstVector->xY), m_ldCurrentValueY, m_cDataSize);
			CTL_fn_vGetMemoryForFloat(&(p_tdstVector->xZ), m_ldCurrentValueZ, m_cDataSize);
		}
	}
}

//************************************************************************************
//Function called to update associated motor's data
void CTL_Editor_VectorData::m_fn_vUpdateMotorData(CTL_tdeUpdateReason eReason,
																									long _lUserDefinedReason /*= 0*/)
{
	BOOL bMustWriteData = TRUE;
	if ( m_pro_td_p_fn_bDataMustBeWritten != NULL )
		bMustWriteData &=	m_pro_td_p_fn_bDataMustBeWritten(this);

	if ( bMustWriteData )
	{
		MTH3D_tdstVector *p_tdstVector = (MTH3D_tdstVector *)m_pub_fn_pvGetMotorData();

		CTL_fn_vSetMemoryForFloat(&(p_tdstVector->xX), m_ldCurrentValueX, m_cDataSize);
		CTL_fn_vSetMemoryForFloat(&(p_tdstVector->xY), m_ldCurrentValueY, m_cDataSize);
		CTL_fn_vSetMemoryForFloat(&(p_tdstVector->xZ), m_ldCurrentValueZ, m_cDataSize);
	
		//Special changes
		m_fn_vDataHasBeenChanged(eReason, _lUserDefinedReason);
	}
}

//************************************************************************
//Function called to update data with another one
void CTL_Editor_VectorData::m_fn_vUpdateData(CTL_Editor_Data *pclSourceData,
																						 CTL_tdeUpdateReason eReason,
																						 long _lUserDefinedReason /*= 0*/)
{
	ERROR_ASSERT(pclSourceData->m_pub_fn_tdeGetDataType() == m_pub_fn_tdeGetDataType());

	m_cDataSize = ((CTL_Editor_VectorData *)pclSourceData)->m_cDataSize;
	m_ldCurrentValueX = ((CTL_Editor_VectorData *)pclSourceData)->m_ldCurrentValueX;
	m_ldCurrentValueY = ((CTL_Editor_VectorData *)pclSourceData)->m_ldCurrentValueY;
	m_ldCurrentValueZ = ((CTL_Editor_VectorData *)pclSourceData)->m_ldCurrentValueZ;
	
	m_fn_vUpdateMotorData();

	m_fn_vLookIfDataHasBeenModified();
}	

//************************************************************************
//Function called to get a string representing the value of the data
CStringList *CTL_Editor_VectorData::m_fn_pcslFormatDataValueString()
{
	m_csStringList.RemoveAll();

	CString csStringValueX;
	csStringValueX.Format("%.3f", m_ldCurrentValueX);
	m_csStringList.AddTail(csStringValueX);
	CString csStringValueY;
	csStringValueY.Format("%.3f", m_ldCurrentValueY);
	m_csStringList.AddTail(csStringValueY);
	CString csStringValueZ;
	csStringValueZ.Format("%.3f", m_ldCurrentValueZ);
	m_csStringList.AddTail(csStringValueZ);

	return &m_csStringList;
}

//************************************************************************
//Function called to set the value of the data acoording to a string representing it
void CTL_Editor_VectorData::m_fn_vSetValueWithString(CStringList *pcslValueStringList)
{
	CString csMessage = "? ? ?";
	CString csMessage2 = "! ! !";
	ERROR_PREPARE(/*_M(g_c_csActorModuleNameForErrors,*/
									csMessage,
									"CTL_Editor_VectorData::m_fn_vSetValueWithString(...)",
									E_ERROR_GRAVITY_WARNING,
									csMessage2);
	ERROR_ASSERT( pcslValueStringList->GetCount() == 3 );

	if ( (pcslValueStringList->GetCount() == 3) )
	{
		//Extracts the three components of the Vector
		CString csIsolatedData;
		
		//X coordinate
		CString csValueString = pcslValueStringList->GetAt(pcslValueStringList->FindIndex(0));
		m_ldCurrentValueX = atof(LPCTSTR(csValueString));

		//Y coordinate
		csValueString = pcslValueStringList->GetAt(pcslValueStringList->FindIndex(1));
		m_ldCurrentValueY = atof(LPCTSTR(csValueString));

		//Z coordinate
		csValueString = pcslValueStringList->GetAt(pcslValueStringList->FindIndex(2));
		m_ldCurrentValueZ = atof(LPCTSTR(csValueString));
	}
	else
	{
		m_ldCurrentValueX = 0.0;
		m_ldCurrentValueY = 0.0;
		m_ldCurrentValueZ = 0.0;
	}

	m_fn_vUpdateMotorData();
}

//************************************************************************
//Function called to look if data has been modified (by motor for example)
void CTL_Editor_VectorData::m_fn_vLookIfDataHasBeenModified()
{
	m_fn_vGetMotorData();

	m_pub_fn_vSetDataHasChanged	(		( m_ldCurrentValueX != m_ldOldValueX )
															||	( m_ldCurrentValueY != m_ldOldValueY )
															||	( m_ldCurrentValueZ != m_ldOldValueZ )
															);

	if ( m_pub_fn_bGetDataHasChanged() )
	{
		m_ldOldValueX = m_ldCurrentValueX;
		m_ldOldValueY = m_ldCurrentValueY;
		m_ldOldValueZ = m_ldCurrentValueZ;
	}
}

//Undo
//************************************************************************
//Function called to save the current Value (for Undo)
void CTL_Editor_VectorData::m_fn_vKeepCurrentValue()
{
	m_ldKeepedValueForUndoX = m_ldCurrentValueX;
	m_ldKeepedValueForUndoY = m_ldCurrentValueY;
	m_ldKeepedValueForUndoZ = m_ldCurrentValueZ;
}
 
//************************************************************************
//Function called to validate Undo for the current change
void CTL_Editor_VectorData::m_fn_vRegisterUndoAction()
{
/*	if ( m_ldKeepedValueForUndo != m_ldCurrentValue )
	{
		EdActors_ActorFloatDataModif *pclModif = new EdActors_ActorFloatDataModif(this, 
																																							m_ldKeepedValueForUndo,
																																							m_ldCurrentValue);
		m_pclParentActor->m_clUndoManager.AskFor(pclModif);
	}
*/
}

//##################################################################"
//************************************************************************
//Constructor
/*EdActors_ActorFloatDataModif::EdActors_ActorFloatDataModif(	CTL_Editor_Data *pclData,
																													long double ldOldValue,
																													long double ldNewValue)
															: EdActors_ActorDataModif(pclData)
{
	m_ldNewValue = ldNewValue;
	m_ldKeepedValue = ldOldValue;

	CString csName;
	csName.Format("%s : %.3f --> %.3f", pclData->m_csScriptName,
																			ldOldValue,
																			ldNewValue);
	SetName(csName);
}

//************************************************************************
//Destructor
EdActors_ActorFloatDataModif::~EdActors_ActorFloatDataModif()
{

}

//************************************************************************
BOOL EdActors_ActorFloatDataModif::Do()
{
	((CTL_Editor_VectorData *)m_pclData)->m_ldCurrentValue = m_ldNewValue;

	m_pclData->m_fn_vUpdateMotorData();
	m_pclData->m_bDataHasChanged = TRUE;
	m_pclData->m_pclParentControl->m_fn_vUpdate();

	m_pclData->m_pclParentActor->m_pclActor->fn_vNotifySave();

	return TRUE;
}

//************************************************************************
BOOL EdActors_ActorFloatDataModif::Undo()
{
	((CTL_Editor_VectorData *)m_pclData)->m_ldCurrentValue = m_ldKeepedValue;
	
	m_pclData->m_fn_vUpdateMotorData();
	m_pclData->m_bDataHasChanged = TRUE;
	m_pclData->m_pclParentControl->m_fn_vUpdate();

	return TRUE;
}
*/

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method	: mfn_bEqualsModifiedDataOldValue 
// Date		: 98/06/24
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description	:
// Author		: Stegaru Cristian - CPA
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification	: 
// Date			:
// By			:
//////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL CTL_Editor_VectorData::mfn_bEqualsModifiedDataOldValue (CTL_Editor_Data *pModifiedData)
{
	ASSERT (pModifiedData);

	ERROR_ASSERT(pModifiedData->m_pub_fn_tdeGetDataType() == m_pub_fn_tdeGetDataType());

	return	m_ldCurrentValueX == ((CTL_Editor_VectorData *)pModifiedData)->m_ldOldValueX
			&& m_ldCurrentValueY == ((CTL_Editor_VectorData *)pModifiedData)->m_ldOldValueY
			&& m_ldCurrentValueZ == ((CTL_Editor_VectorData *)pModifiedData)->m_ldOldValueZ;
}

