Menu.qml 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. import "Game.js" as GameScript
  6. Rectangle {
  7. id: menu
  8. Text {
  9. id: title
  10. smooth: true
  11. anchors.bottom: menu.top
  12. anchors.horizontalCenter: menu.horizontalCenter
  13. color: "red"
  14. font.pixelSize: gameArea.width / 12
  15. font.bold: true
  16. horizontalAlignment: Text.AlignHCenter
  17. }
  18. border.color: "black"
  19. border.width: 2
  20. radius: 8
  21. smooth: true
  22. opacity: 0 // transparent by default
  23. color: "black"
  24. // gradient: Gradient {
  25. // GradientStop { position: 0.0; color: "white" }
  26. // GradientStop { position: 1.0; color: "black" }
  27. // }
  28. // Signals
  29. signal levelSelected(int levelIndex)
  30. signal resumeSelected()
  31. // Show level menu
  32. function showLevelMenu() {
  33. menuData.clear()
  34. title.text = "Select Your Level"
  35. var plugins = GameEngine.pluginList()
  36. for(var i=0;i<plugins.length;i++) {
  37. menuData.append({"name": plugins[i],"type":"0"})
  38. }
  39. menuData.append({"name": "About","type":"4"})
  40. menuData.append({"name": "Exit","type":"1"})
  41. menu.opacity = 0.8
  42. listView.focus = true
  43. }
  44. function showPauseMenu() {
  45. menuData.clear()
  46. title.text = "Resume level?"
  47. menuData.append({"name": "Resume","type":"2"})
  48. menuData.append({"name": "End Game","type":"3"})
  49. menu.opacity = 0.8
  50. listView.focus = true
  51. }
  52. // Hide menu
  53. function hideMenu() {
  54. menu.opacity = 0
  55. listView.focus = false
  56. }
  57. function menuItemSelected(index, type) {
  58. switch (type) {
  59. case "0": {
  60. // Level selection
  61. menu.levelSelected(index)
  62. break;
  63. }
  64. case "1": {
  65. // Exit
  66. Qt.quit()
  67. break;
  68. }
  69. case "2": {
  70. // Resume
  71. menu.resumeSelected()
  72. break;
  73. }
  74. case "3": {
  75. // End Game
  76. gameArea.endGame()
  77. break;
  78. }
  79. case "4": {
  80. // Show about
  81. gameArea.messageBox.showInfoMessage()
  82. break;
  83. }
  84. default: {
  85. break;
  86. }
  87. }
  88. }
  89. ListModel {
  90. id: menuData
  91. }
  92. ListView {
  93. id: listView
  94. clip: true
  95. anchors.fill: parent
  96. keyNavigationWraps: true
  97. function doSelect() {
  98. menu.menuItemSelected(listView.currentIndex,menuData.get(listView.currentIndex).type)
  99. }
  100. model: menuData
  101. delegate: MenuItem {
  102. id: menuItem
  103. selectedItem: menuItem.ListView.isCurrentItem ? true : false
  104. onItemSelected: {
  105. listView.currentIndex = model.index
  106. listView.doSelect()
  107. }
  108. }
  109. }
  110. }