// GeoPanel.cpp : implementation file
//

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

/////////////////////////////////////////////////////////////////////////////
// CGeoPanel dialog


CGeoPanel::CGeoPanel(CWnd* pParent /*=NULL*/)
: CDialog(CGeoPanel::IDD, pParent)
{
	//{{AFX_DATA_INIT(CGeoPanel)
	m_HFEdit = _T("");
	m_HScale = 1.0f;
	m_SPasses = 3;
	m_PolyCount = 3000;
	m_FinalPass = FALSE;
	m_InterestData = FALSE;
	m_TriCap = 2;
	//}}AFX_DATA_INIT

	Create(IDD,pParent);
	SetWindowPos(NULL,5,25,0,0,SWP_NOSIZE|SWP_NOZORDER|SWP_NOACTIVATE);
LowHFVal=0.0f;
}


void CGeoPanel::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGeoPanel)
	DDX_Text(pDX, IDC_HFEDIT, m_HFEdit);
	DDX_Control(pDX, IDC_SPASSES, m_SPassCon);
	DDX_Text(pDX, IDC_HSCALE, m_HScale);
	DDV_MinMaxFloat(pDX, m_HScale, 1.e-007f, 1000.f);
	DDX_Slider(pDX, IDC_SPASSES, m_SPasses);
	DDX_Text(pDX, IDC_POLYCOUNT, m_PolyCount);
	DDV_MinMaxInt(pDX, m_PolyCount, 500, 3500);
	DDX_Check(pDX, IDC_FINALPASS, m_FinalPass);
	DDX_Check(pDX, IDC_INTERESTDATA, m_InterestData);
	DDX_CBIndex(pDX, IDC_TRICAP, m_TriCap);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CGeoPanel, CDialog)
//{{AFX_MSG_MAP(CGeoPanel)
	ON_BN_CLICKED(IDC_HFBROWSE, OnHfbrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeoPanel message handlers


BOOL CGeoPanel::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_SPassCon.SetRange(0,5,TRUE);
	m_SPassCon.SetPos(m_SPasses);
	

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


void CGeoPanel::WriteEntries(Stuff::Page *page)
{
	UpdateData(TRUE);
	
	int fnd=m_HFEdit.ReverseFind('\\');
	CString hfpath,hfname;
	if(fnd>=0) 
	{
		hfpath=m_HFEdit.Left(fnd);
		hfname=m_HFEdit.Mid(fnd+1);
	}
	else
		hfname=m_HFEdit;
	page->SetEntry("InputPath", hfpath+"\\");
	CString tstr;
	tstr.Format("%f %f",HFX*5.0f/2.0f,HFY*5.0f/2.0f);
	page->SetEntry("MapCenter",tstr);
	page->SetEntry("SmoothingPasses",m_SPasses);
	page->SetEntry("PolyCount",m_PolyCount);
	page->SetEntry("InputPath", hfpath+"\\");
	page->SetEntry("HeightField", hfname);
	page->SetEntry("DeltaY", m_HScale);
	
}

bool CGeoPanel::ValidateEntries()
{
	UpdateData(TRUE);
	
	if(m_HFEdit=="" || !gos_DoesFileExist(m_HFEdit)) 
	{
		MessageBox("HeightField Does Not Exist","Bad Height Field",MB_OK|MB_ICONERROR);
			return false;

	}
	else
	{
		Image hfimg;
		hfimg.Load((char *)(LPCSTR)m_HFEdit);
		HFX=hfimg.GetWidth();
		HFY=hfimg.GetHeight();
		if(HFX<256)
		{ MessageBox("HeightField Width is too small, must be at least 256","Bad Height Field",MB_OK|MB_ICONERROR);return false; }
		
		
		#ifndef LAB_ONLY
		if(HFX>1024)
		{ MessageBox("HeightField Width is too large, Cannot be larger than 1024","Bad Height Field",MB_OK|MB_ICONERROR);return false; }
		#endif

		if(HFY<256)
		{ MessageBox("HeightField Height is too small, must be at least 256","Bad Height Field",MB_OK|MB_ICONERROR);return false; }
		
		#ifndef LAB_ONLY
		if(HFY>1024)
		{ MessageBox("HeightField Height is too large, must be at most 2048","Bad Height Field",MB_OK|MB_ICONERROR);return false; }
		#endif
		
		if( HFX%256 || HFY%256)
		{
			MessageBox("HeightField has incorrect size, must be a multiple of 256 i.e. 256,512,768,1024...","Bad Height Field",MB_OK|MB_ICONERROR);
				return false;

		}

		if(m_InterestData && hfimg.GetBpp()!=24) 
		{
			MessageBox("HeightField must be 24-bit while using \"Interest data\"","Bad Height Field",MB_OK|MB_ICONERROR);
				return false;

		}

		ZoneCountX=HFX/256;
		ZoneCountY=HFY/256;
	BYTE *dat;
	dat=(BYTE *)hfimg.Lock();

	int x,y,lowval=256,tv;

if(m_InterestData)
{
	for(x=0;x<HFX;x++)
		for(y=0;y<HFY;y++)
		{
		tv=dat[(x+y*HFX)*3];
		if(tv<lowval) lowval=tv;
		}
}	
else
{
	for(x=0;x<HFX;x++)
		for(y=0;y<HFY;y++)
		{
		tv=dat[x+y*HFX];
		if(tv<lowval) lowval=tv;
		}
}
	hfimg.UnLock();
	LowHFVal=lowval*m_HScale;

	} //end if else GEo
	
	return true;
}


void CGeoPanel::OnHfbrowse() 
{
UpdateData(TRUE);
	// TODO: Add your control notification handler code here
CFileDialog fdlg(TRUE,NULL,m_HFEdit,OFN_NOCHANGEDIR,"Bitmap Images (*.tif,*.gif,*.tga,*.png)|*.tif;*.gif;*.tga;*.png||",NULL);	
if(fdlg.DoModal()==IDOK) 
	{
	m_HFEdit=fdlg.GetPathName();	
//	m_OBase=fdlg.GetFileName();
//	m_OBase=m_OBase.Left(m_OBase.GetLength()-4);
	UpdateData(FALSE);

	}

	

}
