// CamInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "TCTb.h"
#include "CamInfoDlg.h"

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

#define VALIDATE_TIMER  1234
#define VALIDATE_TIME	1000
/////////////////////////////////////////////////////////////////////////////
// CCamInfoDlg dialog


CCamInfoDlg::CCamInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCamInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCamInfoDlg)
	m_CamLocX = 0.0;
	m_CamLocZ = 0.0;
	m_CamLocY = 0.0;
	m_CamTrkAlt = 0.0;
	m_TarLocX = 0.0;
	m_TarLocY = 0.0;
	m_TarLocZ = 0.0;
	m_TarTAlt = 0.0;
	m_TmeSli = 0;
	m_VelSli = 0;
	m_FOVSli = 0;
	m_FarClip = 0.0;
	m_NearClip = 0.0;
	m_FOVText = 0;
	//}}AFX_DATA_INIT
	CamInfo=NULL;
	FCon=0;
	NoUpdate=false;
}


void CCamInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCamInfoDlg)
	DDX_Control(pDX, IDC_NEARCLIP, m_NearClipCtrl);
	DDX_Control(pDX, IDC_FARCLIP, m_FarClipCtrl);
	DDX_Control(pDX, IDC_FOVTEXT, m_FOVTextCtrl);
	DDX_Control(pDX, IDC_FOVSLI, m_FOVSliCtrl);
	DDX_Control(pDX, IDC_VELTEXT, m_VelText);
	DDX_Control(pDX, IDC_VELSLI, m_VelSliCtrl);
	DDX_Control(pDX, IDC_CAMLOCZ, m_CLOCZCtrl);
	DDX_Control(pDX, IDC_CAMLOCY, m_CLOCYCtrl);
	DDX_Control(pDX, IDC_CAMLOCX, m_CLOCXCtrl);
	DDX_Control(pDX, IDC_SPOSSLI, m_TSliCtrl);
	DDX_Control(pDX, IDC_POSTEXT, m_PosText);
	DDX_Control(pDX, IDC_CAMTALT, m_CTAlt);
	DDX_Control(pDX, IDC_TLOCTT, m_TTT);
	DDX_Control(pDX, IDC_TARTALT, m_TTACtrl);
	DDX_Control(pDX, IDC_TARLOCZ, m_TLZCtrl);
	DDX_Control(pDX, IDC_TARLOCY, m_TLYCtrl);
	DDX_Control(pDX, IDC_TARLOCX, m_TLXCtrl);
	DDX_Text(pDX, IDC_CAMLOCX, m_CamLocX);
	DDX_Text(pDX, IDC_CAMLOCZ, m_CamLocZ);
	DDX_Text(pDX, IDC_CAMLOCY, m_CamLocY);
	DDX_Text(pDX, IDC_CAMTALT, m_CamTrkAlt);
	DDX_Text(pDX, IDC_TARLOCX, m_TarLocX);
	DDX_Text(pDX, IDC_TARLOCY, m_TarLocY);
	DDX_Text(pDX, IDC_TARLOCZ, m_TarLocZ);
	DDX_Text(pDX, IDC_TARTALT, m_TarTAlt);
	DDX_Slider(pDX, IDC_SPOSSLI, m_TmeSli);
	DDX_Slider(pDX, IDC_VELSLI, m_VelSli);
	DDX_Slider(pDX, IDC_FOVSLI, m_FOVSli);
	DDX_Text(pDX, IDC_FARCLIP, m_FarClip);
	DDV_MinMaxDouble(pDX, m_FarClip, 1.e-003, 99999999.);
	DDX_Text(pDX, IDC_NEARCLIP, m_NearClip);
	DDV_MinMaxDouble(pDX, m_NearClip, 1.e-003, 9999999.);
	DDX_Text(pDX, IDC_FOVTEXT, m_FOVText);
	DDV_MinMaxInt(pDX, m_FOVText, 1, 180);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CCamInfoDlg, CDialog)
//{{AFX_MSG_MAP(CCamInfoDlg)
ON_WM_CLOSE()
ON_BN_CLICKED(IDC_CLOCTT, OnCloctt)
ON_BN_CLICKED(IDC_USETARGET, OnUsetarget)
ON_BN_CLICKED(IDC_TLOCTT, OnTloctt)
ON_BN_CLICKED(IDC_TRACK, OnTrack)
ON_BN_CLICKED(IDC_FREE, OnFree)
ON_WM_CREATE()
ON_BN_CLICKED(IDC_ANIMATE, OnAnimate)
ON_WM_HSCROLL()
ON_WM_RBUTTONUP()
ON_WM_RBUTTONDOWN()
ON_EN_CHANGE(IDC_CAMLOCX, OnChangeCamlocx)
ON_EN_CHANGE(IDC_CAMLOCY, OnChangeCamlocy)
ON_EN_CHANGE(IDC_CAMLOCZ, OnChangeCamlocz)
ON_EN_CHANGE(IDC_TARLOCX, OnChangeTarlocx)
ON_EN_CHANGE(IDC_TARLOCY, OnChangeTarlocy)
ON_EN_CHANGE(IDC_TARLOCZ, OnChangeTarlocz)
ON_BN_CLICKED(IDC_RESETCURVE, OnResetcurve)
ON_EN_CHANGE(IDC_FARCLIP, OnChangeFarclip)
ON_EN_CHANGE(IDC_NEARCLIP, OnChangeNearclip)
ON_WM_TIMER()
ON_EN_CHANGE(IDC_TARTALT, OnChangeTartalt)
ON_EN_CHANGE(IDC_FOVTEXT, OnChangeFovtext)
ON_EN_CHANGE(IDC_CAMTALT, OnChangeCamtalt)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCamInfoDlg message handlers

void CCamInfoDlg::UpdateInfo()
{
	if(!CamInfo) return;
	if(NoUpdate) return;
	
	m_CamLocX=CamInfo->Loc.x;
	m_CamLocY=CamInfo->Loc.y;
	m_CamLocZ=CamInfo->Loc.z;
	
	m_TarLocX=CamInfo->TargetLoc.x;
	m_TarLocY=CamInfo->TargetLoc.y;
	m_TarLocZ=CamInfo->TargetLoc.z;
	
	m_TarTAlt=CamInfo->TarTrackAlt;
	m_CamTrkAlt=CamInfo->CamTrackAlt;
	m_NearClip=CamInfo->NearClip;
	m_FarClip=CamInfo->FarClip;
	m_FOVSli=m_FOVText=(int)(CamInfo->FOV*Degrees_Per_Radian);
	
	m_TmeSli=((int)(CamInfo->GetTime()*100));
	m_VelSli=((int)(CamInfo->GetVel()*100));
	
	
	CheckDlgButton(IDC_USETARGET,CamInfo->UseTarget);
	CheckDlgButton(IDC_ANIMATE,CamInfo->Animate);
	CheckDlgButton(IDC_CLOCTT,CamInfo->CamATrkFlg);
	CheckDlgButton(IDC_TLOCTT,CamInfo->TarATrkFlg);
	
	CheckRadioButton(IDC_FREE,IDC_TRACK,CamInfo->GetMode()==CameraInfo::CM_TRACK?IDC_TRACK:IDC_FREE);
	
	UpdateData(FALSE);
	UpdateControlStates();
}

void CCamInfoDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	//	CDialog::OnCancel();
}

void CCamInfoDlg::OnOK() 
{
	// TODO: Add extra validation here
	
	//	CDialog::OnOK();
}

void CCamInfoDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	//	ShowWindow(SW_MINIMIZE);
	ShowWindow(SW_HIDE);
	
	//	CDialog::OnClose();
}

void CCamInfoDlg::UpdateControlStates()
{
	m_TLXCtrl.EnableWindow(IsDlgButtonChecked(IDC_USETARGET));
	m_TLYCtrl.EnableWindow(IsDlgButtonChecked(IDC_USETARGET));
	m_TLZCtrl.EnableWindow(IsDlgButtonChecked(IDC_USETARGET));
	m_TTACtrl.EnableWindow(IsDlgButtonChecked(IDC_USETARGET));
	m_TTT.EnableWindow(IsDlgButtonChecked(IDC_USETARGET));
	
	
	if(IsDlgButtonChecked(IDC_USETARGET))
		m_TTACtrl.EnableWindow(IsDlgButtonChecked(IDC_TLOCTT));
	
	m_CTAlt.EnableWindow(IsDlgButtonChecked(IDC_CLOCTT));
	//m_TSliCtrl.EnableWindow(!IsDlgButtonChecked(IDC_ANIMATE));
	
	CheckDlgButton(IDC_ANIMATE,CamInfo->Animate);
	
	CString tstr;
	tstr.Format("%i%%",m_TmeSli);
	m_PosText.SetWindowText(tstr);
	
	tstr.Format("%i%%",m_VelSli);
	m_VelText.SetWindowText(tstr);
}

void CCamInfoDlg::OnCloctt() 
{
	UpdateControlStates();
	CamInfo->CamATrkFlg=IsDlgButtonChecked(IDC_CLOCTT)?true:false;
}

void CCamInfoDlg::OnTloctt() 
{
	UpdateControlStates();
	CamInfo->TarATrkFlg=IsDlgButtonChecked(IDC_TLOCTT)?true:false;
}

void CCamInfoDlg::OnUsetarget() 
{
	CamInfo->UseTarget=IsDlgButtonChecked(IDC_USETARGET)?true:false;
}


void CCamInfoDlg::SetCamInfo(CameraInfo *CIn)
{
	CamInfo=CIn;
}

void CCamInfoDlg::OnTrack() 
{
	CamInfo->SetMode(CameraInfo::CM_TRACK);
}

void CCamInfoDlg::OnFree() 
{
	CamInfo->SetMode(CameraInfo::CM_FREE);
}

int CCamInfoDlg::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CDialog::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	return 0;
}

BOOL CCamInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	m_TSliCtrl.SetRange(0,100);
	m_VelSliCtrl.SetRange(-100,100);
	m_FOVSliCtrl.SetRange(1,180);
	m_TSliCtrl.SetFocus();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CCamInfoDlg::OnAnimate() 
{
	CamInfo->Animate=IsDlgButtonChecked(IDC_ANIMATE)?true:false;
	UpdateControlStates();
}

void CCamInfoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CString tstr;
	
	if(pScrollBar==(CScrollBar* )&m_TSliCtrl || pScrollBar==(CScrollBar* )&m_VelSliCtrl)
		CamInfo->SetMode(CameraInfo::CM_TRACK);
	
	if(pScrollBar==(CScrollBar* )&m_TSliCtrl)
	{
		CamInfo->Animate=false;
		CheckDlgButton(IDC_ANIMATE,CamInfo->Animate);
		tstr.Format("%i%%",m_TSliCtrl.GetPos());
		m_PosText.SetWindowText(tstr);
		CamInfo->SetTime(m_TSliCtrl.GetPos()/100.0f);
	}
	
	if(pScrollBar==(CScrollBar* )&m_VelSliCtrl)
	{
		tstr.Format("%i%%",m_VelSliCtrl.GetPos());
		m_VelText.SetWindowText(tstr);
		CamInfo->SetVel(m_VelSliCtrl.GetPos()/100.0f);
		CheckDlgButton(IDC_ANIMATE,TRUE);
		CamInfo->Animate=true;
	}
	
	if(pScrollBar==(CScrollBar* )&m_FOVSliCtrl)
	{
		tstr.Format("%i",m_FOVSliCtrl.GetPos());
		m_FOVTextCtrl.SetWindowText(tstr);
		CamInfo->FOV=m_FOVSliCtrl.GetPos()*Radians_Per_Degree;
	}
	CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CCamInfoDlg::OnRButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnRButtonUp(nFlags, point);
}

void CCamInfoDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnRButtonDown(nFlags, point);
}



void CCamInfoDlg::OnResetcurve() 
{
	if(!CamInfo || !CamInfo->ViewLoop) return;
	CamInfo->ViewLoop->ResetCurve(); 
	
}


void CCamInfoDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	Validate();
	CDialog::OnTimer(nIDEvent);
}

void CCamInfoDlg::Validate()
{
	KillTimer(VALIDATE_TIMER);
	if(!CamInfo) return;
	
	UpdateData(TRUE);
	if(m_FarClip<m_NearClip)  m_FarClip=m_NearClip;
	
	
	CamInfo->NearClip=(float)m_NearClip;
	CamInfo->FarClip=(float)m_FarClip;
	CamInfo->Loc.x=(float)m_CamLocX;
	CamInfo->Loc.y=(float)m_CamLocY;
	CamInfo->Loc.z=(float)m_CamLocZ;
	CamInfo->TargetLoc.x=(float)m_TarLocX;
	CamInfo->TargetLoc.y=(float)m_TarLocY;
	CamInfo->TargetLoc.z=(float)m_TarLocZ;
	CamInfo->FOV=(float)m_FOVText*Radians_Per_Degree;
	CamInfo->CamTrackAlt=(float)m_CamTrkAlt;
	CamInfo->TarTrackAlt=(float)m_TarTAlt;
	
	NoUpdate=false;
}


void CCamInfoDlg::DelayedValidate(int tme)
{
	NoUpdate=true;
	KillTimer(VALIDATE_TIMER);
	SetTimer(VALIDATE_TIMER,tme,NULL);
}

void CCamInfoDlg::OnChangeTartalt()  { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeFovtext()  { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeCamtalt()  { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeFarclip()  { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeNearclip()  { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeTarlocx() { DelayedValidate(VALIDATE_TIME); }
void CCamInfoDlg::OnChangeTarlocz() { DelayedValidate(VALIDATE_TIME);}

void CCamInfoDlg::OnChangeTarlocy() 
{ 
	DelayedValidate(VALIDATE_TIME); 
	CheckDlgButton(IDC_TLOCTT,false);
	CamInfo->TarATrkFlg=false;
}


void CCamInfoDlg::OnChangeCamlocx() 
{
	if(!CamInfo) return;
	CamInfo->SetMode(CameraInfo::CM_FREE);
	DelayedValidate(VALIDATE_TIME);
}

void CCamInfoDlg::OnChangeCamlocy() 
{
	if(!CamInfo) return;
	DelayedValidate(VALIDATE_TIME);
	CheckDlgButton(IDC_CLOCTT,false);
	CamInfo->CamATrkFlg=false;
}


void CCamInfoDlg::OnChangeCamlocz() 
{
	if(!CamInfo) return;
	CamInfo->SetMode(CameraInfo::CM_FREE);
	DelayedValidate(VALIDATE_TIME);
}
