S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SIPExGameContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SIPExGameContainer.cpp
00004 *  Part of     : SIPEx
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 // INCLUDE FILES
00017 #include "SIPExGameContainer.h"
00018 #include "SIPExGameEngine.h"
00019 #include "SIPExDoc.h"
00020 
00021 #include <akndef.h>
00022 #include <aknutils.h>
00023 #include <eiklabel.h>
00024 #include <eikenv.h>
00025 
00026 #include <AknsDrawUtils.h>
00027 #include <AknsBasicBackgroundControlContext.h>
00028 
00029 const TInt KComponentCount( 0 );
00030 
00031 // ============================ MEMBER FUNCTIONS ===============================
00032 
00033 // -----------------------------------------------------------------------------
00034 // CSIPExGameContainer::CSIPExGameContainer
00035 // C++ default constructor
00036 // -----------------------------------------------------------------------------
00037 //
00038 CSIPExGameContainer::CSIPExGameContainer( CSIPExEngine& aEngine )
00039     : iEngine( aEngine )
00040     {
00041     // no implementation required
00042     }
00043 
00044 // -----------------------------------------------------------------------------
00045 // CSIPExGameContainer::ConstructL
00046 // Two phased constructor
00047 // -----------------------------------------------------------------------------
00048 //
00049 void CSIPExGameContainer::ConstructL( const TRect& aRect )
00050     {
00051         CalculateBoardDimensions( aRect );
00052         
00053         iBgContext = 
00054             CAknsBasicBackgroundControlContext::NewL( KAknsIIDQsnBgAreaMain, 
00055                                                       aRect, 
00056                                                       ETrue );
00057         
00058     CreateWindowL();
00059 
00060     SetRect( aRect );
00061     
00062     ActivateL();
00063     }
00064     
00065 // -----------------------------------------------------------------------------
00066 // CSIPExGameContainer::~CSIPExGameContainer
00067 // Destructor
00068 // -----------------------------------------------------------------------------
00069 //
00070 CSIPExGameContainer::~CSIPExGameContainer()
00071     {
00072     delete iBgContext;
00073     }
00074 
00075 // -----------------------------------------------------------------------------
00076 // CSIPExGameContainer::SizeChanged
00077 // Called by framework when the view size is changed
00078 // -----------------------------------------------------------------------------
00079 //
00080 void CSIPExGameContainer::SizeChanged()
00081     {
00082     // Control resize code
00083     }
00084 
00085 // -----------------------------------------------------------------------------
00086 // CSIPExGameContainer::CountComponentControls
00087 // -----------------------------------------------------------------------------
00088 //
00089 TInt CSIPExGameContainer::CountComponentControls() const
00090     {
00091     return KComponentCount; // return nbr of controls inside this container
00092     }
00093 
00094 // -----------------------------------------------------------------------------
00095 // CSIPExGameContainer::ComponentControl
00096 // -----------------------------------------------------------------------------
00097 //
00098 CCoeControl* CSIPExGameContainer::ComponentControl( TInt /*aIndex*/ ) const
00099     {
00100     return NULL;
00101     }
00102 
00103 // -----------------------------------------------------------------------------
00104 // CSIPExGameContainer::Draw
00105 // -----------------------------------------------------------------------------
00106 //
00107 void CSIPExGameContainer::Draw( const TRect& aRect ) const
00108     {
00109     CWindowGc& gc = SystemGc();
00110     
00111     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00112     gc.SetBrushColor( KRgbWhite );
00113     gc.Clear();
00114     
00115     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
00116     MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
00117     
00118     AknsDrawUtils::Background( skin, cc, this, gc, aRect );
00119 
00120     DrawGame( gc, aRect );
00121     }
00122    
00123 // -----------------------------------------------------------------------------
00124 // CSIPExGameView::DrawGame
00125 // Draws the game view. Decides whether we should draw the board or cursor 
00126 // or not
00127 // -----------------------------------------------------------------------------
00128 //
00129 void CSIPExGameContainer::DrawGame( CWindowGc& aGc, const TRect& aRect ) const 
00130     {
00131     if( iEngine.DrawBoard() )
00132         {
00133                 DrawBoard( aGc, aRect );
00134 
00135         if( iEngine.DrawCursor() )
00136             {
00137                         DrawCursor( aGc, aRect );
00138             }
00139             }    
00140     }
00141 
00142 // -----------------------------------------------------------------------------
00143 // CSIPExGameView::DrawBoard
00144 // Draws the game board.
00145 // -----------------------------------------------------------------------------
00146 //
00147 void CSIPExGameContainer::DrawBoard( CWindowGc& aGc, const TRect& aRect ) const 
00148     {
00149     CWindowGc& gc = aGc;
00150 
00151     gc.SetPenSize( TSize(1,1) );
00152     gc.SetPenStyle( CGraphicsContext::ESolidPen );
00153     gc.SetPenColor( TRgb(0,0,0) );
00154     gc.SetBrushColor( TRgb(0,0,255) );
00155     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00156 
00157 
00158         TPoint p1 = aRect.iTl;
00159         p1.iX = p1.iX + iXOffset;
00160         p1.iY = p1.iY + iYOffset;
00161 
00162         TPoint p2 = aRect.iBr;
00163         p2.iX = p1.iX + ( ( KBoxCountX * iBoxW ) + ( ( KBoxCountX + 1 ) * iMidW ) );
00164         p2.iY = p1.iY + ( ( KBoxCountY * iBoxW ) + ( ( KBoxCountY + 1 ) * iMidW ) );
00165         TRect box2 = TRect(p1, p2);
00166         gc.DrawRect( box2 );
00167 
00168 
00169         gc.SetPenColor( TRgb(0,0,0) );
00170     gc.SetBrushColor( TRgb(255,255,255) );
00171     gc.SetPenStyle( CGraphicsContext::ESolidPen );
00172     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
00173 
00174         for (TInt i=0 ; i < KBoxCountX; i++) 
00175         {
00176             for (TInt j=0 ; j < KBoxCountY; j++) 
00177             {
00178                 p1 = aRect.iTl;
00179                 p2 = aRect.iBr;
00180 
00181                 p1.iX = p1.iX + iXOffset + iMidW + (i*iMidW) + (i*iBoxW);
00182                 p1.iY = p1.iY + iYOffset + iMidW + (j*iMidW) + (j*iBoxW);
00183 
00184                 p2.iX = p1.iX + iBoxW;
00185                 p2.iY = p1.iY + iBoxW;
00186 
00187                 if ( iEngine.BoardValue( i, j ) == -1 )
00188                 {
00189                         gc.SetBrushColor( TRgb(255,255,255) );
00190                 }
00191                 else if ( iEngine.BoardValue( i, j ) == 1 )
00192                 {
00193                         gc.SetBrushColor( TRgb(255,0,0) );
00194                 }
00195                 else if ( iEngine.BoardValue( i, j ) == 2 )
00196                 {
00197                         gc.SetBrushColor( TRgb(0,255,0) );
00198                 }
00199             else { /* Nothing to do */ }
00200 
00201                 TRect rect( p1, p2 ); 
00202                 gc.DrawEllipse( rect );
00203                 }
00204         }
00205     }
00206 
00207 // -----------------------------------------------------------------------------
00208 // CSIPExGameView::DrawCursor
00209 // Draws the cursor.
00210 // -----------------------------------------------------------------------------
00211 //
00212 void CSIPExGameContainer::DrawCursor( CWindowGc& aGc, const TRect& aRect ) const
00213     {
00214     CWindowGc& gc = aGc;
00215 
00216         gc.SetPenColor(TRgb(0,0,0));
00217         gc.SetBrushColor(TRgb(255,0,0));
00218     gc.SetPenStyle(CGraphicsContext::ESolidPen);
00219     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00220 
00221 
00222         TInt i = iEngine.Cursor();
00223 
00224         TPoint p1 = aRect.iTl;
00225         TPoint p2 = aRect.iBr;
00226 
00227         p1.iX = p1.iX + iXOffset + iMidW + (i*iMidW) + (i*iBoxW);
00228         p1.iY = p1.iY + iYOffset - iBoxW;
00229 
00230         p2.iX = p1.iX + iBoxW;
00231         p2.iY = p1.iY + iBoxW;
00232 
00233         TRect rect( p1, p2 ); 
00234         gc.DrawEllipse( rect );
00235     }
00236 
00237 // -----------------------------------------------------------------------------
00238 // CSIPExGameContainer::CalculateBoardDimensions
00239 // -----------------------------------------------------------------------------
00240 //   
00241 void CSIPExGameContainer::CalculateBoardDimensions( const TRect& aRect )
00242     {
00243         TInt boxAndMid( 0 );
00244     if ( aRect.Height() < aRect.Width() )
00245         {
00246         boxAndMid = ( aRect.Height() ) / ( KBoxCountY + 2 );
00247         iXOffset = ( aRect.Width() - KBoxCountX * boxAndMid ) / 2;
00248         }
00249     else
00250         {
00251         boxAndMid = aRect.Width() / ( KBoxCountX + 2 );
00252         iXOffset = boxAndMid;
00253         }
00254     iYOffset = 2 * boxAndMid;
00255 
00256         iMidW = boxAndMid / 4;
00257         iBoxW = boxAndMid - iMidW;
00258     }
00259 
00260 // -----------------------------------------------------------------------------
00261 // CSIPExGameContainer::HandleControlEventL
00262 // -----------------------------------------------------------------------------
00263 //
00264 void CSIPExGameContainer::HandleControlEventL(
00265     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/ )
00266     {
00267     
00268     }
00269 
00270 // -----------------------------------------------------------------------------
00271 // CSIPExGameContainer::OfferKeyEventL
00272 // From CCoeControl.
00273 // -----------------------------------------------------------------------------
00274 //
00275 TKeyResponse CSIPExGameContainer::OfferKeyEventL( 
00276     const TKeyEvent& aKeyEvent, 
00277     TEventCode aType )
00278     {
00279     if( aType == EEventKeyUp )
00280         {
00281         switch( aKeyEvent.iScanCode )
00282             {
00283             case EStdKeyLeftArrow:
00284                 iEngine.CursorLeft();
00285                 DrawDeferred();
00286                 return EKeyWasConsumed;
00287             case EStdKeyRightArrow:
00288                 iEngine.CursorRight();
00289                 DrawDeferred();
00290                 return EKeyWasConsumed;
00291             case EStdKeyDownArrow:
00292             case EStdKeyEnter:
00293                         case EStdKeyDevice7: // FLOWTHROUGH
00294                 iEngine.CursorPressed();
00295                 DrawDeferred();
00296                 return EKeyWasConsumed;
00297             default:
00298                 return EKeyWasNotConsumed;
00299             }
00300         }
00301 
00302     return EKeyWasNotConsumed;
00303     }
00304     
00305 // -----------------------------------------------------------------------------
00306 // CSIPExGameContainer::HandleResourceChange
00307 // From CCoeControl.
00308 // -----------------------------------------------------------------------------
00309 // 
00310 void CSIPExGameContainer::HandleResourceChange( TInt aType )
00311         {
00312         CCoeControl::HandleResourceChange( aType );
00313         
00314     if ( aType == KEikDynamicLayoutVariantSwitch )
00315         {
00316         TRect mainPaneRect;
00317         AknLayoutUtils::LayoutMetricsRect( AknLayoutUtils::EMainPane, 
00318                                            mainPaneRect );
00319         SetRect( mainPaneRect );
00320         
00321         CalculateBoardDimensions( mainPaneRect );
00322         
00323         if ( iBgContext )
00324             {
00325             iBgContext->SetRect( mainPaneRect );
00326             }
00327         
00328         DrawNow();
00329         }
00330         }
00331 
00332 // -----------------------------------------------------------------------------
00333 // CSIPExGameContainer::MopSupplyObject
00334 // From CCoeControl.
00335 // -----------------------------------------------------------------------------
00336 //      
00337 TTypeUid::Ptr CSIPExGameContainer::MopSupplyObject( TTypeUid aId )
00338     {
00339     if ( aId.iUid == MAknsControlContext::ETypeId ) 
00340         { 
00341         return MAknsControlContext::SupplyMopObject( aId, iBgContext );
00342         }
00343     return CCoeControl::MopSupplyObject( aId );
00344     }
00345 
00346 
00347 // End of File  

© Nokia 2006

Back to top