// balltimeView.cpp : implementation of the CBalltimeView class
//

#include "stdafx.h"
#include "balltime.h"

#include "balltimeDoc.h"
#include "balltimeView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView

IMPLEMENT_DYNCREATE(CBalltimeView, CView)

BEGIN_MESSAGE_MAP(CBalltimeView, CView)
        //{{AFX_MSG_MAP(CBalltimeView)
        ON_COMMAND(IDM_SHOW, OnShow)
        ON_WM_SIZE()
        ON_WM_CREATE()
        ON_WM_TIMER()
        ON_WM_DESTROY()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView construction/destruction

CBalltimeView::CBalltimeView()
{
        // TODO: add construction code here
        m_bDrawOn=FALSE;

}

CBalltimeView::~CBalltimeView()
{
}

BOOL CBalltimeView::PreCreateWindow(CREATESTRUCT& cs)
{
        // TODO: Modify the Window class or styles here by modifying
        //  the CREATESTRUCT cs

        return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView drawing

void CBalltimeView::OnDraw(CDC* pDC)
{
        CBalltimeDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView diagnostics

#ifdef _DEBUG
void CBalltimeView::AssertValid() const
{
        CView::AssertValid();
}

void CBalltimeView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CBalltimeDoc* CBalltimeView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBalltimeDoc)));
        return (CBalltimeDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CBalltimeView message handlers

void CBalltimeView::OnShow() 
{
        // TODO: Add your command handler code here
        if (m_bDrawOn)
                m_bDrawOn = FALSE ;  /* toggle drawing on/off */
        else
                m_bDrawOn = TRUE ;
        
}

void CBalltimeView::OnSize(UINT nType, int cx, int cy) 
{
        CView::OnSize(nType, cx, cy);
        
        // TODO: Add your message handler code here
        m_nXSize = cx ;
        m_nYSize = cy ;
        
}

int CBalltimeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        
        // TODO: Add your specialized creation code here
        SetTimer(1,50,NULL);
        
        return 0;
}

void CBalltimeView::OnTimer(UINT nIDEvent) 
{
        // TODO: Add your message handler code here and/or call default
        if (m_bDrawOn) DrawBall();

        
        CView::OnTimer(nIDEvent);
}

void CBalltimeView::DrawBall()
{


/* DrawBall() draws a red ball on the window hWnd every time the */
/* function is called.  The ball is BALLRAD in radius.  The walls */
/* (client area size) are from 0,0 to nXSize,nYsize.  The location */
/* is moved each time by VELOCITY units. The signs of the X and Y */
/* velocity terms nVelX,nVelY are reversed each time the ball gets */
/* within MINRAD units of a wall.  This causes the ball to appear */
/* to bounce off the wall.  Drawing the ball at the new location */
/* causes the old ball to be erased, as the ball is drawn using a */
/* white pen for the outline that is 1.5 * VELOCITY in width.  The */
/* white pen ends up painting over any portion of the ball that is */
/* otherwise visible from the previous location. */


        static  int bX = 2*BALLRAD, bY = 2*BALLRAD, /* ball start X,Y */
                bVelX = VELOCITY, bVelY = VELOCITY; /* X,Y velocity */

        bX = bX + bVelX ;        /* compute new center X,Y of ball */
        bY = bY + bVelY ;

        if (bY > m_nYSize)              /* check if user moved walls and hid ball */
                bY = 2 * BALLRAD ;  /* put the ball back at the starting.. */
        if (bX > m_nXSize)      /* position if it is not going to be.. */
                bX = 2 * BALLRAD ;  /* visible with the new window size */

        if (bY < MINRAD || m_nYSize - bY < MINRAD)  /* check if hit wall */
                bVelY = -1 * bVelY ;                    /* if so, change direction */
        if (bX < MINRAD || m_nXSize - bX < MINRAD)
                bVelX = -1 * bVelX ;

        pDC = GetDC () ;    /* get a device context */

        /* Create a thick white pen and a red brush */
        CPen newPen(PS_SOLID, VELOCITY+VELOCITY/2, RGB(255,255,255));
        CBrush newBrush(RGB (255, 0, 0)) ;

        CPen* pOldPen = pDC->SelectObject (&newPen) ;      /* select into the DC */
        CBrush* pOldBrush = pDC->SelectObject (&newBrush) ;

        /* draw the ball in the new location */
        /* the outline pen deletes around ball */
        pDC->Ellipse (bX-BALLRAD, bY-BALLRAD, bX+BALLRAD, bY+BALLRAD) ;

        /* select objects out of DC */
        pDC->SelectObject (pOldPen) ;
        pDC->SelectObject (pOldBrush) ;

}

void CBalltimeView::OnDestroy() 
{
        CView::OnDestroy();
        
        // TODO: Add your message handler code here
        KillTimer(1);
}