//
/*=============================================================================
 *
 *  Filename:      CMatObj.cpp
 *  Version:            1.0
 *  Date:								06/11/96
 *  Author:             B.G.
 *
 *  Description:    implementation of editor material class
 *
 *===========================================================================*/

#include "stdafx.h"
#include "Acp_base.h"
#include "itf.h"
#include "gmt.h"
#include "IncMEC.h"
#include "gli.h"
#include "x:\cpa\main\inc\_editid.h"
#include "inctex.h"

#include "_interf.hpp"
#include "CMatObj.hpp"
#include "GMatObj.hpp"

extern Material_Interface *gs_p_oMaterialInterface;

//=================================================================================
//=================================================================================
//=================================================================================
//constructor. the object is name with the following rules
//1. it is not a clone but will directly handle the engine material
//   -> will take the name from the section for the engine material
//2. it is a clone, and a name is specified
//   ->  an underscore is added to the original's name,
//       because someday the base constructor will add a number there
//3. it is a clone, and no name is specified
//   -> the default name is given by the base constructor
//=================================================================================
tdoEditorCollideMaterial::tdoEditorCollideMaterial(
	CPA_FileObject *_p_oOwnerFile,
	CString _csId,
	BOOL _bSectionAlreadyExists,
	GMT_tdxHandleToCollideMaterial _hTemplateCollideMaterial
)
: CPA_SaveObject(
	gs_p_oMaterialInterface, //editor
	C_szCollideMaterialTypeName, //type
	E_ss_Responsible, //responsibility
	_p_oOwnerFile, //owner (there is none)
	FALSE, //available
	(char*)LPCTSTR(_p_oOwnerFile->GetCompletionPath(gs_p_oMaterialInterface, C_szFileReferencedNameKey)), //data path to complete the referenced section name
	m_vCallBackSaveCMT//callback
)
{
	//tell the file object that an object exists for its section
	g_oCoherenceManager.m_fn_vAddALink(_p_oOwnerFile, this);

	m_hCollideMaterial = _hTemplateCollideMaterial;
	SetAvailable(_hTemplateCollideMaterial != GMT_C_InvalidCollideMaterial);

	// construct section name (hack: szSectionName is here as an anti-bug for ...Split...
	char szSectionName[SCR_CV_ui_Cfg_MaxLenName];
	SCR_fn_v_RdL0_ComputeSectionName(
		szSectionName,
		(char*)LPCTSTR(_p_oOwnerFile->GetReferencedName(gs_p_oMaterialInterface, C_szFileReferencedNameKey)),
		C_SectionCollideMaterial,
		(char*)LPCTSTR(_csId)
	);

	// section object
	SetSectionData(this) ;
	SetReferencedSectionName(szSectionName) ;

	// name
	if ( fn_eRename(_csId) != E_mc_None )
		SetDefaultValidName() ;

	fn_vUpdateSectionName() ;

	if ( _bSectionAlreadyExists )
		fn_vSectionSaved(); //the section is saved, and has no pending notification
	else
		fn_vSectionDeleted(); //the section does not exist, and has no pending nofitication

	if ( fn_bCanBeNotified() && !fn_bSectionExists() )
		fn_vNotifySave();

	m_bIsHighlighted = FALSE;
	m_stHighlightColor.xR = (GEO_tdxColorValue) 0.7;
	m_stHighlightColor.xG = (GEO_tdxColorValue) 0.3;
	m_stHighlightColor.xB = (GEO_tdxColorValue) 0.0;
}

//=================================================================================
//=================================================================================
tdoEditorCollideMaterial::~tdoEditorCollideMaterial()
{
  //remove myself totally from the coherence manager database
	if ( !fn_bSectionExists() )
		g_oCoherenceManager.m_fn_vRemoveAllLinks(this, C_cLinkedAsChild);
}

//=================================================================================
//=================================================================================
void tdoEditorCollideMaterial::m_vAssociateEngineMaterial(GMT_tdxHandleToCollideMaterial _hCollideMaterial, BOOL /*_bSectionAlreadyExists*/)
{
	//make the list update the data sort order
	fn_vUpdateData(_hCollideMaterial);
	//now we can set the value that will be returned by GetData()
	m_hCollideMaterial = _hCollideMaterial;
	SetAvailable(_hCollideMaterial != GMT_C_InvalidCollideMaterial);
}

//================================================================================
//================================================================================
BOOL tdoEditorCollideMaterial::m_bIsReferenced()
{
	return g_oCoherenceManager.m_fn_iGetFatherCount(this) > 1;
}

//=================================================================================
//=================================================================================
void tdoEditorCollideMaterial::m_vLoadAssociatedEngineMaterial()
{
	if ( !fn_bIsAvailable() )
	{
		//if the section name is not in the linktable, analyse the section
		SCR_tdst_Cxt_Values *p_stAnalyzeResult = SCR_fnp_st_RdL0_AnalyseSection(
			(char *) LPCTSTR(GetReferencedSectionName()), 
			SCR_CDF_uw_Anl_Normal
		);
		GMT_tdxHandleToCollideMaterial hCollideMaterial = (GMT_tdxHandleToCollideMaterial) p_stAnalyzeResult->a_ulValues[0];
		//there must not already exist an editor material for the loaded game material!
		ASSERT(!gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hCollideMaterial));
		//create the association
		m_vAssociateEngineMaterial(hCollideMaterial, TRUE);
	}
}

/*=================================================================================
//save a section of the form:
{CollideMaterial:<name>
	Type(2)
	Identifier(3)
}
//=================================================================================*/
void tdoEditorCollideMaterial::m_vCallBackSaveCMT(
	SCR_tdst_File_Description *_p_stFile,
	char *_p_szSectionName,
	void *_p_vData,
	SCR_tde_Ntfy_Action _eAction
)
{
	tdoEditorCollideMaterial *p_oEditorMaterial = (tdoEditorCollideMaterial *) _p_vData;

	if ( _eAction == SCR_EA_Ntfy_DeleteSection )
		//now the section exists, if this was not the case
		p_oEditorMaterial->fn_vSectionDeleted(); //the section does not exist, and has no pending nofitication

	if ( _eAction != SCR_EA_Ntfy_AddSection && _eAction != SCR_EA_Ntfy_RebuildSection )
	 return;

	if ( _eAction == SCR_EA_Ntfy_AddSection )
	{
		// go to end of file (we need all file directive to load this section)
		SCR_fn_v_SvL1_ToEndSection(_p_stFile);
		SCR_M_SvL0_SaveBlankLine (_p_stFile);
	}

	char szSectionName[SCR_CV_ui_Cfg_MaxLenName],szAction[SCR_CV_ui_Cfg_MaxLenName],szId[SCR_CV_ui_Cfg_MaxLenName];

	// construct section name (hack: szSectionName is here as an anti-bug for ...Split...
	SCR_fn_v_RdL0_SplitSectionName(_p_szSectionName, szSectionName, szAction, szId);
	SCR_fn_v_RdL0_ComputeSectionName(szSectionName, NULL, szAction, szId);
	// save begin section for the game material
	SCR_M_SvL0_SaveBeginSection(_p_stFile, szSectionName, SCR_CC_C_Cfg_EOL);

  //get the parameters we want to save
	GMT_tdxHandleToCollideMaterial hMaterialCollide = p_oEditorMaterial->m_hGetEngineMaterial();
	short wType, wIdentifier;

	wType = GMT_fn_wGetTypeOfCollideMaterial(hMaterialCollide);
	char *pszType = GMT_C_szZDE;
	switch ( wType )
	{
	case GMT_C_wZDM:
		pszType = GMT_C_szZDM;
		break;

	case GMT_C_wZDR:
		pszType = GMT_C_szZDR;
		break;

	case GMT_C_wZDE:
		pszType = GMT_C_szZDE;
		break;

	case GMT_C_wZDD:
		pszType = GMT_C_szZDD;
		break;
	}
	wIdentifier = GMT_fn_hGetCollideMaterialIdentifier(hMaterialCollide);
	SCR_M_SvL0_SaveEntry(_p_stFile, C_EntryCollideMaterial, SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(
		_p_stFile,
		SCR_EF_SvL0_Scanf, 3, "%s,%hd",
		pszType, wIdentifier
	);

	// End Section
	SCR_M_SvL0_SaveEndSection(_p_stFile, SCR_CC_C_Cfg_EOL);

	//now the section exists, if this was not the case
	p_oEditorMaterial->fn_vSectionSaved(); // the section is saved and has no pending notification
}

//=================================================================================
//=================================================================================
BOOL tdoEditorCollideMaterial::m_bGetHighlightColor(GEO_tdstColor *_p_stColor /*= NULL*/)
{
	if ( m_bIsHighlighted && _p_stColor )
		*_p_stColor = m_stHighlightColor;

	return m_bIsHighlighted;
}

//=================================================================================
//=================================================================================
void tdoEditorCollideMaterial::m_vSetHighlightColor(GEO_tdstColor _stColor)
{
	m_stHighlightColor = _stColor;
}

//=================================================================================
//=================================================================================
//=================================================================================
// static functions for the class
//=================================================================================
//=================================================================================
//=================================================================================
SCR_tdst_Link_Table *tdoEditorCollideMaterial::m_p_stGetAssociatedLinkTable()
{
	return GMT_fn_p_stGetZoneLinkTable();
}

BOOL tdoEditorCollideMaterial::m_bIsEngineMaterialValid(GMT_tdxHandleToCollideMaterial _hCollideMaterial) 
{ 
	return _hCollideMaterial != GMT_C_InvalidCollideMaterial; 
}

/*CString tdoEditorCollideMaterial::m_csGetBaseDataPath() 
{ 
	return fn_szGetCollideMaterialDataPath(); 
}*/

void tdoEditorCollideMaterial::m_vInvalidateEngineMaterial(GMT_tdxHandleToCollideMaterial &r_hCollideMaterial) 
{ 
	r_hCollideMaterial = GMT_C_InvalidCollideMaterial; 
}

CString tdoEditorCollideMaterial::m_csGetMaterialType() 
{ 
	return C_szCollideMaterialTypeName; 
}

CString tdoEditorCollideMaterial::m_csGetScriptExtension()
{
	return ".cmt";
}

//ROMTEAM WorldEditor (Cristi Petrescu 11/12/97)

//////////////////////////////////////////////////////////////////////////////////////////////////////
// Method : word tdoEditorCollideMaterial::m_uwTypeOfZonesUsingMe (void)
// Date   : 97.12.11
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Description : 
//  Tells which zone types are using this material
//	The result is a word of bit flags
//  Uses the coherence manager to find the objects using the material
// Author      : Cristi Petrescu
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Modification :
// Date         :
// By           :
//////////////////////////////////////////////////////////////////////////////////////////////////////
WORD tdoEditorCollideMaterial::m_uwTypeOfZonesUsingMe (void)
{
	CList< CPA_BaseObject *, CPA_BaseObject *> oFatherList;
	int iNoObjects;
	WORD uwResult = 0;
	iNoObjects = g_oCoherenceManager . m_fn_iGetFatherList (this, & oFatherList);
	if (iNoObjects)
	{
		POSITION xPos;
		CPA_BaseObject* poBaseObject;

		for ( xPos=oFatherList.GetHeadPosition(); xPos; oFatherList.GetNext(xPos) )
		{
			poBaseObject = 	oFatherList.GetAt (xPos);
			if (poBaseObject -> fn_bIsOfType (C_szGameMaterialTypeName))
				uwResult |= ((tdoEditorGameMaterial *) poBaseObject) -> m_uwTypeOfZonesUsingMe ();
		}
	}
	return uwResult;
}
//ENDROMTEAM WorldEditor (Cristi Petrescu)
