/*
 =======================================================================================
  Name        : _Minterf.cpp
  Author      : David Reizer         
  Description : main functions for Particle Generato editor interface
 =======================================================================================
  Modification -> Author : Tahiti Bob                 Date :20/03/98
  Description : adding some functionnalities
 =======================================================================================
 */

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include "StdAfx.h"

#include "ACP_Base.h"
#include "ITF.h"
#include "x:\cpa\main\inc\_EditID.h"

#include "_Minterf.hpp"
#include "PGen_Res.h"
#include "TPG_Strg.hpp"

#include "ErO.h"

// infos
static CString g_csTPGName = C_szDLLPartGenName;
static CString g_csTPGAuthor = "D. Reizer";
static CString g_csTPGVersion = "V 2.0.2   18/03/98";
static CString g_csTPGFrenchHelpFile = "";
static CString g_csTPGEnglishHelpFile = "";

BOOL g_bTipFirstTime = TRUE;



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
CPA_PartGen_Interface *g_pclInterface;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define PartGen_C_MenuID_OpenID								31
#define PartGen_C_MenuID_CloseID							32
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

// the DLL global definition
tdstDLLIdentity g_stPartGenDLLIdentity; 

//--------------------------------------------------------------------------------
// CONSTRUCTOR.
// You must put here all your DLL interface definitions.
//--------------------------------------------------------------------------------
CPA_PartGen_Interface::CPA_PartGen_Interface(void)
{
	// ************************** private internal
	m_p_stDLLIdentity = &g_stPartGenDLLIdentity;
	m_stBaseDLLDefinition.bCanOutputIn3DView = FALSE;		
	m_stBaseDLLDefinition.bCanBeRefreshedByEngine = FALSE;

	// Specific Init.
	m_pclPartGenMainFrame = NULL;

	m_csEditorVersion = "2.0.0 [" + CString(__DATE__) + "]";

    SetEditorInfo(g_csTPGName, g_csTPGAuthor, g_csTPGVersion, g_csTPGFrenchHelpFile, g_csTPGEnglishHelpFile);

	SetCurrent(FALSE);

	//For errors
	ERROR_g_fn_vAddAModuleDescriptor
		(	
			g_c_csPartGenModuleNameForErrors,
			m_csEditorVersion,
			"David REIZER",
			"01 48 18 53 74 (Montreuil)",
			"dreizer@ubisoft.fr",
			"Daniel PALIX",
			"04 50 51 26 63 (Annecy)",
			"dpalix@ubisoft.fr",
			"Vincent Lhullier (Annecy)",
			"04 50 52 90 84",
			"vlhullier@ubisoft.fr"
		);

	m_pub_p_clView = NULL;
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vJustAfterRegistered(void)
{
	CString a_csTypes[] =
	{
		g_C_csPartGenGeneratorName,
		g_C_csPartGenSourceName,
		g_C_csPartGenSystemName,
		g_C_csPartGenEnvName
	};
	fn_vRegisterObjectsType(a_csTypes, 4);
}

//*****************************************
void CPA_PartGen_Interface::fn_vConstruct()
{
#ifndef __INACTIVE_INTERFACE__
	/*
	 * Creates the main frame
	 */
	if ( m_pclPartGenMainFrame == NULL )
	{
		ERROR_PREPARE_M
			(
				g_c_csPartGenModuleNameForErrors,
				"Creating the Main Frame",
				"CPA_PartGen_Interface::fn_vConstruct",
				E_ERROR_GRAVITY_FATAL,
				"New main frame is not allocated"
			);
		m_pclPartGenMainFrame = new CPA_PartGen_MainFrame;
		ERROR_ASSERT( m_pclPartGenMainFrame != NULL );

		m_pclPartGenMainFrame->CreateBase(	"",1,2,GetInterface()->GetMainWorld() );
		m_pclPartGenMainFrame->mfn_vEnableCaption(FALSE);
		m_pclPartGenMainFrame->m_iInitWidth = 350;
		m_pclPartGenMainFrame->m_bKeepPos = TRUE;
		m_pclPartGenMainFrame->m_stWinRight.cLinkSame = FRM_C_MoveLeft;
	}

	m_clDocument.m_pub_p_clDialogList->fn_vInitAllLists();
	m_clDocument.m_pub_p_clDialogList->fn_vReinitDialog();

#endif
}

//***********************************************
void CPA_PartGen_Interface::fn_vLevelHasChanged()
{
	/*
	 * get all loaded engine structure
	 */
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedSystems();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedEnvironments();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedGenerators();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedSources();
	
	/*
	 * get all unloaded engine structure
	 */
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForUnLoadedGeneratorsAndSources();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForUnLoadedSystems();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForUnLoadedEnvironments();

	m_pub_fn_p_clGetDocument()->m_pub_fn_vFillListOfSprites();
	m_pub_fn_p_clGetDocument()->m_pub_fn_vUpdateChangingEnumDescriptors();
}

//*************************************************************************
BOOL CPA_PartGen_Interface::fn_bLoadBaseObject( CPA_BaseObject *p_oObject )
{
	BOOL bValue;

	ERROR_PREPARE_M
		(
			g_c_csPartGenModuleNameForErrors,
			"Loading an object",
			"CPA_PartGen_Interface::fn_bLoadBaseObject",
			E_ERROR_GRAVITY_FATAL,
			"Trying to load a NULL object"
		);
	ERROR_ASSERT( p_oObject != NULL );

	/*
	 * Particle generator
	 */
	if( p_oObject->fn_bIsOfType(g_C_csPartGenGeneratorName) )
	{		
		bValue = ( (CPA_PartGen_Generator*)p_oObject )->m_pub_fn_vLoadIfUnloaded();
		if( bValue )
		{
			//Updates system and environment possibly loaded with the generator
			m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedGenerators();
			m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedSources();
			m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedSystems();
			m_pub_fn_p_clGetDocument()->m_pub_fn_vSearchForLoadedEnvironments();
			( (CPA_PartGen_Generator*)p_oObject )->m_pub_fn_vSetGenInEnv();
		}
		m_clDocument.m_pub_fn_vUpdateChangingEnumDescriptors();
		m_clDocument.m_pub_fn_vUpdateGenerator( (CPA_PartGen_Generator*) p_oObject );
		return bValue;
	}
	/*
	 * Particle source
	 */
	else if( p_oObject->fn_bIsOfType(g_C_csPartGenSourceName) )
	{
		bValue = ( (CPA_PartGen_Source *) p_oObject )->m_pub_fn_vLoadIfUnloaded();
		m_clDocument.m_pub_fn_vUpdateChangingEnumDescriptors();
		m_clDocument.m_pub_fn_vUpdateSource( (CPA_PartGen_Source *) p_oObject );
		return bValue;
	}
	/*
	 * Particle system
	 */
	else if( p_oObject->fn_bIsOfType(g_C_csPartGenSystemName) )
	{
		bValue = ( (CPA_PartGen_System*)p_oObject )->m_pub_fn_vLoadIfUnloaded();
		m_clDocument.m_pub_fn_vUpdateChangingEnumDescriptors();
		m_clDocument.m_pub_fn_vUpdateSystem( (CPA_PartGen_System*) p_oObject );
		return bValue;
	}
	/*
	 * particle environment
	 */
	else if( p_oObject->fn_bIsOfType(g_C_csPartGenEnvName) )
	{
		bValue = ( (CPA_PartGen_Environment*)p_oObject )->m_pub_fn_vLoadIfUnloaded();
		m_clDocument.m_pub_fn_vUpdateChangingEnumDescriptors();
		m_clDocument.m_pub_fn_vUpdateEnv( (CPA_PartGen_Environment*) p_oObject );
		return bValue;
	}

	return FALSE;
}

//**********************************************************************************
tdePermission CPA_PartGen_Interface::fn_eAcceptNewEditor(CPA_EditorBase *pNewEditor)
{
	return C_Allow;
}

//**********************************************************************************
BOOL CPA_PartGen_Interface::fn_bCanActivateEditor(CPA_List<CPA_BaseObject> *pParams)
{
#ifdef __INACTIVE_INTERFACE__
	return FALSE;
#endif
	return TRUE;
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vOnActivateEditor(CPA_List<CPA_BaseObject> *pParams,BOOL bBackActivated)
{
	m_fn_vShowEditor();
}

//**************************************************************************************
BOOL CPA_PartGen_Interface::fn_bCanCloseEditor(void)
{
	return TRUE;
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vOnCloseEditor(void)
{
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vBeforeEngine(void)
{
	m_fn_vHideEditor();
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vBeforeEditor(void)
{
	if ( fn_bIsCurrentEditor() ) 
		m_fn_vShowEditor();
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vHasLostFocus(void)
{
}

//**************************************************************************************
BOOL CPA_PartGen_Interface::fn_bHasGainedFocus(void)
{
	m_pub_fn_p_clGetDocument()->m_pub_fn_vUpdateChangingEnumDescriptors();

	// display Tip of the Day
	GetInterface()->fn_vDisplayTipOfDay("Particle Generators", "TPG", g_bTipFirstTime);
	g_bTipFirstTime = FALSE;

	return TRUE;
}

//**************************************************************************************
//Begin Mircea Dunka 31 Aug 1998 - DialogBar interface
void CPA_PartGen_Interface::fn_vDefineDlgBarBtnIcon (tde_DlgBarCategory wCategory, tds_DlgBarBtn *_pDlgBarBtn)
{
	if(wCategory == C_DlgBarCategoryEditor)
	{
		_pDlgBarBtn->hIcon = ::LoadIcon((HINSTANCE)(GetDLLIdentity()->hModule), MAKEINTRESOURCE(IDI_TPG_DLGBAR_ICON));
		CString oCst;
        oCst = C_szDLLPartGenName;
        if( (M_GetMainApp()) && (M_GetMainApp()->mp_oAppKeyboard) )
        {
            oCst += "  (";
            oCst += M_GetMainApp()->mp_oAppKeyboard->mfn_oConvertIniStringToKeyString((const CString*)&CString("Activate Particle Editor"));
			oCst += ")";
        }
		strcpy(_pDlgBarBtn->szToolTip, (char*)(LPCTSTR)oCst);
		_pDlgBarBtn->uID = (UINT)this;
	}
}
//end Mircea Dunka 31 Aug 1998

//**************************************************************************************
BOOL CPA_PartGen_Interface::fn_bDefineSubMenu(EDT_SubMenu *_p_oEDTSubMenu)
{
	if ( _p_oEDTSubMenu->GetSubMenuType() == C_SubMenuEditor )
	{
		CString csMenuDescString;
		CString csMenuAccelString = M_GetMainApp()->mp_oAppKeyboard->mfn_oConvertIniStringToKeyString((const CString*)&CString("Activate Particle Editor"));
		csMenuDescString.Format("%s\t%s", C_szDLLPartGenName, csMenuAccelString);

		if ( fn_bIsCurrentEditor() )
			_p_oEDTSubMenu->AddAnEntry(this, M_MAKECHAR(csMenuDescString), PartGen_C_MenuID_CloseID, TRUE);
		else
			_p_oEDTSubMenu->AddAnEntry(this, M_MAKECHAR(csMenuDescString), PartGen_C_MenuID_OpenID, FALSE);
	}
	return TRUE;
}

//**************************************************************************************
void CPA_PartGen_Interface::_OnSubMenuCommand(	EDT_SubMenu *_p_oEDTSubMenu, UINT uiMsgID)
{
	switch (uiMsgID)
	{
		case PartGen_C_MenuID_OpenID:
			if ( !fn_bIsCurrentEditor() )
				GetMainWorld()->fn_bActivateEditor(this, NULL);
			break;

		case PartGen_C_MenuID_CloseID:
			if ( fn_bIsCurrentEditor() )
				GetMainWorld()->fn_bCloseEditor(this);
			break;
	}
}

//**************************************************************************************
BOOL CPA_PartGen_Interface::fn_bAcceptToSave()
{
	BOOL bReturn = TRUE;

	CPA_BaseObjectList * p_clList = GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenGeneratorName);
	Position pos = p_clList->GetHeadPosition();
	
	while( (pos != NULL) && bReturn )
	{
		CPA_PartGen_Generator * p_clGen = (CPA_PartGen_Generator*)p_clList->GetNext( pos );
		if( p_clGen->fn_bIsAvailable() )
		{
			CString csMessage = "The generator " + p_clGen->GetName() + " does not have an associated system or environment : operation aborted";
			ERROR_PREPARE_M
				(
					g_c_csPartGenModuleNameForErrors,
					"Saving a Generator",
					"CPA_PartGen_Interface::fn_bAcceptToSave",
					E_ERROR_GRAVITY_WARNING,
					csMessage 
				);
			bReturn = ( (p_clGen->m_pub_fn_p_clGetSystem() != NULL) && (p_clGen->m_pub_fn_p_clGetEnv() != NULL) );
			ERROR_ASSERT( bReturn );
		}
	}

	p_clList = GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenSystemName);
	pos = p_clList->GetHeadPosition();
	while( (pos != NULL) && bReturn )
	{
		CPA_PartGen_System * p_clSys = (CPA_PartGen_System*)p_clList->GetNext( pos );
		if( p_clSys->fn_bIsAvailable() )
		{
			CString csMessage = "System " + p_clSys->GetName() + " needs parameters : operation aborted";
			ERROR_PREPARE_M
				(
					g_c_csPartGenModuleNameForErrors,
					"Saving a System",
					"CPA_PartGen_Interface::fn_bAcceptToSave",
					E_ERROR_GRAVITY_WARNING,
					csMessage 
				);
			bReturn = ( (p_clSys->m_pub_fn_p_stGetSystem() != NULL) && ((p_clSys->m_pub_fn_hGetPriGameMaterial() != NULL ) || (p_clSys->m_pub_fn_hGetPriSprite() != NULL)) );
			ERROR_ASSERT( bReturn );
		}
	}

	return bReturn;
}


//************************
// for dialog list
//************************

//**************************************************************************************
void CPA_PartGen_Interface::GetListsForDialog(CPA_DialogList *pDialog)
{
	pDialog->fn_vAddANewList( C_szPartGenGeneratorName, GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenGeneratorName) );
	pDialog->fn_vAddANewList( C_szPartGenSourceName,	GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenSourceName) );
	pDialog->fn_vAddANewList( C_szPartGenSystemName,	GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenSystemName) );
	pDialog->fn_vAddANewList( C_szPartGenEnvName,		GetMainWorld()->fn_p_oGetOriginalObjectList(C_szPartGenEnvName) );
}

//**************************************************************************************
void CPA_PartGen_Interface::fn_vInitDefaultParameters(CPA_DialogList *pDialog)
{
	pDialog->SetDefaultButtonState(FALSE);
	pDialog->SetDefaultTestName("<< No Filter >>");
	pDialog->SetDefaultTypeName(C_szPartGenGeneratorName);
}


//****************************************************
BOOL CPA_PartGen_Interface::fn_bOnSelChangeListObjects(CPA_DialogList *pDialog, CString csTypeName, CPA_BaseObject *pSelectedObject, BOOL bUnselect)
{
	if ( (! bUnselect) && pSelectedObject->fn_bIsAvailable() )
	{
		if ( csTypeName == g_C_csPartGenGeneratorName )
		{
			m_clDocument.m_pub_fn_vUpdateGenerator( (CPA_PartGen_Generator*)pSelectedObject );
		}
		else if ( csTypeName == g_C_csPartGenSourceName )
		{
			m_clDocument.m_pub_fn_vUpdateSource( (CPA_PartGen_Source *) pSelectedObject );
		}
		else if ( csTypeName == g_C_csPartGenSystemName )
		{
			m_clDocument.m_pub_fn_vUpdateSystem( (CPA_PartGen_System*)pSelectedObject );
		}
		else if ( csTypeName == g_C_csPartGenEnvName )
		{
			m_clDocument.m_pub_fn_vUpdateEnv( (CPA_PartGen_Environment*)pSelectedObject );
		}			
	}
	//ROMTEAM Bugs Correction (Cristian Stegaru 24/03/98)
	//return TRUE;
	return FALSE;
	//ENDROMTEAM Bugs Correction (Cristian Stegaru)
}

//**************************************************************************************
CPA_BaseObject* CPA_PartGen_Interface::GetDialogSelection(CPA_DialogList *pDialog, CString csTypeName, tdeListDrawMode eDrawMode)
{
	if ( csTypeName == g_C_csPartGenGeneratorName ) 
		return m_clDocument.m_pub_p_clCurrentGenerator;
	if ( csTypeName == g_C_csPartGenSourceName ) 
		return m_clDocument.m_pub_p_clCurrentSource;
	if ( csTypeName == g_C_csPartGenSystemName ) 
		return m_clDocument.m_pub_p_clCurrentSystem;
	if ( csTypeName == g_C_csPartGenEnvName ) 
		return m_clDocument.m_pub_p_clCurrentEnv;

	return NULL;
}

//**************************************************************************************
// Specific members
//***********************************************************************
void CPA_PartGen_Interface::m_fn_vShowEditor()
{
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if ( m_pclPartGenMainFrame != NULL )
		g_oFrameGest.mfn_vActivateWindow(m_pclPartGenMainFrame);

	g_oFrameGest.mfn_vSetRefresh(TRUE);
}

//***********************************************************************
void CPA_PartGen_Interface::m_fn_vHideEditor()
{						  
	g_oFrameGest.mfn_vSetRefresh(FALSE);

	if ( m_pclPartGenMainFrame != NULL )
		g_oFrameGest.mfn_vDisactivateWindow(m_pclPartGenMainFrame);

	g_oFrameGest.mfn_vSetRefresh(TRUE);
}


//***********************************************************************
CPA_PartGen_MyDocument *CPA_PartGen_Interface::m_pub_fn_p_clGetDocument()
{
	return &m_clDocument;
}

//***************************************************************************
CPA_PartGen_Source *CPA_PartGen_Interface::m_pub_fn_p_clFindSourceWithEngine(  PRT_tdstParticlesSource* _p_stPrtSrc )
{
	return (CPA_PartGen_Source*)( GetMainWorld()->fn_p_oFindObjectWithEngine((void*)_p_stPrtSrc, C_szPartGenSourceName) );	
}


//***************************************************************************
CPA_PartGen_System * CPA_PartGen_Interface::m_pub_fn_p_clFindSystemWithEngine(  PRT_tdstParticlesSystem* _p_stSystem )
{
	return (CPA_PartGen_System*)( GetMainWorld()->fn_p_oFindObjectWithEngine((void*)_p_stSystem, C_szPartGenSystemName) );	
}


//**************************************************************************************
CPA_PartGen_Environment * CPA_PartGen_Interface::m_pub_fn_p_clFindEnvWithEngine( PRT_tdstParticlesEnvironment* _p_stEnv )
{
	return (CPA_PartGen_Environment*)( GetMainWorld()->fn_p_oFindObjectWithEngine((void*)_p_stEnv, C_szPartGenEnvName) );	
}

//**************************************************************************************
CPA_PartGen_Generator	*CPA_PartGen_Interface::m_pub_fn_p_clFindPrtGenWithEngine( tdxHandleToParticleGenerator hPrtGen )
{
	return (CPA_PartGen_Generator*)( GetMainWorld()->fn_p_oFindObjectWithEngine((void*)hPrtGen, C_szPartGenGeneratorName) );	
}

