// GraphView.cpp : implementation file
//

#include "stdafx.h"
#include "AnimScript.h"
#include "GraphView.h"

#include "AnimScriptDoc.h"
#include <math.h>

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView

IMPLEMENT_DYNCREATE(CGraphView, CScrollView)

CGraphView::CGraphView()
{
}

CGraphView::~CGraphView()
{
}


BEGIN_MESSAGE_MAP(CGraphView, CScrollView)
	//{{AFX_MSG_MAP(CGraphView)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphView drawing
CButton *pButton = NULL;
void CGraphView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CRect rc;
	GetClientRect(&rc);
	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = rc.right;
	sizeTotal.cy = rc.bottom;
	//sizeTotal = this->GetTotalSize();
	SetScrollSizes(MM_TEXT, sizeTotal);
}

void CGraphView::OnDraw(CDC* pDC)
{
	CAnimScriptDoc* pDoc = (CAnimScriptDoc *)GetDocument();
	CAnimNode *pNode = pDoc->m_pSelectedNode;

	CSize sz = GetTotalSize();
	CDC dcMemory;
	dcMemory.CreateCompatibleDC(pDC);
	pDC->BitBlt(0,0,sz.cx,sz.cy,&dcMemory,sz.cx,sz.cy,BLACKNESS);
	
	bool hascurves = false;

	if (pNode)
	{
		POSITION pos = pNode->m_mapParams.GetStartPosition();
		while (pos)
		{
			CObject *pObj;
			WORD iKey;
			pNode->m_mapParams.GetNextAssoc(pos,iKey,pObj);
			CParamNode *pParam = (CParamNode *)pObj;
			switch (iKey)
			{
			case PARAM_STARTOVERLAPCURVE:
			case PARAM_ENDOVERLAPCURVE:
				{
					// Draw the curve
					CCurveTypeNode *pCurve = (CCurveTypeNode *)pParam;
					float xMult = ((float)sz.cx)/100.0f;
					float yMult = ((float)sz.cy)/2.0f;
					if (pCurve->curveType>=0)
					{
						hascurves = true;
						for (int x=0;x<100;x++)
						{
							double y = pCurve->Play(((double)x)/(double)100.0);
							if ((y>0.0)&&(y<sz.cy))
								pDC->SetPixel(x*xMult,sz.cy-y*yMult,RGB(255,255,0));
						}
					}
					break;
				}
			default:
				{
				}
			}
		}
		if (!hascurves)
		{
			for (int x=0;x<sz.cx;x++)
			{
				int y = (int)(50.0+50.0*sin(x/10.0));
				if ((y>0)&&(y<sz.cy))
					pDC->SetPixel(x,y,RGB(0,255,0));
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CGraphView diagnostics

#ifdef _DEBUG
void CGraphView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CGraphView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CGraphView message handlers
