// DVPanel.cpp : implementation file
//

#include "stdafx.h"
#include "pixelwhippro.h"
#include "DVPanel.h"
#include "EffectEditWin.h"
#include "CurveEditorFrame.h"



/////////////////////////////////////////////////////////////////////////////
// DVPanel dialog

extern CPixelWhipProApp theApp;

DVPanel::DVPanel(CWnd* pParent /*=NULL*/)
: CDynaPanel(DVPanel::IDD, pParent)
{
	LastPos=-1;
	LastNum=0.0f;
	//{{AFX_DATA_INIT(DVPanel)
	//}}AFX_DATA_INIT
RecurseGaurd=false;
}


void DVPanel::DoDataExchange(CDataExchange* pDX)
{
	CDynaPanel::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(DVPanel)
	DDX_Control(pDX, IDC_USEVAR, m_varbut);
	DDX_Control(pDX, IDC_NUM, m_numedit);
	DDX_Control(pDX, IDC_SLIDER, m_slider);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(DVPanel, CDynaPanel)
//{{AFX_MSG_MAP(DVPanel)
ON_WM_HSCROLL()
ON_WM_LBUTTONDOWN()
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER, OnReleasedcaptureSlider)
ON_EN_CHANGE(IDC_NUM, OnChangeNum)
ON_EN_KILLFOCUS(IDC_NUM, OnKillfocusNum)
ON_BN_CLICKED(IDC_USEVAR, OnUsevar)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// DVPanel message handlers

void DVPanel::UpdateControl()
{
	
	if(MyCSet && MyCSet->Curve)
	{
		
		UpdateCurve();
		
		gosFX::Curve *Curve=MyCSet->Curve->GetSubCurve(theApp.ParamWin->ViewVar?1:0);
		
		if(Curve)
		{
			
			Check_Object(Curve);
			
			float Max,Min;
			float SliVal;
			BaseCap=SliVal=Curve->ExpensiveCompute(0.0f,theApp.ParamWin->ViewVar);
			Curve->ExpensiveComputeRange(&Min,&Max,theApp.ParamWin->ViewVar);
			
			
			
			SliUnit=Max/100;
			if(SliUnit<.00001f) SliUnit=.01f;
			CString str;
			str.Format("%5.3f",SliVal);
			m_numedit.SetWindowText(str);
			m_numedit.RedrawWindow();
			m_slider.SetRange(0,200);
			m_slider.SetPos(100);
			
			if(Curve->IsComplex())
				CheckDlgButton(IDC_USEVAR,Curve->GetSeedFlagIfComplex());
			m_slider.ShowWindow(SW_SHOW);
			m_numedit.ShowWindow(SW_SHOW);
			
			
			//			m_slider.EnableWindow(TRUE);
			//			m_numedit.EnableWindow(TRUE);
		}
		else
		{
			m_slider.ShowWindow(SW_HIDE);
			m_numedit.ShowWindow(SW_HIDE);
			//		m_slider.EnableWindow(FALSE);
			//		m_numedit.EnableWindow(FALSE);
			//		m_numedit.SetWindowText("");
		}
		
	}
	
}

void DVPanel::Set(void *pt)
{
	
}

void DVPanel::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	theApp.Modified=true;
	if(LastPos==-1)
	{
		LastPos=m_slider.GetPos();
	}
	else
	{
		
		if(MyCSet)
		{
			gosFX::Curve *Curve=MyCSet->Curve->GetSubCurve(theApp.ParamWin->ViewVar?1:0);
			
			if(Curve)
			{
				Check_Object(Curve);
				
				Stuff::DynamicMemoryStream stream;
				
				if(Curve)
					MyCSet->Curve->Save(&stream);
				
				
				Curve->TranslateTo(BaseCap+(float)(m_slider.GetPos()-LastPos)*(SliUnit),theApp.ParamWin->ViewVar);
				
				
				CString str;
				if(MyCSet->IsDataValid())
				{
					str.Format("%5.3f",Curve->ExpensiveCompute(0.0f,theApp.ParamWin->ViewVar));
					m_numedit.SetWindowText(str);
				}
				else
				{
					stream.Rewind();
					MyCSet->Curve->Load(&stream, gosFX::CurrentGFXVersion);
				}
	
				
				theApp.CurveWin->m_wndView.ReDraw();
				if(Curve->IsComplex())
					CheckDlgButton(IDC_USEVAR,Curve->GetSeedFlagIfComplex());
				UpdateData(FALSE);
				
				
				UpdateCurve();
			}
			CDynaPanel::OnHScroll(nSBCode, nPos, pScrollBar);
		}
	}
}

BOOL DVPanel::OnInitDialog() 
{
	CDynaPanel::OnInitDialog();
	m_slider.SetRange(0,200);
	m_slider.SetPos(100);
	if(MyCSet->Curve->IsComplex())
	{
		m_varbut.ShowWindow(SW_SHOW);
		CheckDlgButton(IDC_USEVAR,MyCSet->Curve->GetSeedFlagIfComplex());
	}
	else
	{
		m_varbut.ShowWindow(SW_HIDE);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void DVPanel::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CDynaPanel::OnLButtonDown(nFlags, point);
}

void DVPanel::OnReleasedcaptureSlider(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LastPos=-1;
	m_slider.SetPos(100);
	//	UpdateControl();
	if(MyCSet->Curve)
	{
		Check_Object(MyCSet->Curve);
		BaseCap=MyCSet->Curve->ExpensiveCompute(0.0f,theApp.ParamWin->ViewVar);
	}
	*pResult = 0;
}

void DVPanel::OnChangeNum() 
{
	if(RecurseGaurd==true) return;

	theApp.Modified=true;

	if(LastPos==-1)
	{
		CString str;
		m_numedit.GetWindowText(str);
		m_slider.SetPos(100);
		
		gosFX::Curve *Curve=MyCSet->Curve->GetSubCurve(theApp.ParamWin->ViewVar?1:0);
		
		if(Curve)
		{
			Stuff::DynamicMemoryStream stream;
			if(MyCSet->Curve)
				MyCSet->Curve->Save(&stream);
			
			Check_Object(Curve);
			Curve->TranslateTo((float)atof(str),theApp.ParamWin->ViewVar);
			
			stream.Rewind();
			if(!MyCSet->IsDataValid())
			{
				str.Format("%5.3f",LastNum);
			RecurseGaurd=true;
				m_numedit.SetWindowText(str);
			RecurseGaurd=false;
				MyCSet->Curve->Load(&stream, gosFX::CurrentGFXVersion);
			if(!MyCSet->IsDataValid())
				STOP(("Curve %s Cannot Be fixed",(LPCSTR)MyCSet->Name));
			}
			theApp.CurveWin->m_wndView.ReDraw();
			
		}
		
	}
	
	{
		CString str;
		m_numedit.GetWindowText(str);
		LastNum=(float)atof(str);
	}
}


void DVPanel::OnKillfocusNum() 
{
	UpdateControl();	
}

void DVPanel::OnUsevar() 
{
	UpdateData(TRUE);	

	bool newcheckstate=(IsDlgButtonChecked(IDC_USEVAR)==0)?false:true;
	MyCSet->Curve->SetSeedFlagIfComplex(newcheckstate);
if(!MyCSet->IsDataValid())
	{
	newcheckstate=!newcheckstate;
	CheckDlgButton(IDC_USEVAR,newcheckstate?1:0);
	MyCSet->Curve->SetSeedFlagIfComplex(newcheckstate);
	}
}
