MFC-Simulator/TestSimulator/BlockFun.cpp

257 lines
7.9 KiB
C++
Raw Normal View History

2023-02-03 03:07:52 +01:00
#include "stdafx.h"
#include "BlockFun.h"
#define PIN_RADIUS 8 //Pin<69>b<EFBFBD>|
#define GOLD RGB (255, 215, 0) //Pin<69>}<7D>C<EFBFBD><43>
#define BLACK RGB (0, 0, 0) //<2F>s<EFBFBD>u<EFBFBD>C<EFBFBD><43>
#define FALSE_VALUE 10 //FALSE<53>^<5E>ǭ<EFBFBD>
#define TRUE_VALUE 11 //TRUE<55>^<5E>ǭ<EFBFBD>
#define NO_VALUE -10 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>J<EFBFBD>T<EFBFBD><54>
#define NO_OPER_FLAG -11 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD>B<EFBFBD>
#define NO_INPUT_FLAG -12 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>J<EFBFBD>i<EFBFBD><69>
#define COMPUTE_ERROR -13 //<2F><><EFBFBD><EFBFBD><EFBFBD>\<5C><><EFBFBD>B<EFBFBD><42>
#define NO_BLOCK_HEAD -14 //<2F><><EFBFBD>s<EFBFBD>b<EFBFBD>s<EFBFBD><73>Block
#define PLUS 1 // + flag
#define MINUS 2 // - flag
#define MULTI 3 // * flag
#define DIV 4 // / flag
#define DIGITAL_VALUE 4 //<2F>Ʀ<EFBFBD><C6A6>T<EFBFBD><54>flag
#define ANALOG_VALUE 5 //<2F><><EFBFBD><EFBFBD><EFBFBD>T<EFBFBD><54>flag
#define BLK_FUN 6 //Block<63><6B><EFBFBD><EFBFBD><EFBFBD>s<EFBFBD><73>
CBlockFun::CBlockFun ()
{
m_rcPinIn1 = CRect ();
m_rcPinIn2 = CRect ();
m_rcPinOut = CRect ();
m_pBlkHead1 = NULL;
m_pBlkHead2 = NULL;
m_iBlkHead1Num = NO_BLOCK_HEAD;
m_iBlkHead2Num = NO_BLOCK_HEAD;
m_iOperFlag = NO_OPER_FLAG;
}
CBlockFun::~CBlockFun ()
{
}
void CBlockFun::SetBlkRect (CPoint ptCenter, int iWidth, int iHeight)
{
CBlockBasis::SetBlkRect (ptCenter, iWidth, iHeight);
m_rcPinIn1.left = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn1.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn1.right = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn1.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinIn2.left = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn2.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn2.right = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn2.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinOut.left = m_rcBlk.CenterPoint ().x - PIN_RADIUS;
m_rcPinOut.top = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinOut.right = m_rcBlk.CenterPoint ().x + PIN_RADIUS;
m_rcPinOut.bottom = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) + PIN_RADIUS;
}
CRect CBlockFun::GetPinIn1Rect () const
{
return m_rcPinIn1;
}
CRect CBlockFun::GetPinIn2Rect () const
{
return m_rcPinIn2;
}
CRect CBlockFun::GetPinOutRect () const
{
return m_rcPinOut;
}
CBlockBasis* CBlockFun::GetBlkHead1Ptr () const
{
return m_pBlkHead1;
}
CBlockBasis* CBlockFun::GetBlkHead2Ptr () const
{
return m_pBlkHead2;
}
void CBlockFun::SetBlkHead1Ptr (CBlockBasis* pBlkHead1)
{
m_pBlkHead1 = pBlkHead1;
}
void CBlockFun::SetBlkHead2Ptr (CBlockBasis* pBlkHead2)
{
m_pBlkHead2 = pBlkHead2;
}
int CBlockFun::GetBlkHead1Num () const
{
return m_iBlkHead1Num;
}
int CBlockFun::GetBlkHead2Num () const
{
return m_iBlkHead2Num;
}
void CBlockFun::SetBlkHead1Num (int iHeadBlkNum)
{
m_iBlkHead1Num = iHeadBlkNum;
}
void CBlockFun::SetBlkHead2Num (int iHeadBlkNum)
{
m_iBlkHead2Num = iHeadBlkNum;
}
double CBlockFun::GetBlkValue () const
{
switch (m_iOperFlag)
{
case PLUS: //<2F>N<EFBFBD><4E><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ȭۥ[
return m_pBlkHead1->GetBlkValue () + m_pBlkHead2->GetBlkValue ();
break;
case MINUS: //<2F>N<EFBFBD><4E><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ȭ۴<C8AC>
return m_pBlkHead1->GetBlkValue () - m_pBlkHead2->GetBlkValue ();
break;
case MULTI: //<2F>N<EFBFBD><4E><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ȭۭ<C8AC>
return m_pBlkHead1->GetBlkValue () * m_pBlkHead2->GetBlkValue ();
break;
case DIV: //<2F>N<EFBFBD><4E><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>Ȭ۰<C8AC>
return m_pBlkHead1->GetBlkValue () / m_pBlkHead2->GetBlkValue ();
break;
default:
return NO_VALUE;
break;
}
}
int CBlockFun::GetValueFlag () const
{
if (m_pBlkHead1 == NULL || m_pBlkHead2 == NULL) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>I<EFBFBD><49><EFBFBD>s<EFBFBD><73><EFBFBD>A<EFBFBD>^<5E><>"<22>L<EFBFBD><4C>"
return NO_VALUE;
else if (m_pBlkHead1->GetValueFlag () == NO_VALUE || m_pBlkHead2->GetValueFlag () == NO_VALUE) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>L<EFBFBD>ȡA<C8A1>^<5E><>"<22>L<EFBFBD><4C>"
return NO_VALUE;
else if (m_pBlkHead1->GetValueFlag () == COMPUTE_ERROR || m_pBlkHead2->GetValueFlag () == COMPUTE_ERROR) //<2F>Y<EFBFBD>s<EFBFBD>b<EFBFBD><62><EFBFBD>@<40><><EFBFBD>J<EFBFBD><4A><EFBFBD>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>~<7E>A<EFBFBD>^<5E><>"<22>p<EFBFBD><70><EFBFBD><EFBFBD><EFBFBD>~"
return COMPUTE_ERROR;
else if (m_pBlkHead1->GetValueFlag () == DIGITAL_VALUE || m_pBlkHead2->GetValueFlag () == DIGITAL_VALUE) //<2F><><EFBFBD><EFBFBD><EFBFBD>\<5C>Ʀ<EFBFBD><C6A6><EFBFBD><EFBFBD>J
return COMPUTE_ERROR;
else if (m_iOperFlag == NO_OPER_FLAG) //<2F><><EFBFBD>]<5D>w<EFBFBD>B<EFBFBD>
return NO_OPER_FLAG;
else if (m_pBlkHead1->GetValueFlag () == NO_OPER_FLAG || m_pBlkHead2->GetValueFlag () == NO_OPER_FLAG) //<2F><><EFBFBD>]<5D>w<EFBFBD>B<EFBFBD>
return NO_OPER_FLAG;
else if (m_pBlkHead1->GetValueFlag () == ANALOG_VALUE && m_pBlkHead2->GetValueFlag () == ANALOG_VALUE) //<2F>Y<EFBFBD><59><EFBFBD>J<EFBFBD>Ҭ<EFBFBD><D2AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T<EFBFBD><54><EFBFBD>A<EFBFBD>h<EFBFBD>^<5E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>T<EFBFBD><54>
return ANALOG_VALUE;
else
return NO_VALUE;
}
int CBlockFun::GetOperFlag () const
{
return m_iOperFlag;
}
void CBlockFun::SetOperFlag (int iOperFlag)
{
m_iOperFlag = iOperFlag;
}
void CBlockFun::Move (CPoint ptCursor)
{
CBlockBasis::Move (ptCursor);
m_rcPinIn1.left = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn1.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn1.right = m_rcBlk.CenterPoint ().x - int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn1.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinIn2.left = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) - PIN_RADIUS;
m_rcPinIn2.top = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinIn2.right = m_rcBlk.CenterPoint ().x + int (m_rcBlk.Width () / 4) + PIN_RADIUS;
m_rcPinIn2.bottom = m_rcBlk.CenterPoint ().y - int (m_rcBlk.Height () / 2) + PIN_RADIUS;
m_rcPinOut.left = m_rcBlk.CenterPoint ().x - PIN_RADIUS;
m_rcPinOut.top = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) - PIN_RADIUS;
m_rcPinOut.right = m_rcBlk.CenterPoint ().x + PIN_RADIUS;
m_rcPinOut.bottom = m_rcBlk.CenterPoint ().y + int (m_rcBlk.Height () / 2) + PIN_RADIUS;
}
void CBlockFun::Draw (CDC* pDC)
{
CBrush brushPin (GOLD), * pOldBrush;
pOldBrush = pDC->SelectObject (&brushPin);
pDC->Ellipse (m_rcPinIn1);
pDC->Ellipse (m_rcPinIn2);
pDC->Ellipse (m_rcPinOut);
pDC->SelectObject (pOldBrush);
CBlockBasis::Draw (pDC);
pDC->DrawTextA (_T ("FUN"), -1, &m_rcBlk, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
void CBlockFun::DrawLine (CDC* pDC)
{
if (m_pBlkHead1 != NULL)
{
CPen penLine (PS_SOLID, 5, BLACK);
CPen* pOldPen;
pOldPen = pDC->SelectObject (&penLine);
CPoint ptStart = m_pBlkHead1->GetPinOutRect ().CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD>_<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>JBlock<63><6B><EFBFBD><EFBFBD><EFBFBD>XPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
CPoint ptEnd = m_rcPinIn1.CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD>Block<63><6B><EFBFBD><EFBFBD><EFBFBD>JPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
int iGrid = (int) (m_rcBlk.Height () / 2. + 0.5); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD>j
int iCornerGridY = (int) (((ptStart.y + ptEnd.y) / 2. - ptStart.y) / iGrid + 0.5); //<2F>p<EFBFBD><70><EFBFBD>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>Z<EFBFBD><5A><EFBFBD><EFBFBD><EFBFBD>JBlock<63>X<EFBFBD>ӹj<D3B9>I
int iCornerY = iCornerGridY * iGrid + ptStart.y; //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>BY<42>y<EFBFBD>Ц<EFBFBD><D0A6>m
pDC->MoveTo (ptStart);
pDC->LineTo (CPoint (ptStart.x, iCornerY));
pDC->MoveTo (CPoint (ptStart.x, iCornerY));
pDC->LineTo (CPoint (ptEnd.x, iCornerY));
pDC->MoveTo (CPoint (ptEnd.x, iCornerY));
pDC->LineTo (ptEnd);
pDC->SelectObject (pOldPen);
}
if (m_pBlkHead2 != NULL)
{
CPen penLine (PS_SOLID, 5, BLACK);
CPen* pOldPen;
pOldPen = pDC->SelectObject (&penLine);
CPoint ptStart = m_pBlkHead2->GetPinOutRect ().CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD>_<EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD><EFBFBD>JBlock<63><6B><EFBFBD><EFBFBD><EFBFBD>XPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
CPoint ptEnd = m_rcPinIn2.CenterPoint (); //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD>I<EFBFBD><49><EFBFBD><EFBFBD>Block<63><6B><EFBFBD><EFBFBD><EFBFBD>JPin<69>}<7D><><EFBFBD><EFBFBD><EFBFBD>I
int iGrid = (int) (m_rcBlk.Height () / 2. + 0.5); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>I<EFBFBD><49><EFBFBD>j
int iCornerGridY = (int) (((ptStart.y + ptEnd.y) / 2. - ptStart.y) / iGrid + 0.5); //<2F>p<EFBFBD><70><EFBFBD>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>Z<EFBFBD><5A><EFBFBD><EFBFBD><EFBFBD>JBlock<63>X<EFBFBD>ӹj<D3B9>I
int iCornerY = iCornerGridY * iGrid + ptStart.y; //<2F>s<EFBFBD>u<EFBFBD><75><EFBFBD><EFBFBD><EFBFBD>BY<42>y<EFBFBD>Ц<EFBFBD><D0A6>m
pDC->MoveTo (ptStart);
pDC->LineTo (CPoint (ptStart.x, iCornerY));
pDC->MoveTo (CPoint (ptStart.x, iCornerY));
pDC->LineTo (CPoint (ptEnd.x, iCornerY));
pDC->MoveTo (CPoint (ptEnd.x, iCornerY));
pDC->LineTo (ptEnd);
pDC->SelectObject (pOldPen);
}
}
int CBlockFun::BlkTypeIs () const
{
return BLK_FUN;
}