// OptimizeOptions.cpp : implementation file
//

#include "stdafx.h"
#include "tctb.h"
#include "OptimizeOptions.h"

extern CTCTbApp theApp;

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

/////////////////////////////////////////////////////////////////////////////
// COptimizeOptions dialog


COptimizeOptions::COptimizeOptions(CWnd* pParent /*=NULL*/)
	: CDialog(COptimizeOptions::IDD, pParent)
{
	//{{AFX_DATA_INIT(COptimizeOptions)
	m_opolys = _T("");
	m_fpolys = 0;
	m_bsizetext = _T("");
	m_binfo = _T("");
	//}}AFX_DATA_INIT
}


void COptimizeOptions::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COptimizeOptions)
	DDX_Control(pDX, IDC_BINSIZE, m_bsizesli);
	DDX_Text(pDX, IDC_ORGPOLYS, m_opolys);
	DDX_Text(pDX, IDC_FINPOLYS, m_fpolys);
	DDX_Text(pDX, IDC_BSIZETEXT, m_bsizetext);
	DDX_Text(pDX, IDC_BINFO, m_binfo);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(COptimizeOptions, CDialog)
	//{{AFX_MSG_MAP(COptimizeOptions)
	ON_WM_HSCROLL()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COptimizeOptions message handlers

void COptimizeOptions::OnOK() 
{
	// TODO: Add extra validation here
	UpdateData(TRUE);
	int plimit;
	plimit=atoi(m_opolys);
	if(m_fpolys>plimit || m_fpolys<2)
	{
	MessageBox("Final Polys must be in range 2 - Original Polys","Invalid Input",MB_OK|MB_ICONWARNING);
	m_fpolys=plimit/2;
	UpdateData(FALSE);
	return;
	}

	CDialog::OnOK();
}

BOOL COptimizeOptions::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_bsizesli.SetRange(10,MidLevelRenderer::Limits::Max_Number_Vertices_Per_Mesh/3-2); 
	m_bsizesli.SetPos(64);

	float xm=theApp.RVWnd->ActiveHField->GetXinM();
	float zm=theApp.RVWnd->ActiveHField->GetZinM();
	
	float dim = static_cast<float>(sqrt(m_fpolys/m_bsizesli.GetPos()));

	if(dim < 1.0f)
		dim = 1.0f;

	m_bsizetext.Format("%i",m_bsizesli.GetPos());
	m_binfo.Format("Avg. Mesh Size: %.1f m x %.1f m", xm/dim, zm/dim);

	UpdateData(FALSE);

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

void COptimizeOptions::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	// TODO: Add your message handler code here and/or call default
	float xm=theApp.RVWnd->ActiveHField->GetXinM();
	float zm=theApp.RVWnd->ActiveHField->GetZinM();
	
	UpdateData(TRUE);

	float dim = static_cast<float>(sqrt(m_fpolys/m_bsizesli.GetPos()));

	if(dim < 1.0f)
		dim = 1.0f;

	m_bsizetext.Format("%i",m_bsizesli.GetPos());
	m_binfo.Format("Avg. Mesh Size: %.1f m x %.1f m", xm/dim, zm/dim);

	UpdateData(FALSE);

	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
