// CASE0.cpp :
//
#define  __cplusplus
#include <stdlib.h>
#include "stdafx.h"
#include "tutorial.h"
#include "mainfrm.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
//paint Case 0

extern BOOL g_mbCanDoNext;                        
extern BOOL g_bAuto;
extern BOOL g_bAutoBtn;
extern BOOL g_bBasicFirst;
extern void Delay(int nDelay,BOOL bShow=0);
/*-------------------------------------------------------*/
/*        paint case0 background                         */
/*-------------------------------------------------------*/
void CMainFrame::PaintCase0(CDC &dc)
{ 
  if(bDrawBtn)
    {        
     bDrawBkg=1;
     bDrawBtn=0;
     return;
    }
  m_pBitText=new CBitmap;
  CRect   rectDText1(35,58,35+213,58+74);  
  CRect   rectSText1(0,0,213,74);
  CRect   rectDText2(28,156,28+262,156+28);
  CRect   rectSText2(0,0,262,28);
  CRect   rectWin;        
  CRect   rectWnd;
//////////////////////////////////////////////////////////  
  GetWindowRect(&rectWin);
  rectWnd.SetRect(0,0,rectWin.Width(),rectWin.Height());
  
  m_pBitText->LoadBitmap(IDB_BITMAP7);
  PaintWall(dc,rectWnd,m_pBitmap);
  TransparentBitmap(dc,rectWnd,m_pTBitmap,(LPSTR)MAKEINTRESOURCE(IDB_BITMAP6));
//  PaintBitmap(dc,rectDText1,rectSText1,m_pBitText);
  m_pBitText->DeleteObject();
  
  m_pBitText->LoadBitmap(IDB_BITMAP8);
//  PaintBitmap(dc,rectDText2,rectSText2,m_pBitText);                
    
/////////////////////////////////////////////////////////////////    
  m_pBitText->DeleteObject();
  delete m_pBitText;                      
  if(bDrawBkg)
   {               
    DrawControl(dc,posLine.x,posLine.y);
//    rectMenu.SetRect(rectWnd.Width()/640*370,rectWnd.Height()/480*200,0,0);
    DrawTPmap(dc,rectMenu,IDB_BITMAP14,IDB_BITMAP15);
    if(g_bAutoBtn)
       DrawTPmap(dc,m_AutoBtn,IDB_AUTOC,IDB_AUTOM);
    if(m_pExitBtn)         
      {    
       m_pExitBtn->Invalidate(0);
       m_pExitBtn->UpdateWindow();
//       m_pExitBtn->MoveWindow(rectBtn.left,rectBtn.top,rectBtn.Width(),rectBtn.Height());
//       m_pExitBtn->ShowWindow(SW_SHOW);
       }
    }      
   

/////////////////////////////////////////////////////////////////  
 static int y=0;
 if(!y)
  {      
   UINT delay;
   y++;
   bFirst=1;     
   bClose=0;
   delay=55;
   SetMouse(0);
   SetFullKey(0);
   SetTimer(UINT(TIMER),delay,NULL);	
   m_pBit     =   new CBitmap;
   m_pBit->LoadBitmap(IDB_BITMAP9);//Load bug bitmap
   m_pMonoBit  =  new CBitmap;
   m_pMonoBit->LoadBitmap(IDB_BITMAP10);
   AfxGetApp()->DoWaitCursor(1);
  } 

}

/*-------------------------------------------------------*/
/*             case 0 moving picture  100ms a time       */ 
/* The function is draw moving picture and restore BKG   */
/*-------------------------------------------------------*/ 
void CMainFrame::Timer(UINT nIDEvent)
{     
 if(nIDEvent!=TIMER)
    return;

 static       y=0;
 if(y<0)
   return;
 y++;
 if(y>4 && y<16)
   return;
 BITMAP       bm;
 CRect        rectWin;
 CRect        rectWnd; 
 CWindowDC    dc(this);
 CBitmap      *pOldBitmap;
 CDC          *pStoreBitDC = new CDC; 
 GetWindowRect(&rectWin);            
 static int   nHeight=int(rectWin.Height()/480)*420;

 m_pBit->GetObject(sizeof(BITMAP),&bm);
 static CRect rectDest(0-bm.bmWidth,nHeight,0,int(nHeight+bm.bmHeight));
 CRect        rectSrc(0,0,bm.bmWidth,bm.bmHeight);
 rectWnd.SetRect(0,0,rectWin.Width(),rectWin.Height());
///////////////////////////////////////////////////////////
   
 if(!bFirst) // Store background    
   {   
    PaintBitmap(dc,rectDest,rectSrc,m_pStoreBit);
    m_pStoreBit->DeleteObject();
    m_pStoreBit->CreateCompatibleBitmap(&dc,bm.bmWidth,bm.bmHeight);
   } 
 else 
   {      
     bFirst=0;
     m_pStoreBit->CreateCompatibleBitmap(&dc,bm.bmWidth,bm.bmHeight);
   } 
 rectDest.left+=bm.bmWidth/4;
 rectDest.right+=bm.bmWidth/4;
 // save current background;
 pStoreBitDC->CreateCompatibleDC(&dc);
 pOldBitmap = (CBitmap *)pStoreBitDC->SelectObject(m_pStoreBit);
 pStoreBitDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,
                     &dc,rectDest.left,rectDest.top,SRCCOPY);//rectDest.left,rectDest.top,SRCCOPY));  
 MovingBitmap(dc,rectDest,m_pBit,m_pMonoBit);            
 posLine.x=rectDest.left+2;
 posLine.y=int(nHeight+bm.bmHeight/2-6);
 DrawControl(dc,posLine.x,posLine.y);
 
 pStoreBitDC->SelectObject(pOldBitmap);
 pStoreBitDC->DeleteDC();
 delete pStoreBitDC;
 
 if(rectDest.left>=rectWnd.right)
   {          
    y=-100;
    KillTimer(UINT(TIMER));
    m_pBit->DeleteObject();
    delete m_pBit;   
    m_pMonoBit->DeleteObject();
    delete m_pMonoBit;
    m_pStoreBit->DeleteObject();
/////////////////////////////////////
//    delete m_pStoreBit;
//    m_pStoreBit=0;
////////////////////////////////////    
    m_pExitBtn = new CButton();           
    CRect rectBtn;
    rectBtn.left  = int(rectWnd.Width()*3/4);
    rectBtn.top   = int(posLine.y-29)-19;
    rectBtn.right = rectBtn.left+100;
    rectBtn.bottom= rectBtn.top+20;

    m_pExitBtn->Create("&Exit Tutorial",WS_CHILD|WS_VISIBLE|WS_TABSTOP,rectBtn,this,ID_APP_EXIT);
     
    bDrawBtn=1;
    bClose=1;
    rectMenu.SetRect(rectWnd.Width()/640*370,rectWnd.Height()/480*200,0,0);
    DrawTPmap(dc,rectMenu,IDB_BITMAP14,IDB_BITMAP15);
    m_AutoBtn.SetRect(rectMenu.left,rectBtn.top-100,
                      rectMenu.left+87, rectBtn.top-100+50); 
    DrawTPmap(dc,m_AutoBtn,IDB_AUTOC,IDB_AUTOM);
    SetMouse(1); 
    SetFullKey(1);
    AfxGetApp()->DoWaitCursor(-1);
   } 
}   
/*-------------------------------------------------------*/
/*              Draw Lines & button                      */
/*-------------------------------------------------------*/
void CMainFrame::DrawControl(CDC &dc,int nXCurrent,int nYHeight)
{
 CPen penBlue(PS_SOLID,2,RGB(128,128,0));//0,128,128));
 CPen penGreen(PS_SOLID,2,RGB(128,128,0));
 CPen penRed(PS_SOLID,2,RGB(128,128,0));//,0));
 //////////////////////////////////////////////////////////
 DrawLine(dc,penBlue ,nXCurrent,nYHeight);
 DrawLine(dc,penGreen,nXCurrent,nYHeight+4);
 DrawLine(dc,penRed  ,nXCurrent,nYHeight+8);
}                                    
 
/*-------------------------------------------------------*/
/*       case 0 mouse move massage handler code          */
/*-------------------------------------------------------*/ 
void CMainFrame::Case0MouseMove(UINT nFlags,CPoint point)
{ 
 HCURSOR hHandCur=0;     
/* 
 ClientToScreen(&point);
 point.x-=WinCxy.x;
 point.y-=WinCxy.y;
*/                      
 if(( m_AutoBtn.PtInRect(point)&& g_bAutoBtn)||
    (point.x>=rectMenu.left && point.x<=rectMenu.right)&&(
    (point.y>=rectMenu.top && point.y<=rectMenu.top+16)||
    (point.y>=rectMenu.top+16*2 && point.y<=rectMenu.top+16*3)||
    (point.y>=rectMenu.top+16*4 && point.y<=rectMenu.top+16*5)))
   { 
    hHandCur=hCursor1;
   } 
  else
   { 
    hHandCur=hArrow;
   }
    
  ::SetCursor(hHandCur);
}                

/*-------------------------------------------------------*/
/*       case 0 mouse Left down massage handler code     */
/*-------------------------------------------------------*/ 
void CMainFrame::Case0MLD(UINT nFlags,CPoint point)
{       
  static BOOL bSet=0; 
  if(g_mbCanDoNext==0)
     return;
  ClientToScreen(&point);           
  point.x-=WinCxy.x;
  point.y-=WinCxy.y;
  CWindowDC dc(this);
 if(m_AutoBtn.PtInRect(point) && !g_bAuto)
   {          
       g_bAuto=1;
       //SetTimer(IDAUTOBTN,100,NULL);
       //DrawTPmap(dc,m_AutoBtn,IDB_AUTOF,IDB_AUTOM);
       
       SetSysMouse(1);
       SetSysKey(1);
       bSet=1;  
       PostMessage(WM_COMMAND,ID_CASE0_FIRST_LOOK,0L);    
       return;
   }
 if(g_bAuto)
    return;
 if((point.x>=rectMenu.left && point.x<=rectMenu.right)&&
    (point.y>=rectMenu.top && point.y<=rectMenu.top+16))
   { 
    SendMessage(WM_COMMAND,ID_CASE0_FIRST_LOOK,0L);    
    return;
   }
   
  if((point.x>=rectMenu.left && point.x<=rectMenu.right)&&
    (point.y>=rectMenu.top+16*2 && point.y<=rectMenu.top+16*3))
   {
    SendMessage(WM_COMMAND,ID_CASE0_BASIC,0L);
    return;
   }                                                        
   
  if((point.x>=rectMenu.left && point.x<=rectMenu.right)&&
    (point.y>=rectMenu.top+16*4 && point.y<=rectMenu.top+16*5))
   {
    SendMessage(WM_COMMAND,ID_CASE0_ADVANCE,0L);
    return;
   }

}
 
void CMainFrame::OnCase0Basic()
{
	// TODO: Add your command handler code here
	SetSysKey(1); 
	g_bAutoBtn=0;
	FilterTaskChange(0);                  
	SetTipActive(1);
	m_pExitBtn->DestroyWindow();

///////////////////////////////////////////////////////////////	
	m_uNextCaseID=ID_CASE_SET_MAP;
	m_uCase=ID_CASE_SET_MAP;
	m_nSourceWnd=0;
    m_nVariableWnd=0;
	m_pDlg=0;
	m_pWnd=0;                       
	m_pVarWnd=0;
	m_bTipActive=1;     
	m_bPostToMain=0;
	g_mbCanDoNext=1;
	m_pMeter=0;
	m_bSetMap_Action=1;
	bPaintMsg=1;//paint set map message box
	bCase2Paint=1;
	m_uNextAction=0;
	OverMsg.rectOld.SetRect(0,0,0,0);
    StepMsg.rectOld.SetRect(0,0,0,0);

//	DrawMenuBar();

    SendMessage(WM_COMMAND,ID_VIEW_STATUS_BAR);
    SendMessage(WM_COMMAND,ID_VIEW_TOOLBAR);
    //Delay(50);
  
   	lstrcpy(szCaseTitle,"Set map");
	
////////////////////////////////////////////////////////
// initial case 2;
    m_uColorID=IDB_BITMAP18;
    m_uMonoID=IDB_BITMAP19;
    m_uMsgID=IDS_SET_MAP_MSG1;
    OverMsg.bOverMsg=0;
    StepMsg.bOverMsg=0;
    bByUser=0;              
///////////////////////////////////////////////////////////////////////                

    if(!(CWnd::FromHandle(m_hWndMDIClient)->IsWindowVisible()))
       {(CWnd::FromHandle(m_hWndMDIClient))->ShowWindow(SW_SHOW);
        (CWnd::FromHandle(m_hWndMDIClient))->EnableWindow(TRUE);}
    Delay(55);

    CRect rectWin;   
    CRect rectBtn;

    GetWindowRect(&rectWin);
    rectBtn.SetRect(380+WinCxy.x-15,
                    370+WinCxy.y+7,
                    450+WinCxy.x-15,
                    390+WinCxy.y+7);

    m_pCancelBtn = new CButton;
    m_pNextBtn = new CButton;
    m_pCancelBtn->Create("Skip",WS_CHILD|WS_TABSTOP,rectBtn,this,ID_CANCEL);
    rectBtn.left+=160;
    rectBtn.right=rectBtn.left+70;
    m_pNextBtn->Create("Next >>",WS_CHILD|WS_TABSTOP,rectBtn,this,ID_NEXT);
	bPaintMsg=1;//paint set map message box
	bCase2Paint=0;           
	Invalidate();
    RedrawWindow(NULL,NULL,RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|
                           RDW_ALLCHILDREN|RDW_UPDATENOW);
    UpdateWindow();                       
    if(g_bAuto)
    {
      Delay(500); 
      PostMessage(WM_COMMAND,ID_NEXT);
    }
    else
    g_bAutoBtn=1;  
  
    bCase2Paint=1;
    
    m_pCancelBtn->ShowWindow(SW_SHOW);
    m_pNextBtn->ShowWindow(SW_SHOW);

    if(g_bBasicFirst==0){
       bCase2Paint=1;
       CWindowDC dc(this);
       PaintSetMapMsg(dc);}
    if(g_bAuto)   
      {               
       SetSysKey(1);
       SetSysMouse(1);
       Invalidate();
       UpdateWindow();
      }
   // Invalidate();
    //UpdateWindow();                       
    
}

void CMainFrame::OnCase0FirstLook()
{
	// TODO: Add your command handler code here
	StartFirstLook();
}

void CMainFrame::OnCase0Advance()
{
	// TODO: Add your command handler code here 
	StartAdvance();
}


void CMainFrame::InitMenuItem(CMenu *pAppMenu)
{
 UINT  nItem = pAppMenu->GetMenuItemCount();
 CMenu *pMenu;
 for(UINT x=0;x<nItem;x++)
    {
     pMenu=pAppMenu->GetSubMenu(x);
     for(UINT y=0;y<pMenu->GetMenuItemCount();y++)
         pMenu->EnableMenuItem(y,MF_BYPOSITION|MF_ENABLED);
    }
}