// Advance.cpp : implementation of the CMainFrame class
//
#include "stdafx.h"
#include "tutorial.h"
#include "tmouse.h"
#include "mainfrm.h"
#include "time.h"
                    
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif                       
extern BOOL       g_bCanDoExit;
extern BOOL       g_bAuto;
extern BOOL       g_bAutoBtn;
extern void Delay(int nDelay,BOOL bShow=0);
extern BOOL       g_mbCanDoNext;
extern BOOL       g_bBasicFirst;

/*-------------------------------------------------------*/
/*                   Paint Window Map                    */
/*-------------------------------------------------------*/
void CMainFrame::PaintUpBitmap(int orgx,
                               int orgy,
                               LPSTR lpBitmap,
                               BOOL bMove/*=1*/)
{
 BITMAP bm;
 MSG msg;
 CWindowDC dc(this);
 CDC       SrcDC;
 HBITMAP hBitmap=::LoadBitmap(m_hFlookInst,lpBitmap);//"TRACE_ONLY");
                               
 ::GetObject(hBitmap,sizeof(BITMAP),&bm);
 m_rectDes.SetRect(orgx,orgy,orgx+bm.bmWidth,orgy+bm.bmHeight); 
 SrcDC.CreateCompatibleDC(&dc);
 SrcDC.SelectObject(CBitmap::FromHandle(hBitmap));
 int org=bMove ? 0 : (bm.bmHeight-1);
 for(int i=org;i<bm.bmHeight;i+=4)
   {
    if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
       DispatchMessage(&msg);
       
    dc.BitBlt(orgx,orgy+bm.bmHeight-i,bm.bmWidth,i,
              &SrcDC,0,0,SRCCOPY);
   } 
 SrcDC.DeleteDC();
 DeleteObject(hBitmap);
 
} 
/*-------------------------------------------------------*/
/*                   Paint Advance                       */
/*-------------------------------------------------------*/
void CMainFrame::PaintAdvance(CDC &dc)
{ 
  if(!bCase2Paint )
    {bCase2Paint=1;
     return;}
     
  CRect rectWin;                             
  GetWindowRect(&rectWin);
  PaintWall(dc,rectWin,m_pBitmap);
  SetBottomToolBar(dc);
  if(m_NextBtn.IsWindowVisible())
    {
     m_NextBtn.Invalidate(FALSE);
     m_NextBtn.UpdateWindow();
    }
  if(m_Trace.IsWindowVisible())
    {
     m_Trace.Invalidate(FALSE);
     m_Trace.UpdateWindow();
    }
  if(m_Memory.IsWindowVisible())
    {
     m_Memory.Invalidate(FALSE);
     m_Memory.UpdateWindow();
    }
/*  
  if(m_Stack.IsWindowVisible())
    {
     m_Stack.Invalidate(FALSE);
     m_Stack.UpdateWindow();
    }
*/  
  if(m_pStart)    
    {m_pStart->Invalidate(FALSE);
     m_pStart->UpdateWindow();}
  if(m_pAdvanceWnd)
     m_pAdvanceWnd->RedrawWindow(NULL,NULL,
                                 RDW_ERASE|RDW_FRAME|RDW_INVALIDATE|
                                 RDW_ALLCHILDREN|RDW_UPDATENOW);
}
/*--------------------------------------------------------*/
/*                  StartAdvance                          */
/*--------------------------------------------------------*/
void CMainFrame::StartAdvance()
{ 
 SetSysKey(1); 
 g_bAutoBtn=0;                                                 
 FilterTaskChange(0);
 bByUser=0;
 m_pDlg=0;
 m_bDC=0;
 m_Move_Count_Delay=8;
 m_nCursorStep=12;
 m_uNextAction=0;
 m_uNextCaseID=ID_ADVANCE_EVENT;//ID_ADVANCE_MEM;//ID_ADVANCE_EVENT;
 m_nCanDoNext=1;
 m_bUpArrow=1;
 m_pAdvanceWnd=0;
 g_bCanDoExit=1; 
 m_bTrace=1;
 m_bMemory=1;
 g_mbCanDoNext=1;
 
 OverMsg.bOverMsg=0;
 SetPaintDO(1);
 SetAMsgDlg(0);
////////////////////////////////////////////////////  
  SetCase(ID_CASE_ADVANCE);  
  m_pExitBtn->ShowWindow(SW_HIDE);
  m_pExitBtn->DestroyWindow();  

  if(int(m_hFlookInst = ::LoadLibrary("firstres.dll"))<32)
   {
     AfxMessageBox("Can not find firstres.dll library",MB_OK | MB_ICONEXCLAMATION);
     return ;
   }             


////////////////////////////////////////////////////

  CWindowDC dc(this); 
  PaintAdvance(dc);     
  CRect rectBtn(2,410,60,434); 
                                             
                                             
  if(!m_pStart->Create("Start",WS_CHILD|WS_VISIBLE|WS_TABSTOP|
                               BS_OWNERDRAW|BS_CHECKBOX|BS_LEFTTEXT,
                       rectBtn,this,ID_EXIT_ADVANCE))
     { AfxMessageBox("Can not create start button!"); 
       return;}     
       
  if(!m_pStart->LoadBitmaps("EXIT_DOWN","EXIT_UP",
                            "EXIT_DOWN","EXIT_DISABLE"))
     { AfxMessageBox("Can not Load start button!"); 
       AfxThrowResourceException();
       return;}  


///////////////////////////////////////////////////////////  

  m_pMsgWnd = new CMsgWnd();                               
  m_pMsgWnd->SetTitle("Advanced Tutorial");
  m_pMsgWnd->SetContent("ADVANCE.TXT");
  
  rectBtn.SetRect(325,410,430,434);
 if(!m_NextBtn.Create(NULL,WS_CHILD|WS_TABSTOP|WS_VISIBLE|
                        BS_OWNERDRAW|BS_LEFTTEXT,
                        rectBtn,this,ID_NEXT))
     { AfxMessageBox("Can not create Next button!"); 
       return;}     
       
  m_pNextBtn=&m_NextBtn;     
  if(!m_NextBtn.LoadBitmaps("NEXT_DOWN","NEXT_UP","NEXT_DOWN"))
     { AfxMessageBox("Can not Load Next button!"); 
       AfxThrowResourceException();
       return;}          
///////////////////////////////////////////////////////////       
  rectBtn.SetRect(70,410,152,434);
 if(!m_Trace.Create(NULL,WS_CHILD|WS_TABSTOP|WS_VISIBLE|
                        BS_OWNERDRAW|BS_LEFTTEXT,
                        rectBtn,this,ID_TRACE))
     { AfxMessageBox("Can not create Next button!"); 
       return;}     
       
  if(!m_Trace.LoadBitmaps("TRACE_DOWN","TRACE_UP","TRACE_DOWN","TR_GRAY"))
     { AfxMessageBox("Can not Load Next button!"); 
       AfxThrowResourceException();
       return;}                                   
///////////////////////////////////////////////////////////       
  rectBtn.SetRect(154,410,236,434);
 if(!m_Memory.Create(NULL,WS_CHILD|WS_TABSTOP|WS_VISIBLE|
                        BS_OWNERDRAW|BS_LEFTTEXT,
                        rectBtn,this,ID_MEMORY))
     { AfxMessageBox("Can not create Next button!"); 
       return;}     
       
  if(!m_Memory.LoadBitmaps("MEMORY_DOWN","MEMORY_UP","MEMORY_DOWN","MEM_GRAY"))
     { AfxMessageBox("Can not Load Next button!"); 
       AfxThrowResourceException();
       return;}                                    
///////////////////////////////////////////////////////////       
/*
  rectBtn.SetRect(238,410,320,434);
 if(!m_Stack.Create(NULL,WS_CHILD|WS_TABSTOP|WS_VISIBLE|
                        BS_OWNERDRAW|BS_LEFTTEXT,
                        rectBtn,this,ID_NEXT))
     { AfxMessageBox("Can not create Next button!"); 
       return;}     
       
  if(!m_Stack.LoadBitmaps("STACK_DOWN","STACK_UP","STACK_DOWN"))
     { AfxMessageBox("Can not Load Next button!"); 
       AfxThrowResourceException();
       return;}          
*/
  m_pMeter = new  CPlayMeter (CWnd::FromHandle(m_hWndMDIClient),0);
//  m_pArrow.CreateEx();
/////////////////////////////////////////////////////////// 
  SetTimer(SYSTIME,1000,NULL);
  if(g_bAuto)                     
    {     
     m_NextBtn.EnableWindow(FALSE);
     m_Memory.EnableWindow(FALSE);
     m_Trace.EnableWindow(FALSE);
    
     Delay(1000);
     PostMessage(WM_COMMAND,ID_NEXT);
    } 
}
 
/*--------------------------------------------------------*/
/*                  StartAdvance                          */
/*--------------------------------------------------------*/
void CMainFrame::OnExitAdvance()
{               
  if(g_bCanDoExit==0 || g_mbCanDoNext==0)  
      return;      
  //g_bBasicFirst=0;
  if(g_bAuto)
     KillTimer(IDAUTO);
  RestoreBk(0);      
  //m_pArrow.DestroyWindow();
  if(m_pAdvanceWnd)
    { m_pAdvanceWnd->DestroyWindow();
      delete m_pAdvanceWnd;//->DestroyWindow();
      m_pAdvanceWnd=0;}
  m_NextBtn.DestroyWindow();
  m_Memory.DestroyWindow();
  m_Trace.DestroyWindow();
  m_pNextBtn=0;
//  m_Stack.DestroyWindow();
  if(m_pStart)                      
     m_pStart->DestroyWindow();
  if(m_pMsgWnd)   
     m_pMsgWnd->DestroyWindow();
  if(m_pMeter)
    m_pMeter->DestroyWindow();
  delete m_pMsgWnd;         
  delete m_pMeter;
  m_pMeter=0;
  m_pMsgWnd=0;  

  CloseDlg(m_pDlg); 
////////////////////////////////////////////////////
  SetCase(ID_CASE_START);
  CRect rectWin;
  GetWindowRect(&rectWin);
  CRect rectBtn(
                int(rectWin.Width()*3/4),
                int(posLine.y-6),
                int(rectWin.Width()*3/4)+100,
                int(posLine.y-6+20) );
  ScreenToClient(&rectBtn);              
  
  m_pExitBtn->Create("&Exit Tutorial",
                     WS_CHILD|WS_VISIBLE|WS_TABSTOP,
                     rectBtn,this,ID_APP_EXIT);
                     
  FilterTaskChange(1);
  SetKey(1);
  SetMouse(1);
  SetSysFilter(0);
  if(int(m_hFlookInst)>=32)
    { FreeLibrary(m_hFlookInst);
      m_hPlayInst=0;}
  SetSysMouse(0);
  SetSysKey(0);  
  g_bAutoBtn=1;
  g_mbCanDoNext=1;
  if(g_bAuto)                     
  {         
    g_bAuto=0;
  }   
}

/*-------------------------------------------------------*/
/*                Advance Trace                          */
/*-------------------------------------------------------*/
void CMainFrame::OnTrace()
{ 
 static DWORD lDelay=0;
 if(m_uCase!=ID_CASE_ADVANCE || !g_bCanDoExit || g_bAuto)
    return;       
    
 if(DWORD(lDelay+2000)>GetTickCount())   
   {
     m_Trace.SetState(!m_bTrace);
     return;      
   }  
 lDelay=GetTickCount();   
// Delay(50);   
// m_Trace.SetState(m_bTrace);   
// m_Memory.SetState(0);
// m_Stack.SetState(0); 

 if(m_bTrace)
   {        
	    m_bTrace=0;
	
	    if(m_pAdvanceWnd)
	      { m_pAdvanceWnd->DestroyWindow();
	        delete m_pAdvanceWnd;//->DestroyWindow();
	        m_pAdvanceWnd=0;}  
	        
	    if(!m_pDlg)
	      {
	       Invalidate();
	       UpdateWindow();              
	      }
	    else  
	       CloseDlg(m_pDlg);
	    
	    m_uNextCaseID=ID_ADVANCE_EVENT;
	    m_uNextAction=0;    
/*
	    CRect rect;
	    m_NextBtn.GetWindowRect(&rect); 
	    SetCursorPos(rect.left+rect.Width()/2, rect.top+rect.Height()/2);
*/	    
	    if(g_bAuto)       
	    {
	      KillTimer(IDAUTO);
	      SetTimer(IDAUTO,5000,NULL);
	    }  
	    PostMessage(WM_COMMAND,ID_NEXT);

   }
 else{  
       m_bTrace=1;
    
	    ToMemCase();

	    m_uNextCaseID=ID_ADVANCE_EVENT;
	    m_uNextAction=0;
	    if(g_bAuto)
	       KillTimer(IDAUTO);
 
 }   
}
/*-------------------------------------------------------*/
/*                Advance Memory                         */
/*-------------------------------------------------------*/
void CMainFrame::OnMemory()
{
 static DWORD lDelay=0;
 if(m_uCase!=ID_CASE_ADVANCE || !g_bCanDoExit || g_bAuto)
    return;       
    
 if(DWORD(lDelay+2000)>GetTickCount())   
    {
     m_Memory.SetState(!m_bMemory);
     return; }
     
 lDelay=GetTickCount();   

// m_Trace.SetState(0);   
// m_Memory.SetState(m_bTrace);
// m_Stack.SetState(0);
 if(m_bMemory)
   {
	    m_bMemory=0;        
	    if(m_pAdvanceWnd)
	      { m_pAdvanceWnd->DestroyWindow();
	        delete m_pAdvanceWnd;//->MDIDestroy();
	        m_pAdvanceWnd=0;}
	    if(!m_pDlg)
	      {
	       Invalidate();
	       UpdateWindow();              
	      }
	    else  
	       CloseDlg(m_pDlg);
	
	    m_uNextCaseID=ID_ADVANCE_MEM;
	    m_uNextAction=0;
/*
	    CRect rect;
	    m_NextBtn.GetWindowRect(&rect); 
	    SetCursorPos(rect.left+rect.Width()/2, rect.top+rect.Height()/2);
*/
	    if(g_bAuto){
	    KillTimer(IDAUTO);
	    SetTimer(IDAUTO,5000,NULL);}
	    PostMessage(WM_COMMAND,ID_NEXT);
   } 
 else{       
        m_bMemory=1;        
	
	    ToMemCase();
	    m_uNextCaseID=ID_ADVANCE_EVENT;
	    m_uNextAction=0;
	    KillTimer(IDAUTO);
 }
   
}
void CMainFrame::InitButton(int x)
{            
  
 //m_Stack.SetState(0);   
  switch(x){
      case 1:
           m_Trace.SetState(1);  
           m_Memory.SetState(0);
           m_bTrace=0;
           m_bMemory=1;
           break;
      case 2:
           m_Memory.SetState(1);
           m_Trace.SetState(0);                 
           m_bMemory=0;
           m_bTrace=1;
           break;
      case 3:
     //      m_Stack.SetState(1);   
           break;             
      default:
           break;
    }       
}