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

#include "TD_PlaySDK.h"
extern CLS_PlaySDK	g_PlaySDK_API;

//播放方向
#define DIR_FORWARD						0				//正方向
#define DIR_REVERSE						1				//反方向

IMPLEMENT_DYNAMIC(CLS_DlgVedio, CDialog)

CLS_DlgVedio::CLS_DlgVedio(CWnd* pParent /*=NULL*/)
	: CDialog(CLS_DlgVedio::IDD, pParent)
{
	m_iSelectID = -1;
	m_iWhatDlg = MAINWINDOW;
	m_bIsCheckArea = false;

	//播放相关
	InitPlay();
}

CLS_DlgVedio::~CLS_DlgVedio()
{
}

void CLS_DlgVedio::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CLS_DlgVedio, CDialog)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

void CLS_DlgVedio::OnPaint()
{
	CPaintDC dc(this);
	if (m_iWhatDlg == EZOOMPNL)
	{

		//背景填充为白色,不建议这么做
		CRect rect;
		CBrush brush;
		brush.CreateSolidBrush(RGB(212, 208, 200));
		this->GetClientRect(&rect);		
		dc.FillRect(&rect, &brush); //用FillRect成员函数利用笔刷填充指定区域
		brush.DeleteObject();		
		return;
	}
	RECT rect = {0};	
	this->GetClientRect(&rect);
	int m_iWidth = (rect.right - rect.left);
	int m_iHeight = (rect.bottom - rect.top);
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_BTP_NO_VEDIO);
	CDC *pDC = GetDC();
	CDC memDC;//兼容DC
	memDC.CreateCompatibleDC(pDC);
	memDC.SelectObject(&bitmap);
	BITMAP bmp;
	bitmap.GetBitmap(&bmp);
	pDC->StretchBlt(0, 0, m_iWidth, m_iHeight, &memDC, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
	memDC.DeleteDC();
	ReleaseDC(pDC);
	::DeleteObject(&bitmap);
}

void CLS_DlgVedio::OnLButtonDown(UINT nFlags, CPoint point)
{
	if (MAINWINDOW == m_iWhatDlg)
	{
		CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针		
		for (int i = 0; i != 16; ++i)
		{
			if (pDlg->m_pDlgVedio[i] == this)
			{
				pDlg->m_iCurWinNum = i;
				CString cstr;
				cstr.Format("%d",i);
				(pDlg->GetDlgItem(IDC_EDT_NUM))->SetWindowText(cstr);
				
				pDlg->SetFocus();				//选中后设置焦点
				pDlg->UpdateUI_ShowAudioVolume(i);

				pDlg->m_chkSync.SetCheck(GetPlaySync());
				break;
			}
		}
	}
	else if(DRAWPOLYGONWIN == m_iWhatDlg)
	{
		::SendMessage(GetParent()->GetParent()->GetSafeHwnd(), WM_ITEMLBUTTON_DOWN, point.x, point.y);
	}
	else if(EZOOMWIN == m_iWhatDlg)
	{
		CLS_DlgEZoom* pDlg = (CLS_DlgEZoom*)(GetParent()->GetParent());//获得EZoom的实例指针	
		int iID = pDlg->m_iEdtPanelId ;
		if (iID < 0 || iID >= 5)
			return;

		pDlg->m_rctShow[iID].left = point.x;
		pDlg->m_rctShow[iID].top = point.y;	
		pDlg->m_bMouseDown = true;
	}
	else if(EZOOMPNL == m_iWhatDlg)			//待完成
	{
		::SendMessage(GetParent()->GetParent()->GetSafeHwnd(), WM_ITEMLBUTTON_DOWN, m_iSelectID, 0);
	}
	
	CDialog::OnLButtonDown(nFlags, point);
}

void CLS_DlgVedio::OnLButtonDblClk(UINT nFlags, CPoint point)
{
	if (DRAWPOLYGONWIN == m_iWhatDlg)		//如果是DRAWPOLYGON窗体这屏蔽双击
	{
		return;
	}
	else if (MAINWINDOW == m_iWhatDlg)
	{
		CPlayDemo_VCDlg* pDlg = (CPlayDemo_VCDlg*)AfxGetMainWnd();//获得主窗口的实例指针		
		RECT nRect;	
		pDlg->m_gpVedio.GetClientRect(&nRect);
		pDlg->m_iCurVedio_Count = 0;//cheyueyong 2014-02-14
		pDlg->ShutWindow();
		this->MoveWindow(&nRect);//移动窗体
		this->ShowWindow(TRUE);
		
	}
	else if (EZOOMPNL == m_iWhatDlg)
	{
		::SendMessage(GetParent()->GetParent()->GetSafeHwnd(), WM_ITEMLBUTTON_DBCLICK, m_iSelectID, 0);
		CRect rect;
		this->GetClientRect(&rect);		
		InvalidateRect(&rect);
	}
	CDialog::OnLButtonDblClk(nFlags, point);
}

BOOL CLS_DlgVedio::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->message == WM_KEYDOWN)
	{
		switch (pMsg->wParam)
		{
		case VK_RETURN:
			return TRUE;
		case VK_ESCAPE:
			return TRUE;
		default:
			break;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}

void CLS_DlgVedio::OnMouseMove(UINT nFlags, CPoint point)
{
	if (m_iWhatDlg != EZOOMWIN)
	{
		return;
	}
	::SendMessage(GetParent()->GetParent()->GetSafeHwnd(), WM_ITEMLBUTTON_MOVE, point.x, point.y);
	CDialog::OnMouseMove(nFlags, point);
}

void CLS_DlgVedio::OnLButtonUp(UINT nFlags, CPoint point)
{
	if (m_iWhatDlg != EZOOMWIN)
	{
		return;
	}

	::SendMessage(GetParent()->GetParent()->GetSafeHwnd(), WM_ITEMLBUTTON_UP, point.x, point.y);
	CDialog::OnLButtonUp(nFlags, point);
}

/**************************************************************************
***************************************************************************
***************************************************************************
* 播放库相关操作                                               
***************************************************************************
***************************************************************************
**************************************************************************/

int CLS_DlgVedio::InitPlay()
{
	m_blIsPlay = false;
	m_iPlayerId = -1;
	m_iPlayDirect = DIR_FORWARD;
	m_blSync = FALSE;
	m_iSpeed = 0;
	return RET_SUCCESS;
}

int CLS_DlgVedio::CreateFilePlayer(CString _strFile)
{
	int iPlayerId = g_PlaySDK_API.CreatePlayerFromFile(GetSafeHwnd(), _strFile.GetBuffer(), 0);
	if(iPlayerId >= 0) {
		m_iPlayerId = iPlayerId;		
	}
	return iPlayerId;
}

int CLS_DlgVedio::DeletePlayer()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}
	int iRet = g_PlaySDK_API.DeletePlayer(m_iPlayerId);
	
	InitPlay();

	return iRet;
}

int CLS_DlgVedio::Play()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}
	
	int iRet = RET_FAILED;
	if (DIR_FORWARD == m_iPlayDirect) {
		iRet = g_PlaySDK_API.Play(m_iPlayerId);
	} else {
		iRet = g_PlaySDK_API.PlayBackControl(m_iPlayerId, CTRL_RESVERSE_PLAY, 0, 0, 0);
	}

	if (RET_SUCCESS == iRet) {
		m_blIsPlay = true;
		m_iSpeed = 0;
	}
	return iRet;
}

int CLS_DlgVedio::Pause()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}
	return g_PlaySDK_API.Pause(m_iPlayerId);
}

int CLS_DlgVedio::Step()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}
	return g_PlaySDK_API.StepForward(m_iPlayerId);
}

int CLS_DlgVedio::Fast()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}

	m_iSpeed ++;
	if (m_iSpeed <= 0) {
		m_iSpeed = 1;
	} else if (m_iSpeed > 4) {
		m_iSpeed = 4;
	}

	return g_PlaySDK_API.FastForward(m_iPlayerId, m_iSpeed);
}

int CLS_DlgVedio::Slow()
{
	if (m_iPlayerId < 0) {
		return RET_FAILED;
	}

	m_iSpeed --;
	if (m_iSpeed >= 0) {
		m_iSpeed = -1;
	} else if (m_iSpeed < -4) {
		m_iSpeed = -4;
	}

	return g_PlaySDK_API.SlowForward(m_iPlayerId, abs(m_iSpeed));
}	
