// GraphView1.cpp : implementation file
//

#include "stdafx.h"
#include "animscript.h"
#include "GraphView1.h"

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView1

IMPLEMENT_DYNCREATE(CGraphView1, CView)

CGraphView1::CGraphView1()
{
}

CGraphView1::~CGraphView1()
{
}


BEGIN_MESSAGE_MAP(CGraphView1, CView)
	//{{AFX_MSG_MAP(CGraphView1)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphView1 drawing

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

	CSize sz;
	CRect rc;
	GetClientRect(&rc);
	sz.cx = rc.right;
	sz.cy = rc.bottom;
	//pDC->BitBlt(0,0,sz.cx,sz.cy,NULL,sz.cx,sz.cy,BLACKNESS);
	

	bool hascurves = false;

	if (pNode)
	{
		CCurveTypeNode *pStartOverlapCurve = NULL;
		CCurveTypeNode *pEndOverlapCurve = NULL;
		float 
			start = 0.0f,
			transitionstart = 0.0f,
			playoldstatetill = 0.0f,
			startnewstate = 0.0f,
			overridenewstateposition = 0.0f;

		CPathNode *pPath = NULL,*pNewPath = NULL;
		CTransStateNode *pTrans = NULL;
		if (pNode->IsTransition())
			pTrans = (CTransStateNode *)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_START:
				{
					start = ((CFloatNode *)pParam)->m_fValue;
					break;
				}
			case PARAM_TRANSITIONSTART:
				{
					transitionstart = ((CFloatNode *)pParam)->m_fValue;
					break;
				}
			case PARAM_PLAYOLDSTATETILL:
				{
					playoldstatetill = ((CFloatNode *)pParam)->m_fValue;
					break;
				}
			case PARAM_STARTNEWSTATE:
				{
					startnewstate = ((CFloatNode *)pParam)->m_fValue;
					break;
				}
			case PARAM_OVERRIDENEWSTATEPOSITION:
				{
					overridenewstateposition = ((CFloatNode *)pParam)->m_fValue;
					break;
				}
			case PARAM_STARTOVERLAPCURVE:
				{
					pStartOverlapCurve = (CCurveTypeNode *)pParam;
					break;
				}
			case PARAM_ENDOVERLAPCURVE:
				{
					pEndOverlapCurve = (CCurveTypeNode *)pParam;
					CObject *pNObj;
					for (int x=PARAM_SLOWUPANIM;x<PARAM_COUNT;x++)
					{
						if (pTrans->m_pEndNode->m_mapParams.Lookup(x,pNObj))
						{
							pNewPath = (CPathNode *)pNObj;
							break;
						}
					}
					hascurves = true;
					break;
				}
			case PARAM_ANIM:
			case PARAM_FASTANIM:
			case PARAM_SLOWANIM:
			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:
				{
					pPath = (CPathNode *)pParam;

					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));
			}
		}
		else if (pNewPath)
		{
			float totaltime = (transitionstart+playoldstatetill)-start;
			totaltime += startnewstate - playoldstatetill;
			totaltime += pNewPath->m_pAnimData->animHeaderBlock->endTime - overridenewstateposition;

			{
				// Draw a tick every 0.5 seconds
				float yMult = ((float)sz.cy)/2.0f;
				float y = 1.4f;
				for (float x=0.0f;x<totaltime;x+=0.5f)
				{
					float xpos = ((float)sz.cx)*(x/totaltime);
					pDC->SetPixel(xpos,sz.cy-y*yMult,RGB(255,255,255));
				}
			}
			{
				// Draw the Old State
				float curvestart = 0.0f;
				float curveend = transitionstart+playoldstatetill;
				float range = curveend-curvestart;
				float xMult = (((float)sz.cx)*(range/totaltime))/100.0f;
				float yMult = ((float)sz.cy)/2.0f;
				if (range>0.00001)
				{
					int xstart = sz.cx*(curvestart/totaltime);

					for (int x=0;x<100;x++)
					{
						double y = 1.1f;
						pDC->SetPixel(xstart+((float)x)*xMult,sz.cy-y*yMult,RGB(0,255,0));
					}
				}

			}
			{
				// Draw the Start overlap curve
				float curvestart = start+pStartOverlapCurve->timeStart;
				float curveend = (transitionstart+pStartOverlapCurve->timeEnd);
				float range = curveend-curvestart;
				float xMult = (((float)sz.cx)*(range/totaltime))/100.0f;
				float yMult = ((float)sz.cy)/2.0f;
				if (range>0.00001)
				{
					int xstart = sz.cx*(curvestart/totaltime);

					for (int x=pStartOverlapCurve->timeStart*100;x<(100*pStartOverlapCurve->timeEnd);x++)
					{
						double y = pStartOverlapCurve->Play(((double)x)/(double)100.0);
						if ((y>0.0)&&(y<sz.cy))
							pDC->SetPixel(xstart+((float)x)*xMult,sz.cy-y*yMult,RGB(255,255,0));
					}
				}
			}

			if (pPath && pPath->m_pAnimData && pPath->m_pAnimData->animHeaderBlock)
			{
				// Draw the Transition State
				float curvestart = transitionstart+pPath->m_pAnimData->animHeaderBlock->startTime;
				float curveend = transitionstart+pPath->m_pAnimData->animHeaderBlock->endTime;
				float range = curveend-curvestart;
				float xMult = (((float)sz.cx)*(range/totaltime))/100.0f;
				float yMult = ((float)sz.cy)/2.0f;
				if (range>0.00001)
				{
					int xstart = sz.cx*(curvestart/totaltime);

					for (int x=0;x<100;x++)
					{
						double y = 1.2f;
						pDC->SetPixel(xstart+((float)x)*xMult,sz.cy-y*yMult,RGB(255,0,0));
					}
				}

			}
			if (pEndOverlapCurve && pEndOverlapCurve->curveType>=0)
			{
				// Draw the End overlap curve
				float curvestart = transitionstart+startnewstate+pEndOverlapCurve->timeStart;
				float curveend = transitionstart+startnewstate+(pEndOverlapCurve->timeEnd-overridenewstateposition);
				float range = curveend-curvestart;
				float xMult = (((float)sz.cx)*(range/totaltime))/100.0f;
				float yMult = ((float)sz.cy)/2.0f;
				if (range>0.00001)
				{
					int xstart = sz.cx*(curvestart/totaltime);

					for (int x=pEndOverlapCurve->timeStart*100;x<(100*pEndOverlapCurve->timeEnd);x++)
					{
						double y = pEndOverlapCurve->Play(((double)x)/(double)100.0);
						if ((y>0.0)&&(y<sz.cy))
							pDC->SetPixel(xstart+((float)x)*xMult,sz.cy-y*yMult,RGB(255,255,0));
					}
				}
			}
			if (pNewPath && pNewPath->m_pAnimData && pNewPath->m_pAnimData->animHeaderBlock)
			{
				// Draw the new state
				float curvestart = transitionstart+startnewstate;
				float curveend = curvestart+(pNewPath->m_pAnimData->animHeaderBlock->endTime - overridenewstateposition);
				float range = curveend-curvestart;
				float xMult = (((float)sz.cx)*(range/totaltime))/100.0f;
				float yMult = ((float)sz.cy)/2.0f;
				if (range>0.00001)
				{
					int xstart = sz.cx*(curvestart/totaltime);

					for (int x=0;x<100;x++)
					{
						double y = 1.3f;
						pDC->SetPixel(xstart+((float)x)*xMult,sz.cy-y*yMult,RGB(0,0,255));
					}
				}
			}
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CGraphView1 diagnostics

#ifdef _DEBUG
void CGraphView1::AssertValid() const
{
	CView::AssertValid();
}

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

/////////////////////////////////////////////////////////////////////////////
// CGraphView1 message handlers

int CGraphView1::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{	
	return 0;
}

BOOL CGraphView1::PreCreateWindow(CREATESTRUCT& cs) 
{
	CCreateContext *pContext = (CCreateContext *)cs.lpCreateParams;
	return CView::PreCreateWindow(cs);
}

void CGraphView1::OnDestroy() 
{
	CView::OnDestroy();
}

void CGraphView1::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{	
	RedrawWindow();	

}

BOOL CGraphView1::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CSize sz;
	CRect rc;
	GetClientRect(&rc);
	sz.cx = rc.right;
	sz.cy = rc.bottom;
	pDC->BitBlt(0,0,sz.cx,sz.cy,NULL,sz.cx,sz.cy,BLACKNESS);
	return 0;
	//CView::OnEraseBkgnd(pDC);
}
