/*=========================================================================
* 
* LPSinter.cpp :  LipSync DLL Interface
* 
*
* 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"///LoadLSyn.h"

#include "LPSinter.hpp"
#include "LPSobj.hpp"
#include "LPSdLoad.hpp"

#include "TLS/LPSconst.hpp"

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

// infos
static CString g_csTLSName = "Lips Synchro";
static CString g_csTLSAuthor = "Chantal Oury";
static CString g_csTLSVersion = "V 1.0.0   (not updated)";
static CString g_csTLSHelpFile = "";

/*=============================================================================
		the DLL global definition
=============================================================================*/
tdstDLLIdentity g_stLipSyncIdentity;

/*=============================================================================
		defines
=============================================================================*/
#define C_uiPopupLoad	10


//#################################################################################
// Constructor / Destructor
//#################################################################################

/*===========================================================================
*		Description:	Constructor
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
LipSync_Interface::LipSync_Interface(void)
{
	// ************************** private internal
	m_p_stDLLIdentity = &g_stLipSyncIdentity;

	// Does your DLL can output in main game view ?
	m_stBaseDLLDefinition.bCanOutputIn3DView = FALSE;

	// Does your DLL can be refresh by engine ?
	m_stBaseDLLDefinition.bCanBeRefreshedByEngine = FALSE;

    // Editor Info
    SetEditorInfo(g_csTLSName, g_csTLSAuthor, g_csTLSVersion, g_csTLSHelpFile);
}


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

}

//#################################################################################
// Inits
//#################################################################################

/*===========================================================================
*		Description:	Inits for Scripts
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
void LipSync_Interface::fn_vJustAfterRegistered (void)
{
	// register type
	CString a_csType[] = { C_szLipSyncTypeName };
	fn_vRegisterObjectsType ( a_csType , 1 ) ;

    // static init for scripts
	LipSync_Object::StaticInit();
}

/*===========================================================================
*		Description:	Construct
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:         
*=========================================================================*/
void LipSync_Interface::fn_vConstruct(void)
{
	// resources
	HINSTANCE hOldInst = AfxGetResourceHandle();
    AfxSetResourceHandle(m_p_stDLLIdentity->hModule);

    // dialog for load
	m_pDialogLoad = new LPS_DialogLoad(this);

	// resources
	AfxSetResourceHandle(hOldInst);
}



//#################################################################################
// Loads
//#################################################################################

/*===========================================================================
*		Description:	New level => build all Editor LipSyncs
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan       
*=========================================================================*/
void LipSync_Interface::fn_vLevelChanges (void)
{
	WIN32_FIND_DATA				stFindFileData;
	HANDLE						hFind;
	char						szSearchName[MAX_PATH];

	// we want all family directories
    strcpy(szSearchName, fn_szGetSyncLipsDataPath());
	strcat(szSearchName, "\\*");

    m_lNbUnloaded = 0;

	// get first one
	hFind = FindFirstFile(szSearchName, &stFindFileData);
	if (hFind == INVALID_HANDLE_VALUE)
		return;

	M_GetMainWnd()->UpdateStatus("Loading Lips Synchros", C_STATUSPANE_INFOS, C_STATUS_NORMAL);

	do
	{
		// load all LipSync files in family directory
		if ((stFindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (stFindFileData.cFileName[0] != '.'))
		{
			fn_vLoadFamilyLipSynchros(stFindFileData.cFileName);
		}
	}
	while (FindNextFile(hFind, &stFindFileData));
	
	// close FindData handle
	FindClose(hFind);

	M_GetMainWnd()->UpdateStatus("Load Lips Synchros >>> 100% Completed", C_STATUSPANE_INFOS, C_STATUS_NORMAL);
}


/*===========================================================================
*		Description:	Build all Editor LipSync in a family
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan       
*=========================================================================*/
void LipSync_Interface::fn_vLoadFamilyLipSynchros (CString csFamilyName)
{
	WIN32_FIND_DATA		stFindFileData;
	LipSync_Object		*pLipSynchro;
	CPA_BaseObject		*pFamily;
	CPA_DLLBase			*pFamilyDLL;
	HANDLE				hFind;
	CString				csText;
	char				szSearchName[MAX_PATH];

	// get family
	pFamilyDLL = GetMainWorld()->GetToolDLLWithName(C_szDLLFamilyName);
	pFamily = pFamilyDLL->GetBaseObject(csFamilyName, C_szFamilyTypeName);
	
	// no family => lips synchro cannot be loaded
	if (!pFamily)
	{
		csText.Format("Family %s does not exist.\n Lips Synchros of this directory won't be loaded", csFamilyName);
		MessageBox(NULL, (char*)(LPCSTR)csText, "Family not found", MB_OK | MB_ICONWARNING);
		return; 
	}
	
	// we want all lip synchro files
    strcpy(szSearchName, fn_szGetSyncLipsDataPath());
	strcat(szSearchName, "\\");
	strcat(szSearchName, csFamilyName);
	strcat(szSearchName, "\\*.SYN");

	// get first one
	hFind = FindFirstFile(szSearchName, &stFindFileData);
	if (hFind == INVALID_HANDLE_VALUE)
		return;
	
	csText.Format("Loading Lips Synchros for Family : %s", csFamilyName);
	M_GetMainWnd()->UpdateStatus((char *)(LPCSTR) csText, C_STATUSPANE_INFOS, C_STATUS_NORMAL);

	do
	{
		// file name (short & complete)
		pLipSynchro = new LipSync_Object(this, NULL, stFindFileData.cFileName, csFamilyName, pFamily);
		m_lNbUnloaded++;

	}
	while (FindNextFile(hFind, &stFindFileData));

	// close FindData handle
	FindClose(hFind);
}


/*===========================================================================
*		Description:	Load given object
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		    
*=========================================================================*/
BOOL LipSync_Interface::fn_bLoadBaseObject (CPA_BaseObject *p_oObject)
{
    LipSync_Object *pLipSync;
	
	if (!p_oObject->fn_bIsAvailable())
    {
        // load LipSync
		pLipSync = (LipSync_Object *) p_oObject;
        pLipSync->fn_vLoadEngineStruct();
        return TRUE;
    }
    return FALSE;
}



//#################################################################################
// Tool DLL functions Overload
//#################################################################################

/*===========================================================================
*		Description:	OnQueryAction => load one LipSync
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:	18/08/97		Author: Shaitan       
*=========================================================================*/
long LipSync_Interface::OnQueryAction (CPA_EditorBase *p_oSender, WPARAM wParam, LPARAM lParam)
{
    LipSync_Object	*pLipSync;
	tdstLipSyn_Info *pInfo;

	switch (wParam)
    {
		case C_uiLoadLipSynchro:
			// get corresponding LipSync
			pInfo = (tdstLipSyn_Info *)lParam;
			pLipSync = (LipSync_Object *) GetBaseObject(pInfo->csName, C_szLipSyncTypeName, pInfo->pFamily, E_rc_AllObjects);
			// if necessary, load engine struct
			if ((pLipSync)&&(!pLipSync->fn_bIsAvailable()))
			{
				fn_bLoadBaseObject(pLipSync);
				m_lNbUnloaded--;
			}
			// give corresponding LipSync
			return (long) pLipSync;
			break;
    }
	return 0;
}


/*===========================================================================
*		Description:	Tools Menu => add entry for load
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		       
*=========================================================================*/
BOOL LipSync_Interface::fn_bDefineSubMenu (EDT_SubMenu *p_oEDTSubMenu)
{
//ANNECY Shaitan NewInterface 24/03/98 {
    if (p_oEDTSubMenu->GetSubMenuType() == C_SubMenuLoad)
//ENDANNECY Shaitan NewInterface }
    {
        p_oEDTSubMenu->AddAnEntry(this, "Load Lips Synchros", C_uiPopupLoad, FALSE, m_lNbUnloaded > 0);
        return TRUE;
    }
    return FALSE;
}

/*===========================================================================
*		Description:	Tools Menu => answer to load command
*		Creation date:	16/08/97
*		Author:			Shaitan
*---------------------------------------------------------------------------
*		Revision date:					Author:		       
*=========================================================================*/
void LipSync_Interface::_OnSubMenuCommand (EDT_SubMenu *p_oEDTSubMenu, UINT uiMsgID)
{
	CPA_List<CPA_BaseObject> *pObjectList;
	CPA_BaseObject			 *pObject;
	POSITION				 pos;
	CString					 csText;
	BOOL					 bRes;
	long					 lNbLoaded = 0;
	
//ANNECY Shaitan NewInterface 24/03/98 {
	if (p_oEDTSubMenu->GetSubMenuType() == C_SubMenuLoad) 
//ENDANNECY Shaitan NewInterface }
    {
        switch (uiMsgID)
		{
			case C_uiPopupLoad:
				if (m_pDialogLoad->DoModal() == IDOK)
				{
					// get list of objects to load
					pObjectList = m_pDialogLoad->GetObjectsToLoad();
					// load each object
					for (pObject = pObjectList->GetHeadElement(pos); pObject;
						 pObject = pObjectList->GetNextElement(pos))
					{
						bRes = fn_bLoadBaseObject(pObject);
						if (bRes)
							lNbLoaded++;
					}
					m_lNbUnloaded -= lNbLoaded;
					if (lNbLoaded)
					{
						csText.Format("%ld Lips Synchros loaded", lNbLoaded);
						M_GetMainWnd()->UpdateStatus((char*)(LPCSTR)csText, C_STATUSPANE_INFOS, C_STATUS_NORMAL);
					}
				}
				break;
		}
	}
}


