// Implementation file for the definition of a characteristic type
/////////////////////////////////////////////////////////////////////
#include "StdAfx.h"

#include "scr.h"
#include "ai/aibase/gsparam.h"
#define ACTIVE_EDITOR /* we don't want optimized arrays in editors, but defining ACTIVE_EDITOR in the project's settings doesn't work... */
#include "ai/aibase/array.h"
#undef ACTIVE_EDITOR

#include "Data\CTL_DatA.hpp"

#include "WControls\CTL_WCkB.hpp"

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

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

//**************************************************************************************
CTL_Editor_ArrayData::CTL_Editor_ArrayData ( CString csScriptName,
											 CTL_Editor_DataList *_pclParentList,
											 CTL_Editor_Data *pclCurrentBaseData)
												: CTL_Editor_Data(CTL_DATA_TYPE__ARRAY,
																	csScriptName,
																	_pclParentList,
																	pclCurrentBaseData)
{	
}

//**************************************************************************************
CTL_Editor_ArrayData::~CTL_Editor_ArrayData()
{

}

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

}

//**************************************************************************************
//Function called to read associated motor's data
void CTL_Editor_ArrayData::m_fn_vGetMotorData()
{
	BOOL bMustReadData =	TRUE;
	if ( m_pro_td_p_fn_bDataMustBeRead != NULL )
		bMustReadData &=	m_pro_td_p_fn_bDataMustBeRead(this);

	if ( bMustReadData )
	{
		tdstArray * pstValue = ((tdstArray *) m_pub_fn_pvGetMotorData());

		M_ARRAY_COPY (& m_stCurrentValue, pstValue);	
	}
}

//**************************************************************************************
//Function called to update associated motor's data
void CTL_Editor_ArrayData::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 )
	{
		tdstArray * p_stValue = (tdstArray *) m_pub_fn_pvGetMotorData();
		
		M_ARRAY_COPY (p_stValue, & m_stCurrentValue);

		//Special changes
		m_fn_vDataHasBeenChanged(eReason, _lUserDefinedReason);
	}
}

//************************************************************************
//Function called to update data with another one
void CTL_Editor_ArrayData::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_ARRAY_COPY (& m_stCurrentValue, ((CTL_Editor_ArrayData *)pclSourceData) -> mfn_pstGetCurrentValue ());
	//m_fn_bSetLink(pclSourceData->m_fn_eGetLink());
	
	m_fn_vUpdateMotorData(eReason, _lUserDefinedReason);
}

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

	CString csStringToReturn;
	//TODO
	//csStringToReturn.Format("%u",(unsigned long)m_ulCurrentValue);
	m_csStringList.AddTail(csStringToReturn);

	return &m_csStringList;
}

//************************************************************************
//Function called to set the value of the data acoording to a string representing it
void CTL_Editor_ArrayData::m_fn_vSetValueWithString(CStringList *pcslValueStringList)
{
	ERROR_ASSERT(pcslValueStringList->GetCount() == 1);
	CString csValueString = pcslValueStringList->GetHead();
	
	//TODO
	//m_ulCurrentValue = atol((char *)LPCTSTR(csValueString));	

	m_fn_vUpdateMotorData();
}

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

	m_pub_fn_vSetDataHasChanged (! M_ARRAY_COMPARE (& m_stCurrentValue, & m_stOldValue));

	if ( m_pub_fn_bGetDataHasChanged() )
		M_ARRAY_COPY (& m_stOldValue, & m_stCurrentValue);
}

//Undo
//************************************************************************
//Function called to save the current Value (for Undo)
void CTL_Editor_ArrayData::m_fn_vKeepCurrentValue()
{
	M_ARRAY_COPY (& m_stKeepedValueForUndo, & m_stCurrentValue);
}
 
//************************************************************************
//Function called to validate Undo for the current change
void CTL_Editor_ArrayData::m_fn_vRegisterUndoAction()
{
/*	if ( m_bKeepedValueForUndo != m_bCurrentState )
	{
		EdActors_ActorBooleanDataModif *pclModif = new EdActors_ActorBooleanDataModif(this, 
																																									m_bKeepedValueForUndo,
																																									m_bCurrentState);
		m_pclParentActor->m_clUndoManager.AskFor(pclModif);
	}
*/
}


//************************************************************************
/*
CTL_Editor_MaskDescriptor *CTL_Editor_MaskedData::m_pub_fn_pclGetMaskdescriptor()
{
	return m_pri_pclMaskdescriptor;
}
*/

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

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

	tdstArray *pstCompareValue = & ((CTL_Editor_ArrayData *)pModifiedData)->m_stOldValue;

	return M_ARRAY_COMPARE (& m_stCurrentValue, pstCompareValue);
}

/*
Macros...
void fn_vCopyArray (tdstArray *pstSource, tdstArray *pstDest)
{
	M_ARRAY_SIZE (pstDest) = M_ARRAY_SIZE (pstSource);
	M_ARRAY_TYPE (pstDest) = M_ARRAY_TYPE (pstSource);
	memcpy (M_ARRAY_ELEMENT (pstDest, 0), M_ARRAY_ELEMENT (pstSource, 0), M_ARRAY_SIZE (pstSource) * C_SizeOfArrayElement);
}

BOOL fn_bCompareArray (tdstArray *pstArray1, tdstArray *pstArray2)
{
	if (M_ARRAY_SIZE (pstArray1) != M_ARRAY_SIZE (pstArray2)
		return FALSE;

	if (M_ARRAY_TYPE (pstArray1) != M_ARRAY_TYPE (pstArray2)
		return FALSE;

	// not quite sure about this, but ...
	// should work most of times
	return memcmp (M_ARRAY_ELEMENT (pstArray1, 0), M_ARRAY_ELEMENT (pstArray2, 0), M_ARRAY_SIZE (pstArray1) * C_SizeOfArrayElement) == 0;
}
*/