card.qml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation.
  3. */
  4. import Qt 4.7
  5. Item {
  6. id: card
  7. property int index // used to calculate the card position
  8. property int hgap: 10 // horizontal gap between cards
  9. property int vgap: 10 // vertical gap between cards
  10. property int number // contains the number of the card
  11. property bool turned
  12. property bool vanished: false
  13. signal cardSelected(int index)
  14. function calWidth() {
  15. return (game.width - hgap) / game.columns - hgap
  16. }
  17. function calHeight() {
  18. return (game.height - vgap) / game.rows - vgap
  19. }
  20. function calX() {
  21. var xcell = index % game.columns
  22. return hgap + xcell * (width + hgap)
  23. }
  24. function calY() {
  25. return vgap + Math.floor(index / game.columns) * (height + vgap)
  26. }
  27. function born() {
  28. startAnimation.start()
  29. }
  30. function vanish() {
  31. vanished = true
  32. card.z = 0
  33. vanishAnimation.start()
  34. }
  35. function die() {
  36. vanishAnimation.stop()
  37. dieAnimation.start()
  38. }
  39. x: hgap; y: vgap
  40. width: calWidth(); height: calHeight()
  41. Flipable {
  42. property int angle: 0
  43. id: flipable
  44. anchors.fill: parent
  45. MouseArea {
  46. anchors.fill: parent
  47. onClicked: { if(vanished == false) game.handleClick(index) }
  48. }
  49. transform: Rotation {
  50. id: rotation
  51. origin.x: flipable.width / 2; origin.y: flipable.height / 2
  52. axis.x: 0; axis.y: 1; axis.z: 0 // rotate around y-axis
  53. angle: flipable.angle
  54. }
  55. front: Image { anchors.fill: parent; source: "card_back.png" }
  56. back: Image {
  57. anchors.fill: parent; source: "card_front.png"
  58. Text {
  59. text: number
  60. anchors.centerIn: parent
  61. font.bold: true; font.pixelSize: parent.width * 0.60
  62. color: "#FF202020"
  63. }
  64. }
  65. SequentialAnimation {
  66. id: startAnimation
  67. ParallelAnimation {
  68. PropertyAnimation { target: card; property: "x"; to: calX(); easing.type: "InOutCubic"; duration: 2000 }
  69. PropertyAnimation { target: card; property: "y"; to: calY(); easing.type: "InOutCubic"; duration: 2000 }
  70. }
  71. }
  72. SequentialAnimation {
  73. id: vanishAnimation
  74. PauseAnimation { duration: 500 }
  75. ParallelAnimation {
  76. PropertyAnimation { target: card; property: "rotation"; to: 900; easing.type: "InCubic"; duration: 3000 }
  77. PropertyAnimation { target: card; property: "x"; to: game.width * 2; easing.type: "InOutCubic"; duration: 4000 }
  78. }
  79. }
  80. SequentialAnimation {
  81. id: dieAnimation
  82. ParallelAnimation {
  83. PropertyAnimation { target: card; property: "rotation"; to: 0; easing.type: "InOutCubic"; duration: 2500 }
  84. PropertyAnimation { target: card; property: "x"; to: calX(); easing.type: "InOutCubic"; duration: 3000 }
  85. }
  86. ScriptAction { script: { turned = false } }
  87. PauseAnimation { duration: 1000 }
  88. ParallelAnimation {
  89. PropertyAnimation { target: card; property: "x"; to: hgap; easing.type: "InOutCubic"; duration: 2000 }
  90. PropertyAnimation { target: card; property: "y"; to: vgap; easing.type: "InOutCubic"; duration: 2000 }
  91. }
  92. ScriptAction { script: { game.initialize() } }
  93. }
  94. states: State {
  95. name: "back"
  96. when: turned
  97. PropertyChanges { target: flipable; angle: 180 }
  98. }
  99. transitions: Transition {
  100. NumberAnimation { property: "angle"; duration: 500 }
  101. }
  102. }
  103. }