S60UIExamplePlayContainer.cpp
上传用户:laixiong
上传日期:2007-03-11
资源大小:2994k
文件大小:7k
源码类别:
Symbian
开发平台:
C/C++
- // Copyright (c) 2006 Nokia Corporation.
- #include "S60UIExamplePlayContainer.h"
- #include "S60UIExampleModel.h"
- #include <aknutils.h>
- #include <aknViewAppUi.h>
- #include <akniconutils.h>
- #include <S60UIExample.mbg>
- #include <aknnavi.h>
- #include <aknnavide.h>
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::NewL()
- // Two-phased constructor.
- // -----------------------------------------------------------------------------
- //
- CS60UIExamplePlayContainer* CS60UIExamplePlayContainer::NewL(
- const TRect& aRect,
- CS60UIExampleModel& aModel)
- {
- CS60UIExamplePlayContainer* self =
- CS60UIExamplePlayContainer::NewLC(aRect, aModel);
- CleanupStack::Pop(self);
- return self;
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::NewLC()
- // Two-phased constructor.
- // -----------------------------------------------------------------------------
- //
- CS60UIExamplePlayContainer* CS60UIExamplePlayContainer::NewLC(
- const TRect& aRect,
- CS60UIExampleModel& aModel)
- {
- CS60UIExamplePlayContainer* self =
- new (ELeave) CS60UIExamplePlayContainer(aModel);
- CleanupStack::PushL(self);
- self->ConstructL(aRect);
- return self;
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePalyContainer::CS60UIExamplePlayContainer()
- // First stage constructor
- // -----------------------------------------------------------------------------
- //
- CS60UIExamplePlayContainer::CS60UIExamplePlayContainer(
- CS60UIExampleModel& aModel)
- : iModel(aModel)
- {
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::ConstructL()
- // Symbian 2nd phase constructor can leave.
- // -----------------------------------------------------------------------------
- //
- void CS60UIExamplePlayContainer::ConstructL(const TRect& aRect)
- {
- CreateWindowL();
- // Get pointer to the navipane
- CEikStatusPane* statusPane = iAvkonViewAppUi->StatusPane();
- iNaviPane = (CAknNavigationControlContainer*)statusPane->ControlL(TUid::Uid(EEikStatusPaneUidNavi));
- // Update navipane with latest level and score
- UpdateNaviPaneL();
- // Get background graphic
- _LIT(KSvgFile,"z:\resource\apps\S60UIExample.mif");
- iBkgBitmap = AknIconUtils::CreateIconL(KSvgFile, EMbmS60uiexampleGame_bkg);
- // Resize layout
- SetRect(aRect);
- ActivateL();
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
- // Destructor
- // -----------------------------------------------------------------------------
- //
- CS60UIExamplePlayContainer::~CS60UIExamplePlayContainer()
- {
- delete iNaviDecorator;
- delete iBkgBitmap;
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::SizeChanged()
- // Handles necessary size changes, in this case resizing the bitmap.
- // -----------------------------------------------------------------------------
- //
- void CS60UIExamplePlayContainer::SizeChanged()
- {
- TRect rect = Rect();
- TInt error = AknIconUtils::SetSize(iBkgBitmap,
- rect.Size(),
- EAspectRatioNotPreserved);
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::HandleResourceChange()
- // Called by framework when screen size changes. Use to cause necessary
- // resizing to be performed.
- // -----------------------------------------------------------------------------
- //
- void CS60UIExamplePlayContainer::HandleResourceChange(TInt aType)
- {
- CCoeControl::HandleResourceChange(aType);
- if (aType == KEikDynamicLayoutVariantSwitch)
- {
- TRect rect;
- AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
- SetRect(rect);
- }
- }
- // -----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::Draw() const
- // Draws the display
- // -----------------------------------------------------------------------------
- //
- void CS60UIExamplePlayContainer::Draw(const TRect& aRect) const
- {
- CWindowGc& gc = SystemGc();
- gc.BitBlt(TPoint(0,0), iBkgBitmap);
- if (iModel.IsGamePaused())
- {
- _LIT(KPausedText,"Game Paused");
- gc.SetPenStyle(CGraphicsContext::ESolidPen);
- gc.SetPenColor(KRgbBlack);
- gc.UseFont(AknLayoutUtils::FontFromId(EAknLogicalFontTitleFont));
- TPoint textPos(aRect.iBr.iX/4, aRect.iBr.iY/2);
- gc.DrawText(KPausedText, textPos);
- gc.DiscardFont();
- }
- }
- // ----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::OfferKeyEventL()
- // Handles the key events.
- // ----------------------------------------------------------------------------
- //
- TKeyResponse CS60UIExamplePlayContainer::OfferKeyEventL(const TKeyEvent&
- aKeyEvent,
- TEventCode aType)
- {
- // Is not key event?
- if (aType != EEventKey)
- {
- return EKeyWasNotConsumed;
- }
- // Is game running?
- if (!iModel.IsGameStarted() || iModel.IsGamePaused())
- {
- return EKeyWasNotConsumed;
- }
- // The key event code is ...
- switch (aKeyEvent.iCode)
- {
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- {
- // Number key pressed. add to score and adjust level
- TInt keyNum = aKeyEvent.iCode - '0';
- TInt score = iModel.Score() + keyNum;
- iModel.SetScore(score);
- TInt level = iModel.LevelNumber();
- if (score > level *100)
- {
- iModel.SetLevelNumber(level + 1);
- }
- // Update navipane with new level and score
- UpdateNaviPaneL();
- break;
- }
- default:
- return EKeyWasNotConsumed;
- }
- return EKeyWasConsumed;
- }
- // ----------------------------------------------------------------------------
- // CS60UIExamplePlayContainer::UpdateNaviPaneL()
- // Handles the key events.
- // ----------------------------------------------------------------------------
- //
- void CS60UIExamplePlayContainer::UpdateNaviPaneL()
- {
- if (iNaviDecorator)
- {
- iNaviPane->Pop(iNaviDecorator);
- delete iNaviDecorator;
- iNaviDecorator = NULL;
- }
- TBuf<100> naviText;
- _LIT(KNaviText,"Level %d Score %d");
- naviText.Format(KNaviText,iModel.LevelNumber(), iModel.Score());
- iNaviDecorator = iNaviPane->CreateNavigationLabelL(naviText);
- iNaviPane->PushL(*iNaviDecorator);
- }
- // End of File