// DiaView.cpp : implementation file
//

#include "stdafx.h"
#include "ACP_Base.h"
#include "matres.h"
#include "itf.h"
#include "gli.h"
#include "gmt.h"
#include "dpt.h"
#include "MatList.hpp"
#include "inctex.h"
#include "_interf.hpp"
#include "GMatObj.hpp"
#include "VMatObj.hpp"
#include "TexObj.hpp"
#include "DiaView.hpp"
#include "PatternList.hpp"
#include "DiaText.hpp"
#include "TextureDlg.h"
#include "choosecolor.hpp"
#include "x:\cpa\main\inc\_editid.h"
#include "TUT.h"


#define C_fAutoScrollEditCoeff 100.0f
#define C_szAutoScrollStringFormat "%6.2f"

extern Material_Interface *gs_p_oMaterialInterface;
/////////////////////////////////////////////////////////////////////////////
// DiaView dialog


IMPLEMENT_DYNCREATE(DiaView, CFormView)

//==================================================================================================
//==================================================================================================
DiaView::DiaView()
	: CFormView(DiaView::IDD)
{
	//{{AFX_DATA_INIT(DiaView)
	//}}AFX_DATA_INIT
  m_bControlsFilledFromMaterial = FALSE;
  m_hEditedVisualMaterial = NULL; //NULL is the invalid value, until otherwise specified...
}

//==================================================================================================
//==================================================================================================
DiaView::~DiaView()
{
}

//==================================================================================================
//==================================================================================================
void DiaView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DiaView)
	//}}AFX_DATA_MAP
}

//==================================================================================================
//==================================================================================================
BEGIN_MESSAGE_MAP(DiaView, CFormView)
	//{{AFX_MSG_MAP(DiaView)
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_CHECK_TEXTURE, OnCheckTexture)
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_DIFFUSE, OnButtonDiffuse)
	ON_BN_CLICKED(IDC_BUTTON_SPECULAR, OnButtonSpecular)
	ON_BN_CLICKED(IDC_BUTTON_AMBIENT, OnButtonAmbient)
	ON_BN_CLICKED(IDC_CHECK_GOURAUD, OnCheckGouraud)
	ON_EN_CHANGE(IDC_EDIT_SPECEXP, OnChangeEditSpecExp)
	ON_BN_CLICKED(IDC_CHECK_CHROME, OnCheckChrome)
	ON_BN_CLICKED(IDC_CHECK_TRANS, OnCheckTrans)
	ON_BN_CLICKED(IDC_CHECK_ALPHASENSITIVE, OnCheckAlphaSensitive)
	ON_BN_CLICKED(IDC_BUTTON_ADDANIMATED, OnButtonAddAnimatedTexture)
	ON_CBN_SELCHANGE(IDC_COMBO_ANIMATEDTEXTURES, OnSelchangeComboAnimatedTextures)
	ON_BN_CLICKED(IDC_BUTTON_REMOVEANIMATED, OnButtonRemoveAnimatedTexture)
	ON_BN_CLICKED(IDC_BUTTON_MOVEUP, OnButtonMoveUpTexture)
	ON_BN_CLICKED(IDC_BUTTON_MOVEDOWN, OnButtonMoveDownTexture)
	ON_BN_CLICKED(IDC_CHECK_AUTOSCROLL, OnCheckAutoScroll)
	ON_EN_KILLFOCUS(IDC_EDIT_HORIZONTALSCROLL, OnKillFocusEditHorizontalScroll)
	ON_EN_KILLFOCUS(IDC_EDIT_VERTICALSCROLL, OnKillFocusEditVerticalScroll)
	ON_BN_CLICKED(IDC_CHECK_BACKFACE, OnCheckBackface)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

//==================================================================================================
//==================================================================================================
int DiaView::OnToolHitTest(CPoint point, TOOLINFO* pTI) const
{
	CString csMessage;
	switch ( CWnd::OnToolHitTest(point, pTI) )
	{
	default:
		csMessage = "???????????????????????????????????????????????????";
		break;

	case IDC_STATIC:
		csMessage = "";
		break;

	case IDC_BUTTON_DIFFUSE:
	case IDC_BUTTON_AMBIENT:
	case IDC_BUTTON_SPECULAR:
		csMessage = "view/edit the color of the current game material";
		break;

	case IDC_EDIT_SPECEXP:
		csMessage = "edit the specularity of the current game material";
		break;

	case IDC_CHECK_CHROME:
		csMessage = "make the material chromed";
		break;

	case IDC_CHECK_BACKFACE:
		csMessage = "tell that the material is to be seen from both sides of a single-sided face";
		break;

	case IDC_CHECK_TEXTURE:
		csMessage = "enable/disable the texture of the current game material";
		break;

	case IDC_CHECK_GOURAUD:
		csMessage = "select the rendering method for the current game material";
		break;

	case IDC_CHECK_TRANS:
		csMessage = "enable/disable transparency of the current game material";
		break;

	case IDC_CHECK_ALPHASENSITIVE:
		csMessage = "enable/disable transparent light sensitivity";
		break;

	case IDC_CHECK_AUTOSCROLL:
		csMessage = "enable/disable texture autoscrolling";
		break;
	}
	//output the help info in the status bar
	fn_vGiveProgressInfo(csMessage, -1);

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

//==================================================================================================
//==================================================================================================
//void DiaView::fn_vInitDialog ( void )
//{
//	UpdateData(FALSE);
//}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vShowMaterial(GMT_tdxHandleToGameMaterial _hMat, int _iTextureIndex /*= -1*/)
{
	tdoEditorGameMaterial *p_oEditedGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
	BOOL bAGameMAterialIsEdited = p_oEditedGameMaterial ? TRUE : FALSE;
	static BOOL s_bFirstDone = FALSE;

	if ( !s_bFirstDone )
	{
		s_bFirstDone = TRUE;
		//enable the buttons so that they will be redisplayed properly
		GetDlgItem(IDC_BUTTON_MOVEUP         )->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_MOVEDOWN       )->EnableWindow(TRUE);
		//attach the icons to the buttons
		HICON hIcon;
		hIcon = AfxGetApp()->LoadIcon(IDI_ICON_MOVEDOWN);
		((CButton *) GetDlgItem(IDC_BUTTON_MOVEDOWN))->SetIcon(hIcon);
		hIcon = AfxGetApp()->LoadIcon(IDI_ICON_MOVEUP);
		((CButton *) GetDlgItem(IDC_BUTTON_MOVEUP))->SetIcon(hIcon);
		//and force redisplay of their contents
		GetDlgItem(IDC_BUTTON_MOVEUP)->Invalidate();
		GetDlgItem(IDC_BUTTON_MOVEDOWN)->Invalidate();
	}

	/* if no material is currently selected, disable everything */
	if ( !bAGameMAterialIsEdited )
	{
		m_hEditedVisualMaterial = NULL; //invalidate the handle
		//clear the controls
		((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->SetCurSel(CB_ERR);
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL )->SetWindowText("");
		GetDlgItem(IDC_EDIT_VERTICALSCROLL   )->SetWindowText("");
		GetDlgItem(IDC_EDIT_SPECEXP          )->SetWindowText("");
		((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->SetCheck(FALSE);
		//and disable them
		GetDlgItem(IDC_CHECK_GOURAUD         )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_DIFFUSE        )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_AMBIENT        )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_SPECULAR       )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_AUTOSCROLL      )->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL )->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_VERTICALSCROLL   )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_TRANS           )->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_SPECEXP          )->EnableWindow(FALSE);
		GetDlgItem(IDC_SPIN_SPECEXP          )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_TEXTURE         )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_CHROME          )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_BACKFACE        )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_ALPHASENSITIVE  )->EnableWindow(FALSE);
		GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_ADDANIMATED    )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_REMOVEANIMATED )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_MOVEUP         )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_MOVEDOWN       )->EnableWindow(FALSE);
		//tell the texture view to clear itself
		gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(_hMat);
		return;												 
	}

	//normally, the specified engine game material must belong to the current editor game material...
	ASSERT(_hMat == p_oEditedGameMaterial->GetData());
	//get the submaterial's handle
	m_hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);

	//prevent self-induced windows messages handling
  m_bControlsFilledFromMaterial = TRUE;

  //here bAGameMAterialIsEdited is TRUE, so enable the controls
	GetDlgItem(IDC_CHECK_GOURAUD        )->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_DIFFUSE       )->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_AMBIENT       )->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_SPECULAR      )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_AUTOSCROLL     )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_TRANS          )->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_SPECEXP         )->EnableWindow(TRUE);
	GetDlgItem(IDC_SPIN_SPECEXP         )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_TEXTURE        )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_CHROME         )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_BACKFACE       )->EnableWindow(TRUE);
	GetDlgItem(IDC_CHECK_ALPHASENSITIVE )->EnableWindow(TRUE);
	GetDlgItem(IDC_BUTTON_ADDANIMATED   )->EnableWindow(TRUE);

	unsigned long xMType;
	CString csEditContents;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xMType);
	BOOL bMaterialIsTextured = (xMType & GLI_C_lIsTextured);
	
	if ( _iTextureIndex < 0 )
		_iTextureIndex = 0;

	((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->ResetContent();
	long lNbTextures = GLI_lGetMaterialNumberOfAnimatedTexture(m_hEditedVisualMaterial);
	if (_iTextureIndex > lNbTextures - 1)
		_iTextureIndex = lNbTextures - 1;

	if ( lNbTextures > 0 )
	{
		//since the first texture will be selected in the combo, it cannot be moved up
		GetDlgItem(IDC_BUTTON_MOVEUP         )->EnableWindow(_iTextureIndex > 0);
		//but it can be moved down, if there are more than one texture, and the selected texture is not the last
		GetDlgItem(IDC_BUTTON_MOVEDOWN       )->EnableWindow(bMaterialIsTextured && (lNbTextures > 1) && (_iTextureIndex < (lNbTextures - 1)) );
		//there are animated textures, so enable the combo if there is more than one
		GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES)->EnableWindow(bMaterialIsTextured && (lNbTextures > 1));
		//TODO: retirer la partie && (lNbTextures > 1) quand la fct moteur fonctionnera pour le cas lNbTextures == 1
		GetDlgItem(IDC_BUTTON_REMOVEANIMATED )->EnableWindow(bMaterialIsTextured);
		//now add entries in the combo for the additional textures
		for ( ; lNbTextures > 0; lNbTextures -- )
		{
			GLI_tdstTexture *hAnimatedTexture;
			float fDuration;
			GLI_vGetMaterialAnimatedTexture(m_hEditedVisualMaterial, lNbTextures - 1, &hAnimatedTexture, &fDuration);
			tdoEditorTexture *p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hAnimatedTexture);
			short wInsert = ((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->InsertString(0, p_oEditorTexture ? p_oEditorTexture->GetName() : "N/A");
			if ( wInsert != CB_ERR )
				((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->SetItemDataPtr(wInsert, p_oEditorTexture);
		}
	}
	else //there is no texture
	{
		if ( bMaterialIsTextured ) //if the material believed it was textured though it is not
		{
			//remove the textured flag attribute
			GLI_xSetMaterialType(m_hEditedVisualMaterial, xMType & ~GLI_C_lIsTextured);
			m_vNotifyChangeToScripts();
			bMaterialIsTextured = FALSE;
		}
		//disable the irrelevant combos
		GetDlgItem(IDC_BUTTON_MOVEUP         )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_MOVEDOWN       )->EnableWindow(FALSE);
		GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_REMOVEANIMATED )->EnableWindow(FALSE);
	}

	//now select the default texture of the combo
	((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->SetCurSel(bMaterialIsTextured ? _iTextureIndex : -1);
	//and make the texture view update its display according to the selected item in the combo
	gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(_hMat, bMaterialIsTextured ? _iTextureIndex : -1);

	//update the rest of the controls according to the edited material
	GetDlgItem(IDC_CHECK_GOURAUD)->SetWindowText((xMType & GLI_C_lIsGouraud) ? "Gouraud" : "Flat");
	((CButton *) GetDlgItem(IDC_CHECK_TEXTURE))->SetCheck(bMaterialIsTextured);
	((CButton *) GetDlgItem(IDC_CHECK_CHROME))->SetCheck(GLI_bIsMaterialChromed(m_hEditedVisualMaterial));
	((CButton *) GetDlgItem(IDC_CHECK_BACKFACE))->SetCheck(GLI_bIsMaterialBackface(m_hEditedVisualMaterial));
	((CButton *) GetDlgItem(IDC_CHECK_ALPHASENSITIVE))->SetCheck(!(xMType & GLI_C_lIsNotLightAlphaSensitive));
	((CButton *) GetDlgItem(IDC_CHECK_TRANS))->SetCheck(!(xMType & GLI_C_lIsNotGrided));
	
	//get the autoscroll speed
	float fUSpeed, fVSpeed;
	long lSwitch;
	GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
	csEditContents.Format(C_szAutoScrollStringFormat, fUSpeed * C_fAutoScrollEditCoeff);
	csEditContents.TrimLeft();
	GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->SetWindowText(csEditContents);
	csEditContents.Format(C_szAutoScrollStringFormat, fVSpeed * C_fAutoScrollEditCoeff);
	csEditContents.TrimLeft();
	GetDlgItem(IDC_EDIT_VERTICALSCROLL)->SetWindowText(csEditContents);
	((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->SetCheck(lSwitch);
	//the autoscroll parameters are modifiable only if autoscroll is enabled
	GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->EnableWindow(lSwitch);
	GetDlgItem(IDC_EDIT_VERTICALSCROLL  )->EnableWindow(lSwitch);

	//get the specular color's exponent
	GEO_tdstColor stColor;
	long lSpecExp;
	GLI_xGetMaterialSpecularCoef(m_hEditedVisualMaterial, &lSpecExp, &stColor);
	csEditContents.Format("%d", lSpecExp);
	GetDlgItem(IDC_EDIT_SPECEXP)->SetWindowText(csEditContents);

	//note that the 3 color buttons contents will be updated by OnDrawItem()
	GetDlgItem(IDC_BUTTON_DIFFUSE )->Invalidate();
	GetDlgItem(IDC_BUTTON_AMBIENT )->Invalidate();
	GetDlgItem(IDC_BUTTON_SPECULAR)->Invalidate();

  m_bControlsFilledFromMaterial = FALSE;
	//Invalidate();
}

/////////////////////////////////////////////////////////////////////////////
// DiaView diagnostics

#ifdef _DEBUG
void DiaView::AssertValid() const
{
	CFormView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// DiaView message handlers


//-----------------------------------------------------------------------------
//  Description : handle function for BN_CLICKED
//---------------------------------------------------------------------------
void DiaView::OnCheckTexture()
{
	unsigned long xMType, xOldType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xMType);
	xOldType = xMType;

	//check if the material references textures
	long lNbTextures = GLI_lGetMaterialNumberOfAnimatedTexture(m_hEditedVisualMaterial);

	if ( ((CButton *) GetDlgItem(IDC_CHECK_TEXTURE))->GetCheck() ) //we try to enable material texturing
	{
		if ( lNbTextures == 0 ) //but there is no texture
		{
			xMType &= ~GLI_C_lIsTextured; //and clear the texture flag
		}
		else //there are textures
			xMType |= GLI_C_lIsTextured; //lets set the texture flag
	}
	else //we want to clear the flag
		xMType &= ~GLI_C_lIsTextured;

	if ( xOldType != xMType )
	{
		M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
			tdoChangeMaterialStateModif::E_st_Textured
		));
/*
		//make listview update the displayed texture for the edited game material
		GLI_tdstTexture *p_stTexture;
		//we display the first texture of the animation
		GLI_xGetMaterialTexture(m_hEditedVisualMaterial, &p_stTexture);
		gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vSetItemImage(
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_lGetSelectedItem(),
			(short)	(
				(xMType & GLI_C_lIsTextured)
				? gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndexByLong((long) p_stTexture)
				: gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndex(C_szNoBitmapName)
			)
		);
		gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vRedrawSelectedItem();

		//update the "textured" attribute of the material
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xMType);
		//tell the object it changed, and has to be saved
		m_vNotifyChangeToScripts();
		//if the edited game material is used in the scene
		if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
			//tell the main view to redraw itself because a material changed aspect
			M_GetMainDevice3D()->DrawObject();
*/
	}

	//now make the visual and texture views update their contents
	gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vShowMaterial(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
		((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel()
	);

	GetDlgItem(IDC_BUTTON_ADDANIMATED)->EnableWindow(((CButton *) GetDlgItem(IDC_CHECK_TEXTURE))->GetCheck());

}

//-----------------------------------------------------------------------------
//  Description : handle function for WM_DRAWITEM
//---------------------------------------------------------------------------
void DiaView::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC      *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	RECT					*rect = &lpDrawItemStruct->rcItem;
	GEO_tdstColor stColor;
	long lSpecularExponent;
	COLORREF			xColor;
	BOOL bAMaterialIsEdited = m_hEditedVisualMaterial ? TRUE : FALSE;
	BOOL bNormalDraw = FALSE;

	switch ( nIDCtl )
	{
	default:
		CFormView::OnDrawItem(nIDCtl, lpDrawItemStruct);
		bNormalDraw = TRUE;
		break;

	case IDC_BUTTON_DIFFUSE:
		if ( bAMaterialIsEdited )
		{
			GLI_xGetMaterialDiffuseCoef(m_hEditedVisualMaterial, &stColor);
			xColor = RGB( stColor.xR * 255, stColor.xG * 255, stColor.xB * 255 );
		}
		else
			xColor = RGB(192,192,192);
		pDC->FillSolidRect( rect, xColor);
		break;

	case IDC_BUTTON_AMBIENT:
		if ( bAMaterialIsEdited )
		{
			GLI_xGetMaterialAmbientCoef(m_hEditedVisualMaterial, &stColor);
			xColor = RGB( stColor.xR * 255, stColor.xG * 255, stColor.xB * 255 );
		}
		else
			xColor = RGB(192,192,192);
		pDC->FillSolidRect( rect, xColor);
		break;

	case IDC_BUTTON_SPECULAR:
		if ( bAMaterialIsEdited )
		{
			GLI_xGetMaterialSpecularCoef(m_hEditedVisualMaterial, &lSpecularExponent, &stColor);
			xColor = RGB( stColor.xR * 255, stColor.xG * 255, stColor.xB * 255 );
		}
		else
			xColor = RGB(192,192,192);
		pDC->FillSolidRect( rect, xColor);
		break;
	}

	//if we have drawn a color button
	if ( bAMaterialIsEdited && !bNormalDraw )
	{
		//draw the alpha color in the button too, in the righthand third of the rectangle
		short wOffset = (rect->right * 2) / 3;
		rect->left += wOffset;
		xColor = RGB(stColor.xA * 255, stColor.xA * 255, stColor.xA * 255);
		pDC->FillSolidRect(rect, xColor);
	}
}

//==================================================================================================
//  Description : handle function for WM_CREATE
//==================================================================================================
int DiaView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	EnableToolTips(TRUE);

	return 0;
}

//==================================================================================================
//  Description : handle function for WM_DRAWITEM
//==================================================================================================
BOOL DiaView::Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName,
                        DWORD dwRequestedStyle, const RECT& rect, CWnd* pParentWnd, UINT nID,
                        CCreateContext* pContext )
{
    BOOL bResult = CFormView::Create( lpszClassName, lpszWindowName, dwRequestedStyle, rect, pParentWnd, nID, pContext );

	//control registration for tutorial and assistants, should be unregistered when view is destroyed
	TUT_M_vGetTutDll(); 
	TUT_M_vRegisterControlID(IDC_CHECK_ALPHASENSITIVE,"TGM_DiaViewCheckAlphaSensitive",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_TRANS,"TGM_DiaViewCheckTransparency",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_TEXTURE,"TGM_DiaViewCheckTextured",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_GOURAUD,"TGM_DiaViewCheckGouraud",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_CHROME,"TGM_DiaViewCheckChrome",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_BACKFACE,"TGM_DiaViewCheckBackface",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_AUTOSCROLL,"TGM_DiaViewCheckAutoScroll",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_EDIT_HORIZONTALSCROLL,"TGM_DiaViewEditHorScroll",TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_EDIT_VERTICALSCROLL,"TGM_DiaViewEditVertScroll",TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_BUTTON_AMBIENT,"TGM_DiaViewButtonAmbientColor",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_DIFFUSE,"TGM_DiaViewButtonDiffusecolor",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_SPECULAR,"TGM_DiaViewButtonSpecularcolor",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_EDIT_SPECEXP,"TGM_DiaViewEditSpecularExponent",TUT_e_TextEdit);
	TUT_M_vRegisterControlID(IDC_SPIN_SPECEXP,"TGM_DiaViewSpinSpecularExponent",TUT_e_Spin);
	TUT_M_vRegisterControlID(IDC_COMBO_ANIMATEDTEXTURES,"TGM_DiaViewTexturesCombo",TUT_e_ComboBox);
	TUT_M_vRegisterControlID(IDC_BUTTON_ADDANIMATED,"TGM_DiaViewButtonAddAnimated",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_REMOVEANIMATED,"TGM_DiaViewButtonRemoveAnimated",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_MOVEUP,"TGM_DiaViewButtonMoveUp",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_MOVEDOWN,"TGM_DiaViewButtonMoveDown",TUT_e_Button);

	return bResult;
}
//==================================================================================================
//  Description : change texture in m_oMemDC
//==================================================================================================
void DiaView::OnButtonDiffuse()
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialColorModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialColorModif::E_ct_Diffuse
	));
}

//==================================================================================================
//  Description : change texture in m_oMemDC
//==================================================================================================
void DiaView::OnButtonSpecular()
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialColorModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialColorModif::E_ct_Specular
	));
}

//==================================================================================================
//  Description : edit a color of the VMT
//==================================================================================================
void DiaView::OnButtonAmbient()
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialColorModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialColorModif::E_ct_Ambient
	));
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckGouraud()
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialStateModif::E_st_Gouraud
	));
/*
	unsigned long xMType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xMType);
	//change
	if ( xMType & GLI_C_lIsGouraud )
		xMType &= ~GLI_C_lIsGouraud;
	else
		xMType |= GLI_C_lIsGouraud;
	GLI_xSetMaterialType(m_hEditedVisualMaterial, xMType);
	m_vNotifyChangeToScripts();

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//update controls according to the edited material
	((CButton *) GetDlgItem(IDC_CHECK_GOURAUD))->SetCheck(FALSE);
	GetDlgItem(IDC_CHECK_GOURAUD)->SetWindowText((xMType & GLI_C_lIsGouraud) ? "Gouraud" : "Flat");
*/
}

//==================================================================================================
//==================================================================================================
void DiaView::m_vNotifyChangeToScripts()
{
	gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedVisualMaterial)
	->fn_vNotifySave();
}

//==================================================================================================
//==================================================================================================
void DiaView::OnChangeEditSpecExp()
{
  if ( !m_bControlsFilledFromMaterial && m_hEditedVisualMaterial != NULL ) //test handle validity
	{
		//get the old values
		long lSpecExp;
		GEO_tdstColor stColor;
		GLI_xGetMaterialSpecularCoef(m_hEditedVisualMaterial, &lSpecExp, &stColor);
		//read the contents of the specular exponent edit
		CString csSpecExp;
		GetDlgItem(IDC_EDIT_SPECEXP)->GetWindowText(csSpecExp);
		lSpecExp = atoi((char*)LPCTSTR(csSpecExp));
		//and store it back in the visual material
		GLI_xSetMaterialSpecularCoef(m_hEditedVisualMaterial, lSpecExp, &stColor);
		//notify the change
		m_vNotifyChangeToScripts();
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckAutoScroll() 
{
	if ( !m_bControlsFilledFromMaterial && m_hEditedVisualMaterial != NULL ) //test handle validity
	{
		//prevent SetWindowText() generated windows messages from being handled
		m_bControlsFilledFromMaterial = TRUE;

		long lSwitch = ((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->GetCheck();

		M_p_oGetEditManager()->AskFor(new tdoChangeAutoScrollModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
			tdoChangeAutoScrollModif::E_as_Switch,
			lSwitch, 0.0f, 0.0f)
			);

/*
		//get the old values
		float fUSpeed, fVSpeed;
		long lSwitch;
		GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
		//read the contents of the autoscroll switch
		if ( lSwitch = (((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->GetCheck() ? 1 : 0) )
		{
			//if we want to enable autoscroll, make sure the engine function will not clear the flag
			//when it sees that the speeds are null...
			if ( fUSpeed == 0.0 && fVSpeed == 0.0 )
			{
				fUSpeed = (float) (1.0 / C_fAutoScrollEditCoeff);
				//dont forget to set the control's contents accordingly
				GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->SetWindowText("1");
			}
			//enable tiling of all the textures used by this material, so that autoscroll remains nice
			long lNbtextures = GLI_lGetMaterialNumberOfAnimatedTexture(m_hEditedVisualMaterial);
			for ( lNbtextures --; lNbtextures >= 0; lNbtextures -- )
			{
				GLI_tdstTexture *hTexture;
				float fDuration;
				GLI_vGetMaterialAnimatedTexture(m_hEditedVisualMaterial, lNbtextures, &hTexture, &fDuration);
				TEX_vSetTextureTilingMode(hTexture, TRUE, TRUE);
				//do not forget to save the section
				gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hTexture)->fn_vNotifySave();
			}
			//make the texture view redisplay the current texture
			gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(
				gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
				((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel()
			);
		}
		//and store it back in the visual material
		GLI_xSetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, lSwitch, fUSpeed, fVSpeed);
		//notify the change
		m_vNotifyChangeToScripts();
		//the autoscroll parameters are modifiable only if autoscroll is enabled
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->EnableWindow(lSwitch);
		GetDlgItem(IDC_EDIT_VERTICALSCROLL  )->EnableWindow(lSwitch);
*/
		//restore normal message handling
		m_bControlsFilledFromMaterial = FALSE;
	}	
}

//==================================================================================================
//==================================================================================================
void DiaView::OnKillFocusEditHorizontalScroll() 
{
	if ( !m_bControlsFilledFromMaterial && m_hEditedVisualMaterial != NULL ) //test handle validity
	{
		//prevent SetWindowText() generated windows messages from being handled
		m_bControlsFilledFromMaterial = TRUE;

		//read the contents of the autoscroll horizontal speed edit
		CString csHorScroll;
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->GetWindowText(csHorScroll);
		float fUSpeed = (float) atof((char*)LPCTSTR(csHorScroll));

		if ( fUSpeed < -C_fAutoScrollEditCoeff )
			fUSpeed = -C_fAutoScrollEditCoeff;
		else if ( fUSpeed > C_fAutoScrollEditCoeff )
			fUSpeed = C_fAutoScrollEditCoeff;
		//and store it back in the visual material
		fUSpeed /= C_fAutoScrollEditCoeff;

		M_p_oGetEditManager()->AskFor(new tdoChangeAutoScrollModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
			tdoChangeAutoScrollModif::E_as_USpeed,
			1, fUSpeed, 0.0f)
			);


/*
		//get the old values
		float fUSpeed, fVSpeed;
		long lSwitch;
		GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
		//read the contents of the autoscroll horizontal speed edit
		CString csHorScroll;
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->GetWindowText(csHorScroll);
		fUSpeed = (float) atof((char*)LPCTSTR(csHorScroll));
		if ( fUSpeed < -C_fAutoScrollEditCoeff )
			fUSpeed = -C_fAutoScrollEditCoeff;
		else if ( fUSpeed > C_fAutoScrollEditCoeff )
			fUSpeed = C_fAutoScrollEditCoeff;
		//and store it back in the visual material
		fUSpeed /= C_fAutoScrollEditCoeff;
		GLI_xSetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, ((fUSpeed != 0.0f) || (fVSpeed != 0.0f)), fUSpeed, fVSpeed);

		//notify the change
		m_vNotifyChangeToScripts();

		//read the values again, to see if the flag is still set
		GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
		((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->SetCheck(lSwitch);

		//rewrite the contents of the edit according to the definitive value of the coefficient
		CString csValue;
		csValue.Format(C_szAutoScrollStringFormat, fUSpeed * C_fAutoScrollEditCoeff);
		csValue.TrimLeft();
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->SetWindowText(csValue);

		//the autoscroll parameters are modifiable only if autoscroll is enabled
//		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->EnableWindow(lSwitch);
//		GetDlgItem(IDC_EDIT_VERTICALSCROLL  )->EnableWindow(lSwitch);
*/
		//restore normal message handling
		m_bControlsFilledFromMaterial = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnKillFocusEditVerticalScroll() 
{
  if ( !m_bControlsFilledFromMaterial && m_hEditedVisualMaterial != NULL ) //test handle validity
	{
		//prevent SetWindowText() generated windows messages from being handled
		m_bControlsFilledFromMaterial = TRUE;

		//read the contents of the autoscroll horizontal speed edit
		CString csVertScroll;
		GetDlgItem(IDC_EDIT_VERTICALSCROLL)->GetWindowText(csVertScroll);
		float fVSpeed = (float) atof((char*)LPCTSTR(csVertScroll));

		if ( fVSpeed < -C_fAutoScrollEditCoeff )
			fVSpeed = -C_fAutoScrollEditCoeff;
		else if ( fVSpeed > C_fAutoScrollEditCoeff )
			fVSpeed = C_fAutoScrollEditCoeff;
		//and store it back in the visual material
		fVSpeed /= C_fAutoScrollEditCoeff;

		M_p_oGetEditManager()->AskFor(new tdoChangeAutoScrollModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
			tdoChangeAutoScrollModif::E_as_VSpeed,
			1, 0.0f, fVSpeed)
			);

/*
		//get the old values
		float fUSpeed, fVSpeed;
		long lSwitch;
		GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
		//read the contents of the autoscroll horizontal speed edit
		CString csVertScroll;
		GetDlgItem(IDC_EDIT_VERTICALSCROLL)->GetWindowText(csVertScroll);
		fVSpeed = (float) atof((char*)LPCTSTR(csVertScroll));
		if ( fVSpeed < -C_fAutoScrollEditCoeff )
			fVSpeed = -C_fAutoScrollEditCoeff;
		else if ( fVSpeed > C_fAutoScrollEditCoeff )
			fVSpeed = C_fAutoScrollEditCoeff;
		//and store it back in the visual material
		fVSpeed /= C_fAutoScrollEditCoeff;
		GLI_xSetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, ((fUSpeed != 0.0f) || (fVSpeed != 0.0f)), fUSpeed, fVSpeed);

		//notify the change
		m_vNotifyChangeToScripts();

		//read the values again, to see if the flag is still set
		GLI_xGetMaterialTextureScrollingCoef(m_hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
		((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->SetCheck(lSwitch);

		//rewrite the contents of the edit according to the definitive value of the coefficient
		CString csValue;
		csValue.Format(C_szAutoScrollStringFormat, fVSpeed * C_fAutoScrollEditCoeff);
		csValue.TrimLeft();
		GetDlgItem(IDC_EDIT_VERTICALSCROLL)->SetWindowText(csValue);

		//the autoscroll parameters are modifiable only if autoscroll is enabled
//		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->EnableWindow(lSwitch);
//		GetDlgItem(IDC_EDIT_VERTICALSCROLL  )->EnableWindow(lSwitch);
*/
		
		//restore normal message handling
		m_bControlsFilledFromMaterial = FALSE;
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckBackface() 
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialStateModif::E_st_Backface
	));
/*
	unsigned long xType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xType);

	BOOL bBackface = ((CButton *) GetDlgItem(IDC_CHECK_BACKFACE))->GetCheck();
	//until the chrome removal function is available... in SetTextureAsEnvironnement()
	if ( bBackface )
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsTestingBackface));
	else
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType | GLI_C_lIsTestingBackface);

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//notify the change
	m_vNotifyChangeToScripts();
*/
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckChrome() 
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialStateModif::E_st_Chrome
	));
/*
	unsigned long xType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xType);

	BOOL bChrome = ((CButton *) GetDlgItem(IDC_CHECK_CHROME))->GetCheck();
	//until the chrome removal function is available... in SetTextureAsEnvironnement()
	if ( bChrome )
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotChromed));
	else
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType | GLI_C_lIsNotChromed);

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//notify the change
	m_vNotifyChangeToScripts();
*/
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckTrans() 
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialStateModif::E_st_Transparent
	));
/*
	unsigned long xType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xType);

	BOOL bTransparent = ((CButton *) GetDlgItem(IDC_CHECK_TRANS))->GetCheck();

	if ( bTransparent )
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotGrided));
	else
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType | GLI_C_lIsNotGrided);

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//notify the change
	m_vNotifyChangeToScripts();
*/
}

//==================================================================================================
//==================================================================================================
void DiaView::OnCheckAlphaSensitive() 
{
	M_p_oGetEditManager()->AskFor(new tdoChangeMaterialStateModif(
		gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
		tdoChangeMaterialStateModif::E_st_Alpha
	));
/*
	unsigned long xType;
	GLI_xGetMaterialType(m_hEditedVisualMaterial, &xType);

	BOOL bAlphaSensitive = ((CButton *) GetDlgItem(IDC_CHECK_ALPHASENSITIVE))->GetCheck();

	if ( bAlphaSensitive ) //set the alpha and transparency flags to make it work
	{
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotLightAlphaSensitive - GLI_C_lIsNotGrided));
		//if we check the alpha sensitiveness, we have to set the transparency flag in order to have it work
		((CButton *) GetDlgItem(IDC_CHECK_TRANS))->SetCheck(TRUE);
	}
	else //clear only the alpha flag, because the transparency flag works by itself too
		GLI_xSetMaterialType(m_hEditedVisualMaterial, xType | GLI_C_lIsNotLightAlphaSensitive);

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//notify the change
	m_vNotifyChangeToScripts();
*/
}

//==================================================================================================
//==================================================================================================
void DiaView::OnButtonAddAnimatedTexture() 
{
	gs_p_oMaterialInterface->m_p_oGetTextureChooserDialog()->DoModal();
	GLI_tdstTexture *hTextureToInsert = NULL;
	int iImage = gs_p_oMaterialInterface->m_p_oGetTextureChooserDialog()->m_fn_iGetSelectedImage();
	if (iImage != -1)
		hTextureToInsert = (GLI_tdstTexture *) gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_lGetBitmapLong(iImage);

	if ( hTextureToInsert )
	{
		short wCurrentTexture = ((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel();
		if ( wCurrentTexture == CB_ERR ) //if there are no texture yet
			wCurrentTexture = 0; //add at the beginning of the list
		else
			wCurrentTexture ++; //else add after the current texture
		//perform the modif
		M_p_oGetEditManager()->AskFor(new tdoInsertAnimatedTextureModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
			wCurrentTexture,
			hTextureToInsert,
			(float) (100.0 + (((float) rand()) / RAND_MAX * 10.0))
		));
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnButtonRemoveAnimatedTexture() 
{
	//get the current animated texture index
	short wCurrentTexture = ((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel();
	if ( wCurrentTexture != CB_ERR )
	{
		tdoEditorGameMaterial *p_oGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
		if (GLI_lGetMaterialNumberOfAnimatedTexture(GMT_fn_hGetVisualMaterial(p_oGameMaterial->m_hGetEngineMaterial())) <= 1)
			return;

		//perform the modif
		M_p_oGetEditManager()->AskFor(new tdoRemoveAnimatedTextureModif(
			p_oGameMaterial->m_hGetEngineMaterial(),
			wCurrentTexture
		));
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnSelchangeComboAnimatedTextures() 
{
	//make the texture view dialog display the correponding animated texture
	short wItem = ((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel();
	if ( wItem != CB_ERR )
	{
		gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), 
			wItem
		);
		//update the moveup/movedown buttons
		GetDlgItem(IDC_BUTTON_MOVEUP)->EnableWindow(wItem > 0);
		GetDlgItem(IDC_BUTTON_MOVEDOWN)->EnableWindow(wItem < GLI_lGetMaterialNumberOfAnimatedTexture(m_hEditedVisualMaterial) - 1);
	}
	else
	{
		GetDlgItem(IDC_BUTTON_MOVEUP)->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_MOVEDOWN)->EnableWindow(FALSE);
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnButtonMoveUpTexture() 
{
	//get the current animated texture index
	short wCurrentTexture = ((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel();
	//if it can be moved up
	if ( wCurrentTexture != CB_ERR && wCurrentTexture > 0 )
	{
		tdoEditorGameMaterial *p_oGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
		//move the texture one step earlier in the animation list
		M_p_oGetEditManager()->AskFor(new tdoMoveAnimatedTextureModif(
			p_oGameMaterial->m_hGetEngineMaterial(),
			wCurrentTexture, -1
		));
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::OnButtonMoveDownTexture() 
{
	//get the current animated texture index
	CComboBox *p_oCombo = (CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES);
	short wCurrentTexture = p_oCombo->GetCurSel();
	//if it can be moved down
	if ( (wCurrentTexture != CB_ERR) && (wCurrentTexture < (p_oCombo->GetCount() - 1)) )
	{
		tdoEditorGameMaterial *p_oGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
		//move the texture one step later in the animation list
		M_p_oGetEditManager()->AskFor(new tdoMoveAnimatedTextureModif(
			p_oGameMaterial->m_hGetEngineMaterial(),
			wCurrentTexture, 1
		));
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateAlphaButton (GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
		((CButton *) GetDlgItem(IDC_CHECK_ALPHASENSITIVE))->SetCheck(bActivate);
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateChromeButton (GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
		((CButton *) GetDlgItem(IDC_CHECK_CHROME))->SetCheck(bActivate);
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateGouraudButton (GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
	{
		((CButton *) GetDlgItem(IDC_CHECK_GOURAUD))->SetCheck(FALSE);
		GetDlgItem(IDC_CHECK_GOURAUD)->SetWindowText(bActivate ? "Gouraud" : "Flat");
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateTextureButton (GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
	{
		((CButton *) GetDlgItem(IDC_CHECK_TEXTURE))->SetCheck(bActivate);

		//now make the visual and texture views update their contents
		m_fn_vShowMaterial(gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
			((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel());

		GetDlgItem(IDC_BUTTON_ADDANIMATED)->EnableWindow(bActivate);
	}
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateBackfaceButton (GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
		((CButton *) GetDlgItem(IDC_CHECK_BACKFACE))->SetCheck(bActivate);
}

//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateTransparentButton	(GMT_tdxHandleToGameMaterial _hMat, BOOL bActivate)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
		((CButton *) GetDlgItem(IDC_CHECK_TRANS))->SetCheck(bActivate);
}

	
//==================================================================================================
//==================================================================================================
void DiaView::m_fn_vUpdateAutoScroll (GMT_tdxHandleToGameMaterial _hMat, long lSwitch, float fUSpeed, float fVSpeed)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	if (hEditedVisualMaterial != m_hEditedVisualMaterial)
		m_fn_vShowMaterial(_hMat);

	else
	{
		CString csValue;

		// update autoscroll button
		((CButton *) GetDlgItem(IDC_CHECK_AUTOSCROLL))->SetCheck(lSwitch);

		// update U value
		csValue.Format(C_szAutoScrollStringFormat, fUSpeed * C_fAutoScrollEditCoeff);
		csValue.TrimLeft();
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->SetWindowText(csValue);
		// update V value
		csValue.Format(C_szAutoScrollStringFormat, fVSpeed * C_fAutoScrollEditCoeff);
		csValue.TrimLeft();
		GetDlgItem(IDC_EDIT_VERTICALSCROLL)->SetWindowText(csValue);

		//make the texture view redisplay the current texture
		gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
			((CComboBox *) GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES))->GetCurSel()
		);
		//the autoscroll parameters are modifiable only if autoscroll is enabled
		GetDlgItem(IDC_EDIT_HORIZONTALSCROLL)->EnableWindow(lSwitch);
		GetDlgItem(IDC_EDIT_VERTICALSCROLL  )->EnableWindow(lSwitch);
	}
}


//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoInsertAnimatedTextureModif::tdoInsertAnimatedTextureModif
(
	GMT_tdxHandleToGameMaterial _MyMatHandle, 
	long _lInsertPoint, 
	GLI_tdstTexture *_hTexture,
	float _fDuration
)
: CPA_Modif(0, "")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);
	m_hTexture = _hTexture;
	m_p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hTexture);
	m_lInsertPoint = _lInsertPoint;
	m_fDuration = _fDuration;
	m_p_oRemoveAnimatedTextureModif = NULL;
	SetName(CString("insertion of texture '") + m_p_oEditorTexture->GetName() + "' in material '" + m_p_oEditedGameMaterial->GetName() + "'");
}

//==================================================================================================
//==================================================================================================
tdoInsertAnimatedTextureModif::~tdoInsertAnimatedTextureModif()
{
	if ( m_p_oRemoveAnimatedTextureModif )
	{
		delete m_p_oRemoveAnimatedTextureModif;
		m_p_oRemoveAnimatedTextureModif = NULL;
	}
}

//==================================================================================================
//==================================================================================================
BOOL tdoInsertAnimatedTextureModif::Do()
{
	if ( m_p_oRemoveAnimatedTextureModif )
		return m_p_oRemoveAnimatedTextureModif->Undo();
	else
	{
		ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
		GLI_vAddMaterialAnimatedTexture(hMaterialVisual, m_lInsertPoint, m_hTexture, m_fDuration);
		if ( m_lInsertPoint == 0 ) //if we insert the texture at the beginning of the list
		{
			//make it the standard unanimated texture too
			GLI_xSetMaterialTexture(hMaterialVisual, m_hTexture); 
			//tell the material list view to update the bitmap associated to this game material
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_vResetMaterialItemTexture(m_p_oEditedGameMaterial);
		}

		//set the textured flag, if it was not set
		unsigned long xMType;
		GLI_xGetMaterialType(hMaterialVisual, &xMType);
		GLI_xSetMaterialType(hMaterialVisual, xMType | GLI_C_lIsTextured);

		//tel the engine that something changed about the texture animation for this material
		GLI_xRefreshAnimatedTexture(hMaterialVisual);

		//notify the changes to the scripts
		tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hMaterialVisual);
		p_oVisualMaterial->fn_vNotifySave();
		//create the coherence manager link
		if ( m_p_oEditorTexture )
			g_oCoherenceManager.m_fn_vAddALink(p_oVisualMaterial, m_p_oEditorTexture);

		//if the material is currenly visible
		if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
			//make the texture and material view update their display
			gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vShowMaterial(m_hEditedGameMaterial, m_lInsertPoint);

		//now create an opposite modif for further do/undo operation
		m_p_oRemoveAnimatedTextureModif = new tdoRemoveAnimatedTextureModif(m_hEditedGameMaterial, m_lInsertPoint);
		return TRUE;
	}
}

//==================================================================================================
//==================================================================================================
BOOL tdoInsertAnimatedTextureModif::Undo()
{
	return m_p_oRemoveAnimatedTextureModif ? m_p_oRemoveAnimatedTextureModif->Do() : FALSE;
}

//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoRemoveAnimatedTextureModif::tdoRemoveAnimatedTextureModif
(
	GMT_tdxHandleToGameMaterial _MyMatHandle, 
	long _lRemovePoint
)
: CPA_Modif(0, "")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);

	m_lRemovePoint = _lRemovePoint;
	GLI_vGetMaterialAnimatedTexture(GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial), m_lRemovePoint, &m_hTexture, &m_fDuration);
	m_p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hTexture);
	m_p_oInsertAnimatedTextureModif = NULL;
	SetName(CString("Removal of texture '") + m_p_oEditorTexture->GetName() + "' from material '" + m_p_oEditedGameMaterial->GetName() + "'");
}

//==================================================================================================
//==================================================================================================
tdoRemoveAnimatedTextureModif::~tdoRemoveAnimatedTextureModif()
{
	if ( m_p_oInsertAnimatedTextureModif )
	{
		delete m_p_oInsertAnimatedTextureModif;
		m_p_oInsertAnimatedTextureModif = NULL;
	}
}

//==================================================================================================
//==================================================================================================
BOOL tdoRemoveAnimatedTextureModif::Do()
{
	if ( m_p_oInsertAnimatedTextureModif )
		return m_p_oInsertAnimatedTextureModif->Undo();
	else
	{
		ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
		//remove the animated texture from the list
		GLI_vDeleteMaterialAnimatedTexture(hMaterialVisual, m_lRemovePoint);
		//if this was the last texture, untexture the material completely
		long lNewNbAnimatedTextures = GLI_lGetMaterialNumberOfAnimatedTexture(hMaterialVisual);
		if ( lNewNbAnimatedTextures == 0 )
		{
			//remove the texture pointer
			GLI_xSetMaterialTexture(hMaterialVisual, NULL);
			//remove the textured flag, and setthe flag telling the chrome effect is disabled
			unsigned long xMType;
			GLI_xGetMaterialType(hMaterialVisual, &xMType);
			GLI_xSetMaterialType(hMaterialVisual, xMType & ~GLI_C_lIsTextured | ~GLI_C_lIsNotChromed);
		}
		else if ( m_lRemovePoint == 0 ) //there remains at least one texture in the animation list
		{
			//make the material reference the new first texture as its standard unanimated texture
			GLI_tdstTexture *hNewStdTexture;
			float fDuration;
			GLI_vGetMaterialAnimatedTexture(hMaterialVisual, m_lRemovePoint, &hNewStdTexture, &fDuration);
			GLI_xSetMaterialTexture(hMaterialVisual, hNewStdTexture);
			//tell the material list view to update the bitmap associated to this game material
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_vResetMaterialItemTexture(m_p_oEditedGameMaterial);
		}

		//tel the engine that something changed about the texture animation for this material
		GLI_xRefreshAnimatedTexture(hMaterialVisual);

		//save the visual material section
		tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hMaterialVisual);
		p_oVisualMaterial->fn_vNotifySave();
		//remove the cohemngr link
		g_oCoherenceManager.m_fn_vRemoveALink(p_oVisualMaterial, m_p_oEditorTexture);

		//if the material is currenly visible
		if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
			//redisplay it completely
			gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vShowMaterial(m_hEditedGameMaterial, m_lRemovePoint);

		m_p_oInsertAnimatedTextureModif = new tdoInsertAnimatedTextureModif(m_hEditedGameMaterial, m_lRemovePoint, m_hTexture, m_fDuration);
	}
	return TRUE;
}

//==================================================================================================
//==================================================================================================
BOOL tdoRemoveAnimatedTextureModif::Undo()
{
	return m_p_oInsertAnimatedTextureModif ? m_p_oInsertAnimatedTextureModif->Do() : FALSE;
}


//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoMoveAnimatedTextureModif::tdoMoveAnimatedTextureModif(GMT_tdxHandleToGameMaterial _MyMatHandle, long _lMoved, long _lDirection)
: CPA_Modif(0, "")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);

	m_lMovedPoint = _lMoved;
	//we work only by swap: we dont exchange position of non-contiguous animation components
	m_lDirection = (_lDirection > 0) ? 1 : -1;

	//check that the move is al least possible
	ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	if ( !GLI_lGetMaterialNumberOfAnimatedTexture(hMaterialVisual) )
		m_lDirection = 0;
	else
	{
		GLI_tdstTexture *hTexture;
		float fDuration;
		GLI_vGetMaterialAnimatedTexture(hMaterialVisual, m_lMovedPoint, &hTexture, &fDuration);
		CString csName = gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hTexture)->GetName() + " texture moved ";
		csName += m_lDirection < 0 ? "up" : "down";
		SetName(csName);
	}
}

//==================================================================================================
//==================================================================================================
tdoMoveAnimatedTextureModif::~tdoMoveAnimatedTextureModif()
{
}

//==================================================================================================
//==================================================================================================
BOOL tdoMoveAnimatedTextureModif::Do()
{
	//if the constructor decided the move is irrelevant, tell the modif did not occur
	if ( !m_lDirection )
		return FALSE;

	//compute the upper and lower index being swapped
	long lUpper, lLower = lUpper = m_lMovedPoint;
	if ( m_lDirection > 0 )
		lUpper += m_lDirection;
	else
		lLower += m_lDirection;
	
	//we swap by removing the element with the higher index and insert it at the lower index
	ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	//read the animation nodes
	GLI_tdstTexture *hTextureUp, *hTextureDown;
	float fDurationUp, fDurationDown;
	GLI_vGetMaterialAnimatedTexture(hMaterialVisual, lUpper, &hTextureUp, &fDurationUp);
	GLI_vGetMaterialAnimatedTexture(hMaterialVisual, lLower, &hTextureDown, &fDurationDown);
	//and modify them, swapping the datae
	GLI_vModifyMaterialAnimatedTexture(hMaterialVisual, lLower, hTextureUp, fDurationUp);
	GLI_vModifyMaterialAnimatedTexture(hMaterialVisual, lUpper, hTextureDown, fDurationDown);

	//if the swapped element is the first of the list
	if ( lLower == 0 )
	{
		//make the ex-upper texture the default unanimated texture
		GLI_xSetMaterialTexture(hMaterialVisual, hTextureUp);
		//tell the material list view to update the bitmap associated to this game material
		gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_vResetMaterialItemTexture(m_p_oEditedGameMaterial);
	}
	//tel the engine that something changed about the texture animation for this material
	GLI_xRefreshAnimatedTexture(hMaterialVisual);
	//and save the visual material section
	gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hMaterialVisual)->fn_vNotifySave();

	//now do the same thing for the combo box, if the material is the one being edited
	if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
	{
		CComboBox *p_oCombo = (CComboBox *) gs_p_oMaterialInterface->m_p_oGetVisualView()->GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES);
		long lCurSel = p_oCombo->GetCurSel();
		//if the element is the lower, resinsert it one step later, else one step earlier
		lCurSel += (lCurSel == lLower) ? 1 : -1;
		gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vShowMaterial(m_hEditedGameMaterial, lCurSel);
		//read the name and texture pointer in the current item of the texture list
		/*CString csName;
		p_oCombo->GetLBText(lCurSel, csName);
		tdoEditorTexture *p_oTexture = (tdoEditorTexture *) p_oCombo->GetItemDataPtr(lCurSel);
		p_oCombo->DeleteString(lCurSel);
		p_oCombo->InsertString(lCurSel, csName);
		p_oCombo->SetItemDataPtr(lCurSel, p_oTexture);
		//reselect the item in the combo
		p_oCombo->SetCurSel(lCurSel);*/
	}
	return TRUE;
}

//==================================================================================================
//==================================================================================================
BOOL tdoMoveAnimatedTextureModif::Undo()
{
	//do and undo are equivalent, because it is a position swap
	return Do();
}

//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoChangeMaterialColorModif::tdoChangeMaterialColorModif
(
	GMT_tdxHandleToGameMaterial _MyMatHandle,
	tdeColorType _eType
)
: CPA_Modif(0, "")
{
	m_bGoForIt = FALSE;

	//get the edited game material handles
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);
	m_eType = _eType;

	//read the color to be edited
	CString csName;
	ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	long lDlgItem;
	switch (m_eType)
	{
	case E_ct_Ambient:
		GLI_xGetMaterialAmbientCoef(hMaterialVisual, &m_xColorBeforeDo);
		csName = "Ambient";
		lDlgItem = IDC_BUTTON_AMBIENT;
		break;

	case E_ct_Diffuse:
		GLI_xGetMaterialDiffuseCoef(hMaterialVisual, &m_xColorBeforeDo);
		csName = "Diffuse";
		lDlgItem = IDC_BUTTON_DIFFUSE;
		break;

	case E_ct_Specular:
		GLI_xGetMaterialSpecularCoef(hMaterialVisual, &m_lSpecExp, &m_xColorBeforeDo);
		csName = "Specular";
		lDlgItem = IDC_BUTTON_SPECULAR;
		break;
	}

	//call the color edit dialog
	m_xColorAfterDo = m_xColorBeforeDo;
	m_bGoForIt = fn_bEditColor(gs_p_oMaterialInterface->m_p_oGetVisualView()->GetDlgItem(lDlgItem), &m_xColorAfterDo);

	//set the name of the modif
	csName += " color change (" + m_p_oEditedGameMaterial->GetName() + ")";
	SetName(csName);
}

//==================================================================================================
//==================================================================================================
tdoChangeMaterialColorModif::~tdoChangeMaterialColorModif()
{
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialColorModif::Do()
{
	return m_bGoForIt ? m_bDoIt(m_xColorAfterDo) : FALSE;
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialColorModif::Undo()
{
	return m_bGoForIt ? m_bDoIt(m_xColorBeforeDo) : FALSE;
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialColorModif::m_bDoIt(GLI_tdstColor _xNewColor)
{
	ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	long lDglItem;
	switch ( m_eType )
	{
	case E_ct_Ambient:
		GLI_xSetMaterialAmbientCoef(hMaterialVisual, &_xNewColor);
		lDglItem = IDC_BUTTON_AMBIENT;
		break;

	case E_ct_Diffuse:
		GLI_xSetMaterialDiffuseCoef(hMaterialVisual, &_xNewColor);
		lDglItem = IDC_BUTTON_DIFFUSE;
		break;

	case E_ct_Specular:
		GLI_xSetMaterialSpecularCoef(hMaterialVisual, m_lSpecExp, &_xNewColor);
		lDglItem = IDC_BUTTON_SPECULAR;
		break;
	}
	if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
	{
		gs_p_oMaterialInterface->m_p_oGetVisualView()->GetDlgItem(lDglItem)->Invalidate();
	}

	//if the edited game material is used in the scene
	if ( m_p_oEditedGameMaterial->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//tell the material has to be saved
	tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hMaterialVisual);
	p_oVisualMaterial->fn_vNotifySave();
	return TRUE;
}


//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoChangeMaterialStateModif::tdoChangeMaterialStateModif (GMT_tdxHandleToGameMaterial _MyMatHandle, tdeStateType _eType)
	: CPA_Modif(0, "")
{
	//get the edited game material handles
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);
	m_eType = _eType;
	m_bFirstTime = TRUE;

	// get current material type
	ACP_tdxHandleOfMaterial hEditedVisualMaterial= GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	GLI_xGetMaterialType(hEditedVisualMaterial, &m_xMaterialType);

	CString csName = "Change ";

	switch (m_eType)
	{
		case E_st_Alpha:
			m_bActivated = !(m_xMaterialType & GLI_C_lIsNotLightAlphaSensitive);
			m_bTransparent = (m_xMaterialType & GLI_C_lIsNotGrided);
			csName += "alpha sensitivity";
			break;
		case E_st_Transparent:
			m_bActivated = !(m_xMaterialType & GLI_C_lIsNotGrided);
			csName += "transparency";
			break;
		case E_st_Textured:
			m_bActivated = (m_xMaterialType & GLI_C_lIsTextured);
			csName += "textured state";
			break;
		case E_st_Gouraud:
			m_bActivated = (m_xMaterialType & GLI_C_lIsGouraud);
			csName += "gouraud state";
			break;
		case E_st_Chrome:
			m_bActivated = GLI_bIsMaterialChromed(hEditedVisualMaterial);
			csName += "chrome state";
			break;
		case E_st_Backface:
			m_bActivated = GLI_bIsMaterialBackface(hEditedVisualMaterial);
			csName += "backface state";
			break;

		default:
			break;
	}

	//set the name of the modif
	csName += " of " + m_p_oEditedGameMaterial->GetName();
	SetName(csName);
}

//==================================================================================================
//==================================================================================================
tdoChangeMaterialStateModif::~tdoChangeMaterialStateModif()
{
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialStateModif::Do()
{
	return fn_bChangeState(!m_bActivated);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialStateModif::Undo()
{
	return fn_bChangeState(m_bActivated);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeMaterialStateModif::fn_bChangeState (BOOL bActivate)
{
	// get current material type
	ACP_tdxHandleOfMaterial hEditedVisualMaterial;
	GLI_tdstTexture			*p_stTexture;
	unsigned long			xType;

	hEditedVisualMaterial= GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	GLI_xGetMaterialType(hEditedVisualMaterial, &xType);

	switch (m_eType)
	{
		case E_st_Alpha:
			// change material state
			if (bActivate)
			{
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotLightAlphaSensitive - GLI_C_lIsNotGrided));
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateTransparentButton(m_hEditedGameMaterial, TRUE);
			}
			else
			{
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsNotLightAlphaSensitive);
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateTransparentButton(m_hEditedGameMaterial, m_bTransparent);
			}
			// if necessary, update sate of controls
			if (!m_bFirstTime)
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateAlphaButton(m_hEditedGameMaterial,bActivate);
			break;

		case E_st_Transparent:
			// change material state
			if (bActivate)
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotGrided));
			else
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsNotGrided);
			// if necessary, update sate of controls
			if (!m_bFirstTime)
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateTransparentButton(m_hEditedGameMaterial,bActivate);
			break;

		case E_st_Textured:
			//we display the first texture of the animation
			GLI_xGetMaterialTexture(hEditedVisualMaterial, &p_stTexture);
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vSetItemImage(
				gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_lGetSelectedItem(),
				(short)	(
					bActivate
					? gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndexByLong((long) p_stTexture)
					: gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndex(C_szNoBitmapName)
				)
			);
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vRedrawSelectedItem();
			// change material state
			if (bActivate)
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsTextured);
			else
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & ~GLI_C_lIsTextured);
			// if necessary, update sate of controls
			if (!m_bFirstTime)
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateTextureButton(m_hEditedGameMaterial,bActivate);
			break;

		case E_st_Gouraud:
			// change material state
			if (bActivate)
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsGouraud);
			else
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & ~GLI_C_lIsGouraud);
			// if necessary, update sate of controls
			gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateGouraudButton(m_hEditedGameMaterial,bActivate);
			break;

		case E_st_Chrome:
			// change material state
			if (bActivate)
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsNotChromed));
			else
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsNotChromed);
			// if necessary, update sate of controls
			if (!m_bFirstTime)
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateChromeButton(m_hEditedGameMaterial,bActivate);
			break;

		case E_st_Backface:
			// change material state
			if (bActivate)
				GLI_xSetMaterialType(hEditedVisualMaterial, xType & (GLI_C_lAllIsEnable - GLI_C_lIsTestingBackface));
			else
				GLI_xSetMaterialType(hEditedVisualMaterial, xType | GLI_C_lIsTestingBackface);
			// if necessary, update sate of controls
			if (!m_bFirstTime)
				gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateBackfaceButton(m_hEditedGameMaterial,bActivate);
			break;

		default:
			return FALSE;
	}

	//if the edited game material is used in the scene
	if ( gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_bIsReferenced() )
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
	//notify the change
	gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial)->fn_vNotifySave();

	m_bFirstTime = FALSE;

	return TRUE;
}




//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoChangeAutoScrollModif::tdoChangeAutoScrollModif (GMT_tdxHandleToGameMaterial _MyMatHandle, tdeAutoScroll eType, long lSwitch, float fUSpeed, float fVSpeed)
	: CPA_Modif(0, "")
{
	//get the edited game material handles
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);
	m_eType = eType;

	// get current material type
	ACP_tdxHandleOfMaterial hEditedVisualMaterial= GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);

	//get the old values
	GLI_xGetMaterialTextureScrollingCoef(hEditedVisualMaterial, 0, &m_lOldSwitch, &m_fOldUSpeed, &m_fOldVSpeed);
	m_lNewSwitch = (eType == E_as_Switch) ? lSwitch : m_lOldSwitch;
	m_fNewUSpeed = (eType == E_as_USpeed) ? fUSpeed : m_fOldUSpeed;
	m_fNewVSpeed = (eType == E_as_VSpeed) ? fVSpeed : m_fOldVSpeed;


	//set the name of the modif
	CString csName = "Change Autoscroll of " + m_p_oEditedGameMaterial->GetName();
	SetName(csName);
}

//==================================================================================================
//==================================================================================================
tdoChangeAutoScrollModif::~tdoChangeAutoScrollModif()
{
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeAutoScrollModif::Do()
{
	return fn_bChangeState(TRUE);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeAutoScrollModif::Undo()
{
	return fn_bChangeState(FALSE);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeAutoScrollModif::fn_bChangeState (BOOL bDo)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial= GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	float					fUSpeed, fVSpeed;
	long					lSwitch;

	switch (m_eType)
	{
		case E_as_Switch:
			// check new state
			lSwitch = (!bDo) ? m_lOldSwitch : m_lNewSwitch;

			//read the contents of the autoscroll switch
			if (lSwitch)
			{
				//if we want to enable autoscroll, make sure the engine function will not clear the flag
				//when it sees that the speeds are null...
				if ( m_fOldUSpeed == 0.0 && m_fOldVSpeed == 0.0 )
					m_fOldUSpeed = (float) (1.0 / C_fAutoScrollEditCoeff);
				//enable tiling of all the textures used by this material, so that autoscroll remains nice
				long lNbtextures = GLI_lGetMaterialNumberOfAnimatedTexture(hEditedVisualMaterial);
				for ( lNbtextures --; lNbtextures >= 0; lNbtextures -- )
				{
					GLI_tdstTexture *hTexture;
					float fDuration;
					GLI_vGetMaterialAnimatedTexture(hEditedVisualMaterial, lNbtextures, &hTexture, &fDuration);
					TEX_vSetTextureTilingMode(hTexture, TRUE, TRUE);
					//do not forget to save the section
					gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hTexture)->fn_vNotifySave();
				}
			}
			//and store it back in the visual material
			GLI_xSetMaterialTextureScrollingCoef(hEditedVisualMaterial, 0, lSwitch, m_fOldUSpeed, m_fOldVSpeed);
			//notify the change
			gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial)->fn_vNotifySave();
			// update dialog
			gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateAutoScroll(m_hEditedGameMaterial,lSwitch, m_fOldUSpeed, m_fOldVSpeed);
			break;

		case E_as_USpeed:
		case E_as_VSpeed:
			// check new values
			fUSpeed = (!bDo) ? m_fOldUSpeed : m_fNewUSpeed;
			fVSpeed = (!bDo) ? m_fOldVSpeed : m_fNewVSpeed;

			// update material
			GLI_xSetMaterialTextureScrollingCoef(hEditedVisualMaterial, 0, ((fUSpeed != 0.0f) || (fVSpeed != 0.0f)), fUSpeed, fVSpeed);

			//notify the change
			gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial)->fn_vNotifySave();

			//read the values again, to see if the flag is still set
			GLI_xGetMaterialTextureScrollingCoef(hEditedVisualMaterial, 0, &lSwitch, &fUSpeed, &fVSpeed);
			// update dialog
			gs_p_oMaterialInterface->m_p_oGetVisualView()->m_fn_vUpdateAutoScroll(m_hEditedGameMaterial,lSwitch, fUSpeed, fVSpeed);
			break;

		default :
			return FALSE;

	}			
	return TRUE;
}

