// EditBatch.cpp : implementation file
//

#include "stdafx.h"
#ifdef STRICT							// Already defined by stdafx,
#undef STRICT							// so we avoid warning linking msg
#endif
#ifdef _MBCS							// The same as above
#undef _MBCS
#endif

#include "animationsuite.h"
#include <Stuff\StuffHeaders.hpp>
#include "EditBatch.h"
#include "DlgSelectMegatextures.h"

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

/////////////////////////////////////////////////////////////////////////////
// CEditBatch dialog


CEditBatch::CEditBatch(Page *page,CWnd* pParent /*=NULL*/)
	: CDialog(CEditBatch::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditBatch)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pPage = page;
}


void CEditBatch::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditBatch)
	DDX_Control(pDX, IDC_EDIT_MEGANAMES, m_MegaNames);
	DDX_Control(pDX, IDC_EDIT_HINT, m_Hint);
	DDX_Control(pDX, IDC_CHECK_CROSSFADE, m_checkFade);
	DDX_Control(pDX, IDC_CHECK_CAGE, m_checkCage);
	DDX_Control(pDX, IDC_EDIT_CAGE, m_Cage);
	DDX_Control(pDX, IDC_CHECK_ANIM, m_checkAnim);
	DDX_Control(pDX, IDC_CHECK_OBB, m_checkOBB);
	DDX_Control(pDX, IDC_EDIT_OBB, m_Obb);
	DDX_Control(pDX, IDC_EDIT_SKEL, m_Skel);
	DDX_Control(pDX, IDC_EDIT_ANIM, m_Anim);
	DDX_Control(pDX, IDC_EDIT_PATH, m_Path);
	DDX_Control(pDX, IDC_LIST_CLASS, m_listclass);
	DDX_Control(pDX, IDC_ALIGN_X, m_checkAlignX);
	DDX_Control(pDX, IDC_ALIGN_Y, m_checkAlignY);
	DDX_Control(pDX, IDC_ALIGN_STARTLOD, m_comboStartLOD);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CEditBatch, CDialog)
	//{{AFX_MSG_MAP(CEditBatch)
	ON_BN_CLICKED(IDC_BUTTON_LOD0, OnButtonLod0)
	ON_BN_CLICKED(IDC_BUTTON_LOD1, OnButtonLod1)
	ON_BN_CLICKED(IDC_BUTTON_LOD2, OnButtonLod2)
	ON_BN_CLICKED(IDC_BUTTON_LOD4, OnButtonLod4)
	ON_BN_CLICKED(IDC_BUTTON_LOD5, OnButtonLod5)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM0, OnButtonLoddam0)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM1, OnButtonLoddam1)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM2, OnButtonLoddam2)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM3, OnButtonLoddam3)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM4, OnButtonLoddam4)
	ON_BN_CLICKED(IDC_BUTTON_LODDAM5, OnButtonLoddam5)
	ON_BN_CLICKED(IDC_BUTTON_LOD3, OnButtonLod3)
	ON_BN_CLICKED(IDC_CHECK_ANIM, OnCheckAnim)
	ON_BN_CLICKED(IDC_CHECK_OBB, OnCheckObb)
	ON_BN_CLICKED(IDC_BROWSE_PATH, OnBrowsePath)
	ON_BN_CLICKED(IDC_BROWSE_SKEL, OnBrowseSkel)
	ON_BN_CLICKED(IDC_BROWSE_ANIM, OnBrowseAnim)
	ON_BN_CLICKED(IDC_BROWSE_OBB, OnBrowseObb)
	ON_BN_CLICKED(IDC_BROWSE_CAGE, OnBrowseCage)
	ON_BN_CLICKED(IDC_CHECK_CAGE, OnCheckCage)
	ON_BN_CLICKED(IDC_RADIO_CHAR, OnRadioChar)
	ON_BN_CLICKED(IDC_RADIO_SHAPE, OnRadioShape)
	ON_BN_CLICKED(IDC_CHECK_CROSSFADE, OnCheckCrossfade)
	ON_BN_CLICKED(IDC_BROWSE_HINT, OnBrowseHint)
	ON_BN_CLICKED(IDC_SELECT_MEGANAMES, OnSelectMeganames)
	ON_CBN_SELCHANGE(IDC_ALIGN_STARTLOD, OnSelectStartLOD)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CEditBatch message handlers

CString g_szbrowsepath;
static int CALLBACK BrowseCallbackProc1(HWND hWnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	RECT rect;
	//
	//Center the browse dialog on the screen...
	//
	switch (uMsg)
	{
	case BFFM_INITIALIZED:
		{
			SendMessage(hWnd,BFFM_SETSELECTION,TRUE,(LPARAM)(LPCTSTR)g_szbrowsepath);
	
			if (GetWindowRect(hWnd, &rect))
			{
				SetWindowPos(hWnd, 
							 HWND_TOP,
							 (GetSystemMetrics(SM_CXSCREEN) - (rect.right - rect.left)) >> 1,
							 (GetSystemMetrics(SM_CYSCREEN) - (rect.bottom - rect.top)) >> 1,
							 0,
							 0,
							 SWP_NOSIZE);
			}
		}
	}
	return 0;
}

CString BrowseDirectory(const char *strCurrent,CWnd *pWnd)
{
	CString str;
	TCHAR        szBuffer[MAX_PATH];
	LPITEMIDLIST pidlBrowse,pidlRoot;
	BROWSEINFO   bi;

	LPMALLOC lpMalloc;
	SHGetMalloc(&lpMalloc);
	ASSERT(lpMalloc);
	bi.hwndOwner = pWnd->GetSafeHwnd();
	// Get the ID for the "My Computer" root folder
	SHGetSpecialFolderLocation(pWnd->GetSafeHwnd(),CSIDL_DRIVES,&pidlRoot);

	bi.pidlRoot  = pidlRoot;
	bi.pszDisplayName = szBuffer;

	// Localize the descriptive text
	bi.lpszTitle = "Select a path";

	// BIF_EDITBOX and BIF_VALIDATE are for versions 4.71 and later, they
	// might be ignored by older versions
	bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;// | BIF_EDITBOX;// | BIF_USENEWUI;// | BIF_VALIDATE;
	bi.lParam = (LPARAM)pWnd;
	bi.iImage = NULL;
	bi.lpfn = BrowseCallbackProc1;
	bi.lParam = (LPARAM)strCurrent;
	g_szbrowsepath = strCurrent;
	//
	//Call the Windows shell folder browsing routine...
	//
	pidlBrowse = SHBrowseForFolder(&bi);
	if (NULL != pidlBrowse)
	{
		if (SHGetPathFromIDList(pidlBrowse, szBuffer))
		{
			if ('\\' == szBuffer[lstrlen(szBuffer)-1])
				szBuffer[lstrlen(szBuffer)-1] = '\0';
			str = CString(szBuffer);
		}
		else
			str = "";
		//
		//Free up shell memory used...
		//
		lpMalloc->Free(pidlBrowse);
	}
	else
		str = "";
	lpMalloc->Free(pidlRoot);
	lpMalloc->Release();
	return str;
}

CString szClasses[] = 
	{
		"MechWarrior4::Airplane",
		"MechWarrior4::Bridge",
		"MechWarrior4::Building",
		"MechWarrior4::Boat",
		"MechWarrior4::Cultural",
		"MechWarrior4::DropShip",
		"MechWarrior4::Helicopter",
		"MechWarrior4::HoverCraft",
		"MechWarrior4::Mech",
		"MechWarrior4::MFB",
		"MechWarrior4::NonCom",
		"MechWarrior4::Tank",
		"MechWarrior4::Truck",
		"MechWarrior4::Turret",
		""
	};


BOOL CEditBatch::OnInitDialog() 
{
	CDialog::OnInitDialog();

	// Initialize the controls
	int index=0;
	while (szClasses[index] != "") {
		m_listclass.InsertString(index, szClasses[index]);
		index++;
	}

	const char *strclass;
	if (m_pPage->GetEntry("Class",&strclass))
	{
		int index=0;
		while (szClasses[index] != "") {
			if (0==strnicmp(szClasses[index],strclass,strlen(szClasses[index])))
				m_listclass.SetCurSel(index);
			index++;
		}
	}

	// Get the entries out of the page
	const char *path;
	if (m_pPage->GetEntry("path",&path))
	{
		m_Path.SetWindowText(path);
	}

	const char *lodSkel;
	if (m_pPage->GetEntry("lodSkel",&lodSkel))
	{
		m_Skel.SetWindowText(lodSkel);
	}

	const char *anim;
	if (m_pPage->GetEntry("anim",&anim))
	{
		this->m_checkAnim.SetCheck(1);
		m_Anim.SetWindowText(anim);
	}
	else
	{
		m_Anim.EnableWindow(false);
	}

	const char *obb;
	if (m_pPage->GetEntry("obb",&obb))
	{
		this->m_checkOBB.SetCheck(1);
		m_Obb.SetWindowText(obb);
	}
	else
	{
		m_Obb.EnableWindow(false);
	}

	const char *cage;
	if (m_pPage->GetEntry("Cage",&cage))
	{
		this->m_checkCage.SetCheck(1);
		m_Cage.SetWindowText(cage);
	}
	else
	{
		m_Cage.EnableWindow(false);
	}

	CString strHint(path);
	const char *hint=NULL;
	if (m_pPage->GetEntry("Hint",&hint))
	{
	}
	else
	{
		strHint.MakeLower();
		int index = strHint.Find("\\content\\");
		if (index>0)
		{
			strHint = strHint.Left(index);
			strHint+="\\content\\textures\\textures.hint";
			hint = strHint;
		}
	}
	if (hint)
		m_Hint.SetWindowText(hint);
	
	const char *meganames;
	if (m_pPage->GetEntry("MegaTextures",&meganames))
	{
		m_MegaNames.SetWindowText(meganames);
	}

	BYTE bChar = 0;
	const char *lodChar[6];
	for (int x=0;x<6;x++)
	{
		CString str;
		str.Format("LODChar%d",x);
		if (m_pPage->GetEntry(str,&lodChar[x]))
		{
			bChar |= 1<<x;
		}
	}

	BYTE bShape = 0;
	const char *lodShape[6];
	for (x=0;x<6;x++)
	{
		CString str;
		str.Format("LODShape%d",x);
		if (m_pPage->GetEntry(str,&lodShape[x]))
		{
			bShape |= 1<<x;
		}
	}
	if (bChar && bShape)
	{
		// We have a problem here
		MessageBox("Both LODShape and LODChar specified, defaulting to LODShape (Shape export)",m_pPage->GetName(),MB_OK);
		bChar = 0;
	}

	BYTE flags = 0;
	const char **lod = NULL;
	if (bChar)
	{
		flags = bChar;
		lod = lodChar;
		CButton *b = (CButton *)this->GetDlgItem(IDC_RADIO_CHAR);
		b->SetCheck(1);
	}

	if (bShape)
	{
		flags = bShape;
		lod = lodShape;
		CButton *b = (CButton *)this->GetDlgItem(IDC_RADIO_SHAPE);
		b->SetCheck(1);
		m_Skel.EnableWindow(false);
	}

	for (x=0;x<6;x++)
	{
		if (flags & (1<<x))
		{
			this->SetDlgItemText(IDC_EDIT_LOD0+x,lod[x]);
		}
	}
	BYTE bDam = 0;
	const char *lodDam[6];
	for (x=0;x<6;x++)
	{
		CString str;
		str.Format("LodDam%d",x);
		if (m_pPage->GetEntry(str,&lodDam[x]))
		{
			bDam &= 1<<x;
			this->SetDlgItemText(IDC_EDIT_DAM0+x,lodDam[x]);
		}
	}

	// Load billboarding stuff
	m_comboStartLOD.ResetContent();
	m_comboStartLOD.AddString("None");
	m_comboStartLOD.AddString("LOD0");
	m_comboStartLOD.AddString("LOD1");
	m_comboStartLOD.AddString("LOD2");
	m_comboStartLOD.AddString("LOD3");
	m_comboStartLOD.AddString("LOD4");
	m_comboStartLOD.AddString("LOD5");
	m_comboStartLOD.SelectString(0, "None");

	bool bFade = false;
	const char *lodDist[6];
	for (x=0;x<6;x++)
	{
		CString str;
		str.Format("LodDist%d",x);
		if (m_pPage->GetEntry(str,&lodDist[x]))
		{
			char dist[6][4][MAX_PATH];
			::memset(dist, 0, 6*4*MAX_PATH);
			int count = sscanf(lodDist[x],"%s %s %s %s",dist[x][0],dist[x][1],dist[x][2],dist[x][3]);
			if (count>1)
				bFade = true;
		}
	}
	m_checkFade.SetCheck(bFade);
	OnCheckCrossfade();

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

void CEditBatch::OnOK() 
{
	m_pPage->DeleteAllNotes();
	
	int index = m_listclass.GetCurSel();
	if (index>=0)
	{
		CString str;
		m_listclass.GetText(index,str);
		m_pPage->AppendEntry("Class",str);
	}
	else
	{
		MessageBox("You must select a Class for this export: Building, Mech, or Vehicle.","Batch Export",MB_OK);
		return;
	}

	CString str;
	this->m_Path.GetWindowText(str);
	m_pPage->AppendEntry("Path",str);

	//this->m_Skel.GetWindowText(str);
	//m_pPage->AppendEntry("LODSkel",str);

	CString lodstr;
	CButton *b1 = (CButton *)this->GetDlgItem(IDC_RADIO_CHAR);
	CButton *b2 = (CButton *)this->GetDlgItem(IDC_RADIO_SHAPE);
	//this->m_Skel.GetWindowText(str);
	//if (str.GetLength())
	//	m_pPage->AppendEntry("LODSkel",str);
	if (b1->GetCheck())
	{
		lodstr = "LODChar";
		this->m_Skel.GetWindowText(str);
		m_pPage->AppendEntry("LODSkel",str);
	}
	if (b2->GetCheck())
	{
		lodstr = "LODShape";
	}

	for (int x=0;x<6;x++)
	{
		this->GetDlgItemText(IDC_EDIT_LOD0+x,str);
		if (str.GetLength())
		{
			CString str1;
			str1.Format("%s%d",lodstr,x);
			m_pPage->AppendEntry(str1,str);
		}
	}

	for (x=0;x<6;x++)
	{
		this->GetDlgItemText(IDC_EDIT_DAM0+x,str);
		if (str.GetLength())
		{
			CString str1;
			str1.Format("LODDam%d",x);
			m_pPage->AppendEntry(str1,str);
		}
	}

	bool bFade = m_checkFade.GetCheck() != 0;
	for (x=0;x<6;x++)
	{
		CString strName;
		strName.Format("LODDist%d",x);

		if (bFade) {
			CString strNear;
			this->GetDlgItemText(IDC_EDIT_NEAR0+x,strNear);
			CString strFar;
			this->GetDlgItemText(IDC_EDIT_FAR0+x,strFar);
			
			if (strNear.GetLength() && strFar.GetLength())
			{				
				CString strin;
				this->GetDlgItemText(IDC_EDIT_FADIN0+2*x,strin);
				
				CString strout;
				this->GetDlgItemText(IDC_EDIT_FADOUT0+2*x,strout);
				
				CString strEntry;
				strEntry.Format("%s %s %s %s",strNear,strin,strout,strFar);
				m_pPage->AppendEntry(strName,strEntry);
			}
		}
		else {
			CString strFar;
			this->GetDlgItemText(IDC_EDIT_DIST0+x,strFar);
			if (strFar.GetLength())
			{				
				m_pPage->AppendEntry(strName,strFar);
			}
		}
	}

	if (this->m_checkAnim.GetCheck())
	{
		this->m_Anim.GetWindowText(str);
		m_pPage->AppendEntry("Anim",str);
	}

	if (this->m_checkOBB.GetCheck())
	{
		this->m_Obb.GetWindowText(str);
		m_pPage->AppendEntry("OBB",str);
	}

	if (this->m_checkCage.GetCheck())
	{
		this->m_Cage.GetWindowText(str);
		m_pPage->AppendEntry("Cage",str);
	}

	m_Hint.GetWindowText(str);
	m_pPage->AppendEntry("Hint",str);

	m_MegaNames.GetWindowText(str);
	m_pPage->AppendEntry("MegaTextures",str);

	// Save billboarding stuff
	this->m_comboStartLOD.GetWindowText(str);
	if (str != "None") {
		m_pPage->AppendEntry("BillboardFrom", str);
		bool bAlignX = this->m_checkAlignX.GetCheck() != 0;
		bool bAlignY = this->m_checkAlignY.GetCheck() != 0;
		if (bAlignX || bAlignY)
		{
			if (bAlignX && bAlignY)
				m_pPage->AppendEntry("BillboardAlign", "X,Y");
			else if (bAlignX)
				m_pPage->AppendEntry("BillboardAlign", "X");
			else if (bAlignY)
				m_pPage->AppendEntry("BillboardAlign", "Y");
		}
	}

	CDialog::OnOK();
}

void CEditBatch::OnButtonLod0() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD0,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD0,str);
	}
}

void CEditBatch::OnButtonLod1() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD1,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD1,str);
	}
}

void CEditBatch::OnButtonLod2() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD2,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD2,str);
	}
	
}

void CEditBatch::OnButtonLod3() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD3,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD3,str);
	}
	
}
void CEditBatch::OnButtonLod4() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD4,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD4,str);
	}
}

void CEditBatch::OnButtonLod5() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_LOD5,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_LOD5,str);
	}	
}

void CEditBatch::OnButtonLoddam0() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM0,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM0,str);
	}
}

void CEditBatch::OnButtonLoddam1() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM1,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM1,str);
	}
}

void CEditBatch::OnButtonLoddam2() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM2,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM2,str);
	}
}

void CEditBatch::OnButtonLoddam3() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM3,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM3,str);
	}
}

void CEditBatch::OnButtonLoddam4() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM4,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM4,str);
	}
}

void CEditBatch::OnButtonLoddam5() 
{
	CString str;
	this->GetDlgItemText(IDC_EDIT_DAM5,str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		this->SetDlgItemText(IDC_EDIT_DAM5,str);
	}
}


void CEditBatch::OnCheckAnim() 
{
	if (this->m_checkAnim.GetCheck())
	{
		this->m_Anim.EnableWindow(true);
	}
	else
	{
		this->m_Anim.EnableWindow(false);
	}
}

void CEditBatch::OnCheckObb() 
{
	if (this->m_checkOBB.GetCheck())
	{
		this->m_Obb.EnableWindow(true);
	}
	else
	{
		this->m_Obb.EnableWindow(false);
	}
}

void CEditBatch::OnBrowsePath() 
{
	CString str;
	this->m_Path.GetWindowText(str);
	CString newstr = BrowseDirectory(str,this);
	if (newstr.GetLength())
	{
		m_Path.SetWindowText(newstr);
	}
}

void CEditBatch::OnBrowseSkel() 
{
	CString str;
	this->m_Skel.GetWindowText(str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		m_Skel.SetWindowText(str);
	}
}

void CEditBatch::OnBrowseAnim() 
{
	CString str;
	this->m_Anim.GetWindowText(str);
	CString newstr = BrowseDirectory(str,this);
	if (newstr.GetLength())
	{
		m_Anim.SetWindowText(newstr);
	}	
}

void CEditBatch::OnBrowseObb() 
{
	CString str;
	this->m_Obb.GetWindowText(str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		m_Obb.SetWindowText(str);
	}
}

void CEditBatch::OnBrowseCage() 
{
	CString str;
	this->m_Cage.GetWindowText(str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		m_Cage.SetWindowText(str);
	}
}

void CEditBatch::OnCheckCage() 
{
	if (this->m_checkCage.GetCheck())
	{
		this->m_Cage.EnableWindow(true);
	}
	else
	{
		this->m_Cage.EnableWindow(false);
	}	
}

void CEditBatch::OnRadioChar() 
{
	m_Skel.EnableWindow(true);	
}

void CEditBatch::OnRadioShape() 
{
	m_Skel.EnableWindow(false);
}

void CEditBatch::OnCheckCrossfade() 
{
	bool bActive = m_checkFade.GetCheck() != 0;

	int iLodDistNear = 0;
	int iLodDistFar = 0;

	const char *lodDist[6];
	for (int x=0;x<6;x++)
	{
		CWnd *pWnd = GetDlgItem(IDC_EDIT_DIST0+x);
		pWnd->EnableWindow(!bActive);
		
		pWnd = GetDlgItem(IDC_EDIT_FADIN0+2*x);
		pWnd->EnableWindow(bActive);
		pWnd = GetDlgItem(IDC_EDIT_FADOUT0+2*x);
		pWnd->EnableWindow(bActive);
		
		pWnd = GetDlgItem(IDC_EDIT_NEAR0+x);
		pWnd->EnableWindow(bActive);
		pWnd = GetDlgItem(IDC_EDIT_FAR0+x);
		pWnd->EnableWindow(bActive);

		CString str;
		str.Format("LodDist%d",x);
		if (m_pPage->GetEntry(str,&lodDist[x]))
		{
			char dist[6][4][MAX_PATH];
			::memset(dist, 0, 6*4*MAX_PATH);
			int count = sscanf(lodDist[x],"%s %s %s %s",dist[x][0],dist[x][1],dist[x][2],dist[x][3]);
			if (count == 1) {
				this->SetDlgItemText(IDC_EDIT_DIST0+x,dist[x][0]);
				if (x!=0)
					iLodDistNear = this->GetDlgItemInt(IDC_EDIT_DIST0+x-1);
				iLodDistFar = this->GetDlgItemInt(IDC_EDIT_DIST0+x);
				
				if (iLodDistFar) {
					this->SetDlgItemInt(IDC_EDIT_NEAR0+x, (iLodDistNear-5>0)?iLodDistNear-5:0);
					this->SetDlgItemInt(IDC_EDIT_FADIN0+2*x, iLodDistNear+5);
					this->SetDlgItemInt(IDC_EDIT_FADOUT0+2*x, (iLodDistFar-5>0)?iLodDistFar-5:0);
					this->SetDlgItemInt(IDC_EDIT_FAR0+x, iLodDistFar+5);
				}
				else{
					this->SetDlgItemText(IDC_EDIT_NEAR0+x, "");
					this->SetDlgItemText(IDC_EDIT_FAR0 +x, "");
					this->SetDlgItemText(IDC_EDIT_FADIN0 +2*x, "");
					this->SetDlgItemText(IDC_EDIT_FADOUT0+2*x, "");
				}
			}
			else {
				this->SetDlgItemText(IDC_EDIT_NEAR0+x,dist[x][0]);
				this->SetDlgItemText(IDC_EDIT_FADIN0+2*x,dist[x][1]);
				this->SetDlgItemText(IDC_EDIT_FADOUT0+2*x,dist[x][2]);
				this->SetDlgItemText(IDC_EDIT_FAR0+x,dist[x][3]);
			}
		}
	}

	const char *billBoardFrom = NULL;
	if (m_pPage->GetEntry("BillBoardFrom", &billBoardFrom)) {
		m_comboStartLOD.SelectString(0, billBoardFrom);

		// Get Align axis for billboarding
		const char *billBoardAlign = NULL;
		if (m_pPage->GetEntry("BillboardAlign",&billBoardAlign)) {
			CString strAlign = billBoardAlign;
			strAlign.MakeLower();
			if (strAlign.Find("x")>=0)
				m_checkAlignX.SetCheck(1);
			if (strAlign.Find("y")>=0)
				m_checkAlignY.SetCheck(1);
		}
	}
	else {
		m_comboStartLOD.SelectString(0, "None");
	}
	
	m_comboStartLOD.EnableWindow(bActive);
	OnSelectStartLOD();
}

void CEditBatch::OnBrowseHint() 
{
	CString str;
	this->m_Hint.GetWindowText(str);
	CFileDialog dlg(true,NULL,str);
	if (IDOK == dlg.DoModal())
	{
		str = dlg.GetPathName();
		m_Hint.SetWindowText(str);
	}
}

void CEditBatch::OnSelectMeganames() 
{
	CString hintfile;
	m_Hint.GetWindowText(hintfile);
	CDlgSelectMegatextures dlg;
	m_MegaNames.GetWindowText(dlg.m_strMegaTextures);
	dlg.m_strHintFile = hintfile;
	if (IDOK == dlg.DoModal())
	{
		m_MegaNames.SetWindowText(dlg.m_strMegaTextures);
	}
}

void CEditBatch::OnSelectStartLOD() 
{
	CString startLOD;
	m_comboStartLOD.GetLBText(m_comboStartLOD.GetCurSel(), startLOD);

	m_checkAlignX.EnableWindow(startLOD!="None" && m_comboStartLOD.IsWindowEnabled());
	m_checkAlignY.EnableWindow(startLOD!="None" && m_comboStartLOD.IsWindowEnabled());
}
