// diatext.cpp : implementation file
//


#include "stdafx.h"

#include "Acp_Base.h"

#define HieFriend
#include "incgli.h"

#include "inctex.h"
#include "_interf.hpp"
#include "GMatObj.hpp"
#include "VMatObj.hpp"
#include "diatext.hpp"
#include "DiaView.hpp"
#include "TextureDlg.h"
#include "TexObj.hpp"
#include "choosecolor.hpp"
#include "MatList.hpp"
#include "x:\cpa\main\inc\_editid.h"
#include "TUT.h"

#define C_szDurationStringFormat "%6.2f"

extern Material_Interface *gs_p_oMaterialInterface;

static BOOL g_sModalLoop = FALSE;

/////////////////////////////////////////////////////////////////////////////
// DiaMain dialog

IMPLEMENT_DYNCREATE(DiaText, CFormView)

DiaText::DiaText()
: CFormView(DiaText::IDD)
{
	m_hEditedTexture = NULL;
	m_bDisplaySupplyTexture = FALSE;
	m_lAnimatedTextureIndex = -1;
	m_nRPixelColor = 0;
	m_nGPixelColor = 0;
	m_nBPixelColor = 0;

	//{{AFX_DATA_INIT(DiaText)
	//}}AFX_DATA_INIT
}


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

DiaText::~DiaText()
{
	m_oMemDC.SelectObject(m_p_oPreviousSelectedBitmap);
	m_oMemBitmap.DeleteObject();
	m_oMemDC.DeleteDC();
}


BEGIN_MESSAGE_MAP(DiaText, CFormView)
	//{{AFX_MSG_MAP(DiaText)
	ON_WM_CREATE()
	ON_WM_DRAWITEM()
	ON_BN_CLICKED(IDC_BUTTON_VISUEL, OnButtonVisuel)
	ON_BN_CLICKED(IDC_BUTTON_PRIORITY, OnButtonPriority)
	ON_BN_CLICKED(IDC_BUTTON_QUALITYLEVEL, OnButtonQualityLevel)
	ON_BN_CLICKED(IDC_CHECK_HORIZONTALTILE, OnCheckTile)
	ON_BN_CLICKED(IDC_BUTTON_CHROMAKEYCOLOR, OnButtonChromakeyColor)
	ON_BN_CLICKED(IDC_CHECK_HORIZONTALFILTERING, OnCheckFilter)
	ON_BN_CLICKED(IDC_BUTTON_SUPPLYTEXTURE, OnButtonSupplyTexture)
	ON_EN_KILLFOCUS(IDC_EDIT_DURATION, OnKillFocusEditDuration)
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDOWN()
	ON_BN_CLICKED(IDC_CHECK_VERTICALTILE, OnCheckTile)
	ON_BN_CLICKED(IDC_CHECK_VERTICALFILTERING, OnCheckFilter)
	ON_BN_CLICKED(IDC_CHECK_DEPTHFILTERING, OnCheckFilter)
	ON_BN_CLICKED(IDC_BUTTON_PIXELCOLOR, OnButtonPixelcolor)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DiaText message handlers

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

//Mircea Dunka 04.11.98
	case IDC_BUTTON_PIXELCOLOR:
		csMessage = "color picked from scene (click to set chromakey)";
		break;
//end Mircea Dunka

	case IDC_STATIC:
		csMessage = "";
		break;

	case IDC_EDIT_DURATION:
		csMessage = "set the duration in seconds that the current texture will be displayed";
		break;

	case IDC_BUTTON_VISUEL:
		csMessage = "left click to view/edit the texture OR right click to get the color";
		break;

	case IDC_BUTTON_PRIORITY:
		csMessage = "change the priority of this texture to adapt to memory restrictions";
		break;

	case IDC_BUTTON_QUALITYLEVEL:
		csMessage = "change the quality of this texture to adapt to memory restrictions";
		break;

	case IDC_CHECK_HORIZONTALTILE:
	case IDC_CHECK_VERTICALTILE:
		csMessage = "enable/disable texture tiling";
		break;

	case IDC_CHECK_HORIZONTALFILTERING:
	case IDC_CHECK_VERTICALFILTERING:
	case IDC_CHECK_DEPTHFILTERING:
		csMessage = "enable/disable texture bilinear filtering";
		break;

	case IDC_BUTTON_CHROMAKEYCOLOR:
		csMessage = "Chromakey / alpha";
		break;

	case IDC_BUTTON_SUPPLYTEXTURE:
		csMessage = "swap display between the main and supply texture";
		break;
	}
	//output the help info in the status bar
	fn_vGiveProgressInfo(csMessage, -1);

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

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

	EnableToolTips(TRUE);
	m_oMemDC.CreateCompatibleDC( NULL );
	m_oMemBitmap.CreateCompatibleBitmap( this->GetDC(), 16, 16);
	m_p_oPreviousSelectedBitmap = m_oMemDC.SelectObject(&m_oMemBitmap);

	return 0;
}

//==================================================================================================
//  Description : handle function for WM_DRAWITEM
//==================================================================================================
BOOL DiaText::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_BUTTON_SUPPLYTEXTURE,"TGM_DiaTextButtonSupplyTexture",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_VISUEL,"TGM_DiaTextButtonVisuel",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_PRIORITY,"TGM_DiaTextButtonPriority",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_QUALITYLEVEL,"TGM_DiaTextButtonQyalityLevel",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_HORIZONTALFILTERING,"TGM_DiaTextCheckHorizontalFiltering",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_VERTICALFILTERING,"TGM_DiaTextCheckVerticalFiltering",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_DEPTHFILTERING,"TGM_DiaTextCheckDepthFiltering",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_HORIZONTALTILE,"TGM_DiaTextcheckHorizontalTiling",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_CHECK_VERTICALTILE,"TGM_DiaTextCheckVerticalTiling",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_BUTTON_CHROMAKEYCOLOR,"TGM_DiaTextButtonChromakeyColor",TUT_e_Button);
	TUT_M_vRegisterControlID(IDC_EDIT_DURATION,"TGM_DiaTextEditDuration",TUT_e_TextEdit);
	//Mircea Dunka 4.11.98
	TUT_M_vRegisterControlID(IDC_BUTTON_PIXELCOLOR,"TGM_PixelColor",TUT_e_Button);
	//end Mircea Dunka
	return bResult;
}
//==================================================================================================
//==================================================================================================
void DiaText::m_fn_vShowMaterial(GMT_tdxHandleToGameMaterial _hMat, long _lAnimatedTextureIndex /*= -1*/)
{
	m_lAnimatedTextureIndex = _lAnimatedTextureIndex;
	tdoEditorGameMaterial *p_oEditedGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
	BOOL bAGameMAterialIsEdited = p_oEditedGameMaterial ? TRUE : FALSE;


	/* if no material is currently selected, disable everything */
	if ( !bAGameMAterialIsEdited )
	{
		m_lAnimatedTextureIndex = -1;
		m_hEditedTexture = NULL; //invalidate the handle
		//clear the controls
		GetDlgItem(IDC_EDIT_DURATION            )->SetWindowText("");
		((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALTILE     ))->SetCheck(FALSE);
		((CButton *) GetDlgItem(IDC_CHECK_VERTICALTILE       ))->SetCheck(FALSE);
		((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALFILTERING))->SetCheck(FALSE);
		((CButton *) GetDlgItem(IDC_CHECK_VERTICALFILTERING  ))->SetCheck(FALSE);
		((CButton *) GetDlgItem(IDC_CHECK_DEPTHFILTERING     ))->SetCheck(FALSE);
		//change the texture before the button is disabled, else it will not be displayed...
		m_fn_vChangeTexture(NULL);
		//Mircea Dunka 5.11.1998
		//I need this button remain this way all the time!!!
		GetDlgItem(IDC_BUTTON_VISUEL            )->EnableWindow(FALSE);
		m_bButtonVisuelEnabled = FALSE;
		GetDlgItem(IDC_BUTTON_PIXELCOLOR		)->EnableWindow(FALSE);
		//end Mircea Dunka
		GetDlgItem(IDC_BUTTON_PRIORITY          )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_QUALITYLEVEL      )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_HORIZONTALTILE     )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_VERTICALTILE       )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_HORIZONTALFILTERING)->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_VERTICALFILTERING  )->EnableWindow(FALSE);
		GetDlgItem(IDC_CHECK_DEPTHFILTERING     )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR    )->EnableWindow(FALSE);
		GetDlgItem(IDC_BUTTON_SUPPLYTEXTURE     )->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_DURATION            )->EnableWindow(FALSE);
		return;												 
	}

	//normally, the specified engine game material must belong to the current editor game material...
	ASSERT(_hMat == p_oEditedGameMaterial->GetData());
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(_hMat);
	float fDuration = 0.0f;
	//get the texture's handle
	if ( m_lAnimatedTextureIndex == -1 )
		m_hEditedTexture = NULL;
	else //get the animated texture instead
		GLI_vGetMaterialAnimatedTexture(hEditedVisualMaterial, m_lAnimatedTextureIndex, &m_hEditedTexture, &fDuration);

	//fill the duration control
	BOOL bEditDuration = (m_hEditedTexture ? TRUE : FALSE) && (GLI_lGetMaterialNumberOfAnimatedTexture(hEditedVisualMaterial) > 1);
	if ( bEditDuration )
	{
		CString csDuration;
		csDuration.Format(C_szDurationStringFormat, fDuration);
		csDuration.TrimLeft();
		GetDlgItem(IDC_EDIT_DURATION)->SetWindowText(csDuration);
	}
	else
		GetDlgItem(IDC_EDIT_DURATION)->SetWindowText("");
	GetDlgItem(IDC_EDIT_DURATION)->EnableWindow(bEditDuration);

	BOOL bTheMaterialIsTextured = (m_hEditedTexture ? TRUE : FALSE);
  m_bControlsFilledFromMaterial = TRUE;
	GLI_tdstTexture *p_stTextureToDisplay = NULL;

	GetDlgItem(IDC_BUTTON_SUPPLYTEXTURE)->SetWindowText(m_bDisplaySupplyTexture ? "Supply texture" : "Primary texture");
	if ( bTheMaterialIsTextured )
	{
		p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
		if ( p_stTextureToDisplay == NULL ) //can only happen if we want to display the supplied texture and there is none
			bTheMaterialIsTextured = FALSE; //display no texture

		unsigned long xMType;
		GLI_xGetMaterialType(hEditedVisualMaterial, &xMType);
		if (xMType & GLI_C_lIsTextured)
			m_fn_vChangeTexture(p_stTextureToDisplay);
		else
			m_fn_vChangeTexture(NULL);
	}
	else
		m_fn_vChangeTexture(NULL);

	if ( bTheMaterialIsTextured )
	{
		//TODO: priority, bilinear filtering

		//fill the controls with the proper values
		switch ( TEX_ucGetTextureQualityLevel(p_stTextureToDisplay) )
		{
		case TEX_C_QLOW:
			GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("Low");
			break;

		case TEX_C_QNORMAL:
			GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("Normal");
			break;

		case TEX_C_QHIGH:
			GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("High");
			break;
		}

		long lMipmappingMode = TEX_lGetTextureMipMappingMode(p_stTextureToDisplay);
		if ( lMipmappingMode == TEX_C_TRILINEAR )
			((CButton *) GetDlgItem(IDC_CHECK_DEPTHFILTERING))->SetCheck(TRUE);
		BOOL bFilterU = FALSE, bFilterV = FALSE;
		if ( lMipmappingMode == TEX_C_BILINEAR )
		{
			//TODO: read filtering axis
		}
		((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALFILTERING))->SetCheck(bFilterU);
		((CButton *) GetDlgItem(IDC_CHECK_VERTICALFILTERING  ))->SetCheck(bFilterV);

		BOOL bTilingX, bTilingY;
		TEX_vGetTextureTilingMode(p_stTextureToDisplay, &bTilingX , &bTilingY);
		((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALTILE ))->SetCheck(bTilingX);
		((CButton *) GetDlgItem(IDC_CHECK_VERTICALTILE   ))->SetCheck(bTilingY);

		BOOL bActive, bFiltering;
		unsigned char ucR, ucG, ucB, ucA;
		TEX_vGetTextureChromakey(p_stTextureToDisplay, &bActive, &bFiltering, &ucR, &ucG, &ucB, &ucA);
		UpdateData(FALSE); //move the data into the control
	}

	//enable the button just after the texture is changed
	//Mircea Dunka 5.11.1998
	//I need this button remain this way all the time!!!
	//GetDlgItem(IDC_BUTTON_VISUEL            )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_BUTTON_VISUEL            )->EnableWindow(FALSE);
	m_bButtonVisuelEnabled = bTheMaterialIsTextured;
	//end Mircea Dunka

	//note that these buttons will be updated by OnDrawItem()
	GetDlgItem(IDC_BUTTON_VISUEL            )->Invalidate();
	GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR    )->Invalidate();

	GetDlgItem(IDC_BUTTON_PRIORITY          )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_BUTTON_QUALITYLEVEL      )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_CHECK_HORIZONTALTILE     )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_CHECK_VERTICALTILE       )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_CHECK_HORIZONTALFILTERING)->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_CHECK_VERTICALFILTERING  )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_CHECK_DEPTHFILTERING     )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR    )->EnableWindow(bTheMaterialIsTextured);
	GetDlgItem(IDC_BUTTON_SUPPLYTEXTURE     )->EnableWindow(bTheMaterialIsTextured || m_bDisplaySupplyTexture);
	//Mircea Dunka 16.11.98
	GetDlgItem(IDC_BUTTON_PIXELCOLOR		)->EnableWindow(bTheMaterialIsTextured);
	//end Mircea Dunka

	m_bControlsFilledFromMaterial = FALSE;
	//Invalidate();
}

//==================================================================================================
//  Description : handle function for WM_DRAWITEM
//==================================================================================================
void DiaText::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	CDC      *pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	RECT					*rect = &lpDrawItemStruct->rcItem;
	BOOL bATextureIsEdited = m_hEditedTexture ? TRUE : FALSE;
	COLORREF			xColor;

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

	case IDC_BUTTON_VISUEL:
		{
			BITMAP stBitmap;
			m_oMemDC.GetCurrentBitmap()->GetBitmap(&stBitmap);
			pDC -> StretchBlt(
				rect->left, rect->top, rect->right, rect->bottom,
				&m_oMemDC,
				0, 0, stBitmap.bmWidth, stBitmap.bmHeight,
				SRCCOPY
			);
		}
		break;

	case IDC_BUTTON_CHROMAKEYCOLOR:
		{
			unsigned char ucR, ucG, ucB, ucA;
			if ( bATextureIsEdited )
			{
				BOOL bActive, bFiltering;
				GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
				if ( p_stTextureToDisplay )
					TEX_vGetTextureChromakey(p_stTextureToDisplay, &bActive, &bFiltering, &ucR, &ucG, &ucB, &ucA);
				else
					ucR = ucG = ucB = ucA = 0;
				xColor = RGB(ucR, ucG, ucB);
			}
			else
				xColor = RGB(192,192,192);
			pDC->FillSolidRect( rect, xColor);
			//draw the alpha color in the button too, in the righthand third of the rectangle
			if ( bATextureIsEdited )
			{
				short wOffset = (rect->right * 2) / 3;
				rect->left += wOffset;
				xColor = RGB(ucA, ucA, ucA);
				pDC->FillSolidRect(rect, xColor);
			}
		}
		break;
		//mircea Dunka 4.11.98
	case IDC_BUTTON_PIXELCOLOR:
		{
			xColor = RGB(m_nRPixelColor, m_nGPixelColor, m_nBPixelColor);
			pDC->FillSolidRect(rect,xColor);

			COLORREF clrNewTextColor;
			if(((m_nRPixelColor >= 128) ? 0:255) + ((m_nGPixelColor >= 128) ? 0:255) + ((m_nBPixelColor >= 128) ? 0:255) > 255)
				clrNewTextColor = 0x00FFFFFF;
			else
				clrNewTextColor = 0x000000000;
			COLORREF clrOldTextColor = pDC->SetTextColor(clrNewTextColor);
			int nOldBkMode = pDC->SetBkMode(TRANSPARENT);
			char szBuffer[20];
			wsprintf(szBuffer, "R:%d G:%d B:%d", m_nRPixelColor, m_nGPixelColor, m_nBPixelColor);
			pDC->DrawText(szBuffer, strlen(szBuffer), rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

			pDC->SetBkMode(nOldBkMode);
			pDC->SetTextColor(clrOldTextColor);
		}
		break;
	}
}

//==================================================================================================
//==================================================================================================
void DiaText::OnButtonQualityLevel() 
{
	GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
	unsigned char ucQuality = TEX_ucGetTextureQualityLevel(p_stTextureToDisplay);
	switch ( ucQuality )
	{
	case TEX_C_QLOW:
		GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("Normal");
		ucQuality = TEX_C_QNORMAL;
		break;

	case TEX_C_QNORMAL:
		GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("High");
		ucQuality = TEX_C_QHIGH;
		break;

	case TEX_C_QHIGH:
		GetDlgItem(IDC_BUTTON_QUALITYLEVEL)->SetWindowText("Low");
		ucQuality = TEX_C_QLOW;
		break;
	}
	TEX_vSetTextureQualityLevel(p_stTextureToDisplay, ucQuality);
	m_vNotifyChangeToScripts(FALSE, TRUE); //only the texture section is modified
}

//==================================================================================================
//==================================================================================================
void DiaText::OnButtonPriority()
{
	static short wPri = 0;
	switch ( wPri )
	{
	case 0:
		GetDlgItem(IDC_BUTTON_PRIORITY)->SetWindowText("roglobo");
		wPri = 1;
		break;

	case 1:
		GetDlgItem(IDC_BUTTON_PRIORITY)->SetWindowText("gougnapla");
		wPri = 2;
		break;

	case 2:
		GetDlgItem(IDC_BUTTON_PRIORITY)->SetWindowText("zorbagnac");
		wPri = 0;
		break;
	}
	//m_vNotifyChangeToScripts(FALSE, TRUE); //only the texture section is modified
}

//==================================================================================================
//==================================================================================================
void DiaText::OnCheckTile() 
{ 
	GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
	TEX_vSetTextureTilingMode(
		p_stTextureToDisplay, 
		((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALTILE))->GetCheck(), 
		((CButton *) GetDlgItem(IDC_CHECK_VERTICALTILE))->GetCheck()
	);
	m_vNotifyChangeToScripts(FALSE, TRUE); //only the texture section is modified
}

//==================================================================================================
//==================================================================================================
void DiaText::OnCheckFilter() 
{
	BOOL bFilterU, bFilterV, bFilterZ;
	bFilterU = ((CButton *) GetDlgItem(IDC_CHECK_HORIZONTALFILTERING))->GetCheck();
	bFilterV = ((CButton *) GetDlgItem(IDC_CHECK_VERTICALFILTERING))->GetCheck();
	bFilterZ = ((CButton *) GetDlgItem(IDC_CHECK_DEPTHFILTERING))->GetCheck();

	GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
	long lTypeOfMipMapping;

	if ( bFilterZ )
		lTypeOfMipMapping = TEX_C_TRILINEAR;
	else if ( bFilterU || bFilterV )
		lTypeOfMipMapping = TEX_C_BILINEAR;
	else
		lTypeOfMipMapping = TEX_C_NO_MIPMAPPING;
	TEX_vSetTextureMipMappingMode(p_stTextureToDisplay, lTypeOfMipMapping);
	//TODO: set axis of bilinear filtering in texture according to bFilterU and bFilterV
	m_vNotifyChangeToScripts(FALSE, TRUE); //only the texture section is modified
}

//==================================================================================================
//==================================================================================================
void DiaText::OnButtonVisuel()
{
	if (g_sModalLoop)
		return;
	
	g_sModalLoop = TRUE;
	gs_p_oMaterialInterface->m_p_oGetTextureChooserDialog()->DoModal();
	g_sModalLoop = FALSE;

	int iImage = gs_p_oMaterialInterface->m_p_oGetTextureChooserDialog()->m_fn_iGetSelectedImage();
	if (iImage != -1)
	{
		GLI_tdstTexture *p_stGliText = (GLI_tdstTexture *) gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_lGetBitmapLong(iImage);
		M_p_oGetEditManager()->AskFor(new tdoChangeTextureModif(
			gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(),
			m_lAnimatedTextureIndex,
			p_stGliText,
			iImage,
			m_bDisplaySupplyTexture
		));
		/*
		GMT_tdxHandleToGameMaterial hGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial();
		ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(hGameMaterial);
		tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial);
		GLI_tdstTexture *p_stTextureToUnlink = NULL;
		CPA_BaseObject *p_oOwnerObject = m_bDisplaySupplyTexture ? gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedTexture) : p_oVisualMaterial;
		//find the editor texture associated to the previous texture, and remove the cohemngr link if there is one
		if ( m_hEditedTexture )
		{
			tdoEditorTexture *p_oEditorTexture = NULL;
			if ( p_stTextureToUnlink = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture) )
				p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(p_stTextureToUnlink);
			if ( p_oEditorTexture )
				g_oCoherenceManager.m_fn_vRemoveALink(p_oOwnerObject, p_oEditorTexture);
		}

		//get the pointer on the new texture
		GLI_tdstTexture *p_stGliText = (GLI_tdstTexture *) gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_lGetBitmapLong(iImage);
		if ( p_stGliText != NULL && p_stTextureToUnlink != p_stGliText ) //if the texture effectively changed
		{
			//try to find the editor texture for this file, and associate it to the visual material
			tdoEditorTexture *p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(p_stGliText);
			if ( p_oEditorTexture )
				g_oCoherenceManager.m_fn_vAddALink(p_oOwnerObject, p_oEditorTexture);

			if ( !m_bDisplaySupplyTexture )
			{
				GLI_tdstTexture *hChangedTexture;
				float fDuration;
				GLI_vGetMaterialAnimatedTexture(hEditedVisualMaterial, m_lAnimatedTextureIndex, &hChangedTexture, &fDuration);
				//give the new texture to the engine visual material
				GLI_vModifyMaterialAnimatedTexture(hEditedVisualMaterial, m_lAnimatedTextureIndex, p_stGliText, fDuration);
				//add the flag saying the material is textured
				ACP_tdxIndex xCurrentType;
				GLI_xGetMaterialType(hEditedVisualMaterial, &xCurrentType);
				GLI_xSetMaterialType(hEditedVisualMaterial, xCurrentType | GLI_C_lIsTextured);
			}
			else //give the supply texture to the main texture
				TEX_vSetSupplyTexture(m_hEditedTexture, p_stGliText);
			//display the new texture
			m_fn_vShowMaterial(hGameMaterial, m_lAnimatedTextureIndex);
			//m_fn_vChangeTexture(p_stGliText);
			//GetDlgItem(IDC_BUTTON_VISUEL)->Invalidate();
			//update the textured flag button
			((CButton *) (gs_p_oMaterialInterface->m_p_oGetVisualMaterialView()->GetDlgItem(IDC_CHECK_TEXTURE)))->SetCheck(TRUE);
			//make listview update the displayed texture for the edited game material
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vSetItemImage(
				gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_lGetSelectedItem(),
				(short) iImage
			);
			gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vRedrawSelectedItem();
			//tell the main view to redraw itself because a material changed aspect
			M_GetMainDevice3D()->DrawObject();
		}
		m_vNotifyChangeToScripts(TRUE, TRUE); //to be sure everybody is notified (but is the primary texture notified as well ?
		*/
	}
}

//==================================================================================================
//  Description : change texture in m_oMemDC (for display)
//==================================================================================================
void DiaText::m_fn_vChangeTexture(GLI_tdstTexture *p_stTexture, BOOL _bEnableButton /*= TRUE*/)
{
	//restore the old bitmap in the DC before destroying the textured one
	m_oMemDC.SelectObject(m_p_oPreviousSelectedBitmap);
	m_oMemBitmap.DeleteObject();

	int iImage = -1;
	tdoEditorGameMaterial * p_oGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial();
	if ( p_oGameMaterial )
	{
		ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(p_oGameMaterial->m_hGetEngineMaterial());
		if ( hEditedVisualMaterial != NULL ) //is the handle valid ?
		{
			// try to get the good bitmap in list
			if ( p_stTexture )
				iImage = gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndexByLong((long) p_stTexture);
			else //if we display the supply texture, show an invalid texture when there is none instead of the rainbow
				iImage = m_bDisplaySupplyTexture ? -1 : gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndex(C_szNoBitmapName);
		}
	}

	if ( iImage == -1 )
		iImage = gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_iGetBitmapIndex(C_szUnviewableBitmapName);

	if ( iImage != -1 )
	{
		BITMAP stBitmap;
		CBitmap *p_oBitmap = CBitmap::FromHandle( gs_p_oMaterialInterface->m_p_oGetMaterialsTextureList()->m_fn_hGetBitmap(iImage));
		p_oBitmap->GetBitmap(&stBitmap);
		m_oMemBitmap.CreateCompatibleBitmap(this->GetDC(), stBitmap.bmWidth, stBitmap.bmHeight);
		m_oMemDC.SelectObject(&m_oMemBitmap);
		m_oMemDC.DrawState(CPoint(0,0), CSize(stBitmap.bmWidth, stBitmap.bmHeight), p_oBitmap, DST_BITMAP);
	}
	//Mircea Dunka 5.11.1998
	//I need this button to remain disabled all the time!!! (for right click messages).
	//GetDlgItem(IDC_BUTTON_VISUEL)->EnableWindow(_bEnableButton && p_stTexture);
	GetDlgItem(IDC_BUTTON_VISUEL            )->EnableWindow(FALSE);
	m_bButtonVisuelEnabled = _bEnableButton && p_stTexture;
	//end Mircea Dunka

	GetDlgItem(IDC_BUTTON_VISUEL)->Invalidate();
}

//==================================================================================================
//==================================================================================================
void DiaText::m_vNotifyChangeToScripts(BOOL _bVisualSection, BOOL _bTextureSection)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial());
	//notify the visual material
	if ( _bVisualSection )
	{
		tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial);
		if ( p_oVisualMaterial )
			p_oVisualMaterial->fn_vNotifySave();
	}

	//notify the texture sections as well (main and supply, just to be sure
	if ( _bTextureSection )
	{
		tdoEditorTexture *p_oEditorTexture;
		p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedTexture);
		if ( p_oEditorTexture )
			p_oEditorTexture->fn_vNotifySave();
		p_oEditorTexture = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(TEX_p_tdstGetSupplyTexture(m_hEditedTexture));
		if ( p_oEditorTexture )
			p_oEditorTexture->fn_vNotifySave();
	}
}

//==================================================================================================
//==================================================================================================
void DiaText::OnButtonChromakeyColor() 
{
	//Mircea Dunka 12.11.98
	GEO_tdstColor stTempColor, stCapturedColor;
	//end Mircea Dunka
	BOOL bActive, bFiltering;
	unsigned char ucR, ucG, ucB, ucA;
	GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
 	TEX_vGetTextureChromakey(p_stTextureToDisplay, &bActive, &bFiltering, &ucR, &ucG, &ucB, &ucA);

	stTempColor.xR = ((float) ucR) / 255;
	stTempColor.xG = ((float) ucG) / 255;
	stTempColor.xB = ((float) ucB) / 255;
	stTempColor.xA = ((float) ucA) / 255;

	//Mircea Dunka 12.11.98
	stCapturedColor.xR = ((float) m_nRPixelColor) / 255;
	stCapturedColor.xG = ((float) m_nGPixelColor) / 255;
	stCapturedColor.xB = ((float) m_nBPixelColor) / 255;
	stCapturedColor.xA = 0;
	// end Mircea Dunka

	if ( fn_bEditColor(GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR), &stTempColor, &stCapturedColor) )
	{
		TEX_vSetTextureChromakey(
			p_stTextureToDisplay, bActive, bFiltering, 
			(unsigned char) (stTempColor.xR * 255), 
			(unsigned char) (stTempColor.xG * 255), 
			(unsigned char) (stTempColor.xB * 255), 
			(unsigned char) (stTempColor.xA * 255)
		);
		GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR)->Invalidate();
		//tell the main view to redraw itself because a material changed aspect
		M_GetMainDevice3D()->DrawObject();
		//tell the texture section has to be saved
		m_vNotifyChangeToScripts(FALSE, TRUE);
	}
}

//==================================================================================================
//==================================================================================================
void DiaText::OnButtonSupplyTexture() 
{
	m_bDisplaySupplyTexture	= !m_bDisplaySupplyTexture;
	m_fn_vShowMaterial(gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial(), m_lAnimatedTextureIndex);
}

//==================================================================================================
//==================================================================================================
void DiaText::OnKillFocusEditDuration() 
{
	//read the contents of the edit
	CString csDuration;
	GetDlgItem(IDC_EDIT_DURATION)->GetWindowText(csDuration);
	//convert the string into a floating point number
	float fDuration = (float) atof(LPCTSTR(csDuration));
	if ( fDuration < 0.0f )
		fDuration = 0.0f;
	//write back in the edit the string that corresponds to the value we parsed
	csDuration.Format(C_szDurationStringFormat, fDuration);
	csDuration.TrimLeft();
	GetDlgItem(IDC_EDIT_DURATION)->SetWindowText(csDuration);

	//change the duration for the texture
	GMT_tdxHandleToGameMaterial hGameMaterial = gs_p_oMaterialInterface->m_p_oGetEditedMaterial()->m_hGetEngineMaterial();
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(hGameMaterial);
	GLI_tdstTexture *hReadTexture;
	float fReadDuration;
	GLI_vGetMaterialAnimatedTexture(hEditedVisualMaterial, m_lAnimatedTextureIndex, &hReadTexture, &fReadDuration);
	GLI_vModifyMaterialAnimatedTexture(hEditedVisualMaterial, m_lAnimatedTextureIndex, hReadTexture, fDuration);
	if ( hReadTexture != m_hEditedTexture )
		AfxMessageBox(
			"TGM INTERNAL ERROR! Tell B. Germain if you see this message\n"
			"You can continue to work normally, this has no influence on your data",
			MB_OK
		);

	//now update the edited animation texture	node.
	m_vNotifyChangeToScripts(FALSE, TRUE);
}


//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
//==================================================================================================
tdoChangeTextureModif::tdoChangeTextureModif
(
	GMT_tdxHandleToGameMaterial _MyMatHandle, 
	long _lChanged, 
	GLI_tdstTexture *_hNewTexture,
	int _iNewImage,
	BOOL _bSupplyTexture
)
: CPA_Modif(0, "Texture Change")
{
	m_hEditedGameMaterial = _MyMatHandle;
	m_p_oEditedGameMaterial = (tdoEditorGameMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(m_hEditedGameMaterial);

	m_hTextureAfterDo = _hNewTexture;

	m_iImageAfterDo = _iNewImage;

	m_lChangeIndex = _lChanged;

	m_bSupplyTexture = _bSupplyTexture;

	ACP_tdxHandleOfMaterial hMaterialVisual = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);
	GLI_xGetMaterialTexture(hMaterialVisual, &m_hTextureBeforeDo);
	if ( m_bSupplyTexture )
		m_hTextureBeforeDo = TEX_p_tdstGetSupplyTexture(m_hTextureBeforeDo);

	m_iImageBeforeDo = gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_iGetTextureIndexForGameMaterial(m_p_oEditedGameMaterial, FALSE);
}

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

//==================================================================================================
//==================================================================================================
BOOL tdoChangeTextureModif::Do()
{
	return m_bDoIt(m_hTextureAfterDo, m_iImageAfterDo);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeTextureModif::Undo()
{
	return m_bDoIt(m_hTextureBeforeDo, m_iImageBeforeDo);
}

//==================================================================================================
//==================================================================================================
BOOL tdoChangeTextureModif::m_bDoIt(GLI_tdstTexture *_hReplacingTexture, int _iImageIndex)
{
	ACP_tdxHandleOfMaterial hEditedVisualMaterial = GMT_fn_hGetVisualMaterial(m_hEditedGameMaterial);

	//change the texture in the material
	GLI_tdstTexture *hReplacedTexture;
	float fDuration;
	GLI_vGetMaterialAnimatedTexture(hEditedVisualMaterial, m_lChangeIndex, &hReplacedTexture, &fDuration);
	GLI_vModifyMaterialAnimatedTexture(hEditedVisualMaterial, m_lChangeIndex, _hReplacingTexture, fDuration);
	//if this is the first texture in the animation, update the unanimated texture too
	if ( m_lChangeIndex == 0 )
		GLI_xSetMaterialTexture(hEditedVisualMaterial, _hReplacingTexture);
	//tell the engine something changed with the animated textures
	GLI_xRefreshAnimatedTexture(hEditedVisualMaterial);

	//update coherence manager links
	tdoEditorVisualMaterial *p_oVisualMaterial = (tdoEditorVisualMaterial *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hEditedVisualMaterial);
	tdoEditorTexture *p_oEditorTextureBefore = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(hReplacedTexture);
	if ( p_oEditorTextureBefore )
		g_oCoherenceManager.m_fn_vRemoveALink(p_oVisualMaterial, p_oEditorTextureBefore);
	tdoEditorTexture *p_oEditorTextureAfter = (tdoEditorTexture *) gs_p_oMaterialInterface->m_p_oGetEditorObjectForEngine(_hReplacingTexture);
	if ( p_oEditorTextureAfter )
		g_oCoherenceManager.m_fn_vAddALink(p_oVisualMaterial, p_oEditorTextureAfter);

	//make the visual material save itself
	p_oVisualMaterial->fn_vNotifySave();

	//if we changed the first texture in tha animation													
	if ( m_lChangeIndex == 0 )
	{
		//find the item for the game material in the material list
		int iMaterialIndex = gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_iFindMaterial(m_p_oEditedGameMaterial);
		//change its image
		gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vSetItemImage(iMaterialIndex, _iImageIndex);
		//then redraw it
		gs_p_oMaterialInterface->m_p_oGetMaterialListView()->m_fn_vRedrawSelectedItem(iMaterialIndex);
	}

	//update display if relevant
	if ( m_p_oEditedGameMaterial == gs_p_oMaterialInterface->m_p_oGetEditedMaterial() )
	{
		//first update the combo of the VMT
		CComboBox *p_oCombo = (CComboBox *) gs_p_oMaterialInterface->m_p_oGetVisualView()->GetDlgItem(IDC_COMBO_ANIMATEDTEXTURES);
		p_oCombo->DeleteString(m_lChangeIndex);
		p_oCombo->InsertString(m_lChangeIndex, p_oEditorTextureAfter ? p_oEditorTextureAfter->GetName() : "ERROR!");
		p_oCombo->SetItemDataPtr(m_lChangeIndex, p_oEditorTextureAfter);
		//then select the modified entry
		p_oCombo->SetCurSel(m_lChangeIndex);
		//then make the texture view redisplay itself
		gs_p_oMaterialInterface->m_p_oGetTexView()->m_fn_vShowMaterial(m_hEditedGameMaterial, m_lChangeIndex);
	}

	//tell the main view to redraw itself because a material changed aspect
	M_GetMainDevice3D()->DrawObject();

	return TRUE;
}

//Mircea Dunka 4.11.98
void DiaText::OnRButtonDown(UINT nFlags, CPoint point) 
{

	CWnd* pWndTexture = GetDlgItem(IDC_BUTTON_VISUEL);

	if(	ChildWindowFromPoint(point, CWP_ALL) == pWndTexture)
	{
		CPoint ptTexture = point;
		ClientToScreen(&ptTexture);
		pWndTexture->ScreenToClient(&ptTexture);
		COLORREF color = pWndTexture->GetDC()->GetPixel(ptTexture);
		m_nRPixelColor = GetRValue(color);
		m_nGPixelColor = GetGValue(color);
		m_nBPixelColor = GetBValue(color);
		GetDlgItem(IDC_BUTTON_PIXELCOLOR)->InvalidateRect(NULL);
	}
	
	CFormView::OnRButtonDown(nFlags, point);
}


void DiaText::OnLButtonDown(UINT nFlags, CPoint point) 
{	
	if( (ChildWindowFromPoint(point, CWP_ALL)==GetDlgItem(IDC_BUTTON_VISUEL)) && m_bButtonVisuelEnabled)
	{
		OnButtonVisuel();
	}
	else
		CFormView::OnLButtonDown(nFlags, point);
}


void DiaText::OnButtonPixelcolor() 
{
	BOOL bActive, bFiltering;
	unsigned char ucR, ucG, ucB, ucA;
	GLI_tdstTexture *p_stTextureToDisplay = (m_bDisplaySupplyTexture ? TEX_p_tdstGetSupplyTexture(m_hEditedTexture) : m_hEditedTexture);
 	TEX_vGetTextureChromakey(p_stTextureToDisplay, &bActive, &bFiltering, &ucR, &ucG, &ucB, &ucA);
	TEX_vSetTextureChromakey(
		p_stTextureToDisplay, bActive, bFiltering, 
		m_nRPixelColor,
		m_nGPixelColor,
		m_nBPixelColor,
		ucA
	);
	GetDlgItem(IDC_BUTTON_CHROMAKEYCOLOR)->Invalidate();
	//tell the main view to redraw itself because a material changed aspect
	M_GetMainDevice3D()->DrawObject();
	//tell the texture section has to be saved
	m_vNotifyChangeToScripts(FALSE, TRUE);

}
///end Mircea Dunka
