/*=========================================================================
* 
* LPSobj.cpp :  LipSync_Object class - implementation
* 
*
* Version			1.0
* Creation date		16/08/97	
* Revision date		18/08/97
*
* Shaitan
*=======================================================================*/


#include "stdafx.h"
#define HieFriend
#include "ACP_Base.h"
#include "DPT.h"

#include "incGAM.h"
#include "GAM/LipsSync.h"
#include "GAM/LoadLSyn.h"

#include "LPSobj.hpp"
#include "LPSinter.hpp"
#include "TLS/LPSconst.hpp"

#include "x:\cpa\main\inc\_editid.h"


/*=============================================================================
	Engine struct
=============================================================================*/
DeclareTemplateStatic(tdxHandleOfLipsSynchroTable);

void del (tdxHandleOfLipsSynchroTable h)
{

}


/*===========================================================================
*		Description:	Static init (engine struct)
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
void LipSync_Object::StaticInit (void)
{
    CPA_EdMot<tdxHandleOfLipsSynchroTable>::Init(NULL/*fn_xAllocateLipsSynchroTable*/, NULL, del);
}


/*===========================================================================
*		Description:	Constructor
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan        
*=========================================================================*/
LipSync_Object::LipSync_Object (LipSync_Interface			*pEditor, 
								tdxHandleOfLipsSynchroTable hLipSynchro, 
								CString						csName, 
								CString						csFamilyName, 
								CPA_BaseObject				*pFamily
								)
	
	: CPA_SaveObject (pEditor,						// owner editor
					  C_szLipSyncTypeName,			// type
					  E_ss_NoSave,					// status
					  pFamily,						// owner
					  (hLipSynchro != NULL),		// validity
					  fn_szGetSyncLipsDataPath(),	// data path
					  NULL							// callback for save
					  ),
	  
	  CPA_EdMot<tdxHandleOfLipsSynchroTable> (hLipSynchro)

{
	CString csFileName;
	char	szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	
	// get referenced section name
	csFileName += csFamilyName + "\\" + csName;
	SCR_fn_v_RdL0_ComputeSectionName(szSectionName, (char *)(LPCSTR)csFileName, C_SectionA3dLSData, "");
    // set parameters
	SetReferencedSectionName(szSectionName);
	SetSectionData (this);
    SetExistingSection (TRUE);
	// init family
	m_pFamily = pFamily;

    // Name
    if (fn_eRename(csName) != E_mc_None)
        SetDefaultValidName();
    fn_vUpdateSectionName();

	// only loaded lipsynchro are valid
	if (hLipSynchro == NULL)
		fn_bUnValidate();
}


/*===========================================================================
*		Description:	Destructor
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
LipSync_Object::~LipSync_Object (void)
{

}

/*===========================================================================
*		Description:	Engine struct
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
void LipSync_Object::SetStruct (tdxHandleOfLipsSynchroTable hLipSynchro)
{
    // update data
	fn_vUpdateData(hLipSynchro);
	
	// set struct
	CPA_EdMot<tdxHandleOfLipsSynchroTable>::SetStruct(hLipSynchro);
}


/*===========================================================================
*		Description:	Section Name  
*						 => FamilyName / Name.SYN ^ C_SectionA3dLSData
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan      
*=========================================================================*/
void LipSync_Object::fn_vUpdateSectionName (void)
{
	CString csSectionName, csFileName;
	char	szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	int		iPos;
	
	// get old section name
	csSectionName = GetReferencedSectionName();
	// build new section name
	iPos = csSectionName.ReverseFind('\\');
	csFileName = csSectionName.Left(iPos+1) + GetName();
	SCR_fn_v_RdL0_ComputeSectionName(szSectionName, (char *)(LPCSTR)csFileName, C_SectionA3dLSData, "");
	csSectionName = szSectionName;
	// set new section name
	SetReferencedSectionName(csSectionName);
}


/*===========================================================================
*		Description:	Name To Draw 
*						 => Family Name + Name  
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		     
*=========================================================================*/
CString LipSync_Object::GetNameToDraw (void)
{	
	CString csName;
	
	csName = m_pFamily->GetName() + "   " + GetName();
	
	return csName;
}


/*===========================================================================
*		Description:	Load engine struct
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author:	Shaitan	     
*=========================================================================*/
void LipSync_Object::fn_vLoadEngineStruct (void)
{
	tdxHandleOfLipsSynchroTable hLipSynchro;
	SCR_tdst_Cxt_Values			*p_stValues;
	CPA_DLLBase					*pFamilyDLL;
	CString						csSectionName, csText;

	if (!fn_bIsAvailable())
    {
		// get section name
		csSectionName = GetCompleteSectionName();
		// get family parameter
		if (!m_pFamily->fn_bIsAvailable())
		{
			// get family to load
			pFamilyDLL = GetMainWorld()->GetToolDLLWithName(C_szDLLFamilyName);
			csText.Format("Loading family %s", m_pFamily->GetName());
			M_GetMainWnd()->UpdateStatus((char*)(LPCSTR)csText, C_STATUSPANE_INFOS, C_STATUS_NORMAL);
			// load family
			pFamilyDLL->fn_bLoadBaseObject(m_pFamily);
			csText.Format("Family %s loaded", m_pFamily->GetName());
			M_GetMainWnd()->UpdateStatus((char*)(LPCSTR)csText, C_STATUSPANE_INFOS, C_STATUS_NORMAL);
		}
		SCR_M_RdL0_SetContextLong(C_GrandChildContext,1,(long)m_pFamily->GetData());
	
		// if section was already loaded, get value from LinkTable
		p_stValues = SCR_fnp_st_RdL0_AnalyseSection((char*)(LPCTSTR) csSectionName, SCR_CDF_uw_Anl_Normal);
		hLipSynchro = (tdxHandleOfLipsSynchroTable) (p_stValues->a_ulValues[0]);

		// update editor struct
		SetStruct(hLipSynchro);
		// update validity
		SetAvailable(TRUE);
		fn_bValidate();
	}
}

/*===========================================================================
*		Description:	Find engine struct
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author:	Shaitan	     
*=========================================================================*/
void LipSync_Object::fn_vFindEngineStruct (void)
{
	tdxHandleOfLipsSynchroTable hLipSynchro;
	SCR_tdst_Link_Value			*pLinkValue;
 	SCR_tdst_Sect_Open			*pSectOpen;
	CString						csSectionName;

	if (!fn_bIsAvailable())
    {
		// get section name
		csSectionName = GetCompleteSectionName();
		// if section was already loaded, get value from LinkTable
		pSectOpen = SCR_fnp_st_RdL0_GetOpenSection((char*)(LPCSTR) csSectionName, 0);
		if (pSectOpen)
		{
			// search link table for engine struct
			pLinkValue = SCR_fnp_st_Link_SearchKey(&g_st3DOSLinkTable.stLipsSynchroList, (char *)(LPCSTR) csSectionName);
			if ((pLinkValue)&&(SCR_M_e_Link_GetState(pLinkValue) == SCR_ELS_Link_Initialized))
			{
				hLipSynchro = (tdxHandleOfLipsSynchroTable) SCR_M_ul_Link_GetValue(pLinkValue);
				// update editor struct
				SetStruct(hLipSynchro);
				// update validity
				SetAvailable(TRUE);
				fn_bValidate();
			}
		}
	}
}

