// ModifyCurveParam.cpp : implementation file
//

#include "stdafx.h"
#include "animscript.h"
#include "ModifyCurveParam.h"

#include "mw4headers.hpp"
#include "Vehicle.hpp"
#include "MechAnimationState.hpp"

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

/////////////////////////////////////////////////////////////////////////////
// CModifyCurveParam dialog


CModifyCurveParam::CModifyCurveParam(CWnd* pParent /*=NULL*/)
	: CDialog(CModifyCurveParam::IDD, pParent)
{
	//{{AFX_DATA_INIT(CModifyCurveParam)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CModifyCurveParam::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CModifyCurveParam)
	DDX_Control(pDX, IDC_EDIT_START, m_ctrlEditStart);
	DDX_Control(pDX, IDC_EDIT_MIN, m_ctrlEditMin);
	DDX_Control(pDX, IDC_EDIT_MAX, m_ctrlEditMax);
	DDX_Control(pDX, IDC_EDIT_END, m_ctrlEditEnd);
	DDX_Control(pDX, IDC_CHECK_INVERTTIME, m_ctrlCheckTime);
	DDX_Control(pDX, IDC_CHECK_INVERTSCALE, m_ctrlCheckScale);
	DDX_Control(pDX, IDC_COMBO_CURVETYPE, m_ctrlCurveType);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CModifyCurveParam, CDialog)
	//{{AFX_MSG_MAP(CModifyCurveParam)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CModifyCurveParam message handlers

extern char g_szCurveTypeNames[][40];

BOOL CModifyCurveParam::OnInitDialog() 
{
	CDialog::OnInitDialog();
	assert(this->m_pCurveNode);
	iCurveType = m_pCurveNode->curveType;
	min = m_pCurveNode->curveMin;
	max = m_pCurveNode->curveMax;
	start = m_pCurveNode->timeStart;
	end = m_pCurveNode->timeEnd;
	inverttime = m_pCurveNode->invertTime;
	invertscale = m_pCurveNode->invertScale;

	for (int x=0;x<=MechWarrior4::AnimCurve::CurveTypeCount;x++)
	{
		int index = m_ctrlCurveType.AddString(g_szCurveTypeNames[x]);
		m_ctrlCurveType.SetItemData(index,x);
	}

	for (x=0;x<m_ctrlCurveType.GetCount();x++)
	{
		if ( (iCurveType+1) == m_ctrlCurveType.GetItemData(x))
			break;
	}
	// So iCurveType is really the index of the current item
	iCurveType = x;

	CString str;
	m_ctrlCurveType.SetCurSel(iCurveType);
	str.Format("%f",start);
	m_ctrlEditStart.SetWindowText(str);
	str.Format("%f",end);
	m_ctrlEditEnd.SetWindowText(str);
	str.Format("%f",min);
	m_ctrlEditMin.SetWindowText(str);
	str.Format("%f",max);
	m_ctrlEditMax.SetWindowText(str);

	m_ctrlCheckTime.SetCheck(inverttime);
	m_ctrlCheckScale.SetCheck(invertscale);

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

void CModifyCurveParam::OnOK() 
{
	// verify that a curve type is selected
	int index = m_ctrlCurveType.GetCurSel();
	if (-1 == index)
	{
		MessageBox("You Must Select a Curve Type",NULL,MB_OK);
		m_ctrlCurveType.SetCurSel(iCurveType);
		return;
	}
	iCurveType = m_ctrlCurveType.GetItemData(index);

	// Verify that all of the text data is correct
	CString str;

	m_ctrlEditStart.GetWindowText(str);
	start = atof(str);

	m_ctrlEditEnd.GetWindowText(str);
	end = atof(str);

	m_ctrlEditMin.GetWindowText(str);
	min = atof(str);

	m_ctrlEditMax.GetWindowText(str);
	max = atof(str);

	inverttime = m_ctrlCheckTime.GetCheck();
	invertscale = m_ctrlCheckScale.GetCheck();

	Clamp(min, 0.0f, 1.0f);
	Clamp(max, 0.0f, 1.0f);
	Clamp(start, 0.0f, 1.0f);
	Clamp(end, 0.0f, 1.0f);
	// Now copy the data back in the node
	m_pCurveNode->m_iType = m_pCurveNode->curveType = iCurveType-1;
	m_pCurveNode->curveMin = min;
	m_pCurveNode->curveMax = max;
	m_pCurveNode->timeStart = start;
	m_pCurveNode->timeEnd = end;
	m_pCurveNode->invertTime = inverttime;
	m_pCurveNode->invertScale  = invertscale;
	CDialog::OnOK();
}
