// BumpMapDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfc gos.h"
#include "BumpMapDlg.h"
#include <GameOS\ToolOS.hpp>

extern Stuff::MString *erf_texture_path;

/////////////////////////////////////////////////////////////////////////////
// CBumpMapDlg dialog


CBumpMapDlg::CBumpMapDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBumpMapDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBumpMapDlg)
	m_BumpTxt = _T("");
	m_DetailTxt = _T("");
	m_EnvTxt = _T("");
	m_TerrainTxt = _T("");
	m_WaterTxt = _T("");
	m_BumpRep = 1.0f;
	m_DetailRep = 4.0f;
	m_EvnRep = 1.0f;
	m_TerrainRep = 1.0f;
	m_WaterRep = 1.0f;
	m_NormTxt = _T("");
	m_NormRep = 1.0f;
	m_BScrollU = 0.0f;
	m_NScrollU = 0.0f;
	m_NScrollV = 0.0f;
	m_WScrollU = 0.0f;
	m_WScrollV = 0.0f;
	m_BScrollV = 0.0f;
	//}}AFX_DATA_INIT
}


void CBumpMapDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBumpMapDlg)
	DDX_Text(pDX, IDC_BUMPTXT, m_BumpTxt);
	DDX_Text(pDX, IDC_DETAILTXT, m_DetailTxt);
	DDX_Text(pDX, IDC_ENVTXT, m_EnvTxt);
	DDX_Text(pDX, IDC_TERRAINTXT, m_TerrainTxt);
	DDX_Text(pDX, IDC_WATERTXT, m_WaterTxt);
	DDX_Text(pDX, IDC_BUMPREP, m_BumpRep);
	DDV_MinMaxFloat(pDX, m_BumpRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_DETAILREP, m_DetailRep);
	DDV_MinMaxFloat(pDX, m_DetailRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_EVNREP, m_EvnRep);
	DDV_MinMaxFloat(pDX, m_EvnRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_TERRAINREP, m_TerrainRep);
	DDV_MinMaxFloat(pDX, m_TerrainRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_WATERREP, m_WaterRep);
	DDV_MinMaxFloat(pDX, m_WaterRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_NORMTXT, m_NormTxt);
	DDX_Text(pDX, IDC_NORMREP, m_NormRep);
	DDV_MinMaxFloat(pDX, m_NormRep, 0.5f, 8.f);
	DDX_Text(pDX, IDC_BSCROLLU, m_BScrollU);
	DDV_MinMaxFloat(pDX, m_BScrollU, -1.f, 1.f);
	DDX_Text(pDX, IDC_NSCROLLU, m_NScrollU);
	DDV_MinMaxFloat(pDX, m_NScrollU, -1.f, 1.f);
	DDX_Text(pDX, IDC_NSCROLLV, m_NScrollV);
	DDV_MinMaxFloat(pDX, m_NScrollV, -1.f, 1.f);
	DDX_Text(pDX, IDC_WSCROLLU, m_WScrollU);
	DDV_MinMaxFloat(pDX, m_WScrollU, -1.f, 1.f);
	DDX_Text(pDX, IDC_WSCROLLV, m_WScrollV);
	DDV_MinMaxFloat(pDX, m_WScrollV, -1.f, 1.f);
	DDX_Text(pDX, IDC_BSCROLLV, m_BScrollV);
	DDV_MinMaxFloat(pDX, m_BScrollV, -1.f, 1.f);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CBumpMapDlg, CDialog)
	//{{AFX_MSG_MAP(CBumpMapDlg)
	ON_BN_CLICKED(IDC_WATERBRW, OnWaterbrw)
	ON_BN_CLICKED(IDC_ENVBRW, OnEnvbrw)
	ON_BN_CLICKED(IDC_DETAILBRW, OnDetailbrw)
	ON_BN_CLICKED(IDC_BUMPBRW, OnBumpbrw)
	ON_BN_CLICKED(IDC_TERRAINBROWSE, OnTerrainbrowse)
	ON_BN_CLICKED(IDC_NRMBRW, OnNrmbrw)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBumpMapDlg message handlers

void CBumpMapDlg::OnWaterbrw() 
{
	BrowseForTexture(&m_WaterTxt);
}

void CBumpMapDlg::OnEnvbrw() 
{
	BrowseForTexture(&m_EnvTxt);
}

void CBumpMapDlg::OnDetailbrw() 
{
	BrowseForTexture(&m_DetailTxt);
}

void CBumpMapDlg::OnBumpbrw() 
{
	BrowseForTexture(&m_BumpTxt);
}

void CBumpMapDlg::OnTerrainbrowse() 
{
	BrowseForTexture(&m_TerrainTxt);
}

void CBumpMapDlg::OnNrmbrw() 
{
	BrowseForTexture(&m_NormTxt);
}

void CBumpMapDlg::BrowseForTexture(CString *str)
{
	UpdateData(TRUE);
	CString texname,tpath=*erf_texture_path;
	texname=tpath+*str;
	CString pngname = texname+".png";
	if (::gos_DoesFileExist(pngname))
	{
		texname = pngname;
	}
	else
		texname+=".tga";
	CFileDialog dlg(true,"*.png",texname,OFN_HIDEREADONLY,"Texture Files (*.png;*.tga)|*.png;*.tga||");
	if (IDOK==dlg.DoModal())
	{
		CString rpath;
		rpath=dlg.GetPathName();
		rpath=rpath.Mid(tpath.GetLength());
		rpath=rpath.Left(rpath.ReverseFind('.'));
		*str=rpath;
	}	
	UpdateData(FALSE);
}

void CBumpMapDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default

	
	CDialog::OnClose();
}

BOOL CBumpMapDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	char TmpStr[512];
	DWORD dsize;

	dsize = 512;
	gos_LoadDataFromRegistry("TerrainTexture",TmpStr,&dsize);
	if(dsize>0) m_TerrainTxt = TmpStr;
	dsize = 512;
	gos_LoadDataFromRegistry("BumpTexture",TmpStr,&dsize);
	if(dsize>0) m_BumpTxt = TmpStr;


	dsize = 512;
	gos_LoadDataFromRegistry("DetailTexture",TmpStr,&dsize);
	if(dsize>0) m_DetailTxt = TmpStr;
	dsize = 512;
	gos_LoadDataFromRegistry("WaterTexture",TmpStr,&dsize);
	if(dsize>0) m_WaterTxt = TmpStr;
	dsize = 512;
	gos_LoadDataFromRegistry("EnvTexture",TmpStr,&dsize);
	if(dsize>0) m_EnvTxt = TmpStr;
	dsize = 512;
	gos_LoadDataFromRegistry("NormalTexture",TmpStr,&dsize);
	if(dsize>0) m_NormTxt  =TmpStr;
	
	gos_LoadDataFromRegistry("TerrainRep",&m_TerrainRep,&dsize);
	gos_LoadDataFromRegistry("BumpRep",&m_BumpRep,&dsize);
	gos_LoadDataFromRegistry("DetailRep",&m_DetailRep,&dsize);
	gos_LoadDataFromRegistry("WaterRep",&m_WaterRep,&dsize);
	gos_LoadDataFromRegistry("EnvRep",&m_EvnRep,&dsize);
	gos_LoadDataFromRegistry("NormalRep",&m_NormRep,&dsize);

	gos_LoadDataFromRegistry("WaterScrollU", &m_WScrollU, &dsize);
	gos_LoadDataFromRegistry("WaterScrollV", &m_WScrollV, &dsize);
	gos_LoadDataFromRegistry("BumpScrollU", &m_BScrollU, &dsize);
	gos_LoadDataFromRegistry("BumpScrollV", &m_BScrollV, &dsize);
	gos_LoadDataFromRegistry("NormalScrollU", &m_NScrollU, &dsize);
	gos_LoadDataFromRegistry("NormalScrollV", &m_NScrollV, &dsize);

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

void CBumpMapDlg::OnOK() 
{
	UpdateData(TRUE);

	gos_SaveDataToRegistry("TerrainTexture",(char *)(LPCSTR)m_TerrainTxt, m_TerrainTxt.GetLength()+1);
	gos_SaveDataToRegistry("BumpTexture",(char *)(LPCSTR)m_BumpTxt, m_BumpTxt.GetLength()+1);
	gos_SaveDataToRegistry("DetailTexture",(char *)(LPCSTR)m_DetailTxt, m_DetailTxt.GetLength()+1);
	gos_SaveDataToRegistry("WaterTexture",(char *)(LPCSTR)m_WaterTxt, m_WaterTxt.GetLength()+1);
	gos_SaveDataToRegistry("EnvTexture",(char *)(LPCSTR)m_EnvTxt, m_EnvTxt.GetLength()+1);
	gos_SaveDataToRegistry("NormalTexture",(char *)(LPCSTR)m_NormTxt, m_NormTxt.GetLength()+1);

	gos_SaveDataToRegistry("TerrainRep",&m_TerrainRep,sizeof(m_TerrainRep));
	gos_SaveDataToRegistry("BumpRep",&m_BumpRep,sizeof(m_BumpRep));
	gos_SaveDataToRegistry("DetailRep",&m_DetailRep,sizeof(m_DetailRep));
	gos_SaveDataToRegistry("WaterRep",&m_WaterRep,sizeof(m_WaterRep));
	gos_SaveDataToRegistry("EnvRep",&m_EvnRep,sizeof(m_EvnRep));
	gos_SaveDataToRegistry("NormalRep",&m_NormRep,sizeof(m_NormRep));

	gos_SaveDataToRegistry("WaterScrollU", &m_WScrollU,sizeof(m_WScrollU));
	gos_SaveDataToRegistry("WaterScrollV", &m_WScrollV,sizeof(m_WScrollV));
	gos_SaveDataToRegistry("BumpScrollU", &m_BScrollU,sizeof(m_BScrollU));
	gos_SaveDataToRegistry("BumpScrollV", &m_BScrollV,sizeof(m_BScrollV));
	gos_SaveDataToRegistry("NormalScrollU", &m_NScrollU,sizeof(m_NScrollU));
	gos_SaveDataToRegistry("NormalScrollV", &m_NScrollV,sizeof(m_NScrollV));
	
	CDialog::OnOK();
}
