// CLS_DlgDrawPolygon.cpp : implementation file
//

#include "stdafx.h"
#include "PlayDemo_VC.h"
#include "PlayDemo_VCDlg.h"
#include "CLS_DlgDrawPolygon.h"



// CLS_DlgDrawPolygon dialog

IMPLEMENT_DYNAMIC(CLS_DlgDrawPolygon, CDialog)

CLS_DlgDrawPolygon::CLS_DlgDrawPolygon(CWnd* pParent /*=NULL*/)
	: CDialog(CLS_DlgDrawPolygon::IDD, pParent),m_pDlgVedio(NULL)
{
	m_iPointCount = 0;		//初始化点的个数为0
}

CLS_DlgDrawPolygon::~CLS_DlgDrawPolygon()
{

}

void CLS_DlgDrawPolygon::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_GP_PANL, m_gpPanl);
	DDX_Control(pDX, IDC_CHK_ARROW, m_chkArrow);
}


BEGIN_MESSAGE_MAP(CLS_DlgDrawPolygon, CDialog)
	ON_BN_CLICKED(IDC_BTN_CLEAR, &CLS_DlgDrawPolygon::OnBnClickedBtnClear)
	ON_WM_CLOSE()
	ON_MESSAGE(WM_ITEMLBUTTON_DOWN, &OnPanelClick)
END_MESSAGE_MAP()

BOOL CLS_DlgDrawPolygon::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog

	ShowWindow(SW_SHOWNORMAL);
	// TODO: Add extra initialization here

	//创建显示视频的PNL并显示
	m_pDlgVedio = new CLS_DlgVedio();
	m_pDlgVedio->Create(IDD_DLG_VIDEO, GetDlgItem(IDC_GP_PANL));
	m_pDlgVedio->m_iWhatDlg = DRAWPOLYGONWIN;

	RECT rect = {0};	
	m_gpPanl.GetClientRect(&rect);		//得到显示的区域
	m_pDlgVedio->MoveWindow(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
	m_pDlgVedio->ShowWindow(SW_SHOW);

	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	pDlg->m_PlaySDK.ResetPlayWnd(pDlg->m_iCurPlayNum, m_pDlgVedio->GetSafeHwnd());
	return TRUE;  // return TRUE  unless you set the focus to a control
}
// CLS_DlgDrawPolygon message handlers

void CLS_DlgDrawPolygon::AbsPointsToVideo(POINT * _ptArray, int _iPointCount)
{
	if (_ptArray == NULL || _iPointCount <= 0)
		return;

	RECT rect = {0};	
	m_gpPanl.GetClientRect(&rect);
	int iPanelWidth = rect.right - rect.left;
	int iPanelHeight = rect.bottom - rect.top;
	int iVdWidth = 0, iVdHeight = 0;

	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	pDlg->m_PlaySDK.GetVideoParam(pDlg->m_iCurPlayNum, &iVdWidth, &iVdHeight, NULL);

	double dWidthRate = (double)iVdWidth/iPanelWidth;
	double dHeightRate = (double)iVdHeight/iPanelHeight;

	for (int i = 0; i < _iPointCount; ++i)
	{
		_ptArray[i].x = (int)(_ptArray[i].x*dWidthRate);
		_ptArray[i].y = (int)(_ptArray[i].y*dHeightRate);
	}
}
int CLS_DlgDrawPolygon::GetPointIndex(int _iX, int _iY, int _iMinDist)
{
	if (_iMinDist < 0)
		_iMinDist = 20;

	int iPointIndex = -1;
	int iMinDist = 0x7FFFFFFF;
	for (int i = 0; i < m_iPointCount; ++i)
	{
		int iDist = abs(_iX-m_ptSet[i].x) + abs(_iY-m_ptSet[i].y);
		if (iDist < _iMinDist && iMinDist > iDist)
		{
			iMinDist = iDist;
			iPointIndex = i;
		}
	}

	return iPointIndex;
}

LRESULT CLS_DlgDrawPolygon::OnPanelClick(WPARAM wParam, LPARAM lParam)
{	
	RECT rect = {0};	
	m_gpPanl.GetClientRect(&rect);
	m_ptCur.x = max(min(rect.right - rect.left, (int)wParam), 0);
	m_ptCur.y = max(min(rect.bottom - rect.top, (int)lParam), 0);

	AbsPointsToVideo(&m_ptCur, 1);
	if (GetPointIndex(m_ptCur.x, m_ptCur.y, 14) != -1)
		return -1;
	if (m_iPointCount >= VCA_MAX_POLYGON_POINT_NUM)
		return -1;
	if (m_iPointCount > 1)
		m_iPointCount--;
	m_ptSet[m_iPointCount++] = m_ptCur;
	m_ptSet[m_iPointCount++] = m_ptSet[0];

	CPlayDemo_VCDlg* Demo_VCDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	Demo_VCDlg->m_PlaySDK.DrawPolygonOnLocalVideo(Demo_VCDlg->m_iCurPlayNum, m_ptSet, m_iPointCount, m_chkArrow.GetCheck());

	return 0;
}


void CLS_DlgDrawPolygon::OnBnClickedBtnClear()
{
	// TODO: Add your control notification handler code here
	m_iPointCount = 0;
	POINT pt[2] = {0};

	memset(m_ptSet, 0, sizeof(m_ptSet));	
	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	pDlg->m_PlaySDK.DrawPolygonOnLocalVideo(pDlg->m_iCurPlayNum, pt, sizeof(pt)/sizeof(POINT), 0);
}

void CLS_DlgDrawPolygon::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	//析构
	OnBnClickedBtnClear();
	if (m_pDlgVedio != NULL)
	{
		delete m_pDlgVedio;		
		m_pDlgVedio = NULL;
	}
	CDialog::OnClose();
}
