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