// PageAnimType.cpp : implementation file
//

#include "stdafx.h"
#include "animscript.h"
#include "PageAnimType.h"

#include <assert.h>

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

/////////////////////////////////////////////////////////////////////////////
// CPageAnimType property page

IMPLEMENT_DYNCREATE(CPageAnimType, CPropertyPage)

CPageAnimType::CPageAnimType() : CPropertyPage(CPageAnimType::IDD)
{
	//{{AFX_DATA_INIT(CPageAnimType)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pDocument = NULL;
}

CPageAnimType::~CPageAnimType()
{
}

void CPageAnimType::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageAnimType)
	DDX_Control(pDX, IDC_MODIFY, m_ctrlModify);
	DDX_Control(pDX, IDC_LIST_PARAM, m_ctrlListParam);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageAnimType, CPropertyPage)
	//{{AFX_MSG_MAP(CPageAnimType)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_PARAM, OnItemchangedListParam)
	ON_BN_CLICKED(IDC_MODIFY, OnModify)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageAnimType message handlers

void CPageAnimType::OnCreateExec()
{
	assert(m_pDocument);
	CAnimNode *pNode = m_pDocument->m_pSelectedNode;
	if (NULL == pNode)
		return;

	switch (m_iPageType)
	{
	case 0:
		// Animation State
		{
			if (pNode->m_bValid)
			{
				// Create Animation State Specific data
			}
			break;
		}
	case 1:
		// Transition State
		{
			if (pNode->m_bValid)
			{
			}
			break;
		}
	case 2:
		// Anim Type Param
		{
			if (pNode->m_bValid)
			{
			}
			break;
		}
	default:
		assert(false);
	}
}

// Called to update the window
BOOL CPageAnimType::OnApply() 
{
	if (::IsWindow(m_ctrlListParam.GetSafeHwnd()))
	{
		m_ctrlListParam.DeleteAllItems();
		if (m_pDocument)
		{
			CAnimNode *pNode = m_pDocument->m_pSelectedNode;
			if (pNode)
			{
				if (pNode->m_bValid)
				{
					POSITION pos = pNode->m_mapParams.GetStartPosition();
					int x=0;
					while (pos)
					{
						CObject *pObj;
						WORD iKey;
						pNode->m_mapParams.GetNextAssoc(pos,iKey,pObj);
						CParamNode *pParam = (CParamNode *)pObj;

						if (ShowParam(pParam))
						{
							int index = m_ctrlListParam.InsertItem(x,pParam->GetKeyString());
							m_ctrlListParam.SetItemText(index,1,pParam->GetValueString());
							m_ctrlListParam.SetItemData(index,(DWORD)pParam);
							x++;
						}
					}
				}
			}
		}
		m_ctrlListParam.EnableWindow(ShouldEnable());
	}

	if (::IsWindow(m_ctrlModify.GetSafeHwnd()))
	{
		bool bEnable = false;
		if (::IsWindow(m_ctrlListParam.GetSafeHwnd()))
		{
			if (m_ctrlListParam.GetSelectedCount())
				bEnable = true;
		}
		m_ctrlModify.EnableWindow(bEnable);
	}
	return CPropertyPage::OnApply();
}

BOOL CPageAnimType::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	m_ctrlListParam.InsertColumn(0,"Parameter",LVCFMT_LEFT,100);
	m_ctrlListParam.InsertColumn(1,"Value",LVCFMT_LEFT,300);
	OnApply();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

bool CPageAnimType::ShouldEnable()
{
	bool bEnable = false;
	if (m_pDocument)
	{
		CAnimNode *pNode = m_pDocument->m_pSelectedNode;
		if (pNode)
		{
			if (pNode->m_bValid)
			{
				bEnable = true;
				if (1 == m_iPageType)	// Is this a Transition Page
				{
					if (!pNode->IsTransition())
						bEnable = false;
				}

			}
		}
	}
	return bEnable;
}

bool CPageAnimType::ShowParam(CParamNode *pNode)
{
	switch (m_iPageType)
	{
	case 0:
		// Animation State
		{
			switch (pNode->m_iParamType)
			{
			case PARAM_START:
				return true;
			default:
				return false;
			}
			break;
		}
	case 1:
		// Transition State
		{
			switch (pNode->m_iParamType)
			{
			case PARAM_STARTOVERLAPCURVE:
			case PARAM_ENDOVERLAPCURVE:
			case PARAM_TRANSITIONSTART:
			case PARAM_PLAYOLDSTATETILL:
			case PARAM_OVERRIDENEWSTATEPOSITION:
			case PARAM_STARTNEWSTATE:
				return true;
			default:
				return false;
			}
			break;
		}
	case 2:
		// Anim Type Param
		{
			switch (pNode->m_iParamType)
			{
			case PARAM_POSITION:
			case PARAM_ANIM:
			case PARAM_SPEEDATTRIB:
			case PARAM_PITCHATTRIB:
			case PARAM_ROLLATTRIB:
			case PARAM_FASTANIM:
			case PARAM_SLOWANIM:
			case PARAM_SLOWSPEEDMULTIPLIER:
			case PARAM_LERPTIME:
			case PARAM_SLOWUPANIM:
			case PARAM_SLOWEVENANIM:
			case PARAM_SLOWDOWNANIM:
			case PARAM_FASTUPANIM:
			case PARAM_FASTEVENANIM:
			case PARAM_FASTDOWNANIM:
			case PARAM_UPANIM:
			case PARAM_EVENANIM:
			case PARAM_DOWNANIM:
			case PARAM_LEFTANIM:
			case PARAM_RIGHTANIM:
			case PARAM_SLOWLEFTANIM:
			case PARAM_SLOWRIGHTANIM:
			case PARAM_FASTLEFTANIM:
			case PARAM_FASTRIGHTANIM:

				return true;
			default:
				return false;
			}
		}
	default:
		assert(false);
	}
	// Item that fell through!
	assert(false);
	return true;
}

void CPageAnimType::OnItemchangedListParam(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	if (pNMListView->uNewState & LVIS_SELECTED)
	{
		m_ctrlModify.EnableWindow(true);
	}
	else
	{
		m_ctrlModify.EnableWindow(false);
	}
	*pResult = 0;
}

void CPageAnimType::OnModify() 
{
	POSITION pos = m_ctrlListParam.GetFirstSelectedItemPosition();
	while (pos)
	{
		int index = m_ctrlListParam.GetNextSelectedItem(pos);
		CParamNode *pNode = (CParamNode *)m_ctrlListParam.GetItemData(index);
		if (pNode)
		{
			if (pNode->OnModify())
				m_pDocument->UpdateAllViews(NULL,0,NULL);
		}
	}
}
