/*=========================================================================
File Name:		DIAMECA.CPP
Purpose:		Implements functions handling the mecanics characteristics
				Implements Following Classes:

				- DiaMeca
				- tdoCopyBuffer
				- tdoDeleteEngineMechanicsModif
				- tdoCreateEngineMechanicsModif
				- tdoPaste

Author:			Yann Le Tensorer
Date:			April/May,1997
==========================================================================*/


#include "stdafx.h"
#include "Acp_Base.h"
#define HieFriend
#include "incgam.h"
#include "gmt.h"
#include "dpt.h"
#include "diameca.hpp"
#include "_interf.hpp"
#include "GMatObj.hpp"
#include "MMatObj.hpp"
#include "x:\cpa\main\inc\_editid.h"
#include "CopyPaste.hpp"
#include "MatList.hpp"
#include "TUT.h"

extern Material_Interface *gs_p_oMaterialInterface;

DiaMeca	*g_oMaterialMeca; // used for loading callbacks

IMPLEMENT_DYNCREATE(DiaMeca, CFormView)

BEGIN_MESSAGE_MAP(DiaMeca, CFormView)
	//{{AFX_MSG_MAP(DiaMeca)
	ON_BN_CLICKED(IDC_CHECK_DISABLE_MECHANICS, OnCheckDisableMechanics)
	ON_EN_CHANGE(IDC_EDIT_ABSORPTION, OnChangeEdits)
	ON_BN_CLICKED(IDC_BUTTON_SET_DEFAULT, OnButtonSetDefault)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_BUTTON_COPYMECA, OnButtonCopyMechanicsMaterial)
	ON_BN_CLICKED(IDC_BUTTON_RENAMEMECA, OnButtonRenameMechanicsMaterial)
	ON_BN_CLICKED(IDC_BUTTON_PASTEMECA, OnButtonPasteMechanicsMaterial)
	ON_CBN_EDITCHANGE(IDC_COMBO_MECMATNAME, OnComboEditChange)
	ON_CBN_SELCHANGE(IDC_COMBO_MECMATNAME, OnComboSelChange)
	ON_BN_CLICKED(IDC_BUTTON_HIGHLIGHTCOLOR, OnButtonHighlightColor)
	ON_BN_CLICKED(IDC_CHECK_HIGHLIGHT, OnCheckHighlight)
	ON_WM_DRAWITEM()
	ON_EN_CHANGE(IDC_EDIT_REBOUND, OnChangeEdits)
	ON_EN_CHANGE(IDC_EDIT_SLIDE, OnChangeEdits)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//==================================================================================================
//==================================================================================================
int DiaMeca::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
	CString csMessage;
	switch ( CWnd::OnToolHitTest(point, pTI) )
	{
	case IDC_COMBO_MECMATNAME:
		csMessage = "Select the current mechanical characteristics for the edited game material / edit its name";
		break;

	case IDC_CHECK_DISABLE_MECHANICS:
		csMessage = "enable/disable mechanical characteristics for the current game material";
		break;

	case IDC_BUTTON_COPYMECA:
		csMessage = "copy the mechanical characteristics of the current game material for further pasting or instanciation";
		break;

	case IDC_BUTTON_PASTEMECA:
		csMessage = "paste the contents of the last copied mechanical characteristics into the current game material";
		break;

	case IDC_BUTTON_RENAMEMECA:
		csMessage = "accept the new name typed in the edit for the current mechanical characteristics";
		break;

	case IDC_BUTTON_SET_DEFAULT:
		csMessage = "use the current values as default when enabling mechanical characteristics for other game materials";
		break;
	}
	//output the help info in the status bar
	fn_vGiveProgressInfo(csMessage, -1);

	//there is no hit for a true tooltip
	return -1;
}

/*=========================================================================
Function Name:  DiaMeca::Diameca
Purpose:		Constructor of Diameca dialog
Author:			Yann Le Tensorer
Date:			April 1997
==========================================================================*/

DiaMeca::DiaMeca()
: CFormView(DiaMeca::IDD)
{
	g_oMaterialMeca=this;

	m_p_oCopyBuffer = NULL;
	m_bAutoDispatch = FALSE;
	m_p_oMaterialCombo = NULL;

	//{{AFX_DATA_INIT(DiaMeca)
	m_Rebound = 0.0;
	m_Slide = 0.0;
	//}}AFX_DATA_INIT

	m_hDefaultMechanicsMaterial = DNM_fn_xMatCharacteristicsCreate();
	m_fn_vLoadDefault();

	m_bAllSelected = FALSE;
	DNM_fn_vInvalidateMatCharacteristics(&m_hHandleToMecMatCharacteristics);
}

/*=========================================================================
Function Name:  DiaMeca::~Diameca
Purpose:		Destructor of Diameca dialog
Author:			Yann Le Tensorer
Date:			April 1997
==========================================================================*/
DiaMeca::~DiaMeca()
{
	if (M_GetMainApp()->m_bLeavingApplication == FALSE)
	{
		if ( DNM_fn_bIsMatCharacteristicsValid(m_hDefaultMechanicsMaterial) )
		{
			DNM_fn_vMatCharacteristicsDestroy(m_hDefaultMechanicsMaterial);
			DNM_fn_vInvalidateMatCharacteristics(&m_hDefaultMechanicsMaterial);
		}
	}
	if ( m_p_oCopyBuffer )
		delete m_p_oCopyBuffer;
}

/*=========================================================================
Function Name:  DiaMeca::DoDataExchange
Purpose:		Used to exchange data between class variables  and dialog
Author:			Yann Le Tensorer
Date:			April 1997
==========================================================================*/
void DiaMeca::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DiaMeca)
	DDX_Control(pDX, IDC_CHECK_DISABLE_MECHANICS, m_CheckDisableMechanics);
	DDX_Text(pDX, IDC_EDIT_REBOUND, m_Rebound);
	DDV_MinMaxDouble(pDX, m_Rebound, -100., 100.);
	DDX_Text(pDX, IDC_EDIT_SLIDE, m_Slide);
	DDV_MinMaxDouble(pDX, m_Slide, -100., 100.);
	//}}AFX_DATA_MAP

	if ( pDX->m_bSaveAndValidate ) //data is moved from the controls to the internal values
	{
		//store the editor values into the engine material
		m_fn_vSetMaterial();
		m_vNotifyChangeToScripts();
	}
	else //move data from internal values to the slider controls
	{
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_REBOUND))->SetPos((int)(m_Rebound * 100));
		((CSliderCtrl*)GetDlgItem(IDC_SLIDER_SLIDE))->SetPos((int)(m_Slide * 100));
	}
}

/*=====================================================================================
Function:	OnChangeEdits()
Purpose:	Handles when any Edit control changed
Author:		Yann Le Tensorer
Date:		April, 1997
====================================================================================*/
void DiaMeca::OnChangeEdits()
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	//move the data from the control to the internal values
	UpdateData(TRUE);

	//and move them into the engine material
	m_fn_vSetMaterial();

	//and make the sliders display the value
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_REBOUND))->SetPos((int)(m_Rebound*100));
	((CSliderCtrl*)GetDlgItem(IDC_SLIDER_SLIDE))->SetPos((int)(m_Slide*100));

	m_bAutoDispatch = FALSE;
}

/*=====================================================================================
Function:	OnHScroll
Purpose:	handle messages send by moving the sliders
Author:		Yann Le Tensorer
Date:		may 5, 1997
====================================================================================*/
void DiaMeca::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	CSliderCtrl* p_oSlider;
	//CEdit *p_oEditToUpdate;

	p_oSlider = (CSliderCtrl*) pScrollBar;

	if ( p_oSlider )
	{
		int iValue = p_oSlider->GetPos(); // value between 0 and 100 of the slider
		double dfValue = ((double) iValue) / 100.0; // same value but between 0 and 1
	
		//compare pointers to find out which slider sent the message
		if ( p_oSlider == ((CSliderCtrl*) GetDlgItem(IDC_SLIDER_REBOUND)) )
		{
			m_Rebound = dfValue;
		}
		else if ( p_oSlider == ((CSliderCtrl*) GetDlgItem(IDC_SLIDER_SLIDE)) )
		{
			m_Slide = dfValue;									 
		}


		/*else
			p_oEditToUpdate = NULL;*/

		/*if ( p_oEditToUpdate )
		{
			CString csText;
			csText.Format("%lf", dfValue);
			//this will cause the value to be stored in the engine material through OnChangeEdits
			p_oEditToUpdate->SetWindowText(csText);
		}*/
		//move the data from the internal values to the control
		UpdateData(FALSE);

		//and move them into the engine material
		m_fn_vSetMaterial();
		//notify changes
		m_vNotifyChangeToScripts();
	}

	m_bAutoDispatch = FALSE;

	CFormView::OnHScroll(nSBCode, nPos, pScrollBar);
}

/*=========================================================================
Function Name:  DiaMeca::m_fn_vSetMaterial
Purpose:		Sets the mechanics paramaters of the current material according
				to those edited in the dialog box
Author:			Yann Le Tensorer
Date:			15-04-97

==========================================================================*/
void DiaMeca::m_fn_vSetMaterial()
{
	if ( DNM_fn_bIsMatCharacteristicsValid(m_hHandleToMecMatCharacteristics) )
	{
		DNM_fn_xMatCharacteristicsSetRebound(m_hHandleToMecMatCharacteristics, (MTH_tdxReal) m_Rebound);
		DNM_fn_xMatCharacteristicsSetSlide(m_hHandleToMecMatCharacteristics, (MTH_tdxReal) m_Slide);
	}
}

/*=========================================================================
Function Name:  DiaMeca::m_fn_vShowMaterial
Purpose:		Show the mechanics paramaters of the current material
Author:			Yann Le Tensorer
Date:			15-04-97

This function is called each time a material is selected.
==========================================================================*/
void DiaMeca::m_fn_vShowMaterial(GMT_tdxHandleToGameMaterial _hMat)
{
//ANNECY Shaitan DisableFunctions (16/04/98) {
	char *szOwnerName;
	char *szFileName;
//ENDANNECY Shaitan DisableFunctions }
	
	tdoEditorGameMaterial *p_oEditedGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
	BOOL bAGameMaterialIsEdited = p_oEditedGameMaterial ? TRUE : FALSE;

//ANNECY Shaitan DisableFunctions (16/04/98) {
	if (bAGameMaterialIsEdited)
	{
		szOwnerName = (char *) (LPCSTR) p_oEditedGameMaterial->GetOwner()->GetName();
		szFileName = strchr(szOwnerName, '\\') + 1;
		m_bCommonFile = (!stricmp(szFileName, C_szCommon)); 
	}

	GetDlgItem(IDC_CHECK_DISABLE_MECHANICS)->EnableWindow(g_bMasterEditor && bAGameMaterialIsEdited);
//ENDANNECY Shaitan DisableFunctions }

	/* if no material is currently selected, disable everything */
	if ( !bAGameMaterialIsEdited || !GMT_fn_b_ulIsValid(_hMat) )
	{
		DNM_fn_vInvalidateMatCharacteristics(&m_hHandleToMecMatCharacteristics);
		m_CheckDisableMechanics.SetCheck(FALSE);
		//clear the name, and disable choosing if no game material is edited
//ANNECY Shaitan DisableFunctions (16/04/98) {
		m_p_oMaterialCombo->EnableWindow(bAGameMaterialIsEdited && (!m_bCommonFile || g_bMasterEditor));
//ENDANNECY Shaitan DisableFunctions }
		m_p_oMaterialCombo->SetCurSel(bAGameMaterialIsEdited ? 0 : CB_ERR);
		m_fn_vEnableWindowFields(FALSE);
		return;
	}	

	//normally, the specified engine game material must belong to the current editor game material...
	ASSERT(_hMat == p_oEditedGameMaterial->GetData());
	//  gets the handle to mecmat
	m_hHandleToMecMatCharacteristics = GMT_fn_hGetMechanicsMaterial(_hMat);

//ANNECY Shaitan DisableFunctions (16/04/98) {
	m_p_oMaterialCombo->EnableWindow(/*TRUE*/!m_bCommonFile || g_bMasterEditor); //enable the selection of another submaterial
//ENDANNECY Shaitan DisableFunctions }

	if ( !DNM_fn_bIsMatCharacteristicsValid(m_hHandleToMecMatCharacteristics) )
	{
		//clear the name
		m_p_oMaterialCombo->SetCurSel(0);
		// pas de MS mechanics
		m_CheckDisableMechanics.SetCheck(FALSE);
		m_fn_vEnableWindowFields(FALSE);
		//there is no submaterial, so disable pasting
		GetDlgItem(IDC_BUTTON_PASTEMECA)->EnableWindow(FALSE);
		//rename is not possible on no sub material...
		GetDlgItem(IDC_BUTTON_RENAMEMECA)->EnableWindow(FALSE);
	}
	else
	{
		tdoEditorMechanicsMaterial *p_oMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
		//put the name in the edit
		m_p_oMaterialCombo->SetCurSel(m_p_oMaterialCombo->FindStringExact(
			-1, p_oMaterial->GetName()
		));
		//enable paste if the buffer is available
//ANNECY Shaitan DisableFunctions (16/04/98) {
		GetDlgItem(IDC_BUTTON_PASTEMECA)->EnableWindow(m_p_oCopyBuffer ? g_bMasterEditor /*TRUE*/ : FALSE);
//ENDANNECY Shaitan DisableFunctions }
		//sice the name in the combo is the name of the submaterial, we cannot rename yet
		GetDlgItem(IDC_BUTTON_RENAMEMECA)->EnableWindow(FALSE);

		//put the material's values into the datae of the dialog
		m_Rebound = DNM_fn_xMatCharacteristicsGetRebound(m_hHandleToMecMatCharacteristics);
		m_Slide = DNM_fn_xMatCharacteristicsGetSlide(m_hHandleToMecMatCharacteristics);
		
		
		//now store the internal datae into the controls
		UpdateData(FALSE);
		// MS mechanics presente
		m_CheckDisableMechanics.SetCheck(TRUE);
		m_fn_vEnableWindowFields(TRUE);
		((CButton *) GetDlgItem(IDC_CHECK_HIGHLIGHT))->SetCheck(p_oMaterial->m_bGetHighlightColor());
	}
	((CButton *) GetDlgItem(IDC_BUTTON_HIGHLIGHTCOLOR))->Invalidate();
}


/////////////////////////////////////////////////////////////////////////////
// DiaMeca diagnostics

#ifdef _DEBUG
//==================================================================================================
//==================================================================================================
void DiaMeca::AssertValid() const
{
	CFormView::AssertValid();
}

//==================================================================================================
//==================================================================================================
void DiaMeca::Dump(CDumpContext &dc) const
{
	CFormView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// DiaMeca message handlers

/*=========================================================================
Function Name:  DiaMeca::m_fn_vEnableWindowFields(true/false)
Purpose:		Called to enable or disable all fields of dialog (except check)
Parameters:		True: Rend les champs actifs, False, les rend inactifs

Author:			Yann Le Tensorer
Date:			16-04-97
==========================================================================*/
void DiaMeca::m_fn_vEnableWindowFields(BOOL bEnable)
{
	if ( bEnable )
		//there is a target submaterial, hance paste is enabled
//ANNECY Shaitan DisableFunctions (16/04/98) {
		GetDlgItem(IDC_BUTTON_PASTEMECA)->EnableWindow(m_p_oCopyBuffer ? g_bMasterEditor /*TRUE*/ : FALSE);
//ENDANNECY Shaitan DisableFunctions }
	else
	{
		//there is no submaterial, so disable pasting
		GetDlgItem(IDC_BUTTON_PASTEMECA)->EnableWindow(FALSE);
		m_Rebound = m_Slide = 0;
		
		//move the data from the internal values to the control
		UpdateData(FALSE);
		((CButton *) GetDlgItem(IDC_CHECK_HIGHLIGHT))->SetCheck(FALSE);
	}

//ANNECY Shaitan DisableFunctions (16/04/98) {
	GetDlgItem( IDC_BUTTON_SET_DEFAULT  )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_BUTTON_LOAD         )->EnableWindow( bEnable );
//	GetDlgItem( IDC_BUTTON_SAVE         )->EnableWindow( bEnable );
//	GetDlgItem( IDC_EDIT_ABSORPTION     )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_FRICTION    )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_SLIDE  )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_ADHESION  )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_PROGRESSION )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_PENETRATION_SPEED   )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_EDIT_PENETRATION_MAX    )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_ABSORPTION)->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_FRICTION    )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_SLIDE  )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_ADHESION  )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_PROGRESSION )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_PENETRATION_SPEED   )->EnableWindow( g_bMasterEditor && bEnable );
//	GetDlgItem( IDC_SLIDER_PENETRATION_MAX    )->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem(IDC_BUTTON_RENAMEMECA)->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem( IDC_BUTTON_COPYMECA )->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem(IDC_CHECK_HIGHLIGHT)->EnableWindow( bEnable );
	GetDlgItem(IDC_BUTTON_HIGHLIGHTCOLOR)->EnableWindow( bEnable );

//ANNECY jt {
	GetDlgItem( IDC_EDIT_REBOUND    )->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem( IDC_EDIT_SLIDE   )->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem( IDC_SLIDER_REBOUND )->EnableWindow( g_bMasterEditor && bEnable );
	GetDlgItem( IDC_SLIDER_SLIDE )->EnableWindow( g_bMasterEditor && bEnable );
//ENDANNECY jt }

//ENDANNECY Shaitan DisableFunctions }
}



/*=========================================================================
Function Name:  DiaMeca::OnCheckDisableMechanics()
Purpose:		Called when clicked on the disable mechanics check box
Author:			Yann Le Tensorer
Date:			16-04-97

This function is called each time the check box is clicked
==========================================================================*/
void DiaMeca::OnCheckDisableMechanics()
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	/* avoid errors */
	if ( !gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
	{
		m_CheckDisableMechanics.SetCheck(FALSE);
		m_fn_vEnableWindowFields(FALSE);
		m_bAutoDispatch = FALSE;
		return;
	}

	//get the handle to the edited game material
	GMT_tdxHandleToGameMaterial hGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial();

	//we enable the mechanics ministructure
	if ( m_CheckDisableMechanics.GetCheck() == TRUE )
	{
		/* Allocate MS Mechanics (using do/undo) */
		if ( !M_p_oGetEditManager()->AskFor(new tdoCreateEngineMechanicsModif(hGameMaterial)) )
		{
			m_CheckDisableMechanics.SetCheck(FALSE);
			m_fn_vEnableWindowFields(TRUE);
		}
	}
	else //we disable the mini structure
	{									  
		/* Free MS Mechanics */
		if ( !M_p_oGetEditManager()->AskFor(new tdoDeleteEngineMechanicsModif(
			hGameMaterial, GMT_fn_hGetMechanicsMaterial(hGameMaterial)
			)) 
		)
		{
			m_CheckDisableMechanics.SetCheck(TRUE);
			m_fn_vEnableWindowFields(FALSE);
		}
	}
	m_bAutoDispatch = FALSE;
}

/*=====================================================================================
Function:	DiaMeca::Create
Purpose:	Overloaded function create of Diameca dialog
Author:		Yann Le Tensorer
Date:		April, 1997
====================================================================================*/
BOOL DiaMeca::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
	if (CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext))
	{
		m_p_oMaterialCombo = (CComboBox *) GetDlgItem(IDC_COMBO_MECMATNAME);
		EnableToolTips(TRUE);
		//move the data from the internal values to the control
		UpdateData(FALSE);
		m_fn_vEnableWindowFields(FALSE);
		GetDlgItem(IDC_CHECK_DISABLE_MECHANICS)->EnableWindow(FALSE);

		//control registration for tutorial and assistants, should be unregistered when view is destroyed
		TUT_M_vGetTutDll(); 
		TUT_M_vRegisterControlID(IDC_CHECK_DISABLE_MECHANICS,"TGM_DiaMecaCheckDisableMechanics",TUT_e_Button);
		TUT_M_vRegisterControlID(IDC_BUTTON_HIGHLIGHTCOLOR,"TGM_DiaMecaButtonHighlightColor",TUT_e_Button);
		TUT_M_vRegisterControlID(IDC_CHECK_HIGHLIGHT,"TGM_DiaMecaCheckHighlight",TUT_e_Button);
		TUT_M_vRegisterControlID(IDC_COMBO_MECMATNAME,"TGM_DiaMecaComboMaterialName",TUT_e_ComboBox);
		TUT_M_vRegisterControlID(IDC_BUTTON_COPYMECA,"TGM_DiaMecaButtonCopyMaterial",TUT_e_Button);
		TUT_M_vRegisterControlID(IDC_BUTTON_PASTEMECA,"TGM_DiaMecaButtonPasteMaterial",TUT_e_Button);
		TUT_M_vRegisterControlID(IDC_BUTTON_RENAMEMECA,"TGM_DiaMecaButtonRenameMaterial",TUT_e_Button);
//		TUT_M_vRegisterControlID(IDC_EDIT_ABSORPTION,"TGM_DiaMecaEditAbsorption",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_FRICTION,"TGM_DiaMecaEditFriction",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_SLIDE,"TGM_DiaMecaEditSlide",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_ADHESION,"TGM_DiaMecaEditAdhesion",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_PROGRESSION,"TGM_DiaMecaEditProgression",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_PENETRATION_SPEED,"TGM_DiaMecaEditPenetrationSpeed",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_EDIT_PENETRATION_MAX,"TGM_DiaMecaEditPenetrationMax",TUT_e_TextEdit);
//		TUT_M_vRegisterControlID(IDC_SLIDER_ABSORPTION,"TGM_DiaMecaSliderAbsorption",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_FRICTION,"TGM_DiaMecaSliderFriction",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_SLIDE,"TGM_DiaMecaSliderSlide",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_ADHESION,"TGM_DiaMecaSliderAdhesion",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_PROGRESSION,"TGM_DiaMecaSliderProgression",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_PENETRATION_SPEED,"TGM_DiaMecaSliderPenetrationSpeed",TUT_e_Slider);
//		TUT_M_vRegisterControlID(IDC_SLIDER_PENETRATION_MAX,"TGM_DiaMecaSliderPenetrationMax",TUT_e_Slider);
		TUT_M_vRegisterControlID(IDC_BUTTON_SET_DEFAULT,"TGM_DiaMecaButtonSetDefault",TUT_e_Button);

		return TRUE;
	}
	else
		return FALSE;
}

/*=====================================================================================
Function:	m_vNotifyChangeToScripts()
Purpose:	Notify changes to script
Author:		Benoit Germain
Date:		May 5, 1997
====================================================================================*/
void DiaMeca::m_vNotifyChangeToScripts()
{
	if ( DNM_fn_bIsMatCharacteristicsValid(m_hHandleToMecMatCharacteristics) )
	{
		gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics)
		->fn_vNotifySave();
	}
}
 
/*=====================================================================================
Function:	OnButtonSetDefault
Purpose:	Handles when button "set default" is pressed
Author:		Yann Le Tensorer
Date:		April, 1997
====================================================================================*/
void DiaMeca::OnButtonSetDefault()
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	if ( DNM_fn_bIsMatCharacteristicsValid(m_hDefaultMechanicsMaterial) )
	{
		DNM_fn_xMatCharacteristicsSetRebound(m_hDefaultMechanicsMaterial, (MTH_tdxReal) m_Rebound);
		DNM_fn_xMatCharacteristicsSetSlide(m_hDefaultMechanicsMaterial, (MTH_tdxReal) m_Slide);
		
		// sauve le fichier mat_meca.ini
		m_fn_vSaveDefault();
	}

	m_bAutoDispatch = FALSE;
}

/*=====================================================================================
Function:	fn_v_SaveDefault
Purpose:	Save default values for meca parameteres
Author:		Yann Le Tensorer
Date:		may 1, 1997
====================================================================================*/
void DiaMeca::m_fn_vSaveDefault()
{
	SCR_tdst_File_Description stFileDesc;

	CString csBaseDataPath = "Edt_Data\\tools\\Material\\";

	SCR_FILE xFileHandle;
	CString csFileName(csBaseDataPath + "mat_meca.ini");

	//p_xFile = fopen(p_FileName, "wb");
	xFileHandle = SCR_M_p_x_File_OpenWrite(csFileName);

	if ( xFileHandle == NULL )
	{
		CString ErrorMsg;

		ErrorMsg = "Could not create file: " + csFileName;
		ErrorMsg+="\n\nEither the path \"" + csBaseDataPath + "\" doesn't exist, or the file is write protected";
		AfxMessageBox( ErrorMsg );
		//free(p_FileName);
		return;
	}

	char	buffer[30];
	
	SCR_fn_v_File_InitFileDes(&stFileDesc, (char*)LPCTSTR(csFileName), xFileHandle);
	SCR_M_SvL0_SaveComment(&stFileDesc,"Editeur de matériaux, paramètres de mécanique, sauvegarde des valeurs par defaut");

	SCR_M_SvL0_SaveBeginSection(&stFileDesc, "MECANIC:", SCR_CC_C_Cfg_EOL); 
	
	sprintf( buffer , "%.4g", (float)m_Rebound);
	SCR_M_SvL0_SaveEntry(&stFileDesc,"Rebound",SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(&stFileDesc, SCR_EF_SvL0_Normal, 1, buffer);

	sprintf( buffer , "%.4g", (float)m_Slide);
	SCR_M_SvL0_SaveEntry(&stFileDesc,"Slide1",SCR_CC_C_Cfg_NoChar);
	SCR_fn_v_SvL0_SaveParameters_MP(&stFileDesc, SCR_EF_SvL0_Normal, 1, buffer);

	SCR_M_SvL0_SaveEndSection(&stFileDesc, SCR_CC_C_Cfg_EOL); 

	//fclose(p_xFile);
	SCR_M_File_Close(&stFileDesc);
	SCR_fn_v_File_CloseFileDes(&stFileDesc);
}

/*=====================================================================================
Function:	fn_v_LoadMecaDefault
Purpose:	callback function for loading default parameters
Author:		Yann Le Tensorer
Date:		may 1, 1997
====================================================================================*/
SCR_tde_Anl_ReturnValue DiaMeca::fn_v_LoadMecaDefault
(
	SCR_tdst_File_Description * /*_p_stFile*/,
	char *_p_szName,
	char *_ap_szPars[],
  SCR_tde_Anl_Action _eAction
)
{
	SCR_tde_Anl_ReturnValue eReturnValue = SCR_ERV_Anl_NormalReturn;
  
	if (_eAction == SCR_EA_Anl_Entry)
	{  
		if ( DNM_fn_bIsMatCharacteristicsValid(g_oMaterialMeca->m_hDefaultMechanicsMaterial) )
		{
			CString csName = _p_szName;
			if ( csName == "Rebound" )
				DNM_fn_xMatCharacteristicsSetRebound(g_oMaterialMeca->m_hDefaultMechanicsMaterial, (MTH_tdxReal) atof(_ap_szPars[0]));
			else if ( csName == "Slide1" )
				DNM_fn_xMatCharacteristicsSetSlide(g_oMaterialMeca->m_hDefaultMechanicsMaterial, (MTH_tdxReal) atof(_ap_szPars[0]));
		}
	}
	return eReturnValue; 
}

/*=====================================================================================
Function:	m_fn_vInitLoad()
Purpose:	register callback for load ing default parameters
Author:		Yann Le Tensorer
Date:		may 1, 1997
====================================================================================*/
void DiaMeca::m_fn_vInitLoad() 
{
	SCR_fn_v_RdL0_RegisterCallback("MECANIC",DiaMeca::fn_v_LoadMecaDefault, SCR_CRC_c_RdL0_ForSection);
}


/*=====================================================================================
Function:	m_fn_vLoadDefault()
Purpose:	Load default values of mecanic parameters
Author:		Yann Le Tensorer
Date:		may 1, 1997
====================================================================================*/
void DiaMeca::m_fn_vLoadDefault()
{
	CString csBaseDataPath = "Edt_Data\\tools\\Material\\";

	FILE    *myfile;
	char	p_FileName[255];

	strcpy(p_FileName,csBaseDataPath + "mat_meca.ini");
     

	if ( (myfile = fopen(p_FileName,"rb")) != NULL ) /* if file exists */
	{
		fclose(myfile);
		SCR_fnp_st_RdL0_AnalyseSection(p_FileName, SCR_CDF_uw_Anl_Normal);
	}

}

/*==================================================================================================
  Réponses à l'appui sur le bouton Copy
==================================================================================================*/
void DiaMeca::OnButtonCopyMechanicsMaterial() 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	if ( m_p_oCopyBuffer )
		delete m_p_oCopyBuffer;
	m_p_oCopyBuffer = new tdoCopyBuffer(m_hHandleToMecMatCharacteristics);

//ANNECY Shaitan DisableFunctions (16/04/98) {
	GetDlgItem(IDC_BUTTON_PASTEMECA)->EnableWindow(g_bMasterEditor /*TRUE*/);
//ENDANNECY Shaitan DisableFunctions }

	m_bAutoDispatch = FALSE;
}

/*==================================================================================================
  when the submaterial is renamed
==================================================================================================*/
void DiaMeca::OnButtonRenameMechanicsMaterial() 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	tdoEditorMechanicsMaterial *p_oMechanicsMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
	if ( p_oMechanicsMaterial )
	{
		CString csOldName = p_oMechanicsMaterial->GetName();
		CString csNewName;
		m_p_oMaterialCombo->GetWindowText(csNewName);
		if ( csNewName.IsEmpty() )
			csNewName = 	C_szMechanicsMaterialTypeName "1";
	  csNewName = gs_p_oMaterialInterface->GetInterface()->GetPrefixedName(csNewName);
		//this will cause a OnModifyChild() message to be sent for each referencing game material
		p_oMechanicsMaterial->fn_eRename(csNewName);
		//now update the combo with the new name
		short wIndex = m_p_oMaterialCombo->FindStringExact(-1, csOldName);
		m_p_oMaterialCombo->DeleteString(wIndex);
		m_p_oMaterialCombo->InsertString(wIndex, p_oMechanicsMaterial->GetName());
		m_p_oMaterialCombo->SetItemDataPtr(wIndex, p_oMechanicsMaterial);
		m_p_oMaterialCombo->SetCurSel(wIndex);
	}

	m_bAutoDispatch = FALSE;
}

/*==================================================================================================
  Réponses à l'appui sur le bouton Paste
==================================================================================================*/
void DiaMeca::OnButtonPasteMechanicsMaterial() 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	if ( m_p_oCopyBuffer )
		M_p_oGetEditManager()->AskFor(new tdoPasteSubMaterialModif(m_p_oCopyBuffer));

	m_bAutoDispatch = FALSE;

	//fake a subsequent copy, so that we can paste or instaciate again if we want to
	OnButtonCopyMechanicsMaterial();
}

/*==================================================================================================
  when the text changes in the edit part of the combo
==================================================================================================*/
void DiaMeca::OnComboEditChange() 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	CString csMaterialName;
	m_p_oMaterialCombo->GetWindowText(csMaterialName);
	if ( csMaterialName.IsEmpty() )
		csMaterialName = 	C_szMechanicsMaterialTypeName "1";
	csMaterialName = gs_p_oMaterialInterface->GetInterface()->GetPrefixedName(csMaterialName);

	tdoEditorMechanicsMaterial *p_oMechanicsMaterial = NULL;
	//if we did not find a material with the engine, try to find one with the name, in case the script was
	//parsed and editor materials were created for unloaded sections -> they have no valid engine material
	if ( !csMaterialName.IsEmpty() )
		p_oMechanicsMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->GetMainWorld()->fn_p_oFindObject(
			csMaterialName, C_szMechanicsMaterialTypeName
		);

	//to enable a rename, the name must be unique, and a current material must be selected
//ANNECY Shaitan DisableFunctions (16/04/98) {
	GetDlgItem(IDC_BUTTON_RENAMEMECA)->EnableWindow(p_oMechanicsMaterial ? FALSE : g_bMasterEditor /*TRUE*/);
//ENDANNECY Shaitan DisableFunctions }

	m_bAutoDispatch = FALSE;
}

/*=========================================================================
//  Description : Edit a color, return true if color has changed
==========================================================================*/
BOOL DiaMeca::m_bEditColor(GLI_tdstColor *p_stColor)
{
	COLORREF				cr = RGB(p_stColor->xR * 255, p_stColor->xG * 255, p_stColor->xB * 255);
	CColorDialog		CColor( cr, CC_RGBINIT | CC_FULLOPEN, this );
	if (CColor.DoModal() == IDOK)
	{
		cr = CColor.GetColor();
		p_stColor->xR = (GEO_tdxColorValue) (GetRValue(cr) / 255.0);
		p_stColor->xG = (GEO_tdxColorValue) (GetGValue(cr) / 255.0);
		p_stColor->xB = (GEO_tdxColorValue) (GetBValue(cr) / 255.0);
		p_stColor->xA = (GEO_tdxColorValue) 0.0;
		return TRUE;
	}
	return FALSE;
}

/*==================================================================================================
  When the user selects another submaterial
==================================================================================================*/
void DiaMeca::OnComboSelChange() 
{
	if ( m_bAutoDispatch )
		return;
	m_bAutoDispatch = TRUE;

	short wIndex = m_p_oMaterialCombo->GetCurSel();
	if ( wIndex != CB_ERR )
	{
		tdoEditorGameMaterial *p_oGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
		GMT_tdxHandleToGameMaterial hGameMaterial = p_oGameMaterial->m_hGetEngineMaterial();
		tdoEditorMechanicsMaterial *p_oNewMechanicsMaterial = (tdoEditorMechanicsMaterial *) m_p_oMaterialCombo->GetItemDataPtr(wIndex);
		tdoEditorMechanicsMaterial *p_oPrevMechanicsMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
		//if the newly selected material is not the previous one
		if ( p_oPrevMechanicsMaterial != p_oNewMechanicsMaterial )
		{
			//update coherence manager links
			if ( p_oPrevMechanicsMaterial )
				g_oCoherenceManager.m_fn_vRemoveALink(p_oGameMaterial, p_oPrevMechanicsMaterial);
			if ( p_oNewMechanicsMaterial )
				g_oCoherenceManager.m_fn_vAddALink(p_oGameMaterial,	p_oNewMechanicsMaterial);

			//get the engine handle for the new submaterial
			DNM_tdxHandleToMecMatCharacteristics hNewMechanicsMaterial;
			if ( p_oNewMechanicsMaterial )
			{
				p_oNewMechanicsMaterial->m_vLoadAssociatedEngineMaterial(); //in case it was not analyzed yet...
				hNewMechanicsMaterial = p_oNewMechanicsMaterial->m_hGetEngineMaterial();
			}
			else
				DNM_fn_vInvalidateMatCharacteristics(&hNewMechanicsMaterial);

			//give the handle to the game material
			GMT_fn_vSetMechanicsMaterial(hGameMaterial, hNewMechanicsMaterial);
			//the game material will have to be saved
			p_oGameMaterial->fn_vNotifySave();
			//update the display
			m_fn_vShowMaterial(hGameMaterial);
			//and update world display if necessary as well
			if 
			( 
				(p_oPrevMechanicsMaterial ? p_oPrevMechanicsMaterial->m_bGetHighlightColor() : FALSE) 
				|| (p_oNewMechanicsMaterial ? p_oNewMechanicsMaterial->m_bGetHighlightColor() : FALSE)
			)
				M_GetMainDevice3D()->DrawObject();
		}
	}

	m_bAutoDispatch = FALSE;
}

//==================================================================================================
//==================================================================================================
void DiaMeca::OnButtonHighlightColor() 
{
	GEO_tdstColor stTempColor;
	tdoEditorMechanicsMaterial *p_oMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
	if ( p_oMaterial && p_oMaterial->m_bGetHighlightColor(&stTempColor) && m_bEditColor(&stTempColor) )
	{
		p_oMaterial->m_vSetHighlightColor(stTempColor);
		GetDlgItem(IDC_BUTTON_HIGHLIGHTCOLOR)->Invalidate();
		//tell the main view to redraw itself because a material highlight changed aspect
		M_GetMainDevice3D()->DrawObject();
	}
}

//==================================================================================================
//==================================================================================================
void DiaMeca::OnCheckHighlight() 
{
	tdoEditorMechanicsMaterial *p_oMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
	if ( p_oMaterial )
	{
		p_oMaterial->m_vEnableHighlight(((CButton *)GetDlgItem(IDC_CHECK_HIGHLIGHT))->GetCheck());
		//the highlight color is no longer displayed in the button...
		GetDlgItem(IDC_BUTTON_HIGHLIGHTCOLOR)->Invalidate();
		M_GetMainDevice3D()->DrawObject();
	}
}

//==================================================================================================
//==================================================================================================
void DiaMeca::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	switch ( nIDCtl )
	{
	default:
		CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
		break;

	case IDC_BUTTON_HIGHLIGHTCOLOR:
		{
			CDC      *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
			RECT					*rect = &lpDrawItemStruct->rcItem;
			GEO_tdstColor stColor;
			COLORREF			xColor;
			tdoEditorMechanicsMaterial *p_oMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hHandleToMecMatCharacteristics);
			if ( p_oMaterial && p_oMaterial->m_bGetHighlightColor(&stColor) )
			{
				xColor = RGB( stColor.xR * 255, stColor.xG * 255, stColor.xB * 255 );
			}
			else
				xColor = RGB(192,192,192);
			pDC->FillSolidRect( rect, xColor);
		}
		break;
	}
}

//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
/*=====================================================================================
Function:	tdoCreateEngineMechanicsModif::~tdoCreateEngineMechanicsModif
Purpose:	constructor of create mecanics mini-structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
tdoCreateEngineMechanicsModif::tdoCreateEngineMechanicsModif(GMT_tdxHandleToGameMaterial _MyMatHandle)
: CPA_Modif(0, "Enable Mechanics")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oDestroyModif = NULL;
	
	DNM_fn_vInvalidateMatCharacteristics(&m_hMechanicsAfterDo);
}

/*=====================================================================================
Function:	tdoCreateEngineMechanicsModif::~tdoCreateEngineMechanicsModif
Purpose:	destructor of create mecanics mini-structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
tdoCreateEngineMechanicsModif::~tdoCreateEngineMechanicsModif()
{
	if ( m_p_oDestroyModif )
	{
		delete m_p_oDestroyModif;
		m_p_oDestroyModif = NULL;
	}
}

/*=====================================================================================
Function:	tdoCreateEngineMechanicsModif::Do()
Purpose:	Do of create mecanics mini-structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
BOOL tdoCreateEngineMechanicsModif::Do()
{
	if ( m_p_oDestroyModif ) //if it is created, this is a Redo -> An Undo of our destroy modif
		return m_p_oDestroyModif->Undo();
	else //this is the first time we Do() the action
	{
		//create an engine material and the associated editor material
		DNM_tdxHandleToMecMatCharacteristics hNewMechanicsMaterial = DNM_fn_xMatCharacteristicsCreate();
		if ( DNM_fn_bIsMatCharacteristicsValid(hNewMechanicsMaterial) )
		{
			//get the material that we see in the dialog, and the material modified by the operation
			tdoEditorGameMaterial *p_oCurrentEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
			tdoEditorGameMaterial *p_oModifiedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);

			//copy the default material in the new material
			DNM_fn_vMatCharacteristicsCopy(
				hNewMechanicsMaterial, gs_p_oMaterialInterface->m_p_oGetMecaView()->m_hDefaultMechanicsMaterial
			);
			//create the associated editor material
			tdoEditorMechanicsMaterial *p_oNewEditorMechanicsMaterial = new tdoEditorMechanicsMaterial(
				p_oGetFileObjectForReferencedPath(CString("ED") + tdoEditorMechanicsMaterial::m_csGetScriptExtension()),
				p_oModifiedGameMaterial->GetName() /*C_szMechanicsMaterialTypeName "1"*/,
				FALSE,
				hNewMechanicsMaterial
			);

			//add the new submaterial in the display combo
			CComboBox *p_oCombo = gs_p_oMaterialInterface->m_p_oGetMecaView()->m_p_oMaterialCombo;
			short wComboIndex = p_oCombo->AddString(p_oNewEditorMechanicsMaterial->GetName());
			p_oCombo->SetItemDataPtr(wComboIndex, p_oNewEditorMechanicsMaterial);

			//give the mechanics material to the game material
			GMT_fn_vSetMechanicsMaterial(m_hEditedGameMaterial, hNewMechanicsMaterial);

			//notify that game material section is to be updated
			p_oModifiedGameMaterial->fn_vNotifySave();
			//tell the coherence manager that the game material references the mechanics material
			g_oCoherenceManager.m_fn_vAddALink(p_oModifiedGameMaterial,	p_oNewEditorMechanicsMaterial);

			//if the modified game material in displayed in the dialogs, update display
			if ( p_oCurrentEditedGameMaterial == p_oModifiedGameMaterial )
			{
				//update controls
				gs_p_oMaterialInterface->m_p_oGetMecaView()->m_fn_vEnableWindowFields(TRUE);
				if ( !gs_p_oMaterialInterface->m_p_oGetMecaView()->m_CheckDisableMechanics.GetCheck() ) 
					gs_p_oMaterialInterface->m_p_oGetMecaView()->m_CheckDisableMechanics.SetCheck(TRUE);
				//fill them with the contents of the edited material
				gs_p_oMaterialInterface->m_p_oGetMecaView()->m_fn_vShowMaterial(p_oCurrentEditedGameMaterial->m_hGetEngineMaterial());
			}

			//create the destroy modif for future undo/redo actions
			m_p_oDestroyModif = new tdoDeleteEngineMechanicsModif(m_hEditedGameMaterial, p_oNewEditorMechanicsMaterial->m_hGetEngineMaterial());
			return TRUE;
		}
		else //the engine object could not be created
			return FALSE;
	}
}

/*=====================================================================================
Function:	tdoCreateEngineMechanicsModif::Undo()
Purpose:	Undo of create mecanics mini-structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
BOOL tdoCreateEngineMechanicsModif::Undo()
{
	return m_p_oDestroyModif ? m_p_oDestroyModif->Do() : FALSE;
}

//==================================================================================================
//==================================================================================================
//==================================================================================================
/*=====================================================================================
Function:	tdoDeleteEngineMechanicsModif::tdoDeleteEngineMechanicsModif()
Purpose:	constructor of delete mecanics mini structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
tdoDeleteEngineMechanicsModif::tdoDeleteEngineMechanicsModif(
	GMT_tdxHandleToGameMaterial _MyMatHandle,
	DNM_tdxHandleToMecMatCharacteristics _hMechanicsMaterial
) : CPA_Modif(0, "Disable Mechanics")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_hEditedMechanicsMaterial = _hMechanicsMaterial;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);
	m_p_oEditedMechanicsMaterial = (tdoEditorMechanicsMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedMechanicsMaterial);
}

/*=====================================================================================
Function:	tdoDeleteEngineMechanicsModif::~tdoDeleteEngineMechanicsModif()
Purpose:	destructor of delete mecanics mini structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
tdoDeleteEngineMechanicsModif::~tdoDeleteEngineMechanicsModif()
{
	if ( HasBeenDone() ) //if the removal is done when the object is destroyed
	{
		//if the mechanics material is not used by any game material, destroy it
		if ( !m_p_oEditedMechanicsMaterial->m_bIsReferenced() )
		{
			//we are the only one to hold the handle for the mechanics material
			//hence we can free it from memory
			DNM_fn_vMatCharacteristicsDestroy(m_hEditedMechanicsMaterial);
			DNM_fn_vInvalidateMatCharacteristics(&m_hEditedMechanicsMaterial);
			//do not forget to destroy the editor object as well
			delete m_p_oEditedMechanicsMaterial;
			m_p_oEditedMechanicsMaterial = NULL;
		}
	}
}

/*=====================================================================================
Function:	tdoDeleteEngineMechanicsModif::Do()
Purpose:	Do of delete mecanics mini structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
BOOL tdoDeleteEngineMechanicsModif::Do()
{
	if ( DNM_fn_bIsMatCharacteristicsValid(m_hEditedMechanicsMaterial) )
	{
		//tell the coherence manager that the game material does not reference the mechanics material
		g_oCoherenceManager.m_fn_vRemoveALink(m_p_oEditedGameMaterial, m_p_oEditedMechanicsMaterial);
		//if there remains no game material using this submaterial
		if ( !m_p_oEditedMechanicsMaterial->m_bIsReferenced() )
		{
			//try to remove the object from the hierarchy
			if ( !m_p_oEditedMechanicsMaterial->fn_bUnValidate() ) //if we failed
			{
				//restore the link as it was before
				g_oCoherenceManager.m_fn_vAddALink(m_p_oEditedGameMaterial, m_p_oEditedMechanicsMaterial);
				//the operation could not be done
				return FALSE;
			}
			//now that the object no longer exists for the hierarchy, tell the scripts not to save it
			m_p_oEditedMechanicsMaterial->fn_vNotifyUnSave();
			//and remove it from the combo in which it is listed
			CComboBox *p_oCombo = gs_p_oMaterialInterface->m_p_oGetMecaView()->m_p_oMaterialCombo;
			short wComboIndex = p_oCombo->FindStringExact(-1, m_p_oEditedMechanicsMaterial->GetName());
			p_oCombo->DeleteString(wComboIndex);
		}
		//tell the game material not to use the mechanics material any more
		DNM_tdxHandleToMecMatCharacteristics hMyMecaHandle;
		DNM_fn_vInvalidateMatCharacteristics(&hMyMecaHandle);
		GMT_fn_vSetMechanicsMaterial(m_hEditedGameMaterial,hMyMecaHandle);
		//tell the scripts that the game material section is to be updated
		m_p_oEditedGameMaterial->fn_vNotifySave();


		//update the display if the modified game material is being displayed by the editor
		if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
		{
			GMT_tdxHandleToGameMaterial hInvalidGameMaterial;
			GMT_fn_vInvalidate(&hInvalidGameMaterial);
			gs_p_oMaterialInterface->m_p_oGetMecaView()->m_fn_vShowMaterial(hInvalidGameMaterial);
		}
		return TRUE;
	}
	//we did nothing
	return FALSE;
}


/*=====================================================================================
Function:	tdoDeleteEngineMechanicsModif::Undo()
Purpose:	Undo of delete mecanics mini structure
Author:		Yann Le Tensorer/Benoit Germain
Date:		April, 1997
====================================================================================*/
BOOL tdoDeleteEngineMechanicsModif::Undo()
{
	if ( DNM_fn_bIsMatCharacteristicsValid(m_hEditedMechanicsMaterial) )
	{
		//give back the mechanics material to the game material
		GMT_fn_vSetMechanicsMaterial(m_hEditedGameMaterial, m_hEditedMechanicsMaterial);

		//tell we want to save it again
		if ( !m_p_oEditedMechanicsMaterial->fn_bIsValid() )
		{
			m_p_oEditedMechanicsMaterial->fn_bValidate();
			m_p_oEditedMechanicsMaterial->fn_vNotifyRestore();
			//and restore it in the combo in which it was listed
			CComboBox *p_oCombo = gs_p_oMaterialInterface->m_p_oGetMecaView()->m_p_oMaterialCombo;
			short wComboIndex = p_oCombo->AddString(m_p_oEditedMechanicsMaterial->GetName());
			p_oCombo->SetItemDataPtr(wComboIndex, m_p_oEditedMechanicsMaterial);
		}
		//tell the scripts that the game material section is to be updated
		m_p_oEditedGameMaterial->fn_vNotifySave();

		//tell the coherence manager that the game material references the mechanics material
		g_oCoherenceManager.m_fn_vAddALink(m_p_oEditedGameMaterial, m_p_oEditedMechanicsMaterial);

		//update the display if the modified game material is being displayed by the editor
		if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
			gs_p_oMaterialInterface->m_p_oGetMecaView()->m_fn_vShowMaterial(m_p_oEditedGameMaterial->m_hGetEngineMaterial());
		return TRUE;
	}
	//we did nothing
	return FALSE;
}


