samegame.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. import "SamegameCore"
  6. import "SamegameCore/samegame.js" as Logic
  7. Rectangle {
  8. id: screen
  9. function initialize() {
  10. Logic.startNewGame()
  11. }
  12. anchors.fill: parent
  13. SystemPalette { id: activePalette }
  14. Item {
  15. width: parent.width
  16. anchors { top: toolBar.bottom; bottom: parent.bottom }
  17. Image {
  18. id: background
  19. anchors.fill: parent
  20. source: "SamegameCore/pics/background.png"
  21. fillMode: Image.PreserveAspectCrop
  22. }
  23. Item {
  24. id: gameCanvas
  25. property int score: 0
  26. property int blockSize: 40
  27. z: 20; anchors.centerIn: parent
  28. width: parent.width - (parent.width % blockSize);
  29. height: parent.height - (parent.height % blockSize);
  30. MouseArea {
  31. anchors.fill: parent; onClicked: Logic.handleClick(mouse.x,mouse.y);
  32. }
  33. }
  34. }
  35. Dialog {
  36. id: dialog
  37. anchors {
  38. centerIn: parent
  39. horizontalCenterOffset: -10 // To allow the score show beside the dialog
  40. }
  41. z: 21
  42. onClicked: forceClose()
  43. }
  44. MouseArea {
  45. id: nameInputDialogExitMouseArea
  46. anchors.fill: parent
  47. enabled: nameInputDialog.opacity == 1
  48. onClicked: nameInputDialog.acceptText()
  49. Dialog {
  50. id: nameInputDialog
  51. property int initialWidth: 0
  52. function acceptText() {
  53. if (nameInputText.text != "")
  54. Logic.saveHighScore(nameInputText.text);
  55. nameInputText.closeSoftwareInputPanel();
  56. nameInputDialog.forceClose();
  57. }
  58. function rejectText() {
  59. nameInputText.closeSoftwareInputPanel();
  60. nameInputDialog.forceClose();
  61. }
  62. function updateWidth() {
  63. var newWidth = nameInputText.width + dialogText.width + 40;
  64. if ( (newWidth > nameInputDialog.width && newWidth < screen.width)
  65. || (nameInputDialog.width > nameInputDialog.initialWidth) )
  66. nameInputDialog.width = newWidth;
  67. }
  68. anchors.centerIn: parent
  69. anchors.verticalCenterOffset: -40
  70. height: dialogText.height + okButton.height + 65
  71. z: 22;
  72. onClicked: {
  73. nameInputText.focus = true;
  74. nameInputText.openSoftwareInputPanel();
  75. }
  76. Behavior on width {
  77. NumberAnimation {}
  78. enabled: nameInputDialog.initialWidth != 0
  79. }
  80. Text {
  81. id: dialogText
  82. anchors {
  83. top: parent.top; topMargin: 20
  84. left: nameInputDialog.left; leftMargin: 20
  85. }
  86. text: "You won! Please enter your name: "
  87. color: "white"
  88. }
  89. Rectangle {
  90. anchors.fill: nameInputText
  91. opacity: 0.3
  92. color: "gray"
  93. }
  94. TextInput {
  95. id: nameInputText
  96. anchors {
  97. top: dialogText.top
  98. left: dialogText.right
  99. }
  100. focus: nameInputDialog.opacity == 1 ? true : false
  101. maximumLength: 20
  102. color: "white"
  103. onTextChanged: nameInputDialog.updateWidth()
  104. onAccepted: nameInputDialog.acceptText()
  105. }
  106. Row {
  107. anchors {
  108. top: nameInputText.bottom; topMargin: 20
  109. horizontalCenter: parent.horizontalCenter
  110. }
  111. spacing: 20
  112. Button {
  113. id: okButton
  114. height: 20
  115. text: " OK "
  116. onClicked: {
  117. nameInputDialog.acceptText()
  118. }
  119. }
  120. Button {
  121. id: cancelButton
  122. height: 20
  123. text: " Cancel "
  124. onClicked: nameInputDialog.rejectText()
  125. }
  126. }
  127. }
  128. }
  129. Rectangle {
  130. id: toolBar
  131. width: parent.width; height: 60
  132. color: activePalette.window
  133. anchors.top: screen.top
  134. Button {
  135. id: newGameButton
  136. anchors { left: parent.left; leftMargin: 3; verticalCenter: parent.verticalCenter }
  137. text: "New Game"
  138. onClicked: Logic.startNewGame()
  139. }
  140. Text {
  141. id: score
  142. anchors { right: parent.right; rightMargin: 80; verticalCenter: parent.verticalCenter }
  143. text: "Score: " + gameCanvas.score
  144. font.bold: true
  145. color: activePalette.windowText
  146. }
  147. }
  148. }