📄 sq3view.cpp
字号:
// Sq3View.cpp : implementation of the CSq3View class
//
#include "stdafx.h"
#include "Sq3.h"
#include "Sq3Doc.h"
#include "Sq3View.h"
#include "MainFrm.h" // for SetStatusText call
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSq3View
IMPLEMENT_DYNCREATE(CSq3View, CView)
BEGIN_MESSAGE_MAP(CSq3View, CView)
//{{AFX_MSG_MAP(CSq3View)
ON_COMMAND(ID_ACTIONS_RUN, OnActionsRun)
ON_UPDATE_COMMAND_UI(ID_ACTIONS_RUN, OnUpdateActionsRun)
ON_COMMAND(ID_ACTIONS_STOP, OnActionsStop)
ON_UPDATE_COMMAND_UI(ID_ACTIONS_STOP, OnUpdateActionsStop)
ON_COMMAND(ID_ACTIONS_REALLYFAST, OnActionsReallyfast)
ON_UPDATE_COMMAND_UI(ID_ACTIONS_REALLYFAST, OnUpdateActionsReallyfast)
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_FILE_NEW, OnFileNew)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSq3View construction/destruction
CSq3View::CSq3View()
{
running = true;
really_fast = false;
its_per_second=0.0F;
}
CSq3View::~CSq3View()
{
}
BOOL CSq3View::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSq3View drawing
void CSq3View::OnDraw(CDC* pDC)
{
CSq3Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if(!this->running || !this->really_fast)
{
// what scale for drawing fits in the client region?
// (really only need to recompute this on resize)
CRect r;
this->GetClientRect(r);
float scale_x = 0.9F*r.Width()/pDoc->GetGrid().GetSize().x;
float scale_y = 0.9F*r.Height()/pDoc->GetGrid().GetSize().y;
this->scale = min(scale_x,scale_y);
pDoc->GetGrid().Draw(pDC,scale);
}
if(this->running)
{
//if(!really_fast)
// Sleep(1); // need to allow a little time to respond to mouse messages, etc.
pDoc->GetGrid().DoTimeStep();
// report the number of iterations so far
const int REPORT_ITERATIONS_EVERY=10;
if(!really_fast || pDoc->GetGrid().GetIterations()%REPORT_ITERATIONS_EVERY==0)
{
CString its;
its.Format("Iterations: %d (%.0f its/s)",pDoc->GetGrid().GetIterations(),
its_per_second);
((CMainFrame*)AfxGetMainWnd())->SetMessageText(its);
}
const int N_ITS=10;
if(pDoc->GetGrid().GetIterations()%N_ITS==0)
{
this->old_time = this->new_time;
this->new_time = clock();
float elapsed = (float)(this->new_time-this->old_time) / CLOCKS_PER_SEC;
this->its_per_second = N_ITS/elapsed;
}
Invalidate(false); // ask for a redraw as soon as convenient (no need to erase the area)
}
}
/////////////////////////////////////////////////////////////////////////////
// CSq3View diagnostics
#ifdef _DEBUG
void CSq3View::AssertValid() const
{
CView::AssertValid();
}
void CSq3View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSq3Doc* CSq3View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSq3Doc)));
return (CSq3Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSq3View message handlers
void CSq3View::OnActionsRun()
{
this->running = true;
Invalidate();
}
void CSq3View::OnUpdateActionsRun(CCmdUI* pCmdUI)
{
// can start the simulation if it is not running already
pCmdUI->Enable(!this->running);
}
void CSq3View::OnActionsStop()
{
this->running = false;
}
void CSq3View::OnUpdateActionsStop(CCmdUI* pCmdUI)
{
// can stop the simulation if it is running
pCmdUI->Enable(this->running);
}
void CSq3View::OnActionsReallyfast()
{
this->really_fast = !this->really_fast;
}
void CSq3View::OnUpdateActionsReallyfast(CCmdUI* pCmdUI)
{
pCmdUI->Enable(this->running);
pCmdUI->SetCheck(this->really_fast);
}
void CSq3View::OnMouseMove(UINT nFlags, CPoint point)
{
// if stopped and moving over the window then report the status of the cell being pointed at
if(!running)
{
((CMainFrame*)AfxGetMainWnd())->SetMessageText(
GetDocument()->GetGrid().ProbeAt((float)point.x,(float)point.y,scale) );
}
CView::OnMouseMove(nFlags, point);
}
void CSq3View::OnFileNew()
{
this->running=false;
GetDocument()->OnNewDocument();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -