#include <Symbol.h>
#include <MyFrame.h>

void MyCustom(CObject* pParent, int nLeft, int nTop);


class CDialCtrl:public CObject
{
private:
	int m_nBoxColor;
	int m_nTotalDial;
	int m_nDial;
public:
	BOOL DrawArmature(int nNew, int nOld, BOOL bFill);
public:
    int GetDial(){return(m_nDial);}
    void SetDial(int nDial){m_nDial = nDial;};

    int GetTotalDial(){return(m_nTotalDial);}
    void SetTotalDial(int nDial){m_nTotalDial = nDial;};

    void SetBoxColor(int nColor){m_nBoxColor = nColor;}
public:
	BOOL Show();
	BOOL ShowNumber(int nNewDial);
public:
	CDialCtrl(CObject* pParent);
};

CDialCtrl::CDialCtrl(CObject* pParent):CObject(pParent)
{
	m_nTotalDial = 12;
    m_nDial = 0;

    m_nBackColor = WHITE;
    m_nForeColor = BLACK;
    m_nBoxColor =  CYAN;

}

BOOL CDialCtrl::DrawArmature(int nNew, int nOld, BOOL bFill)
{
    int nX,nY;

	RECT rc;
    this->GetScreenRect(&rc);

    int nCircleX = (rc.left + rc.right)/2;
    int nCircleY = (rc.top + rc.bottom)/2;
    int nRadius = (rc.bottom - rc.top + 1)/2;

    if (bFill)
    {
        ::SetColor(m_nBoxColor);
        ::SetFillStyle(1, m_nBackColor);
        ::FillEllipse(nCircleX, nCircleY,nRadius,nRadius);
        ::FillEllipse(nCircleX, nCircleY,nRadius-1,nRadius-1);
        ::FillEllipse(nCircleX, nCircleY,nRadius-2,nRadius-2);

        ::SetColor(m_nForeColor);
        for(int i = 0; i < m_nTotalDial; i++)
        {
            nX =   nCircleX + (int)((nRadius - 2) * cos(2.0 * CONST_DOUBLE_T * i / m_nTotalDial));
            nY =   nCircleY + (int)((nRadius - 2) * sin(2.0 * CONST_DOUBLE_T * i / m_nTotalDial));

            int nX2 =   nCircleX + (int)((nRadius - 6) * cos(2.0 * CONST_DOUBLE_T * i / m_nTotalDial));
            int nY2 =   nCircleY + (int)((nRadius - 6) * sin(2.0 * CONST_DOUBLE_T * i / m_nTotalDial));

            ::Line(nX, nY , nX2, nY2);
        }
    }

    if (nOld != -1)
    {
        ::SetColor(m_nBackColor);
        nX =   (int)((nRadius - 10) * cos(2.0 * CONST_DOUBLE_T * nOld / m_nTotalDial));
        nY =   (int)((nRadius - 10) * sin(2.0 * CONST_DOUBLE_T * nOld / m_nTotalDial));

        ::Line(nCircleX, nCircleY, nCircleX + nX, nCircleY + nY);
    }
    ::SetColor(m_nForeColor);
    nX =   (int)((nRadius - 10) * cos(2.0 * CONST_DOUBLE_T * nNew / m_nTotalDial));
    nY =   (int)((nRadius - 10) * sin(2.0 * CONST_DOUBLE_T * nNew / m_nTotalDial));

    ::Line(nCircleX, nCircleY, nCircleX + nX, nCircleY + nY);

    return(TRUE);
}

BOOL CDialCtrl::Show()
{
    struct viewporttype mView;
	BOOL bMouse = CObject::StartControlView(&mView);

	RECT rc;
    this->GetScreenRect(&rc);

    int nCircleX = (rc.left + rc.right)/2;
    int nCircleY = (rc.top + rc.bottom)/2;
    int nRadus = (rc.bottom - rc.top + 1)/2;

	this->DrawArmature(m_nDial, m_nDial, TRUE);

	CObject::EndControlView(&mView, bMouse);
	return(TRUE);
}

BOOL CDialCtrl::ShowNumber(int nNewDial)
{
	//Close Mouse And Set View = (0, 0 ,GetMaxX(), GetMaxY());
    struct viewporttype mView;
	BOOL bMouse = CObject::StartControlView(&mView);

	RECT rc;
    this->GetScreenRect(&rc);

    int nCircleX = (rc.left + rc.right)/2;
    int nCircleY = (rc.top + rc.bottom)/2;
    int nRadus = (rc.bottom - rc.top + 1)/2;

	this->DrawArmature(nNewDial, m_nDial, FALSE);

    m_nDial = nNewDial;

	CObject::EndControlView(&mView, bMouse);
	return(TRUE);

}
void far ClickDial_DialDemoDialog(CObject* pCurObj)
{
	CDialog* pDialog = (CDialog *)pCurObj->GetParent();
	CDialCtrl* pDialCtrl = (CDialCtrl *)pCurObj;


    AfxMessageBox("表盘", "你单击了表盘!!!");

}

void far OnTime_DialDemoDialog(CObject* pCurObj)
{
	CDialog* pDialog = (CDialog *)pCurObj;
	CDialCtrl* pDialCtrl1 = (CDialCtrl *)pDialog->ObjectFromID(2001);
	CDialCtrl* pDialCtrl2 = (CDialCtrl *)pDialog->ObjectFromID(2002);

    int nTotalDial = pDialCtrl1->GetTotalDial();
    int nDial = pDialCtrl1->GetDial();

    int nNewDial = (nDial + 1) % nTotalDial;
    pDialCtrl1->ShowNumber(nNewDial);

    nTotalDial = pDialCtrl2->GetTotalDial();
    nDial = pDialCtrl2->GetDial();

    nNewDial = (nDial + 1) % nTotalDial;
    pDialCtrl2->ShowNumber(nNewDial);

}

void far OnDraw_DialDemoDialog(CObject* pCurObj)
{
	CDialog* pDialog = (CDialog *)pCurObj;

    ::ChPrintf(10, 10 + 16 * 0, YELLOW, WHITE, 1, 0, "你好，这是一个表盘演示！");
    ::ChPrintf(10, 10 + 16 * 1, YELLOW, WHITE, 1, 0, "该摸块演示了如何使用窗口Draw回调函数！");
    ::ChPrintf(10, 10 + 16 * 2, YELLOW, WHITE, 1, 0, "该摸块演示了如何使用窗口Time回调函数！");
    ::ChPrintf(10, 10 + 16 * 3, YELLOW, WHITE, 1, 0, "该摸块演示了如何定制ActiveX控件！");

}



void far DialDemoDialog()
{
    CDialog* pDialog = new CDialog;
    pDialog->CreateWindow(0, 0 , 320,  380, "表盘演示");
    pDialog->Center();

    pDialog->SetTimeFc(OnTime_DialDemoDialog);
    pDialog->SetDrawFc(OnDraw_DialDemoDialog);

    CCloseButton* pCloseButton = new CCloseButton(pDialog);

    CDialCtrl* pDialCtrl1 = new CDialCtrl(pDialog);
    pDialCtrl1->CreateObject(170, 150, 100, 100, NULL);

    CDialCtrl* pDialCtrl2 = new CDialCtrl(pDialog);
    pDialCtrl2->CreateObject(50, 150, 100, 100, NULL);


    pDialCtrl1->SetShape(SHAPE_CIRCLE);
	pDialCtrl1->SetID(2001);
    pDialCtrl1->SetFc(ClickDial_DialDemoDialog);


    pDialCtrl2->SetBackColor(YELLOW);
    pDialCtrl2->SetBoxColor(BLACK);
    pDialCtrl2->SetTotalDial(48);
    pDialCtrl2->SetShape(SHAPE_CIRCLE);
	pDialCtrl2->SetID(2002);
    pDialCtrl2->SetFc(ClickDial_DialDemoDialog);

    ::MyCustom(pDialog, 50, 280);
    ::MyCustom(pDialog, 170, 280);

    pDialog->ShowWindow();
    pDialog->DoModal();
    delete pDialog;
}


//	::DrawArmature(320,320, 100, 0, 3, 20, TRUE);
