mapview.cpp
来自「一个另类的坦克大战源程序」· C++ 代码 · 共 345 行
CPP
345 行
// MapView.cpp : implementation of the CMapView class
//
#include "stdafx.h"
#include "MapEdit.h"
#include "MapDoc.h"
#include "MapView.h"
#include "TileView.h"
#include "MiniMapView.h"
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMapView
IMPLEMENT_DYNCREATE(CMapView, CScrollView)
BEGIN_MESSAGE_MAP(CMapView, CScrollView)
//{{AFX_MSG_MAP(CMapView)
ON_WM_ERASEBKGND()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEWHEEL()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMapView construction/destruction
CMapView::CMapView()
{
// TODO: add construction code here
m_bEnableCursor=TRUE;
}
CMapView::~CMapView()
{
}
BOOL CMapView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMapView drawing
void CMapView::OnDraw(CDC* pDC)
{
CMapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CMainFrame * pMainFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
CTileView * pTileView = pMainFrame->GetTileView();
// TODO: add draw code for native data here
//pDoc->m_Surface_map.Blt2DC(pDC->m_hDC,0,0);
CRect rc;
GetClientRect(rc);
pDC->DPtoLP(rc);
int top = rc.top / TERRAIN_WIDTH;
int left = rc.left / TERRAIN_HEIGHT;
int bottom = (rc.bottom-1) / TERRAIN_HEIGHT;
int right = (rc.right-1) / TERRAIN_WIDTH;
int w,h;
ESurface surface_main;
if(!surface_main.Create(rc.Width(),rc.Height(),SF_CLIP))
return;
surface_main.Clear(0,0,0);
EGroupPic& grp = pDoc->m_Grp_terrain ;
for(h=top;h<=bottom;h++)
{
for(w=left;w<=right;w++)
{
TERRAIN_INDEX terrain=pDoc->GetTerrainIndex(w,h);
if(terrain!=INVALID_TERRAIN_INDEX)
surface_main.Blt(grp,terrain,
w*TERRAIN_WIDTH-rc.left,
h*TERRAIN_HEIGHT-rc.top);
}
}
//_____________________________
EPG * pEpgs = pDoc->m_epg_obstacle;
top = rc.top / OBSTACLE_WIDTH;
left = rc.left / OBSTACLE_HEIGHT;
bottom = (rc.bottom-1) / OBSTACLE_HEIGHT;
right = (rc.right-1) / OBSTACLE_WIDTH;
for(h=top;h<=bottom;h++)
{
for(w=left;w<=right;w++)
{
OBSTACLE_ID obstacle_id=pDoc->GetObstacleID(w,h);
if(obstacle_id!=INVALID_OBSTACLE_ID && !IS_TERRAIN_OBSTACLE(obstacle_id))
{
surface_main.Blt(pEpgs[pDoc->ObstacleIndex(obstacle_id)],0,
w*OBSTACLE_WIDTH-rc.left,
h*OBSTACLE_HEIGHT-rc.top);
}
}
}
if(pTileView->TileType()!=TT_INVALID)
{
int tile_width,tile_height;
if(pTileView->TileType()==TT_OBSTACLE)
tile_width = tile_height = 24;
else
tile_width = tile_height = 48;
CRect CursorRect;
CursorRect.left = m_CursorPos.x * tile_width;
CursorRect.top = m_CursorPos.y * tile_height;
CursorRect.right = CursorRect.left + tile_width;
CursorRect.bottom = CursorRect.top + tile_height;
pDC->LPtoDP(CursorRect);
if(m_bEnableCursor)
surface_main.SetDrawColor(0,255,0);
else
surface_main.SetDrawColor(255,0,0);
for(w = CursorRect.left;w<CursorRect.right;w++)
{
surface_main.PutPixel(w,CursorRect.top);
surface_main.PutPixel(w,CursorRect.bottom);
}
for(h = CursorRect.top;h<CursorRect.bottom;h++)
{
surface_main.PutPixel(CursorRect.left,h);
surface_main.PutPixel(CursorRect.right,h);
}
}
surface_main.Blt2DC(pDC->m_hDC,rc.left ,rc.top);
}
void CMapView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CMapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
EMap& map = pDoc->m_map ;
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = map.Width()*TERRAIN_WIDTH;
sizeTotal.cy = map.Height()*TERRAIN_HEIGHT;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CMapView printing
BOOL CMapView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMapView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMapView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMapView diagnostics
#ifdef _DEBUG
void CMapView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMapView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMapDoc* CMapView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapDoc)));
return (CMapDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMapView message handlers
BOOL CMapView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
return TRUE;
//return CScrollView::OnEraseBkgnd(pDC);
}
void CMapView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
// TODO: Add your specialized code here and/or call the base class
if(lHint==UPDATE_ALLVIEW)
{
CMapDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
EMap& map = pDoc->m_map ;
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = map.Width()*TERRAIN_WIDTH;
sizeTotal.cy = map.Height()*TERRAIN_HEIGHT;
SetScrollSizes(MM_TEXT, sizeTotal);
SetScrollPos(SB_HORZ,0);
SetScrollPos(SB_VERT,0);
RedrawWindow();
}
}
void CMapView::OnMouseMove(UINT nFlags, CPoint point)
{
CMapDoc * pDoc = GetDocument();
CMainFrame * pMainFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
CTileView * pTileView = pMainFrame->GetTileView();
DrawCursorRect(point);
if(nFlags&MK_LBUTTON && m_bEnableCursor)
{
if(pTileView->TileType()==TT_TERRAIN){
pDoc->SetTerrian(m_CursorPos.x,m_CursorPos.y,
pTileView->CurrentTerrainID());
}else if(pTileView->TileType()==TT_OBSTACLE){
pDoc->SetObstacle(m_CursorPos.x,m_CursorPos.y,
pTileView->CurrentObstacleID());
}
RedrawWindow();
}
CScrollView::OnMouseMove(nFlags, point);
}
void CMapView::DrawCursorRect(CPoint point)
{
CRect CursorRect;
int tile_width,tile_height;
CMapDoc * pDoc = GetDocument();
CMainFrame * pMainFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
CTileView * pTileView = pMainFrame->GetTileView();
if(pTileView->TileType()==TT_INVALID){
m_bEnableCursor = FALSE;
return ;
}
CClientDC client_dc(this);
OnPrepareDC(&client_dc);
client_dc.DPtoLP(&point);
if(pTileView->TileType()==TT_TERRAIN)
{
TERRAIN_ID terrain_id;
terrain_id=pTileView->CurrentTerrainID();
tile_width=TERRAIN_WIDTH,tile_height=TERRAIN_HEIGHT;
m_CursorPos.x = point.x / tile_width;
m_CursorPos.y = point.y / tile_height;
m_bEnableCursor=pDoc->TestTerrain(m_CursorPos.x,m_CursorPos.y,terrain_id);
}
else if(pTileView->TileType()==TT_OBSTACLE)
{
tile_width=OBSTACLE_WIDTH,tile_height=OBSTACLE_HEIGHT;
m_CursorPos.x = point.x / tile_width;
m_CursorPos.y = point.y / tile_height;
m_bEnableCursor = pDoc->TestObstacle(m_CursorPos.x,m_CursorPos.y,
pTileView->CurrentObstacleID());
}
RedrawWindow();
}
void CMapView::OnLButtonDown(UINT nFlags, CPoint point)
{
CMapDoc * pDoc = GetDocument();
CMainFrame * pMainFrame = (CMainFrame*)AfxGetApp()->GetMainWnd();
CTileView * pTileView = pMainFrame->GetTileView();
DrawCursorRect(point);
if(m_bEnableCursor){
if(pTileView->TileType()==TT_TERRAIN){
pDoc->SetTerrian(m_CursorPos.x,m_CursorPos.y,
pMainFrame->GetTileView()->CurrentTerrainID());
}else if(pTileView->TileType()==TT_OBSTACLE){
pDoc->SetObstacle(m_CursorPos.x,m_CursorPos.y,
pTileView->CurrentObstacleID());
}
RedrawWindow();
//DrawCursorRect(point);
}
CScrollView::OnLButtonDown(nFlags, point);
}
BOOL CMapView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
// TODO: Add your message handler code here and/or call default
CPoint p;
p.x = GetScrollPos(SB_HORZ);
p.y = GetScrollPos(SB_VERT);
p.y -= zDelta/(WHEEL_DELTA/TERRAIN_HEIGHT);
ScrollToPosition(p);
return CScrollView::OnMouseWheel(nFlags, zDelta, pt);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?