www.pudn.com > CHECKER.zip > CHECKVW.CPP


// checkvw.cpp : implementation of the CCheckerView class 
// 
 
#include "stdafx.h" 
#include "checker.h" 
 
#include "checkdoc.h" 
#include "checkvw.h" 
#include "actdlg.h" 
#include "statuspg.h" 
#include "customco.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char BASED_CODE THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView 
 
IMPLEMENT_DYNCREATE(CCheckerView, CView) 
 
BEGIN_MESSAGE_MAP(CCheckerView, CView) 
        //{{AFX_MSG_MAP(CCheckerView) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_RBUTTONDOWN() 
	ON_COMMAND(IDM_STATUS, OnStatus) 
	ON_COMMAND(IDM_BACKGROUND, OnBackground) 
	//}}AFX_MSG_MAP 
        // Standard printing commands 
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) 
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView construction/destruction 
 
CCheckerView::CCheckerView() 
{ 
       m_background=RGB(0x80,0x80,0x80); 
 
} 
 
CCheckerView::~CCheckerView() 
{ 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView drawing 
 
 
void CCheckerView::DrawCell(CDC *dc,CCheckerDoc *doc,int x,int y) 
  { 
  CBrush white(RGB(0xFF,0xFF,0xFF)),black(RGB(0,0,0)),red(RGB(0xFF,0,0)); 
  CBrush grey(m_background),cyan(RGB(0,0xff,0xff)); 
  CBrush *old,*circ=NULL; 
  CPoint sel; 
  STATE state=doc->CellState(x,y); 
  old=dc->SelectObject(x%2==y%2?&white:&grey); 
  if (doc->GetSelect(sel)&&sel.x==x&&sel.y==y) 
    dc->SelectObject(&cyan);   // draw selection if appropriate 
  dc->Rectangle(x*75,y*50,(x+1)*75,(y+1)*50); 
  switch(state) 
    { 
	case CELL_RED: 
	case CELL_REDKING: 
	  circ=&red; 
	  break; 
	case CELL_BLACK: 
	case CELL_BLACKKING: 
	  circ=&black; 
	  break; 
	} 
  if (circ) 
    { 
	dc->SelectObject(circ); 
	dc->Ellipse(x*75+5,y*50+5,(x+1)*75-5,(y+1)*50-5); 
	} 
  if (state==CELL_REDKING||state==CELL_BLACKKING) 
    { 
	CPen *oldpen=(CPen *)dc->SelectStockObject(WHITE_PEN); 
	dc->Ellipse(x*75+10,y*50+10,(x+1)*75-10,(y+1)*50-10); 
	dc->SelectObject(oldpen); 
	} 
  dc->SelectObject(old); 
  } 
 
void CCheckerView::OnDraw(CDC* pDC) 
{ 
        CCheckerDoc* pDoc = GetDocument(); 
        ASSERT_VALID(pDoc); 
        int i,j; 
        for (i=0;i<8;i++) 
          for (j=0;j<8;j++) 
            DrawCell(pDC,pDoc,j,i); 
 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView printing 
 
BOOL CCheckerView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
        // default preparation 
        return DoPreparePrinting(pInfo); 
} 
 
void CCheckerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
        // TODO: add extra initialization before printing 
} 
 
void CCheckerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
        // TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView diagnostics 
 
#ifdef _DEBUG 
void CCheckerView::AssertValid() const 
{ 
        CView::AssertValid(); 
} 
 
void CCheckerView::Dump(CDumpContext& dc) const 
{ 
        CView::Dump(dc); 
} 
 
CCheckerDoc* CCheckerView::GetDocument() // non-debug version is inline 
{ 
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCheckerDoc))); 
        return (CCheckerDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
BOOL CCheckerView::Screen2Grid(CPoint &pt) 
  { 
  pt.x/=75; 
  pt.y/=50; 
  return TRUE; 
  } 
 
BOOL CCheckerView::Grid2Screen(CPoint &pt) 
  { 
  pt.x*=75; 
  pt.y*=50; 
  return TRUE; 
  } 
 
///////////////////////////////////////////////////////////////////////////// 
// CCheckerView message handlers 
 
 
 
void CCheckerView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	CCheckerDoc *doc=GetDocument(); 
	CPoint select; 
	Screen2Grid(point); 
	if (doc->GetSelect(select)) 
	  { 
	  if (select!=point) 
	    { 
		doc->movenum++; 
	    doc->SetCellState(point.x,point.y,doc->CellState(select.x,select.y)); 
	    doc->SetCellState(select.x,select.y,CELL_EMPTY);		 
		} 
	  doc->ClrSelect(); 
	  } 
	else 
	  if (doc->CellState(point.x,point.y)!=CELL_EMPTY)  
	    doc->SetSelect(point); 
	CView::OnLButtonDown(nFlags, point); 
} 
 
void CCheckerView::OnRButtonDown(UINT nFlags, CPoint point)  
{ 
	CCheckerDoc *doc=GetDocument(); 
	Screen2Grid(point); 
	STATE state=doc->CellState(point.x,point.y); 
	if (state==CELL_EMPTY) return; 
	ActionDialog action; 
	action.m_movenum=doc->movenum; 
	action.m_action=0; 
	 
	if (action.DoModal()==IDOK) 
	  { 
	  doc->movenum=action.m_movenum; 
	  switch (action.m_action) 
	    { 
		case 0: 
		  doc->SetCellState(point.x,point.y,CELL_EMPTY); 
          doc->ClrSelect(); 
		  break; 
 
		case 1: 
		  if (state==CELL_RED||state==CELL_BLACK) 
		    doc->SetCellState(point.x,point.y,(STATE)(state+2)); 
		  break; 
		} 
	  doc->ClrSelect(); 
	  doc->UpdateAllViews(NULL,(LPARAM)&point); 
      } 
	  CView::OnRButtonDown(nFlags, point); 
} 
 
void CCheckerView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)  
{ 
	if (!lHint) 
	  { 
	  CView::OnUpdate(pSender,lHint,pHint);	   
	  return; 
	  } 
	CPoint pt=*(CPoint *)lHint; 
	CSize sz(75,50); 
	Grid2Screen(pt); 
	CRect r(pt,sz); 
	InvalidateRect(&r); 
} 
 
void CCheckerView::OnPrepareDC(CDC* pDC, CPrintInfo* pInfo)  
{ 
	if (pDC->IsPrinting()) 
	  { 
	  pDC->SetMapMode(MM_ANISOTROPIC); 
	  pDC->ScaleViewportExt(2,1,2,1);  // double size 
	  } 
	CView::OnPrepareDC(pDC, pInfo); 
} 
 
void CCheckerView::OnStatus()  
{ 
    CCheckerDoc *doc=GetDocument(); 
	CPropertySheet sheet("Game Status",this); 
	StatusPage redpg(IDS_RED), blackpg(IDS_BLACK); 
	// Init page data 
	redpg.m_pieces.Format("%d",doc->Count(CELL_RED)); 
	redpg.m_kings.Format("%d",doc->Count(CELL_REDKING)); 
	blackpg.m_pieces.Format("%d",doc->Count(CELL_BLACK)); 
	blackpg.m_kings.Format("%d",doc->Count(CELL_BLACKKING)); 
	sheet.AddPage(&redpg); 
	sheet.AddPage(&blackpg); 
	sheet.DoModal(); 
		 
} 
 
void CCheckerView::OnBackground()  
{ 
	CCustomColor dlg(m_background,0,this); 
	if (dlg.DoModal()==IDOK) 
	  { 
	  if (dlg.m_reset) 
	    m_background=RGB(0x80,0x80,0x80); 
	  else 
	    m_background=dlg.GetColor(); 
	  InvalidateRect(NULL); 
	  } 
}