// TexturePanel.cpp : implementation file
//

#include "stdafx.h"
#include "MapCreator.h"
#include "TexturePanel.h"
#include <Stuff\Stuff.hpp>
#include <GameOS\ToolOS.hpp>
#include <ImageLib\Image.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTexturePanel dialog


CTexturePanel::CTexturePanel(CWnd* pParent /*=NULL*/)
: CDialog(CTexturePanel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTexturePanel)
	m_TTEdit = _T("");
	m_DetailTexture = _T("");
	m_FadeIn = 400.0f;
	m_FadeOut = 600.0f;
	m_DRatio = 16;
	m_IGData = TRUE;

	//}}AFX_DATA_INIT

//#ifdef LAB_ONLY
//	m_IGData = TRUE;
//#else
	m_IGData = FALSE;
//#endif
	
	Create(IDD,pParent);
	SetWindowPos(NULL,5,25,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
	
}


void CTexturePanel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTexturePanel)
	DDX_Control(pDX, IDC_IGDATA, m_IGDataCon);
	DDX_Text(pDX, IDC_TTEDIT, m_TTEdit);
	DDX_Text(pDX, IDC_DTEDIT, m_DetailTexture);
	DDX_Text(pDX, IDC_FADEIN, m_FadeIn);
	DDV_MinMaxFloat(pDX, m_FadeIn, 100.f, 1000.f);
	DDX_Text(pDX, IDC_FADEOUT, m_FadeOut);
	DDV_MinMaxFloat(pDX, m_FadeOut, 100.f, 1000.f);
	DDX_Text(pDX, IDC_DRATIO, m_DRatio);
	DDV_MinMaxInt(pDX, m_DRatio, 1, 16);
	DDX_Check(pDX, IDC_IGDATA, m_IGData);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTexturePanel, CDialog)
//{{AFX_MSG_MAP(CTexturePanel)
ON_BN_CLICKED(IDC_TTBROWSE, OnTtbrowse)
ON_BN_CLICKED(IDC_DTBROWSE, OnDtbrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTexturePanel message handlers

void CTexturePanel::WriteEntries(Stuff::Page *page)
{
	UpdateData(TRUE);
	CString basenname;
	int pos=m_DetailTexture.ReverseFind('\\');
	if(pos>0)
		
	
	page->SetEntry("DetailTexture",m_ActualTexturePath);
	page->SetEntry("DetailRatio",m_DRatio);
	page->SetEntry("DetailFadeBegin",m_FadeIn);
	page->SetEntry("DetailFadeEnd",m_FadeOut);
}

bool CTexturePanel::ValidateEntries(int zonex,int zonez)
{
	UpdateData(TRUE);
	if(m_TTEdit=="" || !gos_DoesFileExist(m_TTEdit)) 
	{
		MessageBox("Terrain Texture Does Not Exist","Bad Terrain Texture",MB_OK|MB_ICONERROR);
		return false;
	}

	if(!m_IGData)
	{
		Image img;
		img.Load((char *)(LPCSTR)m_TTEdit);

		if(!m_IGData)
		if(img.GetWidth()!=(zonex*512) || img.GetHeight()!=(zonez*512))
		{
			CString str;
			str.Format("For a Heightfield of %ix%i Terrain Texture should be %ix%i",zonex*256,zonez*256,zonex*512,zonez*512);
			MessageBox(str,"Texture Size Error",MB_OK|MB_ICONERROR);
			return false;
		}
	}

	
	if(m_DetailTexture=="" || !gos_DoesFileExist(m_DetailTexture))
	{
		MessageBox("Detail Texture Does Not Exist","Bad Detail Texture",MB_OK|MB_ICONERROR);
		return false;
	}
	
	{
		Image img;
		img.Load((char *)(LPCSTR)m_DetailTexture);

		
		if(img.GetWidth()!=256 || img.GetHeight()!=256)
		{
			MessageBox("Detail Texture MUST be 256x256","Texture Size Error",MB_OK|MB_ICONERROR);
			return false;
		}
	}
	
	if(m_FadeIn>=m_FadeOut)
	{
		MessageBox("FadeIn Must Be Less Than FadeOut","Bad Range",MB_OK|MB_ICONERROR);
		return false;
		
	}

	if(m_FadeIn<100.f || m_FadeIn>1000.f || 
		m_FadeOut<100.f || m_FadeOut>1000.f ||
		m_DRatio<1 || m_DRatio>16
		)
		return false;
return true;
}


void CTexturePanel::OnTtbrowse() 
{
	UpdateData(TRUE);

	if(m_IGData)
	{
	CFileDialog fdlg(TRUE,NULL,m_TTEdit,OFN_NOCHANGEDIR,"FGD Files (*.fgd)|*.fgd||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_TTEdit=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}		
	
	}
		else
	{
	CFileDialog fdlg(TRUE,NULL,m_TTEdit,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_TTEdit=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}		
	}
}

void CTexturePanel::OnDtbrowse() 
{
	UpdateData(TRUE);
	CFileDialog fdlg(TRUE,NULL,m_DetailTexture,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
	if(fdlg.DoModal()==IDOK) 
	{
		m_DetailTexture=fdlg.GetPathName();	
		UpdateData(FALSE);
		
	}		
	
}

BOOL CTexturePanel::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
//#ifdef LAB_ONLY
	m_IGDataCon.ShowWindow(SW_SHOW);
//#else
//	m_IGDataCon.ShowWindow(SW_HIDE);
//#endif


	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
