Gravity.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. Item {
  4. id: root
  5. signal togglePause
  6. signal toggleChaos
  7. signal next
  8. property bool globalWorld: settings.fitByHeight
  9. property var worldArray: []
  10. property var pictureDelegate: Qt.createComponent(settings.fitByHeight ? "VerticalArtDelegate.qml" : "HorizontalArtDelegate.qml")
  11. anchors.fill: parent
  12. QtObject {
  13. id: d
  14. property double pace: settings.pace/60.0
  15. property int itemCount: 0
  16. }
  17. World {
  18. id: bullshitWorld
  19. timeStep: d.pace
  20. }
  21. World {
  22. // Global world at odds with relative positions!
  23. id: commonWorld
  24. timeStep: d.pace
  25. }
  26. Component {
  27. id: columnComponent
  28. Item {
  29. id: column
  30. x: xOffset - effectiveXOffset
  31. width: parent.width/settings.columnCount
  32. anchors { top: parent.top; bottom: parent.bottom }
  33. property var pictureArray: []
  34. property var physicsWorld: settings.globalWorld ? commonWorld : columnWorld
  35. property bool fixedRotation: true
  36. property int xOffset: width * index
  37. property int effectiveXOffset: settings.globalWorld ? xOffset : 0
  38. World {
  39. id: columnWorld
  40. timeStep: d.pace
  41. Component.onCompleted: worldArray.push(columnWorld)
  42. }
  43. RectangleBoxBody {
  44. world: physicsWorld
  45. height: 1
  46. anchors {
  47. left: parent.left
  48. right: parent.right
  49. top: parent.bottom
  50. }
  51. friction: 1
  52. density: 1
  53. }
  54. Timer {
  55. id: feedTimer
  56. running: true
  57. repeat: true
  58. interval: 1000*(settings.interval > 60 ? 60*(settings.interval-60) : settings.interval)*(Math.random()+1)
  59. onTriggered: {
  60. if (pictureArray.length > 0) {
  61. pictureArray.shift().world = bullshitWorld
  62. }
  63. var colHeight = pictureArray.reduce(function (height, image) { return height + image.height; }, 0)
  64. do {
  65. var item = pictureDelegate.createObject(column)
  66. item.leftViewport.connect(function() { d.itemCount--; })
  67. item.y = -colHeight - item.height
  68. d.itemCount++
  69. pictureArray.push(item)
  70. colHeight += item.height
  71. } while (colHeight < 1.5*root.height)
  72. }
  73. }
  74. Connections {
  75. target: root
  76. onTogglePause: feedTimer.running = !feedTimer.running
  77. onNext: feedTimer.triggered()
  78. onToggleChaos: fixedRotation = !fixedRotation
  79. }
  80. Timer {
  81. id: settleTimer
  82. running: false
  83. interval: 200
  84. onTriggered: feedTimer.triggered()
  85. }
  86. Component.onCompleted: settleTimer.start()
  87. }
  88. }
  89. // floor
  90. RectangleBoxBody {
  91. id: floor
  92. world: commonWorld
  93. height: 1
  94. anchors {
  95. left: parent.left
  96. right: parent.right
  97. top: parent.bottom
  98. }
  99. friction: 1
  100. density: 1
  101. }
  102. Repeater {
  103. model: settings.columnCount
  104. delegate: columnComponent
  105. }
  106. Connections {
  107. target: settings
  108. onColumnCountChanged: d.itemCount = 0
  109. }
  110. // TODO: The boot (Monty Python foot) of death to be applied to the stacks
  111. RectangleBoxBody {
  112. id: rect
  113. enabled: false
  114. visible: false
  115. friction: 1.0
  116. density: 1000
  117. color: "red"
  118. width: 50; height: 50
  119. bullet: true
  120. SequentialAnimation {
  121. id: murderAnimation
  122. //loops: Animation.Infinite
  123. //running: true
  124. ScriptAction { script: { root.togglePause() } }
  125. ScriptAction { script: { rect.world = worldArray.pop() } }
  126. PropertyAction { target: rect; property: "x"; value: -rect.width }
  127. PropertyAction { target: rect; property: "y"; value: root.height }
  128. ParallelAnimation {
  129. NumberAnimation { target: rect; property: "x"; to: 2560; duration: 1000 }
  130. NumberAnimation { target: rect; property: "y"; to: 0; duration: 1000 }
  131. }
  132. }
  133. }
  134. Rectangle {
  135. z: 1
  136. color: "black"
  137. anchors { right: parent.right; top: parent.top }
  138. width: itemCountLabel.width
  139. height: itemCountLabel.height
  140. Text {
  141. id: itemCountLabel
  142. font.pixelSize: 100
  143. text: d.itemCount
  144. color: "white"
  145. }
  146. }
  147. Keys.onUpPressed: root.togglePause()
  148. Keys.onDownPressed: root.toggleChaos() //root.next()
  149. }