// MotionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DVP2420Sample.h"
#include "MotionDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMotionDlg dialog


CMotionDlg::CMotionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMotionDlg::IDD, pParent)
	, m_nLeft(0)
	, m_nRight(0)
	, m_nTop(0)
	, m_nBottom(0)
	, m_nRegionCount(0)
	, m_nThreshold(0)
{
	//{{AFX_DATA_INIT(CMotionDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	for (int i=0; i<9; i++) {
		m_Region[i].enable = 0;
		m_Region[i].left = 0;
		m_Region[i].right = 0;
		m_Region[i].top = 0;
		m_Region[i].bottom = 0;
		m_nThresholdArray[i] = 0;
	}
}


void CMotionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMotionDlg)
	DDX_Text(pDX, IDC_LEFT_EDIT, m_nLeft);
	DDX_Text(pDX, IDC_RIGHT_EDIT, m_nRight);
	DDX_Text(pDX, IDC_TOP_EDIT, m_nTop);
	DDX_Text(pDX, IDC_BOTTOM_EDIT, m_nBottom);
	DDX_Text(pDX, IDC_THRESHOLD_EDIT, m_nThreshold);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMotionDlg, CDialog)
	//{{AFX_MSG_MAP(CMotionDlg)
	ON_BN_CLICKED(IDC_ADD_BTN, OnAddBtn)
	ON_BN_CLICKED(IDC_DELETE_BTN, OnDeleteBtn)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMotionDlg message handlers

BOOL CMotionDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString str;
	for (int i=0; i<9; i++)
	{
		if (m_Region[i].enable)
		{
			str.Format(_T("Region%d: L=%02d      R=%02d      T=%02d      B=%02d      TH=%03d"), i+1, m_Region[i].left, m_Region[i].right, m_Region[i].top, m_Region[i].bottom, m_nThresholdArray[i]);
			((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->AddString(str);
		}
	}

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CMotionDlg::OnAddBtn() 
{
	// TODO: Add your control notification handler code here
	int nCount = ((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->GetCount();
	if (nCount >= 9)
		return;

	UpdateData();
	m_Region[nCount].enable =	1;
	m_Region[nCount].left =		m_nLeft;
	m_Region[nCount].right =	m_nRight;
	m_Region[nCount].top =		m_nTop;
	m_Region[nCount].bottom =	m_nBottom;
	m_nThresholdArray[nCount] = m_nThreshold;

	CString str;
	str.Format(_T("Region%d: L=%02d      R=%02d      T=%02d      B=%02d      TH=%03d"), nCount+1, m_nLeft, m_nRight, m_nTop, m_nBottom, m_nThreshold);
	((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->AddString(str);
}

void CMotionDlg::OnDeleteBtn() 
{
	// TODO: Add your control notification handler code here
	int nIndex = ((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->GetCurSel();
	if (nIndex == -1)
		return;

	int i, j = 0;
	int nCnt = 0;
	for (i=0; i<9; i++) {
		if (m_Region[i].enable)
			j++;
		if (j == nIndex+1) {
			nCnt = i;
			break;
		}
	}

	for (i=nCnt; i<8; i++) {
		m_Region[i].enable =	m_Region[i+1].enable;
		m_Region[i].left =		m_Region[i+1].left;
		m_Region[i].right =		m_Region[i+1].right;
		m_Region[i].top =		m_Region[i+1].top;
		m_Region[i].bottom =	m_Region[i+1].bottom;
		m_nThresholdArray[i] =	m_nThresholdArray[i+1];
	}
	m_Region[8].enable = 0;
	m_Region[8].left = 0;
	m_Region[8].right = 0;
	m_Region[8].top = 0;
	m_Region[8].bottom = 0;
	m_nThresholdArray[8] = 0;

//	((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->DeleteString(nIndex);
	((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->ResetContent();
	CString str;
	for (i=0; i<9; i++)
	{
		if (m_Region[i].enable)
		{
			str.Format(_T("Region%d: L=%02d      R=%02d      T=%02d      B=%02d      TH=%03d"), i+1, m_Region[i].left, m_Region[i].right, m_Region[i].top, m_Region[i].bottom, m_nThresholdArray[i]);
			((CListBox*)GetDlgItem(IDC_MDREGION_LIST))->AddString(str);
		}
	}
}
