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 <eikspane.h>
  6. #include <aknnavi.h>
  7. #include <aknnavide.h>
  8. #include <aknViewAppUi.h>
  9. #include <akniconutils.h>
  10. #include <S60UIExample.mbg>
  11. // -----------------------------------------------------------------------------
  12. // CS60UIExamplePlayContainer::NewL()
  13. // Two-phased constructor.
  14. // -----------------------------------------------------------------------------
  15. //
  16. CS60UIExamplePlayContainer* CS60UIExamplePlayContainer::
  17. NewL(const TRect& aRect, 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::
  30. NewLC(const TRect& aRect, CS60UIExampleModel& aModel)
  31.     {
  32.     CS60UIExamplePlayContainer* self = 
  33.             new (ELeave) CS60UIExamplePlayContainer(aModel);
  34.     CleanupStack::PushL(self);
  35.     self->ConstructL(aRect);
  36.     return self;
  37.     }
  38. // -----------------------------------------------------------------------------
  39. // CS60UIExamplePalyContainer::CS60UIExamplePlayContainer()
  40. // First stage constructor
  41. // -----------------------------------------------------------------------------
  42. //
  43. CS60UIExamplePlayContainer::
  44.     CS60UIExamplePlayContainer(CS60UIExampleModel& aModel) 
  45.     : iModel(aModel)
  46.     {
  47.     }
  48. // -----------------------------------------------------------------------------
  49. // CS60UIExamplePlayContainer::ConstructL()
  50. // Symbian 2nd phase constructor can leave.
  51. // -----------------------------------------------------------------------------
  52. //
  53. void CS60UIExamplePlayContainer::ConstructL(const TRect& aRect)
  54.     {
  55.     CreateWindowL();
  56.     
  57.     // Get pointer to the navipane
  58.     CEikStatusPane* statusPane = iAvkonViewAppUi->StatusPane();
  59.     iNaviPane = (CAknNavigationControlContainer*) 
  60.         statusPane->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
  61.     
  62.     // Update navipane with latest level and score
  63.     UpdateNaviPaneL();
  64.     
  65.     // Get background graphic
  66.     _LIT(KSvgFile,"z:\resource\apps\S60UIExample.mif");
  67.     iBkgBitmap = AknIconUtils::CreateIconL(KSvgFile, EMbmS60uiexampleGame_bkg);
  68.     
  69.     // Resize layout
  70.     SetRect(aRect);
  71.     
  72.     ActivateL();
  73.     }
  74. // -----------------------------------------------------------------------------
  75. // CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
  76. // Destructor
  77. // -----------------------------------------------------------------------------
  78. //
  79. CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
  80.     {
  81.     delete iNaviDecorator;
  82.     delete iBkgBitmap;
  83.     }
  84. // -----------------------------------------------------------------------------
  85. // CS60UIExamplePlayContainer::SizeChanged()
  86. // Handles necessary size changes, in this case resizing the bitmap.
  87. // -----------------------------------------------------------------------------
  88. //
  89. void CS60UIExamplePlayContainer::SizeChanged()
  90.     {
  91.     TRect rect = Rect();
  92.     TInt error = AknIconUtils::SetSize(iBkgBitmap, 
  93.                                        rect.Size(), 
  94.                                        EAspectRatioNotPreserved);    
  95.     }
  96. // -----------------------------------------------------------------------------
  97. // CS60UIExamplePlayContainer::HandleResourceChange()
  98. // Called by framework when screen size changes. Use to cause necessary
  99. // resizing to be performed.
  100. // -----------------------------------------------------------------------------
  101. //
  102. void CS60UIExamplePlayContainer::HandleResourceChange(TInt aType)
  103.     {
  104.     CCoeControl::HandleResourceChange(aType);
  105.     if (aType == KEikDynamicLayoutVariantSwitch)
  106.         {
  107.         TRect rect;
  108.         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
  109.         SetRect(rect);
  110.         }
  111.     }
  112. // -----------------------------------------------------------------------------
  113. // CS60UIExamplePlayContainer::Draw() const
  114. // Draws the display
  115. // -----------------------------------------------------------------------------
  116. //
  117. void CS60UIExamplePlayContainer::Draw(const TRect& aRect) const
  118.     {
  119.     CWindowGc& gc = SystemGc();
  120.     gc.BitBlt(TPoint(0,0), iBkgBitmap);
  121.     
  122.     if (iModel.IsGamePaused())
  123.         {
  124.         _LIT(KPausedText,"Game Paused");
  125.         gc.SetPenStyle(CGraphicsContext::ESolidPen);
  126.         gc.SetPenColor(KRgbBlack);
  127.         gc.UseFont(AknLayoutUtils::FontFromId(EAknLogicalFontTitleFont));
  128.         TPoint textPos(aRect.iBr.iX/4, aRect.iBr.iY/2);
  129.         gc.DrawText(KPausedText, textPos);
  130.         gc.DiscardFont();
  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.             break;
  178.             }
  179.         default:
  180.             return EKeyWasNotConsumed;
  181.         }
  182.         
  183.     return EKeyWasConsumed;
  184.     }
  185. // ----------------------------------------------------------------------------
  186. // CS60UIExamplePlayContainer::UpdateNaviPaneL()
  187. // Handles the key events.
  188. // ----------------------------------------------------------------------------
  189. //
  190. void CS60UIExamplePlayContainer::UpdateNaviPaneL()
  191.     {
  192.     if (iNaviDecorator)
  193.         {
  194.         iNaviPane->Pop(iNaviDecorator);
  195.         delete iNaviDecorator;
  196.         iNaviDecorator = NULL;
  197.         }
  198.         
  199.     TBuf<100> naviText;
  200.     _LIT(KNaviText,"Level %d  Score %d");
  201.     naviText.Format(KNaviText,iModel.LevelNumber(), iModel.Score());
  202.     
  203.     iNaviDecorator = iNaviPane->CreateNavigationLabelL(naviText);
  204.     iNaviPane->PushL(*iNaviDecorator);
  205.     }
  206. // End of File