TicTacToeAppView.cpp
上传用户:laixiong
上传日期:2007-03-11
资源大小:2994k
文件大小:10k
源码类别:

Symbian

开发平台:

C/C++

  1. /*
  2. * ==============================================================================
  3. *  Name        : TicTacToeAppView.cpp
  4. *  Part of     : TicTacToe
  5. *  Description : 
  6. *  Version     : 
  7. *
  8. *  Copyright (c) 2005 Nokia Corporation.
  9. * ==============================================================================
  10. */
  11. // INCLUDE FILES
  12. #include "TicTacToeAppView.h"
  13. #include <TicTacToe.rsg>
  14. #include <TicTacToe.mbg>
  15. #include <coemain.h>
  16. #include <aknnotewrappers.h>
  17. #include <akniconutils.h>
  18. #include <stringloader.h>
  19. // CONSTANTS
  20. const TInt KTttMarginFactor = 24;
  21. _LIT(KTttIconFile, "TicTacToe.mif");
  22. // ============================ MEMBER FUNCTIONS ===============================
  23. // -----------------------------------------------------------------------------
  24. // CTicTacToeAppView::NewL
  25. // Create a CTicTacToeAppView object, which will draw itself to aRect.
  26. // -----------------------------------------------------------------------------
  27. //
  28. CTicTacToeAppView* CTicTacToeAppView::NewL(const TRect& aRect, TInt aGraphicsSet)
  29.     {
  30.     CTicTacToeAppView* self = new (ELeave) CTicTacToeAppView;
  31.     CleanupStack::PushL(self);
  32.     self->ConstructL(aRect, aGraphicsSet);
  33.     CleanupStack::Pop(self);
  34.     return self;
  35.     }
  36. // -----------------------------------------------------------------------------
  37. // CTicTacToeAppView::ConstructL
  38. // Perform the second phase construction of a CTicTacToeAppView object.
  39. // -----------------------------------------------------------------------------
  40. //
  41. void CTicTacToeAppView::ConstructL(const TRect& aRect, TInt aGraphicsSet)
  42.     {
  43.     // Create a window for this application view
  44.     CreateWindowL();
  45.     // Set the windows size
  46.     SetRect(aRect);
  47.     LoadIconsL(aGraphicsSet);
  48.     // Activate the window, which makes it ready to be drawn
  49.     ActivateL();
  50.     }
  51. // -----------------------------------------------------------------------------
  52. // CTicTacToeAppView::CTicTacToeAppView
  53. // Perform the first phase of two phase construction.
  54. // -----------------------------------------------------------------------------
  55. //
  56. CTicTacToeAppView::CTicTacToeAppView()
  57.     {
  58.     NewGame();
  59.     }
  60. // -----------------------------------------------------------------------------
  61. // CTicTacToeAppView::~CTicTacToeAppView
  62. // Destroy the object.
  63. // -----------------------------------------------------------------------------
  64. //
  65. CTicTacToeAppView::~CTicTacToeAppView()
  66.     {
  67.     delete iBitmapGrid;
  68.     delete iBitmapGridMask;
  69.     delete iBitmapNought;
  70.     delete iBitmapNoughtMask;
  71.     delete iBitmapCross;
  72.     delete iBitmapCrossMask;
  73.     }
  74. // -----------------------------------------------------------------------------
  75. // CTicTacToeAppView::LoadIconsL
  76. // Load a set of graphics.
  77. // -----------------------------------------------------------------------------
  78. //
  79. void CTicTacToeAppView::LoadIconsL(TInt aGraphicsSet)
  80.     {
  81.     // Delete the old icon bitmaps
  82.     delete iBitmapGrid;
  83.     iBitmapGrid = NULL;
  84.     delete iBitmapGridMask;
  85.     iBitmapGridMask = NULL;
  86.     delete iBitmapNought;
  87.     iBitmapNought = NULL;
  88.     delete iBitmapNoughtMask;
  89.     iBitmapNoughtMask = NULL;
  90.     delete iBitmapCross;
  91.     iBitmapCross = NULL;
  92.     delete iBitmapCrossMask;
  93.     iBitmapCrossMask = NULL;
  94.     TFileName iconFile(KTttIconFile);
  95.     User::LeaveIfError(CompleteWithAppPath(iconFile));
  96.     
  97.     // Create the new icon bitmaps
  98.     if ( 0 == aGraphicsSet )
  99.         {
  100.         AknIconUtils::CreateIconL(iBitmapGrid, iBitmapGridMask, iconFile, EMbmTictactoeGrid, EMbmTictactoeGrid_mask);
  101.         AknIconUtils::CreateIconL(iBitmapNought, iBitmapNoughtMask, iconFile, EMbmTictactoeNought, EMbmTictactoeNought_mask);
  102.         AknIconUtils::CreateIconL(iBitmapCross, iBitmapCrossMask, iconFile, EMbmTictactoeCross, EMbmTictactoeCross_mask);
  103.         }
  104.     else
  105.         {
  106.         AknIconUtils::CreateIconL(iBitmapGrid, iBitmapGridMask, iconFile, EMbmTictactoeGrid2, EMbmTictactoeGrid2_mask);
  107.         AknIconUtils::CreateIconL(iBitmapNought, iBitmapNoughtMask, iconFile, EMbmTictactoeNought2, EMbmTictactoeNought2_mask);
  108.         AknIconUtils::CreateIconL(iBitmapCross, iBitmapCrossMask, iconFile, EMbmTictactoeCross2, EMbmTictactoeCross2_mask);
  109.         }
  110.     SetIconSizes();
  111.     DrawDeferred();
  112.     }
  113. // -----------------------------------------------------------------------------
  114. // CTicTacToeAppView::NewGame
  115. // Start a new game.
  116. // -----------------------------------------------------------------------------
  117. //
  118. void CTicTacToeAppView::NewGame()
  119.     {
  120.     iBoard.Reset();
  121.     iPlayer = ETttPlayerNought;     // Noughts always go first
  122.     DrawDeferred();
  123.     }
  124. // -----------------------------------------------------------------------------
  125. // CTicTacToeAppView::Draw
  126. // Draw this CTicTacToeAppView to the screen.
  127. // -----------------------------------------------------------------------------
  128. //
  129. void CTicTacToeAppView::Draw(const TRect& /*aRect*/) const
  130.     {
  131.     // Get the standard graphics context 
  132.     CWindowGc& gc = SystemGc();
  133.     // Clear the screen
  134.     gc.Clear(Rect());
  135.     // Draw the board
  136.     gc.BitBltMasked(iBoardRect.iTl, iBitmapGrid, iBoardRect.Size(), iBitmapGridMask, EFalse);
  137.     for ( TInt x = 0; x < KTicTacToeColumns; x++ )
  138.         {
  139.         TPoint topLeft = iBoardRect.iTl + iCellMargin + TPoint(x * iBoardRect.Width() / KTicTacToeColumns, 0);
  140.         for ( TInt y = 0; y < KTicTacToeRows; y++ )
  141.             {
  142.             TTicTacToePlayer player = iBoard.Player(x, y);
  143.             switch ( player )
  144.                 {
  145.                 case ETttPlayerNought:
  146.                     gc.BitBltMasked(topLeft, iBitmapNought, iCellSize, iBitmapNoughtMask, EFalse);
  147.                     break;
  148.                 case ETttPlayerCross:
  149.                     gc.BitBltMasked(topLeft, iBitmapCross, iCellSize, iBitmapCrossMask, EFalse);
  150.                     break;
  151.                 default:
  152.                     // Do nothing
  153.                     break;
  154.                 }
  155.             topLeft += TPoint(0, iBoardRect.Height() / KTicTacToeRows);
  156.             }
  157.         }
  158.     }
  159. // -----------------------------------------------------------------------------
  160. // CTicTacToeAppView::OfferKeyEventL
  161. // Handle any user keypresses.
  162. // -----------------------------------------------------------------------------
  163. //
  164. TKeyResponse CTicTacToeAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
  165.                                                 TEventCode aType)
  166.     {
  167.     // We only want the key press, not the key up/down event
  168.     // Check if a number key 1-9 was pressed
  169.     if ( EEventKey == aType && '1' <= aKeyEvent.iCode && aKeyEvent.iCode <= '9' )
  170.         {
  171.         TInt cell = aKeyEvent.iCode - '1';
  172.         // Try move in the cell
  173.         if ( iBoard.Move(cell, iPlayer) )
  174.             {
  175.             DrawDeferred();
  176.             // Check if the game is finished
  177.             if ( iBoard.HasWon(iPlayer) )
  178.                 {
  179.                 _LIT(KPlayerNoughtStr, "O");
  180.                 _LIT(KPlayerCrossStr, "X");
  181.                 HBufC* message = StringLoader::LoadLC(R_TICTACTOE_WINNER_STR,
  182.                             ( ETttPlayerCross == iPlayer ) ? KPlayerCrossStr : KPlayerNoughtStr);
  183.                 CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
  184.                 informationNote->ExecuteLD(*message);
  185.                 CleanupStack::PopAndDestroy(message);
  186.                 }
  187.             else if ( iBoard.IsDraw() )
  188.                 {
  189.                 HBufC* message = StringLoader::LoadLC(R_TICTACTOE_DRAW_STR);
  190.                 CAknInformationNote* informationNote = new (ELeave) CAknInformationNote;
  191.                 informationNote->ExecuteLD(*message);
  192.                 CleanupStack::PopAndDestroy(message);
  193.                 }
  194.             // Switch player
  195.             iPlayer = ( ETttPlayerCross == iPlayer ) ? ETttPlayerNought : ETttPlayerCross;
  196.             }
  197.         return EKeyWasConsumed;
  198.         }
  199.     // Return the default functionality
  200.     return CCoeControl::OfferKeyEventL(aKeyEvent, aType);
  201.     }
  202. // -----------------------------------------------------------------------------
  203. // CTicTacToeAppView::SizeChanged
  204. // Handle a change in the size of the control.
  205. // -----------------------------------------------------------------------------
  206. //
  207. void CTicTacToeAppView::SizeChanged()
  208.     {
  209.     SetIconSizes();
  210.     DrawDeferred();
  211.     }
  212. // -----------------------------------------------------------------------------
  213. // CTicTacToeAppView::SetIconSizes
  214. // Set the sizes of the scalable icons.
  215. // -----------------------------------------------------------------------------
  216. //
  217. void CTicTacToeAppView::SetIconSizes()
  218.     {
  219.     TRect rect(Rect());
  220.     if ( rect.Width() > rect.Height() )
  221.         {
  222.         iBoardRect.SetRect(TPoint((rect.Width() - rect.Height()) / 2, 0),
  223.                             TSize(rect.Height(), rect.Height()));
  224.         }
  225.     else
  226.         {
  227.         iBoardRect.SetRect(TPoint(0, (rect.Height() - rect.Width()) / 2),
  228.                             TSize(rect.Width(), rect.Width()));
  229.         }
  230.     iCellMargin.iX = iBoardRect.Width() / KTttMarginFactor;
  231.     iCellMargin.iY = iBoardRect.Height() / KTttMarginFactor;
  232.     iCellSize.SetSize((iBoardRect.Width() / KTicTacToeColumns) - (iCellMargin.iX * 2),
  233.                     (iBoardRect.Height() / KTicTacToeRows) - (iCellMargin.iY * 2));
  234.     AknIconUtils::SetSize(iBitmapGrid, iBoardRect.Size());
  235.     AknIconUtils::SetSize(iBitmapGridMask, iBoardRect.Size());
  236.     AknIconUtils::SetSize(iBitmapNought, iCellSize);
  237.     AknIconUtils::SetSize(iBitmapNoughtMask, iCellSize);
  238.     AknIconUtils::SetSize(iBitmapCross, iCellSize);
  239.     AknIconUtils::SetSize(iBitmapCrossMask, iCellSize);
  240.     }
  241. //  End of File