Menu.qml 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import QtQuick 1.0
  5. import "Game.js" as GameScript
  6. Item {
  7. id: menu
  8. signal levelSelected(int levelIndex)
  9. opacity: 0
  10. smooth: true
  11. state: "hidden"
  12. /**
  13. * Creates the items for the main menu (including the levels if any plugins
  14. * are installed) and displays the menu.
  15. */
  16. function showMainMenu()
  17. {
  18. mainMenuData.clear();
  19. listView.model = mainMenuData;
  20. var plugins = GameEngine.pluginList();
  21. // Plugin list have to be first in the menu (because usage of index)
  22. for (var i = 0; i < plugins.length; i++) {
  23. mainMenuData.append({ "name": plugins[i].displayname,
  24. "type": 0,
  25. "thumbnail": "file:/" + plugins[i].thumbnailPath,
  26. "icon": "gfx/icon_resume.png" });
  27. }
  28. // And then rest of level items
  29. if (IAP) {
  30. mainMenuData.append({ "name": "Buy Levels",
  31. "type": 5,
  32. "thumbnail": "qrc:/gfx/thumbnail.png",
  33. "icon":"gfx/icon_buy.png" });
  34. }
  35. mainMenuData.append({ "name": "About",
  36. "type": 4,
  37. "thumbnail": "qrc:/gfx/thumbnail.png",
  38. "icon": "gfx/icon_about.png" });
  39. if (!GameEngine.isHarmattan()) {
  40. // No exit menu item on Harmattan
  41. mainMenuData.append({ "name": "Exit",
  42. "type": 1,
  43. "thumbnail": "qrc:/gfx/thumbnail.png",
  44. "icon":"gfx/icon_exit.png" });
  45. }
  46. menu.state = "mainMenuShown";
  47. }
  48. /**
  49. * Creates the items for a pause menu and displays the menu.
  50. */
  51. function showPauseMenu()
  52. {
  53. listView.model = menuData;
  54. if (menuData.count > 0) {
  55. // Items already created.
  56. menu.state = "pauseMenuShown";
  57. return;
  58. }
  59. menuData.append({ "name": "Resume",
  60. "type": 2,
  61. "thumbnail": "qrc:/gfx/thumbnail.png",
  62. "icon": "gfx/icon_resume.png" });
  63. menuData.append({ "name": "End Game",
  64. "type": 3,
  65. "thumbnail": "qrc:/gfx/thumbnail.png",
  66. "icon": "gfx/icon_end_game.png" });
  67. if (!GameEngine.isHarmattan()) {
  68. // No exit menu item on Harmattan
  69. menuData.append({ "name": "Exit",
  70. "type": 1,
  71. "thumbnail": "qrc:/gfx/thumbnail.png",
  72. "icon": "gfx/icon_exit.png" });
  73. }
  74. menu.state = "pauseMenuShown";
  75. }
  76. /**
  77. * Hides the menu.
  78. */
  79. function hideMenu()
  80. {
  81. menu.state = "hidden";
  82. listView.focus = false;
  83. }
  84. /**
  85. * Takes action according to the selected menu item.
  86. *
  87. * @param index If the menu item is a level selection item, defines which
  88. * of the levels was selected.
  89. * @param type The type of the menu item (e.g. level selection item).
  90. */
  91. function menuItemSelected(index, type)
  92. {
  93. switch (type) {
  94. case 0:
  95. // Level selection
  96. menu.state = "hidden";
  97. menu.levelSelected(index);
  98. break;
  99. case 1:
  100. // Exit
  101. Qt.quit();
  102. break;
  103. case 2:
  104. // Resume
  105. GameScript.pauseGame(false);
  106. menu.state = "hidden";
  107. break;
  108. case 3:
  109. // End game
  110. showMainMenu();
  111. gameArea.endGame(true);
  112. break;
  113. case 4:
  114. // Show about
  115. var infoComp = Qt.createComponent("qrc:/InfoView.qml");
  116. var infoObj = infoComp.createObject(infoView);
  117. infoObj.opacity = 1;
  118. break;
  119. case 5:
  120. // Buy levels
  121. if (btnBack.opacity == 0) {
  122. btnBack.opacity = 1;
  123. menu.state = "hidden";
  124. var levelComp = Qt.createComponent("qrc:/Buy.qml");
  125. gameArea.buyView = levelComp.createObject(buyLevels);
  126. }
  127. break;
  128. default:
  129. break;
  130. }
  131. }
  132. ListModel {
  133. id: mainMenuData
  134. }
  135. ListModel {
  136. id: menuData
  137. }
  138. Component {
  139. id: menuDelegate
  140. Item {
  141. id: container
  142. property int menuIconX : icon.x + icon.width
  143. x: (listView.width - width) / 2
  144. width: listView.width * 0.9
  145. height: 80
  146. // Thumbnail image
  147. Image {
  148. anchors.horizontalCenter: parent.horizontalCenter
  149. anchors.fill: parent
  150. opacity: model.type === 0 ? 1 : 0
  151. source: model.thumbnail
  152. smooth: true
  153. }
  154. // Menu item icon
  155. Image {
  156. id: icon
  157. source: model.icon
  158. smooth: true
  159. anchors.verticalCenter: parent.verticalCenter
  160. anchors.left: parent.left
  161. }
  162. // Borders and text of the menu item
  163. TextLabel {
  164. id: menuItem
  165. property int type: model.type
  166. anchors.fill: parent
  167. mouseAreaEnabled: true
  168. text: model.name
  169. textLeftMargin: parent.menuIconX
  170. onClicked: {
  171. GameEngine.playInternalSound(1);
  172. menu.menuItemSelected(index, menuItem.type);
  173. }
  174. }
  175. }
  176. }
  177. Rectangle {
  178. id: menuBackground
  179. anchors.fill: parent
  180. border.color: "white"
  181. border.width: 2
  182. radius: 8
  183. smooth: true
  184. color: "black"
  185. opacity: 0.7
  186. }
  187. ListView {
  188. id: listView
  189. anchors.centerIn: menu
  190. width: menu.width * 0.95
  191. height: menu.height * 0.9
  192. clip: true
  193. delegate: menuDelegate
  194. model: menuData
  195. spacing: 15
  196. }
  197. ScrollBar { flickableItem: listView }
  198. states: [
  199. State {
  200. name: "hidden"
  201. },
  202. State {
  203. name: "mainMenuShown"
  204. },
  205. State {
  206. name: "pauseMenuShown"
  207. }
  208. ]
  209. transitions: [
  210. Transition {
  211. from: "*"; to: "hidden"
  212. ParallelAnimation {
  213. NumberAnimation { target: menu; property: "x";
  214. to: menu.width * -0.8;
  215. easing.type: Easing.InOutBack;
  216. duration: 500; }
  217. PropertyAnimation { target: menu; property: "opacity"; to: 0;
  218. duration: 500 }
  219. }
  220. },
  221. Transition {
  222. from: "*"; to: "mainMenuShown"
  223. SequentialAnimation {
  224. PropertyAction { target: menu; property: "opacity"; value: 1 }
  225. PropertyAction { target: menu; property: "x";
  226. value: menu.width * -1.2 }
  227. PropertyAction { target: menu; property: "y";
  228. value: (gameArea.yDim - menu.height) * 0.7 }
  229. NumberAnimation { target: menu; property: "x";
  230. to: (gameArea.xDim - menu.width) * 0.5;
  231. easing.type: Easing.InOutBack;
  232. duration: 500 }
  233. }
  234. },
  235. Transition {
  236. from: "*"; to: "pauseMenuShown"
  237. SequentialAnimation {
  238. PropertyAction { target: menu; property: "opacity"; value: 1 }
  239. PropertyAction { target: menu; property: "x";
  240. value: menu.width * -1.2 }
  241. PropertyAction { target: menu; property: "y";
  242. value: (gameArea.yDim - menu.height) * 0.7 }
  243. NumberAnimation { target: menu; property: "x";
  244. to: (gameArea.xDim - menu.width) * 0.5;
  245. easing.type: Easing.InOutBack;
  246. duration: 500 }
  247. }
  248. }
  249. ]
  250. }