CMenu.cpp
上传用户:snevogroup
上传日期:2008-06-06
资源大小:432k
文件大小:15k
源码类别:

Symbian

开发平台:

C/C++

  1. #include <avkon.hrh>
  2. #include <eikenv.h>
  3. #include <gdi.h>
  4. #include <aknutils.h>
  5. #include "CMenu.h"
  6. #include <images.mbg>
  7. #include <Tetris.rsg>
  8. // Construct and destruct
  9. // NewL()
  10. CMenu* CMenu::NewL(CFbsBitGc* aFbsBitGc)
  11. {
  12. CMenu* self = new (ELeave) CMenu(aFbsBitGc);
  13. CleanupStack::PushL(self);
  14. self->ConstructL();
  15. CleanupStack::Pop();
  16. return self;
  17. }
  18. // ~
  19. CMenu::~CMenu()
  20. {
  21. delete iBitmap;
  22. iSettingStr.DeleteAll();
  23. // iSettingStr.Reset();
  24. // cannot delete twice
  25. iMenuItemTxt.At(EOptions_RotateDir) = NULL;
  26. iMenuItemTxt.At(ESound_Sounds) = NULL;
  27. iMenuItemTxt.At(ESound_Music) = NULL;
  28. iMenuItemTxt.At(ESound_Volume) = NULL;
  29. iMenuItemTxt.At(EDifficulty_StartLines) = NULL;
  30. iMenuItemTxt.At(EDifficulty_AddBrick) = NULL;
  31. iMenuItemTxt.At(EDifficulty_Done) = NULL;
  32. iMenuItemTxt.At(EPause_Sound) = NULL;
  33. iMenuItemTxt.DeleteAll();
  34. //iMenuItemTxt.Reset();
  35. }
  36. // CMenu
  37. CMenu::CMenu(CFbsBitGc* aFbsBitGc) : iGc(aFbsBitGc)
  38. {}
  39. // ConsturctL
  40. void CMenu::ConstructL()
  41. {
  42. //iLabel = new (ELeave) CEikLabel;
  43. //iLabel->SetTextL(_L("ahhadkjdjfkldsajfkijoewijfioewjf"));
  44. _LIT(KPathName, "\System\Apps\Tetris\images.mbm");
  45. iBitmap = CEikonEnv::Static()->CreateBitmapL(KPathName, EMbmImagesLogo);
  46. LoadResL(); // Load menu resouce and position
  47. ResetPos();
  48. }
  49. ///////////////////////////////////////////////////////////////////////////////
  50. // Other Method
  51. // Draw
  52. void CMenu::Draw()
  53. {
  54. // draw background
  55. DrawBg();
  56. if(iMenuState == ECreditPage)
  57. {
  58. //iLabel->DrawNow();
  59. const CFont* font;
  60. font = CEikonEnv::Static()->TitleFont();
  61. iGc->UseFont(font);
  62. iGc->DrawText(_L("Tetris v1.2"), TPoint(60, 30));
  63. iGc->DrawText(_L("It's my #1 Symbian game, so"), TPoint(20, 60));
  64. iGc->DrawText(_L("it's written 100% from scratch!"), TPoint(10, 80));
  65. iGc->DrawText(_L("Suggestions, comments, or bug"), TPoint(10, 100));
  66. iGc->DrawText(_L("reports are welcome!"), TPoint(10, 120));
  67. iGc->DrawText(_L("My email : cofd.eric@163.com"), TPoint(10, 140));
  68. iGc->DrawText(_L("Thank you for playing!"), TPoint(10, 160));
  69. iGc->DrawText(_L("CArt Studio 2005"), TPoint(45, 200));
  70. iGc->DiscardFont();
  71. return;
  72. }
  73. if(iMenuState == EHighScorePage)
  74. return; // don't draw here
  75. // use default
  76. if(iMenuState == ENonePage)
  77. {
  78. iMenuState = EMainPage;
  79. iMenuItem  = EStart;
  80. return;
  81. }
  82. // Get an alternative font
  83. _LIT(KMyFontName,"Roman");
  84. CFont* myFont;
  85. TFontSpec myFontSpec(KMyFontName, 1);
  86. myFontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold);
  87. CBitmapDevice* screenDevice=CEikonEnv::Static()->ScreenDevice();
  88. screenDevice->GetNearestFontInTwips(myFont,myFontSpec);
  89. iGc->UseFont(myFont);
  90. // draw menu page
  91. DrawPage(iMenuPagePos[iMenuState].iX, iMenuPagePos[iMenuState].iY);
  92. // Draw the Blink frame and white selected txt
  93. iGc->SetPenColor(TRgb(250, 250, 250));
  94. iBlink++;
  95. if((iBlink >= 2 && iBlink <= 5) || (iBlink >= 18 && iBlink <= 21)) 
  96. iGc->SetPenColor(TRgb(220, 220, 220)); 
  97. else if((iBlink >= 6 && iBlink <= 9) || (iBlink >= 14 && iBlink <= 17))
  98. iGc->SetPenColor(TRgb(180, 180, 180)); 
  99. else if(iBlink >= 10 && iBlink <= 13)
  100. iGc->SetPenColor(TRgb(120, 120, 120));
  101. else if(iBlink == 22)
  102. iBlink = 0;
  103. // dont't modify Frame's positon
  104. SetFrame(iMenuItem);
  105. if(iSelect)
  106. {
  107. iGc->SetBrushColor(TRgb(90, 90, 255));
  108. iGc->DrawPolygon(iPointList, 12, CGraphicsContext::EAlternate);
  109. iSelect = 0;
  110. }
  111. iGc->DrawPolyLineNoEndPoint(iPointList, 12);
  112. // highlight the text
  113. iGc->SetPenColor(KRgbWhite);
  114. iGc->DrawText(*iMenuItemTxt.At(iMenuItem), TRect(iPointList[2]+TPoint(1,1), iPointList[8]-TPoint(1,1)), 15, CGraphicsContext::ECenter, 0);
  115. // delete created Font
  116. iGc->DiscardFont();
  117. screenDevice->ReleaseFont(myFont);
  118. }
  119. // DrawBg()
  120. void CMenu::DrawBg()
  121. {
  122. iGc->SetBrushStyle(CFbsBitGc::ESolidBrush);
  123. iGc->SetFadingParameters(80, 255);
  124. iGc->SetFaded(1);
  125. iGc->SetBrushColor(KRgbBlack);
  126. iGc->Clear();
  127. iGc->BitBlt(iPos + TPoint(0, 69), iBitmap);
  128. iGc->BitBlt(iPos + TPoint(0, 208), iBitmap);
  129. iGc->BitBlt(iPos + TPoint(185, 69), iBitmap);
  130. iGc->BitBlt(iPos + TPoint(185, 208), iBitmap);
  131. iPos -= TPoint(4, 3);
  132. if(iPos.iY <= -139)
  133. iPos = TPoint(0, 0);
  134. iGc->SetFaded(0);
  135. }
  136. // DrawPage()
  137. void CMenu::DrawPage(TInt aItemFrom, TInt aItemTo)
  138. {
  139. if(aItemFrom == ENoneItem)
  140. return;
  141. TInt i;
  142. // draw the frame
  143. iGc->SetBrushColor(TRgb(45, 45, 130));
  144. iGc->SetPenColor(TRgb(20, 20, 120));
  145. for(i = aItemFrom; i <= aItemTo; i++)
  146. {
  147. SetFrameDynamic(i);
  148. iGc->DrawPolygon(iPointList, 12, CGraphicsContext::EAlternate);
  149. }
  150. iSpeed += 5;
  151. // draw the Txt
  152. iGc->SetPenColor(KRgbGray);
  153. for(i = aItemFrom; i <= aItemTo; i++)
  154. {
  155. SetFrame(i);
  156. iGc->DrawText(*iMenuItemTxt.At(i), TRect(iPointList[2]+TPoint(1,1), iPointList[8]-TPoint(1,1)), 15, CGraphicsContext::ECenter, 0);
  157. }
  158. }
  159. // Command
  160. // @return 1 denote quit the game
  161. // @return 2 dentoe start the game
  162. TInt32 CMenu::Command(TInt aCommand)
  163. {
  164. switch(aCommand)
  165. {
  166. case 'q' :
  167. {
  168. if(iMenuState == EMainPage || iMenuState == EPausePage)
  169. break;
  170. iMenuState = iPrevPage;
  171. ResetPos();
  172. iMenuItem = iPrevItem;
  173. }return 5;
  174. case EStdKeyDevice5:
  175. case '5':
  176. case 63557:
  177. {
  178. switch(iMenuItem)
  179. {
  180. case EQuit:
  181. return 1; // Quit
  182. case EStart :
  183. {
  184. iPrevPage = EPausePage;
  185. iMenuState = ENonePage;
  186. iPrevItem = EPause_Continue;
  187. }return 2; // Start the game
  188. case EPause_Continue:
  189. {
  190. iMenuState = ENonePage;
  191. }return 3; // Continue the game
  192. case EOptions_Back:
  193. {
  194. iMenuState = EMainPage;
  195. ResetPos();
  196. iMenuItem = EStart;
  197. }return 11; // set rotation
  198. case ESound_Done :
  199. {
  200. iMenuState = iPrevPage;
  201. ResetPos();
  202. iMenuItem = iPrevItem;
  203. }return 12; // set Sound
  204. case EDifficulty_Done :
  205. {
  206. iMenuState = iPrevPage;
  207. ResetPos();
  208. iMenuItem = iPrevItem;
  209. }return 13; // set Difficulty
  210. case EOptions:
  211. {
  212. iPrevPage = EMainPage;
  213. iMenuState = EOptionsPage;
  214. ResetPos();
  215. iPrevItem = iMenuItem;
  216. iMenuItem = EOptions_Sound;
  217. }break;
  218. case ECredit:
  219. {
  220. iPrevPage = EMainPage;
  221. iMenuState = ECreditPage;
  222. iPrevItem = iMenuItem;
  223. iMenuItem = -1;
  224. }break;;
  225. case EHighScore:
  226. {
  227. iPrevPage = EMainPage;
  228. iMenuState = EHighScorePage;
  229. iPrevItem = iMenuItem;
  230. iMenuItem = -1;
  231. }return 4;
  232. // Options
  233. case EOptions_Sound:
  234. {
  235. iPrevPage = EOptionsPage;
  236. iMenuState = ESoundPage;
  237. ResetPos();
  238. iPrevItem = iMenuItem;
  239. iMenuItem = ESound_Sounds;
  240. }break;
  241. case EOptions_Difficulty:
  242. {
  243. iPrevPage = EOptionsPage;
  244. iMenuState = EDifficultyPage;
  245. ResetPos();
  246. iPrevItem = iMenuItem;
  247. iMenuItem = EDifficulty_StartLines;
  248. }break;
  249. // Pause page
  250. case EPause_Sound:
  251. {
  252. iPrevPage = EPausePage;
  253. iMenuState = ESoundPage;
  254. ResetPos();
  255. iPrevItem = iMenuItem;
  256. iMenuItem = ESound_Sounds;
  257. }break;
  258. case EPause_EndGame:
  259. {
  260. iMenuState = EMainPage; // return to main page
  261. ResetPos();
  262. iMenuItem = EStart;
  263. }break;
  264. case EOptions_RotateDir:
  265. case ESound_Sounds :
  266. case ESound_Music :
  267. case ESound_Volume :
  268. case EDifficulty_StartLines :
  269. case EDifficulty_AddBrick :
  270. {}break;
  271. default:
  272. {
  273. iMenuState = iPrevPage;
  274. ResetPos();
  275. iMenuItem = iPrevItem;
  276. }break;
  277. }
  278. }return 5;
  279. case EKeyUpArrow : // UP
  280. case '2' :
  281. {
  282. iMenuItem = iMenuItem <= iMenuPagePos[iMenuState].iX ? iMenuPagePos[iMenuState].iY : --iMenuItem;
  283. // select the menu item
  284. iSelect = 1;
  285. }return 0;
  286. case EKeyDownArrow : // Down
  287. case '8' :
  288. {
  289. iMenuItem = iMenuItem >= iMenuPagePos[iMenuState].iY ? iMenuPagePos[iMenuState].iX : ++iMenuItem;
  290. iSelect = 1;
  291. }return 0;
  292. case EKeyLeftArrow : // Left
  293. case '4' :
  294. {
  295. TInt Item = Index(iMenuItem);
  296. if(Item == -1)
  297. break;
  298. iSetting[Item] = iSetting[Item] <= iSettingPos[Item].iX ? iSettingPos[Item].iY : --iSetting[Item];
  299. iMenuItemTxt.At(iMenuItem) = iSettingStr.At(iSetting[Item]);
  300. }break;
  301. case EKeyRightArrow : // Right
  302. case '6' :
  303. {
  304. TInt Item = Index(iMenuItem);
  305. if(Item == -1)
  306. break;
  307. iSetting[Item] = iSetting[Item] >= iSettingPos[Item].iY ? iSettingPos[Item].iX : ++iSetting[Item];
  308. iMenuItemTxt.At(iMenuItem) = iSettingStr.At(iSetting[Item]);
  309. }break;
  310. // just hold on,wait for key
  311. default:
  312. {
  313. return 0;
  314. }break;
  315. }
  316. return 0;
  317. }
  318. // SetFrameDynamic()
  319. void CMenu::SetFrameDynamic(TInt aMenuItem)
  320. {
  321. TPoint& pos = iCurrentMenuItemPos[aMenuItem];
  322. TPoint aimPos = iMenuItemPos[aMenuItem];
  323. if(pos.iY < aimPos.iY)
  324. {
  325. pos.iY += iSpeed;
  326. if(pos.iY > aimPos.iY)
  327. pos.iY = aimPos.iY;
  328. }
  329. else if(pos.iY > aimPos.iY)
  330. {
  331. pos.iY -= iSpeed;
  332. if(pos.iY < aimPos.iY)
  333. pos.iY = aimPos.iY;
  334. }
  335. SetFrame(aMenuItem);
  336. }
  337. //
  338. void CMenu::SetFrame(TInt aMenuItem)
  339. {
  340. iPointList[0] = iCurrentMenuItemPos[aMenuItem] - TPoint(1, 0);
  341. iPointList[1] = iPointList[0] + TPoint( 33,  0);
  342. iPointList[2] = iPointList[1] + TPoint(  5, -8);
  343. iPointList[3] = iPointList[2] + TPoint(100,  0);
  344. iPointList[4] = iPointList[3] + TPoint(  5,  8);
  345. iPointList[5] = iPointList[4] + TPoint( 34,  0);
  346. iPointList[6] = iPointList[5] + TPoint( 0,  5);
  347. iPointList[7] = iPointList[4] + TPoint( 0,  5);
  348. iPointList[8] = iPointList[3] + TPoint( 0,  20);
  349. iPointList[9] = iPointList[2] + TPoint( 0,  20);
  350. iPointList[10] = iPointList[1] + TPoint( 0,  5);
  351. iPointList[11] = iPointList[0] + TPoint( 0,  5);
  352. }
  353. // LoadRes()
  354. // Init Menu 's position and Txt
  355. void CMenu::LoadResL()
  356. {
  357. CEikonEnv* eikonEnv = CEikonEnv::Static();
  358. iMenuState = EMainPage;
  359. iMenuItem = EStart;
  360. iSettingStr[0] = eikonEnv->AllocReadResourceL(R_MENUITEM_ROTATION1);
  361. iSettingStr[1] = eikonEnv->AllocReadResourceL(R_MENUITEM_ROTATION2);
  362. iSettingStr[2] = eikonEnv->AllocReadResourceL(R_MENUITEM_SOUNDS1);
  363. iSettingStr[3] = eikonEnv->AllocReadResourceL(R_MENUITEM_SOUNDS2);
  364. iSettingStr[4] = eikonEnv->AllocReadResourceL(R_MENUITEM_MUSIC1);
  365. iSettingStr[5] = eikonEnv->AllocReadResourceL(R_MENUITEM_MUSIC2);
  366. iSettingStr[6] = eikonEnv->AllocReadResourceL(R_MENUITEM_VOLUME1);
  367. iSettingStr[7] = eikonEnv->AllocReadResourceL(R_MENUITEM_VOLUME2);
  368. iSettingStr[8] = eikonEnv->AllocReadResourceL(R_MENUITEM_VOLUME3);
  369. iSettingStr[9] = eikonEnv->AllocReadResourceL(R_MENUITEM_VOLUME4);
  370. iSettingStr[10] = eikonEnv->AllocReadResourceL(R_MENUITEM_STARTLINES1);
  371. iSettingStr[11] = eikonEnv->AllocReadResourceL(R_MENUITEM_STARTLINES2);
  372. iSettingStr[12] = eikonEnv->AllocReadResourceL(R_MENUITEM_STARTLINES3);
  373. iSettingStr[13] = eikonEnv->AllocReadResourceL(R_MENUITEM_STARTLINES4);
  374. iSettingStr[14] = eikonEnv->AllocReadResourceL(R_MENUITEM_ADDBRICK1);
  375. iSettingStr[15] = eikonEnv->AllocReadResourceL(R_MENUITEM_ADDBRICK2);
  376. iSettingStr[16] = eikonEnv->AllocReadResourceL(R_MENUITEM_ADDBRICK3);
  377. // settings start & end
  378. iSettingPos[0] = TPoint(0, 1);
  379. iSettingPos[1] = TPoint(2, 3);
  380. iSettingPos[2] = TPoint(4, 5);
  381. iSettingPos[3] = TPoint(6, 9);
  382. iSettingPos[4] = TPoint(10, 13);
  383. iSettingPos[5] = TPoint(14, 16);
  384. // Init
  385. iSetting[0] = iSettingPos[0].iX;
  386. iSetting[1] = iSettingPos[1].iX;
  387. iSetting[2] = iSettingPos[2].iX;
  388. iSetting[3] = iSettingPos[3].iX;
  389. iSetting[4] = iSettingPos[4].iX;
  390. iSetting[5] = iSettingPos[5].iX;
  391. // set menuPage's start & end item 
  392. iMenuPagePos[EMainPage] = TPoint(0, 4);
  393. iMenuPagePos[EOptionsPage] = TPoint(5, 8);
  394. iMenuPagePos[ESoundPage] = TPoint(9, 12);
  395. iMenuPagePos[EDifficultyPage] = TPoint(13, 15);
  396. iMenuPagePos[EPausePage] = TPoint(16, 18);
  397. iMenuPagePos[ECreditPage] = TPoint(ENoneItem, ENoneItem);
  398. iMenuPagePos[EHighScorePage] = TPoint(ENoneItem, ENoneItem);
  399. // Menu item Position
  400. iMenuItemPos[EStart] = TPoint(0, 54);
  401. iMenuItemPos[EOptions] = TPoint(0, 79);
  402. iMenuItemPos[ECredit] = TPoint(0, 104);
  403. iMenuItemPos[EHighScore]= TPoint(0, 129);
  404. iMenuItemPos[EQuit] = TPoint(0, 154);
  405. iMenuItemPos[EOptions_Sound] = TPoint(0, 67);
  406. iMenuItemPos[EOptions_Difficulty] = TPoint(0, 92);
  407. iMenuItemPos[EOptions_RotateDir] = TPoint(0, 116);
  408. iMenuItemPos[EOptions_Back] = TPoint(0, 141);
  409. iMenuItemPos[ESound_Sounds] = TPoint(0, 67);
  410. iMenuItemPos[ESound_Music] = TPoint(0, 92);
  411. iMenuItemPos[ESound_Volume] = TPoint(0, 116);
  412. iMenuItemPos[ESound_Done] = TPoint(0, 141);
  413. iMenuItemPos[EDifficulty_StartLines] = TPoint(0, 74);
  414. iMenuItemPos[EDifficulty_AddBrick] = TPoint(0, 104);
  415. iMenuItemPos[EDifficulty_Done] = TPoint(0, 134);
  416. iMenuItemPos[EPause_Continue] = TPoint(0, 79);
  417. iMenuItemPos[EPause_Sound] = TPoint(0, 104);
  418. iMenuItemPos[EPause_EndGame] = TPoint(0, 129);
  419. // Menu Item Txt
  420. iMenuItemTxt.At(EStart) = eikonEnv->AllocReadResourceL(R_MENUITEM_START);
  421. iMenuItemTxt.At(EOptions) = eikonEnv->AllocReadResourceL(R_MENUITEM_OPTIONS);
  422. iMenuItemTxt.At(ECredit) = eikonEnv->AllocReadResourceL(R_MENUITEM_CREDIT);
  423. iMenuItemTxt.At(EHighScore) = eikonEnv->AllocReadResourceL(R_MENUITEM_HIGHSCORE);
  424. iMenuItemTxt.At(EQuit) = eikonEnv->AllocReadResourceL(R_MENUITEM_QUIT);
  425. iMenuItemTxt.At(EOptions_Sound) = eikonEnv->AllocReadResourceL(R_MENUITEM_SOUND);
  426. iMenuItemTxt.At(EOptions_Difficulty)= eikonEnv->AllocReadResourceL(R_MENUITEM_DIFFICULTY);
  427. iMenuItemTxt.At(EOptions_RotateDir) = iSettingStr.At(iSetting[0]);
  428. iMenuItemTxt.At(EOptions_Back) = eikonEnv->AllocReadResourceL(R_MENUITEM_BACK);
  429. iMenuItemTxt.At(ESound_Sounds) = iSettingStr.At(iSetting[1]);
  430. iMenuItemTxt.At(ESound_Music) = iSettingStr.At(iSetting[2]);
  431. iMenuItemTxt.At(ESound_Volume) = iSettingStr.At(iSetting[3]);
  432. iMenuItemTxt.At(ESound_Done) = eikonEnv->AllocReadResourceL(R_MENUITEM_DONE);
  433. iMenuItemTxt.At(EDifficulty_StartLines) = iSettingStr.At(iSetting[4]);
  434. iMenuItemTxt.At(EDifficulty_AddBrick) = iSettingStr.At(iSetting[5]);
  435. iMenuItemTxt.At(EDifficulty_Done) = iMenuItemTxt.At(ESound_Done);
  436. iMenuItemTxt.At(EPause_Continue)= eikonEnv->AllocReadResourceL(R_MENUITEM_CONTINUE);
  437. iMenuItemTxt.At(EPause_Sound) = iMenuItemTxt.At(EOptions_Sound);
  438. iMenuItemTxt.At(EPause_EndGame) = eikonEnv->AllocReadResourceL(R_MENUITEM_ENDGAME);
  439. }
  440. // ResetPos()
  441. void CMenu::ResetPos()
  442. {
  443. iSpeed = 1;
  444. TInt start = iMenuPagePos[iMenuState].iX;
  445. TInt end = iMenuPagePos[iMenuState].iY;
  446. for(TInt i = start; i <= end; i++)
  447. iCurrentMenuItemPos[i] = TPoint(0, 104);
  448. }
  449. // Index()
  450. TInt CMenu::Index(TInt aMenuItem)
  451. {
  452. switch(aMenuItem)
  453. {
  454. case EOptions_RotateDir :
  455. return 0;
  456. case ESound_Sounds :
  457. return 1;
  458. case ESound_Music :
  459. return 2;
  460. case ESound_Volume :
  461. return 3;
  462. case EDifficulty_StartLines :
  463. return 4;
  464. case EDifficulty_AddBrick :
  465. return 5;
  466. default: return -1;
  467. }
  468. }