📄 robocupview.cpp
字号:
// RobocupView.cpp : implementation of the CRobocupView class
//
#include "stdafx.h"
#include "Robocup.h"
#include "mainfrm.h"
#include "RobocupDoc.h"
#include "RobocupView.h"
//#include "Debugdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRobocupView
IMPLEMENT_DYNCREATE(CRobocupView, CView)
BEGIN_MESSAGE_MAP(CRobocupView, CView)
//{{AFX_MSG_MAP(CRobocupView)
ON_COMMAND(ID_VIDEO, OnVideo)
ON_COMMAND(ID_CONFIG, OnConfig)
ON_COMMAND(ID_DISPLAY, OnDisplay)
ON_UPDATE_COMMAND_UI(ID_DISPLAY, OnUpdateDisplay)
ON_WM_TIMER()
ON_COMMAND(ID_STOP, OnStop)
ON_COMMAND(ID_PLAY, OnPlay)
ON_UPDATE_COMMAND_UI(ID_CONFIG, OnUpdateConfig)
ON_UPDATE_COMMAND_UI(ID_PLAY, OnUpdatePlay)
ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
ON_UPDATE_COMMAND_UI(ID_VIDEO, OnUpdateVideo)
ON_WM_CREATE()
ON_WM_LBUTTONDOWN()
ON_UPDATE_COMMAND_UI(ID_DEBUG, OnUpdateDebug)
ON_COMMAND(ID_DEBUG, OnDebug)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRobocupView construction/destruction
CRobocupView::CRobocupView()
{
m_pVideoDlg = NULL;
m_pConfigDlg = NULL;
m_pDebugDlg =NULL;
m_pMemDC = NULL;
m_pBitmap = NULL;
m_bIsVirtualDisplay = FALSE;
m_bIsPlay = FALSE;
m_bIsStop = FALSE;
m_bIsConfig = TRUE;
m_bIsVideo = FALSE;
m_bIsDebug=TRUE;
m_MainRect = CRect(0, 0, 1000, 1000);
}
CRobocupView::~CRobocupView()
{
if(m_pVideoDlg != NULL)
delete m_pVideoDlg;
if(m_pConfigDlg != NULL)
delete m_pConfigDlg;
if(m_pDebugDlg != NULL)
delete m_pDebugDlg;
if(m_pMemDC != NULL)
{
delete m_pBitmap;
delete m_pMemDC;
}
CloseHandle(Player::m_hComm);
}
BOOL CRobocupView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CRobocupView drawing
void CRobocupView::OnDraw(CDC* pDC)
{
CRobocupDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(NULL != m_pMemDC)
{
pDC->BitBlt(0,
0,
m_MainRect.Width(),
m_MainRect.Height(),
m_pMemDC,
0,
0,
SRCCOPY);
}
}
/////////////////////////////////////////////////////////////////////////////
// CRobocupView printing
BOOL CRobocupView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CRobocupView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CRobocupView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CRobocupView diagnostics
#ifdef _DEBUG
void CRobocupView::AssertValid() const
{
CView::AssertValid();
}
void CRobocupView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CRobocupDoc* CRobocupView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CRobocupDoc)));
return (CRobocupDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CRobocupView message handlers
void CRobocupView::OnVideo()
{
if(m_pVideoDlg != NULL)
delete m_pVideoDlg;
m_pVideoDlg = new CVideoDlg();
m_pVideoDlg->Create(IDD_VIDEO_DIALOG);
if(m_pConfigDlg != NULL)
{
m_pVideoDlg->SetConfigDlg(m_pConfigDlg);
m_pConfigDlg->SetVideoDlg(m_pVideoDlg);
}
CVideoDlg::m_pVideoDlg=m_pVideoDlg;
CRect ConfigRect;
CRect VideoRect;
CRect MyRect,MainWnd;
int width, height;
m_pVideoDlg->GetWindowRect(&VideoRect);
m_pConfigDlg->GetWindowRect(&ConfigRect);
AfxGetMainWnd()->GetWindowRect(&MainWnd);
width = ConfigRect.Width();
height = ConfigRect.Height();
ConfigRect.left = VideoRect.Width();
ConfigRect.right = VideoRect.Width()+ width;
ConfigRect.top = 0;
ConfigRect.bottom = height;
m_pConfigDlg->MoveWindow(ConfigRect);
MyRect.left = VideoRect.Width();
MyRect.right = 1024;
MyRect.top = height;
MyRect.bottom = 750;
AfxGetMainWnd()->MoveWindow(MyRect);
m_bIsPlay = TRUE;
}
void CRobocupView::OnConfig()
{
if(m_pConfigDlg != NULL)
delete m_pConfigDlg;
m_pConfigDlg = new CConfigDlg();
m_pConfigDlg->Create(IDD_CONFIG_DIALOG);
if(m_pVideoDlg != NULL)
{
m_pVideoDlg->SetConfigDlg(m_pConfigDlg);
m_pConfigDlg->SetVideoDlg(m_pVideoDlg);
}
CRect ConfigRect;
CRect MyRect;
int cfg_width, cfg_height;
int my_width, my_height;
m_pConfigDlg->GetWindowRect(&ConfigRect);
this->GetWindowRect(&MyRect);
cfg_width = ConfigRect.Width();
cfg_height = ConfigRect.Height();
my_width = MyRect.Width();
my_height = MyRect.Height();
MyRect.left = 0;
MyRect.right = my_width;
MyRect.top = 0;
MyRect.bottom = my_height;
AfxGetMainWnd()->MoveWindow(MyRect);
ConfigRect.left = 0;
ConfigRect.right = cfg_width;
ConfigRect.top =MyRect.Height();
ConfigRect.bottom = ConfigRect.top + cfg_height;
m_pConfigDlg->MoveWindow(651,2,360,480);
m_bIsVideo = TRUE;
}
void CRobocupView::OnDisplay()
{
m_bIsVirtualDisplay = !m_bIsVirtualDisplay;
if(m_bIsVirtualDisplay)
{
CDC* pDC;
pDC = GetDC();
m_pMemDC = new CDC();
m_pMemDC->CreateCompatibleDC(pDC);
m_pBitmap = new CBitmap();
m_pBitmap->CreateCompatibleBitmap(pDC, m_MainRect.Width(), m_MainRect.Height());
m_pMemDC->SelectObject(m_pBitmap);
m_pMemDC->PatBlt(m_MainRect.left, m_MainRect.top,
m_MainRect.right, m_MainRect.bottom,
WHITENESS);
ReleaseDC(pDC);
Invalidate(FALSE);
}
else
{
if(NULL != m_pMemDC)
{
m_pMemDC->PatBlt(m_MainRect.left, m_MainRect.top,
m_MainRect.right, m_MainRect.bottom,
WHITENESS);
Invalidate(TRUE);
delete m_pBitmap;
delete m_pMemDC;
m_pBitmap = NULL;
m_pMemDC = NULL;
}
}
}
void CRobocupView::DrawPoint(CDC* pDC, Vector point)
{
CRect rect;
rect.top = (int)(point.y - 2);
rect.left = (int)(point.x - 2);
rect.right = (int)(point.x + 2);
rect.bottom = (int)(point.y + 2);
pDC->Ellipse(&rect);
}
void CRobocupView::DrawBall(CDC* pDC, Vector point)
{
CRect rect;
rect.top = (int)(point.y - 5);
rect.left = (int)(point.x - 5);
rect.right = (int)(point.x + 5);
rect.bottom = (int)(point.y + 5);
pDC->Ellipse(&rect);
}
void CRobocupView::DrawRobot(CDC* pDC, Player robot)
{
CPoint point;
CRect rect;
CPoint p0, p1, p2, p3;
CPoint tp;
double angle;
angle = robot.Get_m_Angle() * 3.1415926535 / 180;
point.x = (int)robot.Get_vPos().x;
point.y = (int)robot.Get_vPos().y;
rect.top = point.y - (int)robot.Get_Length()/2;
rect.left = point.x - (int)robot.Get_Width()/2;
rect.right = point.x + (int)robot.Get_Width()/2;
rect.bottom = point.y + (int)robot.Get_Length()/2;
p0 = CPoint(rect.left, rect.top);
p1 = CPoint(rect.right, rect.top);
p2 = CPoint(rect.right, rect.bottom);
p3 = CPoint(rect.left, rect.bottom);
p0 -= point;
p1 -= point;
p2 -= point;
p3 -= point;
tp.x = (int)(p0.x * cos(angle) - p0.y * sin(angle));
tp.y = (int)(p0.x * sin(angle) + p0.y * cos(angle));
p0 = tp + point;
tp.x = (int)(p1.x * cos(angle) - p1.y * sin(angle));
tp.y = (int)(p1.x * sin(angle) + p1.y * cos(angle));
p1 = tp + point;
tp.x = (int)(p2.x * cos(angle) - p2.y * sin(angle));
tp.y = (int)(p2.x * sin(angle) + p2.y * cos(angle));
p2 = tp + point;
tp.x = (int)(p3.x * cos(angle) - p3.y * sin(angle));
tp.y = (int)(p3.x * sin(angle) + p3.y * cos(angle));
p3 = tp + point;
pDC->MoveTo(p0);
pDC->LineTo(p1);
pDC->MoveTo(p1);
pDC->LineTo(p2);
pDC->MoveTo(p2);
pDC->LineTo(p3);
pDC->MoveTo(p3);
pDC->LineTo(p0);
CString str;
str.Format("%d", robot.Get_Num());
pDC->TextOut(rect.left+5, rect.top+5, str);
point.x = (p1.x + p2.x) / 2;
point.y = (p1.y + p2.y) / 2;
Vector vPt;
vPt.x = point.x;
vPt.y = point.y;
DrawPoint(pDC, vPt);
}
void CRobocupView::DrawScene(CDC* pDC)
{
pDC->PatBlt(m_MainRect.left, m_MainRect.top,
m_MainRect.right, m_MainRect.bottom,
WHITENESS);
DrawField(pDC, m_pVideoDlg->m_FieldRect);
for(int i = 0; i < 5; i++)
{
DrawRobot(pDC, g_MyTeam[i]);
DrawRobot(pDC, g_TheirTeam[i]);
}
DrawBall(pDC, g_Ball.Get_vPos());
Invalidate(FALSE);
}
void CRobocupView::OnUpdateDisplay(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_bIsVirtualDisplay);
}
void CRobocupView::OnTimer(UINT nIDEvent)
{
if(1 == nIDEvent)
{
if(m_bIsVirtualDisplay)
DrawScene(m_pMemDC);//画虚拟场景
}
else
{
}
CView::OnTimer(nIDEvent);
}
void CRobocupView::OnStop()
{
KillTimer(1);
for(int i=0;i<4;i++)//发四遍确保所有机器人都停止走动
{
}
m_bIsPlay = TRUE;
m_bIsStop = FALSE;
}
void CRobocupView::OnPlay()
{
SetTimer(1, 150, NULL);
m_bIsStop = TRUE;
m_bIsPlay = FALSE;
}
void CRobocupView::DrawField(CDC* pDC, CRect FieldRect)
{
pDC->Rectangle(FieldRect);
CRect circle;
double rate_h1, rate_w1;
double rate_h2, rate_w2;
CPoint center;
double radius_h, radius_w;
CRect GoalZone_L, GoalZone_R;
rate_h1 = 152.5 / 45.0;
rate_w1 = 274.0 / 45.0;
radius_h = FieldRect.Height() / rate_h1 / 2.0;
radius_w = FieldRect.Width() / rate_w1 / 2.0;
center.x = (FieldRect.left + FieldRect.right) / 2;
center.y = (FieldRect.top + FieldRect.bottom) / 2;
circle.left = (int)(center.x - radius_w);
circle.right = (int)(center.x + radius_w);
circle.top = (int)(center.y - radius_h);
circle.bottom = (int)(center.y + radius_h);
rate_h2 = 152.5 / 100.0;
rate_w2 = 274.0 / 22.5;
GoalZone_L.left = FieldRect.left;
GoalZone_L.right = (int)(FieldRect.left + FieldRect.Width() / rate_w2);
GoalZone_L.top = (int)( (FieldRect.top+FieldRect.bottom)/2 - FieldRect.Height()/rate_h2/2 );
GoalZone_L.bottom = (int)( (FieldRect.top+FieldRect.bottom)/2 + FieldRect.Height()/rate_h2/2 );
GoalZone_R.left = GoalZone_L.left + FieldRect.Width() - GoalZone_L.Width();
GoalZone_R.right = GoalZone_L.left + FieldRect.Width();
GoalZone_R.top = GoalZone_L.top;
GoalZone_R.bottom = GoalZone_L.bottom;
pDC->Ellipse(&circle);
pDC->MoveTo((FieldRect.left+FieldRect.right)/2, FieldRect.top);
pDC->LineTo((FieldRect.left+FieldRect.right)/2, FieldRect.bottom);
pDC->Rectangle(GoalZone_L);
pDC->Rectangle(GoalZone_R);
}
void CRobocupView::OnUpdateConfig(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bIsConfig);
}
void CRobocupView::OnUpdatePlay(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bIsPlay);
}
void CRobocupView::OnUpdateStop(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bIsStop);
}
void CRobocupView::OnUpdateVideo(CCmdUI* pCmdUI)
{
pCmdUI->Enable(m_bIsVideo);
}
int CRobocupView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// Player::InitComm(1,9600); //直线与转弯的频率
// Player::InitComm(1,19200);
monitor.OpenCom(2);//开启裁判盒监视端口
// Player::InitMMTimer(5);
/*
FILE *pFile;
pFile=fopen("good.txt","w+");
fopen("log.txt","w+");
fprintf(pFile,"-----------------------------\n-------------------------\n");
fclose(pFile);
pFile=fopen("position.txt","w+");
fclose(pFile);
pFile=fopen("position2.txt","w+");
fclose(pFile);
pFile=fopen("command.txt","w+");
fclose(pFile);
pFile=fopen("log1.txt","w+");
fclose(pFile);
pFile=fopen("log.txt","a+");
fprintf(pFile,"-----------------------------\n-------------------------\n");
fclose(pFile);
pFile=fopen("writebuffer.txt","w+");
fclose(pFile);
*/
return 0;
}
void CRobocupView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_test.x = point.x;
m_test.y = point.y;
CView::OnLButtonDown(nFlags, point);
}
void CRobocupView::OnUpdateDebug(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->Enable(m_bIsDebug);
}
void CRobocupView::OnDebug()
{
// TODO: Add your command handler code here
if(m_pDebugDlg != NULL)
delete m_pDebugDlg;
m_pDebugDlg = new CDebugDlg();
m_pDebugDlg->Create(IDD_DEBUG);//,(CMainFrame*)AfxGetMainWnd()
m_pDebugDlg->ShowWindow(SW_SHOW);
if(m_pDebugDlg != NULL)
{
m_pDebugDlg->SetDebugDlg(m_pDebugDlg);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -