home *** CD-ROM | disk | FTP | other *** search
- // Here are the 'public domain' sources
- // of Pyramid v1.1
- // To get a correct indentation of these sources
- // use a Tab char of size 4 !
- // The .mmp project is made for the sources to be in
- // a \epoc32ex\pyramid directory but you can change that
-
- // PYRAMID.CPP
- //
- // Written rapidly by Frederic Bouvry in 1997
- // Email: fb@mailandnews.com
-
- // Note: to compile the cards.mbm file, use 'bmconv cards.txt'
- // in the cards\bmp subdir and then copy cards.mbm to the Pyramid app dir.
- // To compile the pyramid.aif, use 'bmconv pyr.txt' in the cards
- // directory then aiftool.
-
- #include <e32keys.h>
- #include <e32base.h>
- #include <e32math.h>
- #include <e32svr.h>
- #include <e32math.h>
- #include <eikconso.h>
- #include <e32keys.h>
- #include <barsread.h>
- #include <coecntrl.h>
- #include <eikdef.h>
- #include <eikenv.h>
- #include <eikappui.h>
- #include <eikdialg.h>
- #include <eikdialg.hrh>
- #include <eikcapc.h>
- #include <eikcapca.h>
- #include <eikctrls.hrh>
- #include <eikcmds.hrh>
- #include <eikmfne.h>
- #include <eikmfne.hrh>
- #include <eikfpne.h>
- #include <eikedwin.hrh>
- #include <eikcmbut.h>
- #include <eiklabel.h>
- #include <eikseced.h>
- #include <eikrted.h>
- #include <txtrich.h>
- #include <eiktbar.h>
- #include <eikchred.h>
- #include <eikchmap.h>
- #include <eikrted.h>
- #include <eikopbut.h>
- #include <eikhopbt.h>
- #include <eikchkbx.h>
- #include <eikmenub.h>
- #include <eikmenup.h>
- #include <eikmenu.hrh>
- #include <eikfsel.h>
- #include <eikapp.h>
- #include <eikdoc.h>
- #include <basched.h>
- #include <coemain.h>
- #include <eikfnlab.h>
- #include <coeutils.h>
- #include <eikcfdlg.h>
- #include <eikdoc.h>
- #include <eikon.rsg>
- #include <eikfutil.h>
- #include <eikenv.h>
- #include <eikdef.h>
- #include <eikcmds.hrh>
- #include <eiklabel.h>
- #include <fontids.h>
- #include <s32file.h>
- #include "pyramid.rsg"
- #include "pyramid.hrh"
- #include "pyramid.h"
-
- #pragma data_seg(".E32_UID")
- __WINS_UID(0,KAppUidValue,0x1000055D)
- #pragma data_seg()
-
- //
- // TPyramidModel
- //
-
- TPyramidModel::TPyramidModel()
- {
- iLibrary=_L("");
- }
-
- // Differs from what ? :-)
- TBool TPyramidModel::Differs(const TPyramidModel* aCompare) const
- {
- return((*(TInt32*)this)!=(*(TInt32*)aCompare));
- }
-
- void CPyramidControl::Random() // Init a new game and deal the cards
- {
- TInt cards[52];
- TInt i,offset, res;
- for(i = 0; i<52; i++)
- cards[i] = 0;
- offset = 0;
- Score = 0;
- state = 0;
- for(i = 0; i<28; i++)
- {
- Math::Rand(seed);
- res = seed.Low() & 0x3F;
- offset = (offset + res) % 52;
- while(cards[offset] != 0)
- offset = (offset + 1) % 52;
- cards[offset] = Tapis[i] = offset+1;
- }
- for(i = 0; i<23; i++)
- {
- Math::Rand(seed);
- res = seed.Low() & 0x3F;
- offset = (offset + res) % 52;
- while(cards[offset] != 0)
- offset = (offset + 1) % 52;
- cards[offset] = Stock[i] = offset+1;
- }
- while(cards[offset] != 0)
- offset = (offset + 1) % 52;
- Tallon = offset+1;
- StockSize = 23;
- WasteSize = 0;
- for(i = 0; i < 28; i++)
- {
- if(i < 21)
- TapisState[i] = 0;
- else
- TapisState[i] = 1;
- }
- DragCard = 0;
- Redeals = 0;
- }
-
- void CPyramidControl::ConstructL(RWindowTreeNode& parent, const TRect& aRect)
- {
- CWindowGc& gc=SystemGc(); // graphics context we draw to
- int i;
- TFontSpec MyFontSpec(_L("Arial"), 120);
- TFontSpec MyFontSpec2(_L("Arial"), 180);
- TFontStyle MyStyle(EPostureUpright,EStrokeWeightBold,EPrintPosNormal);
- TTime home;
- CreateBackedUpWindowL(parent, EGray4);
- MyFontSpec.iFontStyle = MyStyle; // Font for 'Tallon:', ...
- MyFontSpec2.iFontStyle = MyStyle; // Font for 'Score:'
- iZoomFactor.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
- iZoomFactor.SetZoomFactor(TZoomFactor::EZoomOneToOne);
- iDeviceMap = &iZoomFactor;
- SetRectL(aRect);
- iEikonEnv->InfoMsg(_L("Loading"));
- TBufC<40> multiBitmapFile(_L("\\System\\Apps\\Pyramid\\cards.mbm"));
- iCardsMsBmp = new (ELeave) CFbsBitmap();
- LoadBitmapL(iCardsMsBmp, multiBitmapFile, 0); // Load the bitmap mask for all cards
- iCardsNoBmp = new (ELeave) CFbsBitmap();
- LoadBitmapL(iCardsNoBmp, multiBitmapFile, 1); // Load the empty slot bitmap
- iCardsBkBmp = new (ELeave) CFbsBitmap();
- LoadBitmapL(iCardsBkBmp, multiBitmapFile, 2); // Load the other side of the moon (back of the card)
- for(i = 0; i< 52; i++) // Load the 52 cards
- {
- iCards52Bmp[i] = new (ELeave) CFbsBitmap();
- LoadBitmapL(iCards52Bmp[i], multiBitmapFile, i+3);
- }
- for(i = 0; i< 28; i++) // create the bitmaps that will keep the background of a Tapis' card
- {
- iUnderTapis[i] = new(ELeave) CFbsBitmap;
- iUnderTapis[i]->Create(TSize(73,97), EGray4);
- }
-
- home.HomeTime(); // Base the random numbers generator seed on the current time
- seed = home.DateTime().Second() + home.DateTime().MicroSecond();
- iDeviceMap->GetNearestFontInTwips(MyFont,MyFontSpec);
- iDeviceMap->GetNearestFontInTwips(MyFont2,MyFontSpec2);
- SetObserver(this); // I use an observer for this control but this is not really necessary
- Random(); // Start a new game
- ActivateL();
- UpdateModelL();
- // go for it
- }
-
- CPyramidControl::~CPyramidControl()
- {
- int i;
- delete iCardsMsBmp;
- delete iCardsBkBmp;
- delete iCardsNoBmp;
- for(i = 0; i< 52; i++)
- delete iCards52Bmp[i];
- for(i = 0; i< 28; i++)
- delete iUnderTapis[i];
- CloseWindow();
- }
-
- void CPyramidControl::UpdateModelL()
- {
- }
-
- const TInt TapisRow[28]= {0,1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6};
- const TInt TapisCol[28]= {0,-1,1,-2,0,2,-3,-1,1,3,-4,-2,0,2,4,-5,-3,-1,1,3,5,-6,-4,-2,0,2,4,6};
- const TInt TapisDownTree[28] = {2,4,5,7,8,9,11,12,13,14,16,17,18,19,20,22,23,24,25,26,27,0,0,0,0,0,0,0};
- const TInt TapisUpTree[56] = {0,0,0,1,1,0,0,2,2,3,3,0,0,4,4,5,5,6,6,0,0,7,7,8,8,9,9,10,10,0,0,11,11,12,12,13,
- 13,14,14,15,15,0,0,16,16,17,17,18,18,19,19,20,20,21,21,0};
- const TInt WasteCol = 483;
- const TInt WasteRow = 14;
- const TInt TallonCol = 563;
- const TInt TallonRow = 14;
- const TInt StockCol = 563;
- const TInt StockRow = 128;
-
- void CPyramidControl::DrawTapis(CWindowGc& gc) const
- {
- // if wanting to use sprites, take a look at \eikon\zoom\zoom.cpp
- TInt i;
- TInt posx,posy;
- TRect bmpPieceRect;
- TSize bmpSizeInPixels(73,97);
- bmpPieceRect.SetRect(TPoint(0,0),bmpSizeInPixels); // full size
- for(i = 0; i < 28; i++)
- {
- if(Tapis[i])
- {
- posx = 280 + TapisCol[i] * 40 - 37;
- posy = TapisRow[i] * 23 + 3;
- iEikonEnv->ScreenDevice()->CopyScreenToBitmap(iUnderTapis[i],TRect(posx,posy,posx+73,posy+97));
- gc.BitBltMasked(TPoint(posx,posy), iCards52Bmp[Tapis[i]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- //to restore: gc.BitBlt(TPoint(posx,posy), iUnderTapis[i], TRect(0,0,73,97));
- }
- }
- }
- void CPyramidControl::DrawWaste(CWindowGc& gc) const
- {
- TRect bmpPieceRect;
- TSize bmpSizeInPixels(73,97);
- bmpPieceRect.SetRect(TPoint(0,0),bmpSizeInPixels); // full size
- if(WasteSize > 0)
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- else
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- }
-
- void CPyramidControl::DrawLabels(CWindowGc& gc) const
- {
- TBuf<20> txt;
- TInt textHeight(MyFont->HeightInPixels());
- TInt textHeight2(MyFont2->HeightInPixels());
- TRect box(16,20,120,44);
- TInt offset,margin=0;
- offset=(textHeight2+box.Height())/2 - 2; // 1/2 font ht below halfway down box
- gc.UseFont(MyFont2);
- gc.SetBrushStyle(gc.ESolidBrush);
- gc.SetBrushColor(KRgbBlack);
- gc.SetPenColor(KRgbWhite);
- gc.SetPenStyle(gc.ESolidPen);
- txt.Format(_L(" Score: %d"), Score);
- gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
- gc.UseFont(MyFont);
- box = TRect(WasteCol+6,WasteRow-12,WasteCol+68,WasteRow-1);
- offset=(textHeight+box.Height())/2; // 1/2 font ht below halfway down box
- txt.Format(_L(" Waste: %d"), WasteSize);
- gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
- box = TRect(TallonCol+6,TallonRow-12,TallonCol+68,TallonRow-1);
- txt.Format(_L(" Tallon: %d"), (Tallon ? 1 : 0) );
- gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
- box = TRect(StockCol+6,StockRow-12,StockCol+68,StockRow-1);
- txt.Format(_L(" Stock: %d"), StockSize);
- gc.DrawText(txt,box,offset,CGraphicsContext::ELeft,margin);
- }
-
-
- void CPyramidControl::DrawStock(CWindowGc& gc) const
- {
- TRect bmpPieceRect;
- TSize bmpSizeInPixels(73,97);
- bmpPieceRect.SetRect(TPoint(0,0),bmpSizeInPixels); // full size
- if(StockSize > 0)
- gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsBkBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- else
- gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- }
- void CPyramidControl::DrawTallon(CWindowGc& gc) const
- {
- TRect bmpPieceRect;
- TSize bmpSizeInPixels(73,97);
- bmpPieceRect.SetRect(TPoint(0,0),bmpSizeInPixels); // full size
- if(Tallon)
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
- else
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- }
-
- void CPyramidControl::Draw(const TRect&/*aRect*/) const
- {
- CWindowGc& gc=SystemGc(); // graphics context we draw to
- gc.SetPenStyle(gc.ESolidPen);
- gc.SetBrushStyle(gc.EDiamondCrossHatchBrush);
- gc.SetBrushColor(KRgbWhite);
- gc.DrawRect(Rect()); // clear the area to be drawn to
- DrawTapis(gc);
- DrawStock(gc);
- DrawTallon(gc);
- DrawWaste(gc);
- DrawLabels(gc);
- gc.SetBrushStyle(gc.ESolidBrush);
- }
-
- void CPyramidControl::LoadBitmapL(CFbsBitmap* aBitMap,TDesC& aPathAndFile,TInt aId)
- {
- TParse mbfn;
- TBufC<2> c_drive(_L("C:"));
- TBufC<2> z_drive(_L("D:"));
-
- mbfn.Set(aPathAndFile,&c_drive,NULL);
- if (!aBitMap->Load(mbfn.FullName(),aId,ETrue))
- return;
-
- mbfn.Set(aPathAndFile,&z_drive,NULL);
- User::LeaveIfError(aBitMap->Load(mbfn.FullName(),aId,ETrue));
- return;
- }
-
- void CPyramidControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
- {
- TInt card = 0;
- if (aPointerEvent.iType==TPointerEvent::EButton1Down)
- {
- TInt posx,posy,i;
- if(Score == 52)
- return;
- if(Tallon > 0 &&
- aPointerEvent.iPosition.iX >= TallonCol &&
- aPointerEvent.iPosition.iX < (TallonCol+73) &&
- aPointerEvent.iPosition.iY >= TallonRow &&
- aPointerEvent.iPosition.iY < (TallonRow+97) )
- card = 29;
- else
- if( aPointerEvent.iPosition.iX >= StockCol &&
- aPointerEvent.iPosition.iX < (StockCol+73) &&
- aPointerEvent.iPosition.iY >= StockRow &&
- aPointerEvent.iPosition.iY < (StockRow+97) )
- card = 30;
- else
- if(WasteSize > 0 &&
- aPointerEvent.iPosition.iX >= WasteCol &&
- aPointerEvent.iPosition.iX < (WasteCol+73) &&
- aPointerEvent.iPosition.iY >= WasteRow &&
- aPointerEvent.iPosition.iY < (WasteRow+97) )
- card = 31;
- else
- for(i = 0; i < 28; i++)
- {
- if(Tapis[i] && TapisState[i])
- {
- posx = 280 + TapisCol[i] * 40 - 37;
- posy = TapisRow[i] * 23 + 3;
- if(aPointerEvent.iPosition.iX >= posx &&
- aPointerEvent.iPosition.iX < (posx+73) &&
- aPointerEvent.iPosition.iY >= posy &&
- aPointerEvent.iPosition.iY < (posy+97) )
- {
- card = i+1;
- break;
- }
- }
- }
- if(card != 0)
- {
- if(DragCard == 0)
- {
- DragCardHit = card;
- switch(card)
- {
- case 29:{ // Tallon
- DragCardValue = (Tallon-1)%13 + 1;
- break;
- }
- case 30:break; // Stock
- case 31:{ // Waste
- DragCardValue = (Waste[WasteSize-1]-1)%13 + 1;
- break;
- }
- default:{
- DragCardValue = (Tapis[card-1]-1)%13 + 1;
- break;
- }
- }
- if((card != 30) && (DragCardValue == 13)) // a king
- {
- Score++;
- DropCardHit = 0;
- state = 3;
- }
- else
- {
- state = 1;
- if(card != 30)
- DragCard = card;
- }
- }
- else
- {
- if(card == DragCard)
- {
- state = 2; // cancel drag card
- DragCardHit = card;
- DragCard = 0;
- }
- else
- {
- TInt Bingo = 0;
- switch(card)
- {
- case 29:{ // Tallon
- if((Tallon-1)%13 + DragCardValue == 12)
- Bingo = 1;
- break;
- }
- case 30:break; // Stock
- case 31:{ // Waste
- if((Waste[WasteSize-1]-1)%13 + DragCardValue == 12)
- Bingo = 1;
- break;
- }
- default:{ // Tapis
- if((Tapis[card-1]-1)%13 + DragCardValue == 12)
- Bingo = 1;
- break;
- }
- }
- if(Bingo)
- {
- Score += 2;
- DragCardHit = DragCard;
- DropCardHit = card;
- DragCard = 0;
- state = 3;
- }
- }
- }
- if(state)
- ReportEventL(EEventStateChanged);
- }
- }
- }
-
- void CPyramidControl::Restart()
- {
- Random();
- ActivateGc();
- Draw(TRect(0,0,640,240));
- DeactivateGc();
- }
-
- void CPyramidControl::HandleControlEventL(CCoeControl* aControl,TCoeEvent aEventType)
- {
- int px, py, crd;
- if(aEventType != EEventStateChanged)
- return;
- int oldstate = state;
- state = 0;
- ActivateGc();
- CWindowGc& gc=SystemGc(); // graphics context we draw to
- TRect bmpPieceRect;
- TSize bmpSizeInPixels(73,97);
- bmpPieceRect.SetRect(TPoint(0,0),bmpSizeInPixels); // full size
- gc.SetPenSize(TSize(4,4));
- gc.SetPenColor(KRgbDarkGray);
- switch(oldstate)
- {
- case 1:{
- switch(DragCardHit)
- {
- case 29:{
- gc.DrawEllipse(TRect(TallonCol+6,TallonRow+6,TallonCol+67,TallonRow+91));
- break;
- }
- case 30:{
- if(StockSize == 0)
- {
- if(Redeals >= MaxRedeals)
- {
- iEikonEnv->InfoMsg(_L("No more redeals allowed !"));
- break;
- }
- Redeals++;
- if(Tallon != 0)
- Stock[StockSize++] = Tallon;
- while(WasteSize > 0)
- Stock[StockSize++] = Waste[--WasteSize];
- if(StockSize > 0)
- {
- Tallon = Stock[--StockSize];
- gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsBkBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- }
- else
- gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- if(Tallon > 0)
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
- else
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- DrawLabels(gc);
- break;
- }
- if(Tallon > 0)
- {
- Waste[WasteSize++] = Tallon;
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- }
- if(StockSize > 0)
- {
- Tallon = Stock[--StockSize];
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
- }
- else
- gc.BitBltMasked(TPoint(StockCol,StockRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- DrawLabels(gc);
- break;
- }
- case 31:{
- gc.DrawEllipse(TRect(WasteCol+6,WasteRow+6,WasteCol+67,WasteRow+91));
- break;
- }
- default:{
- TInt posx,posy;
- posx = 280 + TapisCol[DragCardHit-1] * 40 - 37;
- posy = TapisRow[DragCardHit-1] * 23 + 3;
- gc.DrawEllipse(TRect(posx+6,posy+6,posx+67,posy+91));
- break;
- }
- }
- break;
- }
- case 2:{
- switch(DragCardHit)
- {
- case 29:{
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCards52Bmp[Tallon-1], bmpPieceRect, iCardsMsBmp, ETrue);
- break;
- }
- case 31:{
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- break;;
- }
- default:{
- TInt posx,posy;
- posx = 280 + TapisCol[DragCardHit-1] * 40 - 37;
- posy = TapisRow[DragCardHit-1] * 23 + 3;
- gc.BitBltMasked(TPoint(posx,posy), iCards52Bmp[Tapis[DragCardHit-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- break;
- }
- }
- break;
- }
- case 3:{
- TInt card;
- int i;
- for(i = 0; i < 2; i++)
- {
- if(i == 0)
- card = DragCardHit;
- else
- card = DropCardHit;
- if(card == 0)
- continue;
- switch(card)
- {
- case 29:{
- Tallon = 0;
- gc.BitBltMasked(TPoint(TallonCol,TallonRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- break;
- }
- case 31:{
- --WasteSize;
- if(WasteSize > 0)
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCards52Bmp[Waste[WasteSize-1]-1], bmpPieceRect, iCardsMsBmp, ETrue);
- else
- gc.BitBltMasked(TPoint(WasteCol,WasteRow), iCardsNoBmp, bmpPieceRect, iCardsMsBmp, ETrue);
- break;;
- }
- default:{
- TInt posx,posy,cardup,carddown;
- Tapis[card-1] = TapisState[card-1] = 0;
- cardup = TapisUpTree[(card-1)*2];
- if(cardup)
- {
- carddown = TapisDownTree[cardup-1];
- if((Tapis[carddown-1] == 0) && (Tapis[carddown] == 0))
- TapisState[cardup-1] = 1;
- }
- cardup = TapisUpTree[(card-1)*2 + 1];
- if(cardup)
- {
- carddown = TapisDownTree[cardup-1];
- if((Tapis[carddown-1] == 0) && (Tapis[carddown] == 0))
- TapisState[cardup-1] = 1;
- }
- posx = 280 + TapisCol[card-1] * 40 - 37;
- posy = TapisRow[card-1] * 23 + 3;
- gc.BitBlt(TPoint(posx,posy), iUnderTapis[card-1], TRect(0,0,73,97));
- break;
- }
- }
- }
- DrawLabels(gc);
- break;
- }
- }
- if(Score == 52) // He won !
- {
- gc.SetPenStyle(gc.ESolidPen);
- gc.SetBrushStyle(gc.ESolidBrush);
- gc.SetBrushColor(KRgbWhite);
- gc.DrawRect(Rect()); // clear the area to be drawn to
- crd = 0;
- for(py = 0; py < 2 ; py++)
- for(px = 0; px < 8 ; px++)
- {
- gc.BitBltMasked(TPoint(px*78+8,py*120+10), iCards52Bmp[crd], bmpPieceRect, iCardsMsBmp, ETrue);
- crd = (crd + 13) % 52;
- }
- }
- DeactivateGc();
- if(Score == 52)
- {
- CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
- GamesPlayed[Redeals]++;
- GamesWon[Redeals]++;
- MaxScore[Redeals] = 52;
- TotalScore[Redeals] += 52;
- dialog->ExecuteLD(R_EXAMPLE_WIN_DIALOG);
- Random();
- ActivateGc();
- Draw(TRect(0,0,640,240));
- DeactivateGc();
- }
- }
-
- TKeyResponse CPyramidControl::OfferKeyEventL(
- const TKeyEvent& aKeyEvent,TEventCode aType
- )
- {
- return EKeyWasNotConsumed;
- }
-
- //
- // class CPyramidContainer
- //
-
- void CPyramidContainer::ConstructL(const TRect& aRect, TPyramidModel* aModel)
- {
- iModel=aModel;
- CreateWindowL();
- iContext=this;
- iBrushStyle=CGraphicsContext::ESolidBrush;
- iBrushColor=KRgbWhite;
- SetRectL(aRect);
- iPyramidControl = new(ELeave)CPyramidControl;
- iPyramidControl->ConstructL(Window(), TRect(0,0,640,240));
- ActivateL();
- iPyramidControl->MyActivateGc(); // little hack for saving correctly the background
- iPyramidControl->Draw(TRect(0,0,640,240)); // of the cards while using a BackedUpWindow
- iPyramidControl->MyDeactivateGc();
- }
-
- CPyramidContainer::~CPyramidContainer()
- {
- delete iPyramidControl;
- }
-
- TInt CPyramidContainer::CountComponentControls() const
- {
- return 1;
- }
-
- CCoeControl* CPyramidContainer::ComponentControl(TInt aIndex) const
- {
- switch (aIndex)
- {
- case 0: return iPyramidControl;
- default: return 0;
- };
- }
-
-
- TKeyResponse CPyramidContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
- {
- if (iPyramidControl)
- return iPyramidControl->OfferKeyEventL(aKeyEvent,aType);
- else
- return EKeyWasNotConsumed;
- }
-
- void CPyramidContainer::Draw(const TRect& /*aRect*/) const
- {
- CWindowGc& gc = SystemGc();
- gc.SetPenStyle(CGraphicsContext::ENullPen);
- gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
- gc.DrawRect(Rect());
- }
-
- //
- // CPyramidAppUi
- //
-
- void CPyramidAppUi::ConstructL()
- {
- BaseConstructL();
- iModel=((CPyramidDocument*)iDocument)->Model();
- iContainer=new(ELeave) CPyramidContainer;
- iContainer->ConstructL(ClientRect(),iModel);
- ReadProfile();
- // add container to stack; enables key event handling.
- AddToStackL(iContainer);
- }
-
- void CPyramidAppUi::HandleCommandL(TInt aCommand)
- {
- switch (aCommand)
- {
-
- case EEikCmdEditCut: // <---Pre-defined command id
- case EEikCmdEditCopy: // <---Pre-defined command id
- case EEikCmdEditPaste: // <---Pre-defined command id
- case EEikCmdEditSelectAll: // <---Pre-defined command id
- iEikonEnv->InfoMsg(_L("Not used by Pyramid"));
- return;
- // Infrared command
- case EEikCmdIrdaSend:
- iEikonEnv->InfoMsg(_L("Sending cards to Jupiter"));
- break;
- case EEikCmdIrdaReceive:
- iEikonEnv->InfoMsg(_L("Receiving cards from Pluton"));
- break;
-
- case EEikCmdPreferences:
- {
- CEikDialog* dialog = new (ELeave) CPrefDialog(&(iContainer->iPyramidControl->MaxRedeals));
- dialog->ExecuteLD(R_EXAMPLE_PREFERENCES_DIALOG);
- break;
- }
-
- case EEikCmdExit:
- {
- if((iContainer->iPyramidControl->Score > 0) && (iContainer->iPyramidControl->Score < 52))
- {
- CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
- if(dialog->ExecuteLD(R_EXAMPLE_ADDTOSTATISTICS_DIALOG))
- {
- iContainer->iPyramidControl->GamesPlayed[iContainer->iPyramidControl->Redeals]++;
- iContainer->iPyramidControl->TotalScore[iContainer->iPyramidControl->Redeals] += iContainer->iPyramidControl->Score;
- if(iContainer->iPyramidControl->Score > iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals])
- iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals] = iContainer->iPyramidControl->Score;
- }
- }
- UpdateProfile();
- CBaActiveScheduler::Exit();
- }
- case EEikCmdAbout:
- {
- CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
- dialog->ExecuteLD(R_EXAMPLE_ABOUT_DIALOG);
- break;
- }
- case EEikCmdNewGame:
- {
- if((iContainer->iPyramidControl->Score > 0) && (iContainer->iPyramidControl->Score < 52))
- {
- CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
- if(dialog->ExecuteLD(R_EXAMPLE_ADDTOSTATISTICS_DIALOG))
- {
- iContainer->iPyramidControl->GamesPlayed[iContainer->iPyramidControl->Redeals]++;
- iContainer->iPyramidControl->TotalScore[iContainer->iPyramidControl->Redeals] += iContainer->iPyramidControl->Score;
- if(iContainer->iPyramidControl->Score > iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals])
- iContainer->iPyramidControl->MaxScore[iContainer->iPyramidControl->Redeals] = iContainer->iPyramidControl->Score;
- }
- }
- iContainer->iPyramidControl->Restart();
- break;
- }
- case EEikCmdStatistics:
- {
- CEikDialog* dialog = new (ELeave) CStatDialog(&iContainer->iPyramidControl->GamesPlayed[0],
- &iContainer->iPyramidControl->GamesWon[0], &iContainer->iPyramidControl->MaxScore[0],
- &iContainer->iPyramidControl->TotalScore[0]);
- dialog->ExecuteLD(R_EXAMPLE_STATISTICS_DIALOG);
- break;
- }
-
- case EEikCmdCStatistics:
- {
- CEikDialog* dialog = new (ELeave) CExampleAboutDialog();
- if(dialog->ExecuteLD(R_EXAMPLE_CSTATISTICS_DIALOG))
- {
- int i;
- for(i = 0; i < 4; i++)
- {
- iContainer->iPyramidControl->GamesPlayed[i] = 0;
- iContainer->iPyramidControl->GamesWon[i] = 0;
- iContainer->iPyramidControl->MaxScore[i] = 0;
- iContainer->iPyramidControl->TotalScore[i] = 0;
- }
- }
- break;
- }
-
- }
- }
-
- CPyramidAppUi::~CPyramidAppUi()
- {
- delete iContainer;
- }
-
- //
- // CPyramidDocument
- //
-
- CEikAppUi* CPyramidDocument::CreateAppUiL()
- {
- return(new(ELeave) CPyramidAppUi);
- }
-
- //
- // CPyramidApplication
- //
-
- TUid CPyramidApplication::AppDllUid() const
- {
- return KUidPyramidApp;
- }
-
- CApaDocument* CPyramidApplication::CreateDocumentL()
- {
- return new(ELeave) CPyramidDocument(*this);
- }
-
- //
- // EXPORTed functions
- //
-
- EXPORT_C CApaApplication* NewApplication()
- {
- return new CPyramidApplication;
- }
-
- GLDEF_C TInt E32Dll(TDllReason)
- {
- return KErrNone;
- }
-
- void CPrefDialog::PreLayoutDynInitL()
- {
- TInt dlgid;
- switch(*dlgpref)
- {
- case 1:dlgid = EExampleControlIdHorOptButton2; break;
- case 2:dlgid = EExampleControlIdHorOptButton3; break;
- case 3:dlgid = EExampleControlIdHorOptButton4; break;
- default: dlgid = EExampleControlIdHorOptButton1; break;
- }
- SetHorOptionButListCurrentSet(EExampleControlIdHorOptButtonList,dlgid);
- }
-
- TBool CPrefDialog::OkToExitL(TInt)
- {
- switch(HorOptionButListCurrentSet(EExampleControlIdHorOptButtonList))
- {
- case EExampleControlIdHorOptButton2: *dlgpref = 1; break;
- case EExampleControlIdHorOptButton3: *dlgpref = 2; break;
- case EExampleControlIdHorOptButton4: *dlgpref = 3; break;
- default: *dlgpref = 0; break;
- }
- return ETrue;
- }
-
- void CStatDialog::PreLayoutDynInitL()
- {
- TBuf<60> txt;
- TReal r;
- TRealFormat rf(6,2);
- int i;
- txt.Format(_L("Games Played: %d / %d / %d / %d"), gamesp[0], gamesp[1], gamesp[2], gamesp[3]);
- SetLabelL(EExampleControlIdStat2, txt);
- txt.Format(_L("Games Won: %d / %d / %d / %d"), gamesw[0], gamesw[1], gamesw[2], gamesw[3]);
- SetLabelL(EExampleControlIdStat3, txt);
- txt = _L("Win Percent: ");
- for(i = 0;; i++)
- {
- if(gamesp[i] == 0)
- r = 0;
- else
- r = ((TReal)gamesw[i]/(TReal)gamesp[i]) * 100;
- txt.AppendNum(r, rf);
- txt.Append('%');
- if(i == 3)
- break;
- txt.Append(_L(" / "));
- }
- SetLabelL(EExampleControlIdStat4, txt);
- txt = _L("Ave Score: ");
- for(i = 0;; i++)
- {
- if(gamesp[i] == 0)
- r = 0;
- else
- r = (TReal)tscore[i]/(TReal)gamesp[i];
- txt.AppendNum(r, rf);
- if(i == 3)
- break;
- txt.Append(_L(" / "));
- }
- SetLabelL(EExampleControlIdStat5, txt);
- txt.Format(_L("Best Score: %d / %d / %d / %d"), mscore[0], mscore[1], mscore[2], mscore[3]);
- SetLabelL(EExampleControlIdStat6, txt);
- }
-
- const TUid PROFILE_UID={0x1000055E};
-
- void CPyramidAppUi::UpdateProfile()
- {
- int i;
- // Open the "ini" file. Application architecture creates the "ini" file
- // if it does not exist
- CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
-
- // The stream containing the background preference will be associated
- // with a Uid. AssignL() creates the stream if it does not exist
- RDictionaryWriteStream writeStream;
- writeStream.AssignLC(*iniFile,PROFILE_UID);
- writeStream.WriteInt32L(iContainer->iPyramidControl->MaxRedeals);
- for(i = 0; i < 4; i++)
- {
- writeStream.WriteInt32L(iContainer->iPyramidControl->GamesPlayed[i]);
- writeStream.WriteInt32L(iContainer->iPyramidControl->GamesWon[i]);
- writeStream.WriteInt32L(iContainer->iPyramidControl->MaxScore[i]);
- writeStream.WriteInt32L(iContainer->iPyramidControl->TotalScore[i]);
- }
- writeStream.CommitL();
- CleanupStack::PopAndDestroy();
-
- // Commit changes to the store
- if (iniFile->Commit()!=KErrNone)
- iniFile->RevertL();
- // Destroys the "ini" file store object and closes
- // the "ini" file
- CleanupStack::PopAndDestroy(); // iniFile
- }
-
- void CPyramidAppUi::ReadProfile()
- {
- TInt v, i;
- // Open the "ini" file. Application architecture creates the "ini" file
- // if it does not exist
- CDictionaryStore* iniFile = Application()->OpenIniFileLC(iCoeEnv->FsSession());
-
- // The stream containing the background preference will be associated
- // with a Uid.
- // Open the stream associted with the Uid; it will be created
- // if it does not exist.
- RDictionaryReadStream readStream;
- readStream.OpenL(*iniFile,PROFILE_UID);
-
- iContainer->iPyramidControl->MaxRedeals = 2;
- for(i = 0; i < 4; i++)
- {
- iContainer->iPyramidControl->GamesPlayed[i] = 0;
- iContainer->iPyramidControl->GamesWon[i] = 0;
- iContainer->iPyramidControl->MaxScore[i] = 0;
- iContainer->iPyramidControl->TotalScore[i] = 0;
- }
- // If the stream has just been created (because the "ini" file has just
- // been created), it will be "empty" and attempting to read from
- // the stream will leave with a KErrEof.
- TRAPD(err, v = readStream.ReadInt32L());
- if (!err)
- {
- iContainer->iPyramidControl->MaxRedeals = v;
- for(i = 0; i < 4; i++)
- {
- v = readStream.ReadInt32L();
- iContainer->iPyramidControl->GamesPlayed[i] = v;
- v = readStream.ReadInt32L();
- iContainer->iPyramidControl->GamesWon[i] = v;
- v = readStream.ReadInt32L();
- iContainer->iPyramidControl->MaxScore[i] = v;
- v = readStream.ReadInt32L();
- iContainer->iPyramidControl->TotalScore[i] = v;
- }
- }
- readStream.Close();
-
- CleanupStack::PopAndDestroy();
-
- }
-