realtimenetprots/sipfw/SampleApp/gameUI_techview/Src/SIPExGameView.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 
       
     2 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 // All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 //
       
     9 // Initial Contributors:
       
    10 // Nokia Corporation - initial contribution.
       
    11 //
       
    12 // Contributors:
       
    13 //
       
    14 // Description:
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include    "SIPExGameView.h"
       
    21 #include    "SIPExGameEngine.h"
       
    22 #include    "SIPExGameConstants.h"
       
    23 
       
    24 #include    <e32svr.h>
       
    25 
       
    26 // CONSTANTS
       
    27 const TInt KLeft( 25 );
       
    28 const TInt KRight( 213 );
       
    29 const TInt KTop( 75 );
       
    30 
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CSIPExGameView::NewL
       
    34 // Static constructor
       
    35 // -----------------------------------------------------------------------------
       
    36 //
       
    37 CSIPExGameView* CSIPExGameView::NewL(
       
    38     const TRect& aRect, 
       
    39     const CCoeControl* aParent, 
       
    40     CSIPExEngine& aEngine )
       
    41     {
       
    42     CSIPExGameView* self = CSIPExGameView::NewLC( aRect, aParent, aEngine );
       
    43     CleanupStack::Pop( self );
       
    44     return self;
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CSIPExGameView::NewLC
       
    49 // Static constructor. On return the instance is left to the CleanupStack.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CSIPExGameView* CSIPExGameView::NewLC(
       
    53     const TRect& aRect, 
       
    54     const CCoeControl* aParent, 
       
    55     CSIPExEngine& aEngine )
       
    56     {
       
    57     CSIPExGameView* self = new ( ELeave ) CSIPExGameView( aEngine );
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL( aRect, aParent);
       
    60     return self;
       
    61     }
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CSIPExGameView::~CSIPExGameView
       
    65 // Destructor
       
    66 // -----------------------------------------------------------------------------
       
    67 //
       
    68 CSIPExGameView::~CSIPExGameView()
       
    69     {
       
    70     // no implementation required
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CSIPExGameView::ConstructL
       
    75 // Symbian 2nd phase constructor that might leave.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 void CSIPExGameView::ConstructL( const TRect& aRect, const CCoeControl* aParent )
       
    79     {
       
    80     iLabelSize = 20;
       
    81 	iXOffset = 5;
       
    82 	iYOffset = 5;
       
    83 	iBoxW = 18;
       
    84 	iMidW = 4;
       
    85 
       
    86     iRect = aRect;
       
    87     SetContainerWindowL( *aParent );
       
    88     SetRect( iRect );
       
    89 
       
    90     ActivateL();
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CSIPExGameView::CSIPExGameView
       
    95 // C++ default constructor
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 CSIPExGameView::CSIPExGameView( CSIPExEngine& aEngine )
       
    99 : iEngine( &aEngine )
       
   100     {
       
   101     // no implementation required
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CSIPExGameView::Draw
       
   106 // From CCoeControl.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CSIPExGameView::Draw( const TRect& /*aRect*/ ) const
       
   110     {
       
   111     CWindowGc& gc = SystemGc();
       
   112     DrawGame( gc, iRect );
       
   113     }
       
   114 
       
   115 // -----------------------------------------------------------------------------
       
   116 // CSIPExGameView::DrawGame
       
   117 // Draws the game view. Decides whether we should draw the board or cursor 
       
   118 // or not
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 void CSIPExGameView::DrawGame( CWindowGc& aGc, const TRect& aRect ) const 
       
   122     {
       
   123     if( iEngine->DrawBoard() )
       
   124         {
       
   125 		DrawBoard(aGc, aRect);
       
   126 
       
   127         if( iEngine->DrawCursor() )
       
   128             {
       
   129 			DrawCursor(aGc, aRect);
       
   130             }
       
   131 	    }    
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CSIPExGameView::DrawBoard
       
   136 // Draws the game board.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CSIPExGameView::DrawBoard( CWindowGc& aGc, const TRect& aRect ) const 
       
   140     {
       
   141     CWindowGc& gc =aGc;
       
   142 
       
   143     gc.SetPenSize( TSize(1,1) );
       
   144     gc.SetPenStyle( CGraphicsContext::ESolidPen );
       
   145     gc.SetPenColor( TRgb(0,0,0) );
       
   146     gc.SetBrushColor( TRgb(0,0,255) );
       
   147     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   148 
       
   149 
       
   150 	TPoint p1 = aRect.iTl;
       
   151 	p1.iX = p1.iX  + iXOffset;
       
   152 	p1.iY = p1.iY + iLabelSize + iYOffset;
       
   153 
       
   154 	TPoint p2 = aRect.iBr;
       
   155 	p2.iX = p1.iX + ( ( KBoxCountX * iBoxW ) + ( ( KBoxCountX + 1 ) * iMidW ) );
       
   156 	p2.iY = p1.iY + ( ( KBoxCountY * iBoxW ) + ( ( KBoxCountY + 1 ) * iMidW ) );
       
   157 	TRect box2 = TRect(p1, p2);
       
   158 	gc.DrawRect( box2 );
       
   159 
       
   160 
       
   161 	gc.SetPenColor( TRgb(0,0,0) );
       
   162     gc.SetBrushColor( TRgb(255,255,255) );
       
   163     gc.SetPenStyle( CGraphicsContext::ESolidPen );
       
   164     gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   165 
       
   166 	for (int i=0 ; i < KBoxCountX; i++) 
       
   167         {
       
   168 	    for (int j=0 ; j < KBoxCountY; j++) 
       
   169             {
       
   170 	        p1 = aRect.iTl;
       
   171 	        p2 = aRect.iBr;
       
   172 
       
   173 	        p1.iX = p1.iX + iXOffset + iMidW + (i*iMidW) + (i*iBoxW);
       
   174 	        p1.iY = p1.iY + iLabelSize  + iYOffset + iMidW + (j*iMidW) + (j*iBoxW);
       
   175 
       
   176 	        p2.iX = p1.iX + iBoxW;
       
   177 	        p2.iY = p1.iY + iBoxW;
       
   178 
       
   179 	        if ( iEngine->BoardValue( i, j ) == -1 )
       
   180                 {
       
   181 		        gc.SetBrushColor( TRgb(255,255,255) );
       
   182                 }
       
   183 	        else if ( iEngine->BoardValue( i, j ) == 1 )
       
   184                 {
       
   185 		        gc.SetBrushColor( TRgb(255,0,0) );
       
   186                 }
       
   187 	        else if ( iEngine->BoardValue( i, j ) == 2 )
       
   188                 {
       
   189 		        gc.SetBrushColor( TRgb(0,255,0) );
       
   190                 }
       
   191             else { /* Nothing to do */ }
       
   192 
       
   193 	        TRect rect( p1, p2 ); 
       
   194 	        gc.DrawEllipse( rect );
       
   195 	        }
       
   196         }
       
   197     }
       
   198 
       
   199 // -----------------------------------------------------------------------------
       
   200 // CSIPExGameView::DrawCursor
       
   201 // Draws the cursor.
       
   202 // -----------------------------------------------------------------------------
       
   203 //
       
   204 void CSIPExGameView::DrawCursor( CWindowGc& aGc, const TRect& aRect ) const
       
   205     {
       
   206     CWindowGc& gc =aGc;
       
   207 
       
   208 	gc.SetPenColor(TRgb(0,0,0));
       
   209 	gc.SetBrushColor(TRgb(255,0,0));
       
   210     gc.SetPenStyle(CGraphicsContext::ESolidPen);
       
   211     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
       
   212 
       
   213 
       
   214 	TInt i = iEngine->Cursor();
       
   215 
       
   216 	TPoint p1 = aRect.iTl;
       
   217 	TPoint p2 = aRect.iBr;
       
   218 
       
   219 	p1.iX = p1.iX + iXOffset + iMidW + (i*iMidW) + (i*iBoxW);
       
   220 	p1.iY = p1.iY + iLabelSize  + iYOffset - iBoxW;
       
   221 
       
   222 	p2.iX = p1.iX + iBoxW;
       
   223 	p2.iY = p1.iY + iBoxW;
       
   224 
       
   225 	TRect rect( p1, p2 ); 
       
   226 	gc.DrawEllipse( rect );
       
   227     }
       
   228 
       
   229 // -----------------------------------------------------------------------------
       
   230 // CSIPExGameView::OfferKeyEventL
       
   231 // From CCoeControl.
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 TKeyResponse CSIPExGameView::OfferKeyEventL( 
       
   235     const TKeyEvent& aKeyEvent, 
       
   236     TEventCode aType )
       
   237     {
       
   238     if( aType == EEventKeyUp )
       
   239         {
       
   240         switch( aKeyEvent.iScanCode )
       
   241             {
       
   242             case EStdKeyLeftArrow:
       
   243                 iEngine->CursorLeft();
       
   244                 DrawDeferred();
       
   245                 return EKeyWasConsumed;
       
   246             case EStdKeyRightArrow:
       
   247                 iEngine->CursorRight();
       
   248                 DrawDeferred();
       
   249                 return EKeyWasConsumed;
       
   250             case EStdKeyDownArrow:
       
   251             case EStdKeyEnter:
       
   252 			case EStdKeyDevice7: // FLOWTHROUGH
       
   253                 iEngine->CursorPressed();
       
   254                 DrawDeferred();
       
   255                 return EKeyWasConsumed;
       
   256             default:
       
   257                 return EKeyWasNotConsumed;
       
   258             }
       
   259         }
       
   260 
       
   261     return EKeyWasNotConsumed;
       
   262     }
       
   263 
       
   264 // -----------------------------------------------------------------------------
       
   265 // CSIPExGameView::HandlePointerEventL
       
   266 // From CCoeControl. Calculates the column of the game board where the pointer
       
   267 // event occured and moves the cursor according that information. 
       
   268 // -----------------------------------------------------------------------------
       
   269 //
       
   270 void CSIPExGameView::HandlePointerEventL( const TPointerEvent& aPointerEvent )
       
   271     {
       
   272     // Double-click
       
   273 	if( aPointerEvent.iModifiers & EModifierDoubleClick )
       
   274 		{
       
   275 		iEngine->CursorPressed();
       
   276         DrawDeferred();
       
   277 		return;
       
   278 		}
       
   279 
       
   280     // Cursor position
       
   281     switch( aPointerEvent.iType )
       
   282         {
       
   283         case TPointerEvent::EButton1Up:
       
   284             {
       
   285             TInt block( iMidW + iBoxW );
       
   286             TPoint point = aPointerEvent.iPosition;
       
   287             TInt x( point.iX );
       
   288             if( x >= KLeft && x <= KRight && point.iY > KTop )
       
   289                 {
       
   290                 for( TInt i=0; i < KBoxCountX; i++ )
       
   291                     {
       
   292                     if( x < ( KLeft + block * ( i + 1 ) ) )
       
   293                         {
       
   294                         iEngine->MoveCursorL( i );
       
   295                         DrawDeferred();
       
   296                         return;
       
   297                         }
       
   298                     }
       
   299                 }
       
   300             }
       
   301         break;
       
   302 
       
   303         default: 
       
   304             break;
       
   305         }
       
   306     }
       
   307 
       
   308 // End of file