Cascade.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. import Qt.labs.settings 1.0
  4. import ".."
  5. Item {
  6. id: root
  7. signal togglePause
  8. signal next
  9. property var columnArray: []
  10. property var pictureDelegate: Component {
  11. CascadeDelegate {}
  12. }
  13. function drain() {
  14. // TODO: implement draining of all visible artwork
  15. }
  16. anchors.fill: parent
  17. Settings {
  18. id: cascadeSettings
  19. category: "Cascade"
  20. property int columnCount: 6
  21. property int feedRate: 5000
  22. property real ratio: 1.4
  23. property bool useGoldenRatio: true
  24. }
  25. QtObject {
  26. id: d
  27. property int feedrate: populated ? cascadeSettings.feedRate : 500
  28. property bool populated: false
  29. property bool paused: false
  30. property real goldenRatio: 1.61803398875
  31. property real columnRatio: cascadeSettings.useGoldenRatio ? 1.61803398875 : 1.45
  32. property real pace: 1.0/30.0
  33. property real columnWidth: root.width*goldenBeast(cascadeSettings.columnCount)
  34. function goldenBeast(col) {
  35. return (1 - d.columnRatio)/(1 - Math.pow(d.columnRatio, col))
  36. }
  37. }
  38. Repeater {
  39. model: cascadeSettings.columnCount
  40. delegate: columnComponent
  41. }
  42. Component {
  43. id: columnComponent
  44. Item {
  45. id: column
  46. property int stackHeight: 0
  47. property int xOffset: d.columnWidth/d.goldenBeast(index)
  48. property var pictureArray: []
  49. property var pictureQueue: []
  50. property bool full: {
  51. var fullStack = stackHeight > (1.3 + 1/cascadeSettings.columnCount)*root.height
  52. !d.populated && fullStack && (index == (cascadeSettings.columnCount)-1) && (d.populated = true)
  53. return fullStack
  54. }
  55. function addExistingImage(image) {
  56. image.width = width
  57. image.x = image.y = index*-1000
  58. image.linearVelocity.x = image.linearVelocity.y = 0.0
  59. image.beyondThePale.connect(removeImage)
  60. stackHeight += image.height
  61. image.x = xOffset
  62. image.y = index == 0 ? root.height - stackHeight : -stackHeight - pictureArray.length*10
  63. image.world = isolatedWorld
  64. pictureArray.push(image)
  65. }
  66. function addImage() {
  67. var image = pictureDelegate.createObject(column)
  68. addExistingImage(image)
  69. globalUtil.itemCount++
  70. }
  71. function removeImage(image) {
  72. image.beyondThePale.disconnect(removeImage)
  73. if (index === cascadeSettings.columnCount-1) {
  74. image.destroy()
  75. globalUtil.itemCount--
  76. } else {
  77. columnArray[index+1].pictureQueue.push(image)
  78. }
  79. }
  80. function shift() {
  81. var image = pictureArray.shift()
  82. image.world = image.world.limbo
  83. stackHeight -= image.height
  84. }
  85. width: d.columnWidth*Math.pow(d.columnRatio, index)
  86. anchors { top: parent.top; bottom: parent.bottom }
  87. World {
  88. id: isolatedWorld
  89. timeStep: d.pace
  90. running: true
  91. property var limbo: World {
  92. timeStep: isolatedWorld.timeStep
  93. running: isolatedWorld.running
  94. }
  95. }
  96. RectangleBoxBody {
  97. id: floor
  98. world: isolatedWorld
  99. height: 0
  100. width: parent.width
  101. x: xOffset
  102. anchors {
  103. top: parent.bottom
  104. }
  105. friction: 1
  106. }
  107. Timer {
  108. id: pumpTimer
  109. interval: d.feedrate
  110. repeat: true && !d.paused
  111. running: true
  112. onTriggered: {
  113. if (index === 0) {
  114. addImage()
  115. } else {
  116. pictureQueue.length && addExistingImage(pictureQueue.shift())
  117. }
  118. }
  119. }
  120. Timer {
  121. id: deathTimer
  122. running: full
  123. repeat: true
  124. interval: d.feedrate
  125. onTriggered: shift()
  126. }
  127. Connections {
  128. target: root
  129. onTogglePause: d.paused = !d.paused
  130. onNext: deathTimer.triggered()
  131. }
  132. Component.onCompleted: {
  133. columnArray.push(this)
  134. }
  135. }
  136. }
  137. Keys.onUpPressed: d.paused = !d.paused
  138. Keys.onDownPressed: root.drain()
  139. Component.onCompleted: {
  140. globalVars.loadFullImage = true
  141. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  142. }
  143. }