⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stmeditordoc.cpp

📁 一个简单的vc++开发的城市公交地图编辑器源代码及生成的地图样例
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// STMEditorDoc.cpp : implementation of the CSTMEditorDoc class
//

#include "stdafx.h"
#include "STMEditor.h"

#include "STMEditorDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include "math.h"
/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc
#include "MapPropDlg.h"
IMPLEMENT_DYNCREATE(CSTMEditorDoc, CDocument)

BEGIN_MESSAGE_MAP(CSTMEditorDoc, CDocument)
	//{{AFX_MSG_MAP(CSTMEditorDoc)
	ON_COMMAND(ID_MAP_PROP, OnMapProp)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc construction/destruction

CSTMEditorDoc::CSTMEditorDoc()
{
	m_selTool=ST_BROWSE;
	m_arrSel.SetSize(0,10);
}

CSTMEditorDoc::~CSTMEditorDoc()
{
	ClearDataBuffer();
}

BOOL CSTMEditorDoc::OnNewDocument()
{

	if (!CDocument::OnNewDocument())
		return FALSE;
	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	ClearDataBuffer();
	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc serialization

void CSTMEditorDoc::Serialize(CArchive& ar)
{
	CString		filename=ar.GetFile()->GetFilePath();
	CString		path=filename.Left(filename.ReverseFind('\\')+1);
	CString		workpath=path+ar.GetFile()->GetFileTitle()+"(set)";
	CString		crossfilename=workpath+"\\cross.dat";
	CString		pathfilename=workpath+"\\path.dat";
	CString		bsfilename=workpath+"\\busstation.dat";
	CString		trfilename=workpath+"\\traffic.dat";
	CString		streetfilename=workpath+"\\street.dat";
	CString		addressfilename=workpath+"\\address.dat";
	int			i;
	if (ar.IsStoring())
	{
		// TODO: add storing code here
		ar.WriteString(m_Name);
		ar.WriteString("\r\n");
		ar.WriteString(m_Author);
		ar.WriteString("\r\n");
		ar.WriteString(m_Memory);
		ar.WriteString("\r\n");
		CString tmp;
		tmp.Format("%d\r\n%d\r\n%d\r\n",m_Width,m_Height,m_Scale);
		ar.WriteString(tmp);
		CreateDirectory(workpath,NULL);
		if(!m_bkfilename.IsEmpty())
		{
			CString	title=m_bkfilename.Right(m_bkfilename.GetLength()-m_bkfilename.ReverseFind('\\'));
			CString	bkfilename=workpath+title;
			CopyFile(m_bkfilename,bkfilename,TRUE);
			ar.WriteString(bkfilename);
			ar.WriteString("\r\n");
		}
		//save cross data
		CFile crossfile;
		crossfile.Open(crossfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		crosses=m_arrCross.GetSize();
		crossfile.Write(&crosses,sizeof(int));
		CROSSINFO	ci;
		int		namelen;
		for( i=0;i<m_arrCross.GetSize();i++)
		{
			ci=m_arrCross.GetAt(i);
			namelen=strlen(ci.name);
			crossfile.Write(&ci.pos,sizeof(POINT));
			crossfile.Write(&namelen,sizeof(int));
			crossfile.Write(ci.name,strlen(ci.name));
		}
		crossfile.Close();
		
		//save path data
		CFile pathfile;
		pathfile.Open(pathfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		pathes=m_arrPath.GetSize();
		pathfile.Write(&pathes,sizeof(int));
		PATHINFO	pi;
		for( i=0;i<m_arrPath.GetSize();i++)
		{
			pi=m_arrPath.GetAt(i);
			pathfile.Write(&pi,sizeof(PATHINFO));
		}
		pathfile.Close();
		
		//save bus station data
		CFile bsfile;
		bsfile.Open(bsfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		bses=m_arrBS.GetSize();
		bsfile.Write(&bses,sizeof(int));
		BSINFO	bsi;
		for( i=0;i<m_arrBS.GetSize();i++)
		{
			bsi=m_arrBS.GetAt(i);
			bsfile.Write(&bsi,sizeof(BSINFO));
		}
		bsfile.Close();

		//save traffic route data
		CFile trfile;
		trfile.Open(trfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		trafficroutes=m_arrTR.GetSize();
		trfile.Write(&trafficroutes,sizeof(int));
		TRAFFICROUTEINFO	tri;
		int test=sizeof(ELEINFO);
		for( i=0;i<m_arrTR.GetSize();i++)
		{
			tri=m_arrTR.GetAt(i);
			trfile.Write(tri.name,10);
			trfile.Write(&tri.type,sizeof(BYTE));
			trfile.Write(&tri.nStations,sizeof(int)); 
			trfile.Write(tri.pEI,sizeof(ELEINFO)*tri.nStations);
		}
		trfile.Close();

		//save street data
		CFile streetfile;
		streetfile.Open(streetfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		streets=m_arrStreet.GetSize();
		streetfile.Write(&streets,sizeof(int));
		STREETINFO si;
		for(i=0;i<streets;i++)
		{
			si=m_arrStreet.GetAt(i);
			streetfile.Write(si.name,50);
			streetfile.Write(&si.nPathes,sizeof(int));
			streetfile.Write(si.pPathID,sizeof(int)*si.nPathes);
		}
		streetfile.Close();

		//save address data
		CFile addressfile;
		addressfile.Open(addressfilename,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
		int		addresses=m_arrAddress.GetSize();
		addressfile.Write(&addresses,sizeof(int));
		ADDRESSINFO ai;
		for(i=0;i<addresses;i++)
		{
			ai=m_arrAddress.GetAt(i);
			addressfile.Write(&ai,sizeof(ADDRESSINFO));
		}
		addressfile.Close();
	}
	else
	{
		// TODO: add loading code here
		ClearDataBuffer();
		ar.ReadString(m_Name);
		ar.ReadString(m_Author);
		ar.ReadString(m_Memory);
		CString tmp;
		ar.ReadString(tmp);
		m_Width=atoi(tmp.GetBuffer(tmp.GetLength()));
		tmp.ReleaseBuffer();
		ar.ReadString(tmp);
		m_Height=atoi(tmp.GetBuffer(tmp.GetLength()));
		tmp.ReleaseBuffer();
		ar.ReadString(tmp);
		m_Scale=atoi(tmp.GetBuffer(tmp.GetLength()));
		tmp.ReleaseBuffer();
		ar.ReadString(m_bkfilename);
		m_Picture.Load(m_bkfilename);

		//read cross data
		CFile crossfile;
		if(crossfile.Open(crossfilename,CFile::modeRead|CFile::typeBinary))
		{
			int		crosses;
			crossfile.Read(&crosses,sizeof(int));
			m_arrCross.SetSize(crosses,10);
			CROSSINFO	ci;
			int			namelen=0;
			for( i=0;i<crosses;i++)
			{
				crossfile.Read(&ci.pos,sizeof(POINT));
				crossfile.Read(&namelen,sizeof(int));
				crossfile.Read(ci.name,namelen);
				ci.name[namelen]=0;
				m_arrCross.SetAt(i,ci);
			}
			crossfile.Close();	
		}

		//read path data
		CFile pathfile;
		if(pathfile.Open(pathfilename,CFile::modeRead|CFile::typeBinary))
		{
			int		pathes;
			pathfile.Read(&pathes,sizeof(int));
			m_arrPath.SetSize(pathes,10);
			PATHINFO	pi;
			for( i=0;i<pathes;i++)
			{
				pathfile.Read(&pi,sizeof(PATHINFO));
				m_arrPath.SetAt(i,pi);
			}
			pathfile.Close();
		}
		
		//read bus station data
		CFile bsfile;
		if(bsfile.Open(bsfilename,CFile::modeRead|CFile::typeBinary))
		{
			int		bses;
			bsfile.Read(&bses,sizeof(int));
			m_arrBS.SetSize(bses,10);
			BSINFO	bsi;
			for( i=0;i<bses;i++)
			{
				bsfile.Read(&bsi,sizeof(BSINFO));
				m_arrBS.SetAt(i,bsi);
			}
			bsfile.Close();
		}
		//read traffic route data
		CFile trfile;
		if(trfile.Open(trfilename,CFile::modeRead|CFile::typeBinary))
		{
			int		trafficroutes;
			trfile.Read(&trafficroutes,sizeof(int));
			m_arrTR.SetSize(trafficroutes,10);
			TRAFFICROUTEINFO	tri;
			for( i=0;i<trafficroutes;i++)
			{
				trfile.Read(tri.name,10);//get traffic route name
				trfile.Read(&tri.type,sizeof(BYTE));
				trfile.Read(&tri.nStations,sizeof(int));//get traffic route number
				tri.pEI=new ELEINFO[tri.nStations];
				trfile.Read(tri.pEI,sizeof(ELEINFO)*tri.nStations);
				m_arrTR.SetAt(i,tri);
			}
			trfile.Close();
		}

		//read street data
		CFile streetfile;
		if(streetfile.Open(streetfilename,CFile::modeRead|CFile::typeBinary))
		{
			int		streets;
			streetfile.Read(&streets,sizeof(int));
			m_arrStreet.SetSize(streets,10);
			STREETINFO	si;
			for( i=0;i<streets;i++)
			{
				streetfile.Read(si.name,50);//get traffic route name
				streetfile.Read(&si.nPathes,sizeof(int));//get traffic route number
				si.pPathID =new int[si.nPathes];
				streetfile.Read(si.pPathID,sizeof(int)*si.nPathes);
				m_arrStreet.SetAt(i,si);
			}
			streetfile.Close();
		}
		CFile addressfile;
		if(addressfile.Open(addressfilename,CFile::modeRead|CFile::typeBinary))
		{
			int addresses;
			addressfile.Read(&addresses,sizeof(int));
			m_arrAddress.SetSize(addresses,10);
			ADDRESSINFO ai;
			for(i=0;i<addresses;i++)
			{
				addressfile.Read(&ai,sizeof(ADDRESSINFO));
				m_arrAddress.SetAt(i,ai);
			}
			addressfile.Close();
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc diagnostics

#ifdef _DEBUG
void CSTMEditorDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CSTMEditorDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSTMEditorDoc commands

BOOL CSTMEditorDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	return TRUE;
}


RECT CSTMEditorDoc::CalcCrossRect(POINT pt)
{
	RECT	ret;
	ret.left=pt.x-3;
	ret.right=pt.x+3;
	ret.top=pt.y-3;
	ret.bottom=pt.y+3;
	return ret;
}

RECT CSTMEditorDoc::CalcBSRect(POINT pt)
{
	RECT	ret;
	ret.left=pt.x-6;
	ret.right=pt.x+6;
	ret.top=pt.y-4;
	ret.bottom=pt.y+4;
	return ret;
}

void CSTMEditorDoc::OnMapProp() 
{
	CMapPropDlg opendlg;
	opendlg.m_Author=m_Author;
	opendlg.m_Name=m_Name;
	opendlg.m_Memory=m_Memory;
	opendlg.m_Width=m_Width;
	opendlg.m_Height=m_Height;
	opendlg.m_Scale=m_Scale;
	opendlg.m_bkfilename=m_bkfilename;
	if(opendlg.DoModal()==IDOK)
	{
		m_Author=opendlg.m_Author;
		m_Name=opendlg.m_Name;
		m_Memory=opendlg.m_Memory;
		m_Width=opendlg.m_Width;
		m_Height=opendlg.m_Height;
		m_Scale=opendlg.m_Scale;
		m_bkfilename=opendlg.m_bkfilename;
		m_Picture.Load(m_bkfilename);
		UpdateAllViews(NULL);
		SetModifiedFlag();
	}
}

void CSTMEditorDoc::RemoveCross(CRgn &effectrgn,int index)
{
	ASSERT(index<m_arrCross.GetSize());
	PATHINFO pi;
	for(int i=m_arrPath.GetSize()-1;i>=0;i--)
	{
		pi=m_arrPath.GetAt(i);
		if((pi.begin_id==index)||
			(pi.end_id==index))
		{
			CRgn pathrgn;
			RemovePath(pathrgn,i);
			AddRgn(effectrgn,pathrgn);
		}else if((pi.begin_id>index)||
			(pi.end_id>index))
		{
			if(pi.begin_id>index) pi.begin_id--;
			if(pi.end_id>index) pi.end_id--;
			m_arrPath.SetAt(i,pi);
		}
	}
	ELEINFO selEI;
	for(i=m_arrSel.GetSize()-1;i>=0;i--)
	{
		selEI=m_arrSel.GetAt(i);
		if(selEI.eleType==ET_CROSS &&
			selEI.index==index)
		{
			m_arrSel.RemoveAt(i);
		}
		if(selEI.eleType==ET_CROSS &&
			selEI.index>index)
		{
			selEI.index--;
			m_arrSel.SetAt(i,selEI);
		}
	}

	// update traffic route data,in theory the will no cross in path array(deleted 
	// in pathdelete function
	TRAFFICROUTEINFO tri;
	for(i=m_arrTR.GetSize()-1;i>=0;i--)
	{
		tri=m_arrTR.GetAt(i);
		BOOL bused=FALSE;
		for(int j=0;j<tri.nStations;j++)
		{
			if((tri.pEI+j)->eleType==ET_CROSS &&
				(tri.pEI+j)->index==index)
			{
				bused=TRUE;
				break;
			}else if((tri.pEI+j)->eleType==ET_CROSS &&
				(tri.pEI+j)->index>index)
			{
				(tri.pEI+j)->index--;
			}
		}
		if(bused) //used in traffic route:delete the traffic route
			RemoveTR(i);
		else	//not used :update the data 
			m_arrTR.SetAt(i,tri);
	}
	RECT	rc=CalcCrossRect(m_arrCross.GetAt(index).pos);
	CRgn	crossrgn;
	crossrgn.CreateRectRgnIndirect(&rc);
	AddRgn(effectrgn,crossrgn);
	m_arrCross.RemoveAt(index);
	SetModifiedFlag();
}


void CSTMEditorDoc::RemoveBS(CRgn &effectrgn, int index)
{
	ASSERT(index<m_arrBS.GetSize());
	ELEINFO selEI;
	for(int i=m_arrSel.GetSize()-1;i>=0;i--)
	{
		selEI=m_arrSel.GetAt(i);
		if(selEI.eleType==ET_BS &&
			selEI.index==index)
		{
			m_arrSel.RemoveAt(i);
		}
		if(selEI.eleType==ET_BS &&
			selEI.index>index)
		{
			selEI.index--;
			m_arrSel.SetAt(i,selEI);
		}
	}
	// update traffic route data
	TRAFFICROUTEINFO tri;
	BOOL	bused;
	for(i=m_arrTR.GetSize()-1;i>=0;i--)
	{
		tri=m_arrTR.GetAt(i);
		bused=FALSE;
		for(int j=0;j<tri.nStations;j++)
		{
			if((tri.pEI+j)->eleType==ET_BS &&
				(tri.pEI+j)->index==index)
			{
				bused=TRUE;
				break;
			}
		}
		if(bused) //used in traffic route:delete the traffic route
		{
			RemoveTR(i);
		}else	//not used :update the data 
		{
			for(j=0;j<tri.nStations;j++)
			{
				if((tri.pEI+j)->eleType==ET_BS &&
					(tri.pEI+j)->index>index)
				{
					(tri.pEI+j)->index--;
				}
			}
			m_arrTR.SetAt(i,tri);
		}
	}
	CalcBSRgn(effectrgn,index);
	m_arrBS.RemoveAt(index);
	SetModifiedFlag();
}


void CSTMEditorDoc::CalcCrossRgn(CRgn &crossrgn, int index)
{
	POINT	pt=m_arrCross.GetAt(index).pos;
	RECT	rc=CalcCrossRect(pt);
	crossrgn.CreateRectRgnIndirect(&rc);
}

void CSTMEditorDoc::CalcBSRgn(CRgn &bsrgn, int index)
{
	BSINFO bsi=m_arrBS.GetAt(index);
	RECT	hitrc,txtrc;
	hitrc=CalcBSRect(bsi.pos);
	txtrc.left=hitrc.right,txtrc.top=hitrc.top;
	CWindowDC dc(NULL);
	CSize	txtsize=dc.GetTextExtent(bsi.name,strlen(bsi.name));
	txtrc.right=txtrc.left+txtsize.cx;
	txtrc.bottom=txtrc.top+txtsize.cy;
	CRgn	hitrgn,txtrgn;
	hitrgn.CreateRectRgnIndirect(&hitrc);
	txtrgn.CreateRectRgnIndirect(&txtrc);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -