///////////////////////////////////////////////////////////////////////
// The CallBack Functions called by controls when a Data has changed //
///////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
//ANNECY CHRISTOPHE MODIFICATIONS
#define D_ZdxStuff_StructureDefine
//ANNECY END CHRISTOPHE MODIFICATIONS
#include "EDACClBk.hpp"

#include "EDACActr.hpp"
#include "_AInterf.hpp"
#include "EDACStrg.hpp"
#include "CPA_DatS.hpp"

//External Modules
#include "CTL.h"
#include "ErO.h"
//End of External Modules

#undef CPA_WANTS_IMPORT
#undef CPA_EXPORT
#define CPA_WANTS_EXPORT
    #include "_Actors.hpp"
#undef CPA_WANTS_EXPORT
#define CPA_WANTS_IMPORT


//***********************************************************************
void OAC_fn_vCurrentObjectTableHasChanged(class CTL_Editor_Data *_pclChangedData,
																					enum CTL_eUpdateReason _eReason,
																					long _lUserDefinedReason /*= 0*/)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because current Object Table has been changed",
									"EDACT_fn_vCurrentObjectTableHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( _pclChangedData->m_pub_fn_tdeGetDataType() == CTL_DATA_TYPE__ENUM );

	//Sets Actor's pointer on the current Object Table
	CString csOTName = ((CTL_Editor_EnumData *)_pclChangedData)->m_pclCurrentValue->m_pub_fn_csGetElementName();
	CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();

 	EditorObjectTable *pclOT = (EditorObjectTable *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csOTName, C_szObjectTableTypeName, pclOwnerFamily);

	OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetCurrentObjectTable(pclOT);
}

//***********************************************************************
void OAC_fn_vInitialObjectTableHasChanged(class CTL_Editor_Data *_pclChangedData,
																					enum CTL_eUpdateReason _eReason,
																					long _lUserDefinedReason /*= 0*/)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because initial Object Table has been changed",
									"EDACT_fn_vInitialObjectTableHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( _pclChangedData->m_pub_fn_tdeGetDataType() == CTL_DATA_TYPE__ENUM );

	//Sets Actor's pointer on the current Object Table
	CString csOTName = ((CTL_Editor_EnumData *)_pclChangedData)->m_pclCurrentValue->m_pub_fn_csGetElementName();
	CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();

	CPA_BaseObject *pObject = g_pclInterface->GetMainWorld()->fn_p_oFindObject(csOTName, C_szObjectTableTypeName, pclOwnerFamily);
	EditorObjectTable *pclOT = (EditorObjectTable *) pObject;

	if (pObject && pObject->GetData() == NULL)
	{
		pObject->GetEditor()->fn_bLoadBaseObject(pObject);
		((CTL_Editor_EnumData *)_pclChangedData)->m_pclCurrentValue->m_pub_fn_vSetValue((long) pObject->GetData());
		_pclChangedData->m_fn_vUpdateMotorData(_eReason, _lUserDefinedReason);
	}

	OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetInitialObjectTable(pclOT);
}

//***********************************************************************
void OAC_fn_vCurrentActionHasChanged(class CTL_Editor_Data *_pclChangedData,
																		 enum CTL_eUpdateReason _eReason,
																		 long _lUserDefinedReason /*= 0*/)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because current Action has been changed",
									"EDACT_fn_vCurrentActionHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( _pclChangedData->m_pub_fn_lGetUSerDefinedDataType() == OAC_DATA_TYPE__STATE );

	//Sets Actor's pointer on the current Action
	if ( ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForActions != NULL )
	{
		CString csName = ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForActions->m_pub_fn_csGetElementName();
		CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();
		
		CPA_Action *pclAction = (CPA_Action *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csName, C_szActionTypeName, pclOwnerFamily);

		OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetCurrentAction(pclAction);
	}

	//Sets Actor's pointer on the current State
	if ( ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForStates != NULL )
	{
		CString csName = ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForStates->m_pub_fn_csGetElementName();
		CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();
		
		CPA_State *pclState = (CPA_State *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csName, C_szStateTypeName, pclOwnerFamily);

		OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetCurrentState(pclState);
	}
}

//***********************************************************************
void OAC_fn_vInitialActionHasChanged(class CTL_Editor_Data *_pclChangedData,
																		 enum CTL_eUpdateReason _eReason,
																		 long _lUserDefinedReason /*= 0*/)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because initial Action has been changed",
									"EDACT_fn_vInitialActionHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( _pclChangedData->m_pub_fn_lGetUSerDefinedDataType() == OAC_DATA_TYPE__STATE );
	
	//Sets Actor's pointer on the current Action
	if ( ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForActions != NULL )
	{
		CString csName = ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForActions->m_pub_fn_csGetElementName();
		CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();
		
 		CPA_Action *pclAction = (CPA_Action *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csName, C_szActionTypeName, pclOwnerFamily);
		
		OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetInitialAction(pclAction);
	}
	
	//Sets Actor's pointer on the current State
	if ( ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForStates != NULL )
	{
		CString csName = ((CPA_Editor_StateData *)_pclChangedData)->m_pclCurrentValueForStates->m_pub_fn_csGetElementName();
		CPA_BaseObject *pclOwnerFamily = (CPA_BaseObject *)OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_pclGetFamily();
		
		CPA_State *pclState = (CPA_State *)g_pclInterface->GetMainWorld()->fn_p_oFindObject(csName, C_szStateTypeName, pclOwnerFamily);
		
		OAC_fn_pclGetParentActorOfData(_pclChangedData)->m_pclActor->m_pub_fn_vSetInitialState(pclState);
	}
}

//***********************************************************************
void OAC_fn_vCallBackDynamicsObjectTypeHasChanged(class CTL_Editor_Data *_pclChangedData,
																									enum CTL_eUpdateReason _eReason,
																									long _lUserDefinedReason /*= 0*/)
{
	struct tdstDynam_ *p_stDynam = (struct tdstDynam_ *)OAC_fn_pclGetParentAMSOfData(_pclChangedData)->m_fn_pvGetBaseAddress();
	fn_v_DynamUpdateDNMObjectTypeFromMS(p_stDynam);
}

//ANNECY CT 13/01/98 {  
// Local Light flag have been changed
//***********************************************************************
void EDACTORS_fn_vCallBackInitialLocalLightHasChanged(CTL_Editor_Data *pclChangedData, enum CTL_eUpdateReason _eReason, long _lUserDefinedReason = 0)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because Local Light has been changed",
									"EDACTORS_fn_vCallBackLocalLightHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( pclChangedData->m_pub_fn_tdeGetDataType() == CTL_DATA_TYPE__BOOLEAN );


	if (_eReason == CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER) { 
		unsigned char bLocalLight = 
			((CTL_Editor_BooleanData *)pclChangedData)->m_bCurrentState;
		HIE_tdxHandleToSuperObject hCharacter = 
			(HIE_tdxHandleToSuperObject)OAC_fn_pclGetParentActorOfData(pclChangedData)->m_pclActor->m_pub_fn_pclGetSuperObject()->GetSuperObjectStruct();
		HIE_tdxHandleToSuperObject hSector;
		MS_tdxHandleToMSLight hMSLight = M_GetMSHandle(hCharacter,MSLight);

		// get charcter's sector
		hSector = GAM_fn_hGetCurrentSector(hCharacter);

		if (bLocalLight) {
			CTL_Editor_Data *pclOtherData;
			
			// remove from dyn light list
			SECT_fn_vIsolateDNMLightsListNode(fn_hMSLightGetLightNodeInSector(hMSLight));

			// update super object struct
			HIE_fn_vSetSuperObjectExcluLight(hCharacter,*fn_p_ucMSLightGetOnlyLocalInit(hMSLight));

			// update Ministructure, set other flag writable
			pclOtherData = OAC_fn_pclGetParentActorOfData(pclChangedData)->m_fn_p_oFindDataByName("OnlyLocalLight_Init");
			((CTL_Editor_BooleanControl *)pclOtherData->m_pub_fn_pclGetParentControl())->m_fn_vMakeReadWrite();
		}
		else {
			CTL_Editor_Data *pclOtherData;
			
			// update super object struct
			HIE_fn_vSetSuperObjectExcluLight(hCharacter,FALSE);

			// update Ministructure in motor
			*fn_p_ucMSLightGetOnlyLocalInit(hMSLight) = FALSE;
			// update Ministructure in editor and set other flag FALSE & read only
			pclOtherData = OAC_fn_pclGetParentActorOfData(pclChangedData)->m_fn_p_oFindDataByName("OnlyLocalLight_Init");
			((CTL_Editor_BooleanData *)pclOtherData)->m_bCurrentState=FALSE;
			pclOtherData->m_pub_fn_pclGetParentControl()->m_fn_vUpdate();
			((CTL_Editor_BooleanControl *)pclOtherData->m_pub_fn_pclGetParentControl())->m_fn_vMakeReadOnly();

			// add in dyn light list
			if (hSector) {
				SECT_fn_vIsolateDNMLightsListNode(fn_hMSLightGetLightNodeInSector(hMSLight));
				SECT_fn_vAddTailDNMLightsListNode(hSector,fn_hMSLightGetLightNodeInSector(hMSLight));
			}
		}

		// redraw the display window
		g_pclInterface->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);

	}
}

// Only Local Light flag have been changed
//***********************************************************************
void EDACTORS_fn_vCallBackInitialOnlyLocalLightHasChanged(CTL_Editor_Data *pclChangedData, enum CTL_eUpdateReason _eReason, long _lUserDefinedReason = 0)
{
	ERROR_PREPARE_M(g_c_csActorModuleNameForErrors,
									"Into CallBack because Only Local Light has been changed",
									"EDACTORS_fn_vCallBackLocalOnlyLightHasChanged(...)",
									E_ERROR_GRAVITY_FATAL,
									"Data is not of the right type");
	ERROR_ASSERT( pclChangedData->m_pub_fn_tdeGetDataType() == CTL_DATA_TYPE__BOOLEAN );


	if (_eReason == CTL_UPDATE_REASON__DATA_MODIFIED_BY_USER) { 
		unsigned char bOnlyLocalLight = 
			((CTL_Editor_BooleanData *)pclChangedData)->m_bCurrentState;
		HIE_tdxHandleToSuperObject hCharacter = 
			(HIE_tdxHandleToSuperObject)OAC_fn_pclGetParentActorOfData(pclChangedData)->m_pclActor->m_pub_fn_pclGetSuperObject()->GetSuperObjectStruct();
		HIE_tdxHandleToSuperObject hSector;
		MS_tdxHandleToMSLight hMSLight = M_GetMSHandle(hCharacter,MSLight);

		// get charcter's sector
		hSector = GAM_fn_hGetCurrentSector(hCharacter);

		// update effective only if light is local
		if (*fn_p_ucMSLightGetLocalLightInit(hMSLight)) {
			HIE_fn_vSetSuperObjectExcluLight(hCharacter,bOnlyLocalLight);
	
			// redraw the display window
			g_pclInterface->GetInterface()->fn_vUpdateAll(E_mc_JustDraw);
		}


	}
}
//ENDANNECY CT }
