📄 simulatordoc.cpp
字号:
m_isRunning = FALSE;
CDocument::DeleteContents();
CRobotForage::DeleteAllItems();
if (m_threadIsCreated) {
m_updateThread->SuspendThread();
delete m_updateThread;
m_threadIsCreated = FALSE;
}
// sLock.Unlock();
UpdateAllViews(NULL);
}
// call the dialog to initialize the forage application
void CSimulatorDoc::OnApplicationsForage()
{
CDlgForageInit dlgForageInit;
CRobotForage *robot;
int i;
CPoint p;
dlgForageInit.m_nRobots = 5;
dlgForageInit.m_nItems = 30;
dlgForageInit.m_sizeX = 1000;
dlgForageInit.m_sizeY = 600;
dlgForageInit.m_radioGroup = 1;
if (dlgForageInit.DoModal() == IDOK) {
srand( (unsigned)time( NULL ) );
DeleteContents();
// Creating the thread for updating robots
if (!m_threadIsCreated) {
m_updateThread = AfxBeginThread(UpdateThreadProc, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
m_threadIsCreated = TRUE;
}
for(i=0; i<dlgForageInit.m_nRobots; i++) {
CString s;
s.Format("%4d", i);
p = CPoint( (rand() % dlgForageInit.m_sizeX) + 1, (rand() % dlgForageInit.m_sizeY) + 1);
robot = new CRobotForage(p.x, p.y, 0, WANDER, (short) m_robots.GetSize(), s, (short) dlgForageInit.m_radioGroup);
robot->m_localMap = new CMapPath(m_globalMap, FALSE);
AddRobot(robot);
}
for(i=0; i<dlgForageInit.m_nItems; i++) {
p = CPoint( (rand() % dlgForageInit.m_sizeX) + 1, (rand() % dlgForageInit.m_sizeY) + 1);
CRobotForage::AddItem(p);
}
m_globalMap->m_goalReal = CPoint( (rand() % dlgForageInit.m_sizeX) + 1, (rand() % dlgForageInit.m_sizeY) + 1);
CRobotForage::SetArea(CSize(dlgForageInit.m_sizeX, dlgForageInit.m_sizeY));
m_view->ScrollToPosition( CPoint(m_globalMap->m_goalReal.x-400, m_globalMap->m_goalReal.y-400) );
}
}
// Create 100 robots
void CSimulatorDoc::OnApplications100()
{
CRobotHolonomic *robot;
int i;
CPoint p;
for(i=0; i<100; i++) {
CString s;
s.Format("%4d", i);
p = CPoint( (rand() % 5000) + 1, (rand() % 5000) + 1);
robot = new CRobotHolonomic(p.x, p.y, 0, DOCK, (short) m_robots.GetSize(), s);
robot->m_localMap = new CMapPath(m_globalMap, FALSE);
AddRobot(robot);
}
}
//---------------------------------------------------
// Handlers for the control bar and menu events
//---------------------------------------------------
BOOL CSimulatorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
Initialize();
return TRUE;
}
BOOL CSimulatorDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
m_globalMap->ComputeMap();
m_globalMap->ComputePath();
for(int i=0; i<m_robots.GetSize(); i++){
m_robots[i]->m_localMap->ComputeMap();
m_robots[i]->m_localMap->ComputePath();
}
// Creating the thread for updating robots
if (!m_threadIsCreated) {
m_updateThread = AfxBeginThread(UpdateThreadProc, this, THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED, NULL);
m_threadIsCreated = TRUE;
}
return TRUE;
}
// Start update thread
void CSimulatorDoc::OnInitTimer()
{
m_updateThread->ResumeThread();
m_isRunning = TRUE;
}
void CSimulatorDoc::OnUpdateInitTimer(CCmdUI* pCmdUI)
{
if (m_isRunning)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
// Suspend update thread
void CSimulatorDoc::OnFinishTimer()
{
m_updateThread->SuspendThread();
m_isRunning = FALSE;
}
void CSimulatorDoc::OnUpdateFinishTimer(CCmdUI* pCmdUI)
{
if (m_isRunning)
pCmdUI->SetCheck(0);
else
pCmdUI->SetCheck(1);
}
void CSimulatorDoc::OnDrawRange()
{
m_drawRange = !m_drawRange;
UpdateAllViews(NULL);
}
void CSimulatorDoc::OnUpdateDrawRange(CCmdUI* pCmdUI)
{
if (m_drawRange)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnDrawErased()
{
m_drawErased = !m_drawErased;
UpdateAllViews(NULL);
}
void CSimulatorDoc::OnUpdateDrawErased(CCmdUI* pCmdUI)
{
if (m_drawErased)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnSelect()
{
m_selectedButton = SELECT;
SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));
}
void CSimulatorDoc::OnUpdateSelect(CCmdUI* pCmdUI)
{
if (m_selectedButton == SELECT)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateObstacleCircle()
{
m_selectedButton = CIRCLEOBST;
}
void CSimulatorDoc::OnUpdateCreateObstacleCircle(CCmdUI* pCmdUI)
{
if (m_selectedButton == CIRCLEOBST)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateObstacleRect()
{
m_selectedButton = RECTOBST;
}
void CSimulatorDoc::OnUpdateCreateObstacleRect(CCmdUI* pCmdUI)
{
if (m_selectedButton == RECTOBST)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateObstaclePolygon()
{
m_selectedButton = POLYGONOBST;
}
void CSimulatorDoc::OnUpdateCreateObstaclePolygon(CCmdUI* pCmdUI)
{
if (m_selectedButton == POLYGONOBST)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateBoxPolygon()
{
m_selectedButton = POLYGONBOX;
}
void CSimulatorDoc::OnUpdateCreateBoxPolygon(CCmdUI* pCmdUI)
{
if (m_selectedButton == POLYGONBOX)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateBoxCircle()
{
m_selectedButton = CIRCLEBOX;
}
void CSimulatorDoc::OnUpdateCreateBoxCircle(CCmdUI* pCmdUI)
{
if (m_selectedButton == CIRCLEBOX)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateBoxRect()
{
m_selectedButton = RECTBOX;
}
void CSimulatorDoc::OnUpdateCreateBoxRect(CCmdUI* pCmdUI)
{
if (m_selectedButton == RECTBOX)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreateLabmate()
{
m_selectedButton = LABMATEROBOT;
}
void CSimulatorDoc::OnUpdateCreateLabmate(CCmdUI* pCmdUI)
{
if (m_selectedButton == LABMATEROBOT)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnDrawpath()
{
m_drawPath = !m_drawPath;
UpdateAllViews(NULL);
}
void CSimulatorDoc::OnUpdateDrawpath(CCmdUI* pCmdUI)
{
if (m_drawPath)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreaterobotholonomic()
{
m_selectedButton = HOLONOMICROBOT;
}
void CSimulatorDoc::OnUpdateCreaterobotholonomic(CCmdUI* pCmdUI)
{
if (m_selectedButton == HOLONOMICROBOT)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
void CSimulatorDoc::OnCreaterobotnonholonomic()
{
m_selectedButton = NONHOLONOMICROBOT;
}
void CSimulatorDoc::OnUpdateCreaterobotnonholonomic(CCmdUI* pCmdUI)
{
if (m_selectedButton == NONHOLONOMICROBOT)
pCmdUI->SetCheck(1);
else
pCmdUI->SetCheck(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -