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

Symbian

开发平台:

C/C++

  1. // Copyright (c) 2006 Nokia Corporation.
  2. #include "S60UIExamplePlayContainer.h"
  3. #include "S60UIExampleModel.h"
  4. #include <aknutils.h>
  5. #include <aknViewAppUi.h>
  6. #include <akniconutils.h>
  7. #include <S60UIExample.mbg>
  8. #include <aknnavi.h>
  9. #include <aknnavide.h>
  10. // -----------------------------------------------------------------------------
  11. // CS60UIExamplePlayContainer::NewL()
  12. // Two-phased constructor.
  13. // -----------------------------------------------------------------------------
  14. //
  15. CS60UIExamplePlayContainer* CS60UIExamplePlayContainer::NewL(
  16.                                             const TRect& aRect, 
  17.                                             CS60UIExampleModel& aModel)
  18.     {
  19.     CS60UIExamplePlayContainer* self = 
  20.             CS60UIExamplePlayContainer::NewLC(aRect, aModel);
  21.     CleanupStack::Pop(self);
  22.     return self;
  23.     }
  24. // -----------------------------------------------------------------------------
  25. // CS60UIExamplePlayContainer::NewLC()
  26. // Two-phased constructor.
  27. // -----------------------------------------------------------------------------
  28. //
  29. CS60UIExamplePlayContainer* CS60UIExamplePlayContainer::NewLC(
  30.                                             const TRect& aRect, 
  31.                                             CS60UIExampleModel& aModel)
  32.     {
  33.     CS60UIExamplePlayContainer* self = 
  34.             new (ELeave) CS60UIExamplePlayContainer(aModel);
  35.     CleanupStack::PushL(self);
  36.     self->ConstructL(aRect);
  37.     return self;
  38.     }
  39. // -----------------------------------------------------------------------------
  40. // CS60UIExamplePalyContainer::CS60UIExamplePlayContainer()
  41. // First stage constructor
  42. // -----------------------------------------------------------------------------
  43. //
  44. CS60UIExamplePlayContainer::CS60UIExamplePlayContainer(
  45.                                             CS60UIExampleModel& aModel) 
  46.     : iModel(aModel)
  47.     {
  48.     }
  49. // -----------------------------------------------------------------------------
  50. // CS60UIExamplePlayContainer::ConstructL()
  51. // Symbian 2nd phase constructor can leave.
  52. // -----------------------------------------------------------------------------
  53. //
  54. void CS60UIExamplePlayContainer::ConstructL(const TRect& aRect)
  55.     {
  56.     CreateWindowL();
  57.     
  58.     // Get pointer to the navipane
  59.     CEikStatusPane* statusPane = iAvkonViewAppUi->StatusPane();
  60.     iNaviPane = (CAknNavigationControlContainer*)statusPane->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
  61.     // Update navipane with latest level and score
  62.     UpdateNaviPaneL();
  63.     
  64.     // Get background graphic
  65.     _LIT(KSvgFile,"z:\resource\apps\S60UIExample.mif");
  66.     iBkgBitmap = AknIconUtils::CreateIconL(KSvgFile, EMbmS60uiexampleGame_bkg);
  67.     
  68.     // Resize layout
  69.     SetRect(aRect);
  70.     
  71.     ActivateL();
  72.     }
  73. // -----------------------------------------------------------------------------
  74. // CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
  75. // Destructor
  76. // -----------------------------------------------------------------------------
  77. //
  78. CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
  79.     {
  80.     delete iNaviDecorator;
  81.     delete iBkgBitmap;
  82.     }
  83. // -----------------------------------------------------------------------------
  84. // CS60UIExamplePlayContainer::SizeChanged()
  85. // Handles necessary size changes, in this case resizing the bitmap.
  86. // -----------------------------------------------------------------------------
  87. //
  88. void CS60UIExamplePlayContainer::SizeChanged()
  89.     {
  90.     TRect rect = Rect();
  91.     TInt error = AknIconUtils::SetSize(iBkgBitmap, 
  92.                                        rect.Size(), 
  93.                                        EAspectRatioNotPreserved);    
  94.     }
  95. // -----------------------------------------------------------------------------
  96. // CS60UIExamplePlayContainer::HandleResourceChange()
  97. // Called by framework when screen size changes. Use to cause necessary
  98. // resizing to be performed.
  99. // -----------------------------------------------------------------------------
  100. //
  101. void CS60UIExamplePlayContainer::HandleResourceChange(TInt aType)
  102.     {
  103.     CCoeControl::HandleResourceChange(aType);
  104.     if (aType == KEikDynamicLayoutVariantSwitch)
  105.         {
  106.         TRect rect;
  107.         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
  108.         SetRect(rect);
  109.         }
  110.     }
  111. // -----------------------------------------------------------------------------
  112. // CS60UIExamplePlayContainer::Draw() const
  113. // Draws the display
  114. // -----------------------------------------------------------------------------
  115. //
  116. void CS60UIExamplePlayContainer::Draw(const TRect& aRect) const
  117.     {
  118.     CWindowGc& gc = SystemGc();
  119.     gc.BitBlt(TPoint(0,0), iBkgBitmap);
  120.     
  121.     if (iModel.IsGamePaused())
  122.         {
  123.         _LIT(KPausedText,"Game Paused");
  124.         gc.SetPenStyle(CGraphicsContext::ESolidPen);
  125.         gc.SetPenColor(KRgbBlack);
  126.         gc.UseFont(AknLayoutUtils::FontFromId(EAknLogicalFontTitleFont));
  127.         TPoint textPos(aRect.iBr.iX/4, aRect.iBr.iY/2);
  128.         gc.DrawText(KPausedText, textPos);
  129.         gc.DiscardFont();
  130.         }
  131.     }
  132.     
  133. // ----------------------------------------------------------------------------
  134. // CS60UIExamplePlayContainer::OfferKeyEventL()
  135. // Handles the key events.
  136. // ----------------------------------------------------------------------------
  137. //
  138. TKeyResponse CS60UIExamplePlayContainer::OfferKeyEventL(const TKeyEvent& 
  139.                                                         aKeyEvent,
  140.                                                         TEventCode aType)
  141.     {
  142.     // Is not key event?
  143.     if (aType != EEventKey)
  144.         {
  145.         return EKeyWasNotConsumed;
  146.         }
  147.     // Is game running?    
  148.     if (!iModel.IsGameStarted() || iModel.IsGamePaused())
  149.         {
  150.         return EKeyWasNotConsumed;
  151.         }
  152.     // The key event code is ...
  153.     switch (aKeyEvent.iCode)
  154.         {
  155.         case '1': 
  156.         case '2': 
  157.         case '3': 
  158.         case '4': 
  159.         case '5': 
  160.         case '6': 
  161.         case '7': 
  162.         case '8': 
  163.         case '9': 
  164.             {
  165.             // Number key pressed. add to score and adjust level
  166.             TInt keyNum = aKeyEvent.iCode - '0';
  167.             TInt score = iModel.Score() + keyNum;
  168.             iModel.SetScore(score);
  169.             TInt level = iModel.LevelNumber();
  170.             if (score > level *100)
  171.                 {
  172.                 iModel.SetLevelNumber(level + 1);
  173.                 }
  174.             
  175.             // Update navipane with new level and score
  176.          UpdateNaviPaneL();
  177.             
  178.             break;
  179.             }
  180.         default:
  181.             return EKeyWasNotConsumed;
  182.         }
  183.         
  184.     return EKeyWasConsumed;
  185.     }
  186. // ----------------------------------------------------------------------------
  187. // CS60UIExamplePlayContainer::UpdateNaviPaneL()
  188. // Handles the key events.
  189. // ----------------------------------------------------------------------------
  190. //
  191. void CS60UIExamplePlayContainer::UpdateNaviPaneL()
  192.     {
  193.     if (iNaviDecorator)
  194.         {
  195.         iNaviPane->Pop(iNaviDecorator);
  196.         delete iNaviDecorator;
  197.         iNaviDecorator = NULL;
  198.         }
  199.         
  200.     TBuf<100> naviText;
  201.     _LIT(KNaviText,"Level %d  Score %d");
  202.     naviText.Format(KNaviText,iModel.LevelNumber(), iModel.Score());
  203.     
  204.     iNaviDecorator = iNaviPane->CreateNavigationLabelL(naviText);
  205.     iNaviPane->PushL(*iNaviDecorator);   
  206.     }
  207. // End of File