// CLS_DlgEZoom.cpp : implementation file
//

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

// CLS_DlgEZoom dialog

template<class T>
void SwapIfBig(T& _t1, T& _t2)
{
	if (_t1 > _t2)
	{
		T t = _t1;
		_t1 = _t2;
		_t2 = t;
	}
}


IMPLEMENT_DYNAMIC(CLS_DlgEZoom, CDialog)

CLS_DlgEZoom::CLS_DlgEZoom(CWnd* pParent /*=NULL*/)
	: CDialog(CLS_DlgEZoom::IDD, pParent)
	, m_iEdtPanelId(0)
	, m_iEdtZoomPnlId(0)
	, m_iEdtBottom(0)
	, m_iEdtLeft(0)
	, m_iEdtRight(0)
	, m_iEdtTop(0)
{
	m_bMouseDown = false;		//默认设置鼠标未按下
	RECT rect = {0};
	for (int i = 0; i < MAXPANELNUM; i++)		//初始化m_pDlgVedio
	{
		m_pDlgVedio[i] = NULL;
		m_rctShow[i] = rect;
		m_rctVideo[i] = rect;
	}
	memset(m_iEZoomID, -1, sizeof(m_iEZoomID));
}

CLS_DlgEZoom::~CLS_DlgEZoom()
{

}

void CLS_DlgEZoom::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_GP_PANELVIDEO, m_gpVideoPnl);
	DDX_Control(pDX, IDC_STC_PANEL1, m_gpZoomPnl1);
	DDX_Control(pDX, IDC_STC_PANEL2, m_gpZoomPnl2);
	DDX_Control(pDX, IDC_STC_PANEL3, m_gpZoomPnl3);
	DDX_Control(pDX, IDC_STC_PANEL4, m_gpZoomPnl4);
	DDX_Text(pDX, IDC_EDT_PANELID, m_iEdtPanelId);
	DDX_Text(pDX, IDC_EDT_ZOOMPNLID, m_iEdtZoomPnlId);
	DDX_Text(pDX, IDC_EDT_BOTTOM, m_iEdtBottom);
	DDX_Text(pDX, IDC_EDT_LEFT, m_iEdtLeft);
	DDX_Text(pDX, IDC_EDT_RIGHT, m_iEdtRight);
	DDX_Text(pDX, IDC_EDT_TOP, m_iEdtTop);
}


BEGIN_MESSAGE_MAP(CLS_DlgEZoom, CDialog)
		ON_MESSAGE(WM_ITEMLBUTTON_DOWN, &OnPanelClick)
		ON_MESSAGE(WM_ITEMLBUTTON_DBCLICK, &OnPanelDblClick)
		ON_MESSAGE(WM_ITEMLBUTTON_UP, &pnlVideoMouseUp)
		ON_MESSAGE(WM_ITEMLBUTTON_MOVE, &pnlVideoMouseMove)		
		ON_BN_CLICKED(IDC_BTN_SET, &CLS_DlgEZoom::OnBnClickedBtnSet)
		ON_WM_CLOSE()
END_MESSAGE_MAP()

BOOL CLS_DlgEZoom::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

	RECT rect = {0};	
	int ipDlgVedioID[MAXPANELNUM] = {IDC_GP_PANELVIDEO, IDC_STC_PANEL1, IDC_STC_PANEL2, IDC_STC_PANEL3, IDC_STC_PANEL4};
	//创建显示原视频的PNL并显示
	for (int i = 0; i < MAXPANELNUM; i++)
	{
		m_pDlgVedio[i] = new CLS_DlgVedio();
		m_pDlgVedio[i]->Create(IDD_DLG_VIDEO, GetDlgItem(ipDlgVedioID[i]));
		m_pDlgVedio[i]->m_iWhatDlg = EZOOMPNL;
		GetDlgItem(ipDlgVedioID[i])->GetClientRect(&rect);
		m_pDlgVedio[i]->MoveWindow(rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
		m_pDlgVedio[i]->ShowWindow(SW_SHOW);
		m_pDlgVedio[i]->m_iSelectID = i;
	}
	m_pDlgVedio[0]->m_iWhatDlg = EZOOMWIN;
	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	pDlg->m_PlaySDK.ResetPlayWnd(pDlg->m_iCurPlayNum, m_pDlgVedio[0]->GetSafeHwnd());

	m_iPlayerID = pDlg->m_iCurPlayNum;		//设置当前播放的ID
	m_iEdtPanelId = 1;						//默认使用PNL1显示

	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// CLS_DlgEZoom message handlers

LRESULT CLS_DlgEZoom::OnPanelClick(WPARAM wParam, LPARAM lParam)
{	
	int iID = (int)wParam;
	if (m_pDlgVedio[iID] == NULL)
		return -1;
	m_iEdtPanelId = iID ? iID : 1;	
	UpdateUI(m_iEdtPanelId);
	return 0;
}

LRESULT CLS_DlgEZoom::OnPanelDblClick(WPARAM wParam, LPARAM lParam)
{
	int iID = (int)wParam;
	if (m_pDlgVedio[iID] == NULL)
		return -1;

	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	pDlg->m_PlaySDK.RemoveEZoom(m_iPlayerID, m_iEZoomID[iID]);

	m_iEZoomID[iID] = -1;
	memset(&m_rctVideo[iID], 0, sizeof(RECT));
	memset(&m_rctShow[iID], 0, sizeof(RECT));
	UpdateUI(iID);
	return 0;
}

LRESULT CLS_DlgEZoom::pnlVideoMouseUp(WPARAM wParam, LPARAM lParam)
{		
	RECT rect = {0};	
	m_gpVideoPnl.GetClientRect(&rect);
	int iPanelWidth = rect.right - rect.left;
	int iPanelHeight = rect.bottom - rect.top;

	int iID = m_iEdtPanelId ;
	if (iID < 0 || iID >= MAXPANELNUM || !m_bMouseDown)
		return -1;

	m_bMouseDown = false;
	m_rctShow[iID].right = max(min(iPanelWidth, (int)wParam), 0);
	m_rctShow[iID].bottom = max(min(iPanelHeight, (int)lParam), 0);

	RECT rctZero = {0};
	CPlayDemo_VCDlg* Demo_VCDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	Demo_VCDlg->m_PlaySDK.DrawRect(m_iPlayerID, &rctZero, 1);

	EZoom(iID);
	UpdateUI(iID);
	return 0;
}

LRESULT CLS_DlgEZoom::pnlVideoMouseMove(WPARAM wParam, LPARAM lParam)
{		

	int iID = m_iEdtPanelId;
	if (iID < 0 || iID >= MAXPANELNUM || !m_bMouseDown)
		return -1;

	RECT rect = {0};	
	m_gpVideoPnl.GetClientRect(&rect);

	int iPanelWidth = rect.right - rect.left;
	int iPanelHeight = rect.bottom - rect.top;
	RECT& rct = m_rctShow[iID];
	rct.right = max(min(iPanelWidth, (int)wParam), 0);
	rct.bottom = max(min(iPanelHeight, (int)lParam), 0);

	m_rctVideo[iID] = rct;
	PanelRectToVideoRect(m_rctVideo[iID]);
	SwapIfBig(m_rctVideo[iID].left, m_rctVideo[iID].right);
	SwapIfBig(m_rctVideo[iID].top, m_rctVideo[iID].bottom);

	CPlayDemo_VCDlg* Demo_VCDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	Demo_VCDlg->m_PlaySDK.DrawRect(m_iPlayerID, &m_rctVideo[iID], 1);

	UpdateUI(iID);
	return 0;
}

void CLS_DlgEZoom::UpdateUI(const int _iPanelID)
{
	m_iEdtPanelId = _iPanelID;
	m_iEdtZoomPnlId = -1;
	m_iEdtBottom = m_iEdtTop = m_iEdtRight = m_iEdtLeft = 0;
	if (m_iEZoomID[_iPanelID] < 4 && m_iEZoomID[_iPanelID] >= 0 || m_bMouseDown)
	{
		m_iEdtZoomPnlId = m_iEZoomID[_iPanelID];
		m_iEdtLeft = m_rctVideo[_iPanelID].left;
		m_iEdtTop = m_rctVideo[_iPanelID].top;
		m_iEdtRight = m_rctVideo[_iPanelID].right;
		m_iEdtBottom = m_rctVideo[_iPanelID].bottom;
	}
	UpdateData(FALSE);
}
void CLS_DlgEZoom::PanelRectToVideoRect(RECT &_rct)
{
	RECT rect = {0};	
	m_gpVideoPnl.GetClientRect(&rect);
	int iPanelWidth = rect.right - rect.left;
	int iPanelHeight = rect.bottom - rect.top;

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

	_rct.left = (int)((double)_rct.left / iPanelWidth * iVdWidth);
	_rct.right = (int)((double)_rct.right / iPanelWidth * iVdWidth);
	_rct.top = (int)((double)_rct.top / iPanelHeight * iVdHeight);
	_rct.bottom = (int)((double)_rct.bottom / iPanelHeight * iVdHeight);
}

void CLS_DlgEZoom::EZoom(const int _iID)
{
	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	int iID = _iID ;
	if (m_iEZoomID[iID] != -1)
	{		
		pDlg->m_PlaySDK.RemoveEZoom(m_iPlayerID, m_iEZoomID[iID]);
		m_iEZoomID[iID] = -1;
	}

	RECT rctShow = {0};
	m_iEZoomID[iID] = pDlg->m_PlaySDK.AddEZoom(m_iPlayerID, m_pDlgVedio[iID]->GetSafeHwnd(), &rctShow, 1);		//添加电子放大
	pDlg->m_PlaySDK.SetEZoom(m_iPlayerID, m_iEZoomID[iID], m_rctVideo[iID]);		//设置电子放大区域
}
void CLS_DlgEZoom::OnBnClickedBtnSet()
{
	// TODO: Add your control notification handler code here
	int iID = m_iEdtPanelId;
	if (iID < 0 || iID >= MAXPANELNUM)
		return;
	UpdateData(TRUE);

	m_rctVideo[iID].left = m_iEdtLeft;
	m_rctVideo[iID].top = m_iEdtTop;
	m_rctVideo[iID].right = m_iEdtRight;
	m_rctVideo[iID].bottom = m_iEdtBottom;

	EZoom(iID);
	UpdateUI(iID);    
}

void CLS_DlgEZoom::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针
	for(int i = 0; i < 5; i++)
	{
		if(m_iEZoomID[i] != -1)
		{
			pDlg->m_PlaySDK.RemoveEZoom(m_iPlayerID, m_iEZoomID[i]);

			if (m_pDlgVedio[i] != NULL)
			{
				delete m_pDlgVedio[i];
				m_pDlgVedio[i] = NULL;
			}	
		}
	}
	CDialog::OnClose();
}