Cascade.qml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 initialFeedRate: 500
  21. }
  22. QtObject {
  23. id: d
  24. property int feedrate: populated ? globalUtil.adjustedInterval : cascadeSettings.initialFeedRate
  25. property bool populated: false
  26. property bool paused: false
  27. property real columnRatio: globalSettings.useGoldenRatio ? globalVars.goldenRatio : globalSettings.lessGoldenRatio
  28. property real pace: 1.0/30.0
  29. property real columnWidth: root.width*goldenBeast(globalSettings.columnCount)
  30. function goldenBeast(col) {
  31. return (1 - d.columnRatio)/(1 - Math.pow(d.columnRatio, col))
  32. }
  33. }
  34. Repeater {
  35. model: globalSettings.columnCount
  36. delegate: columnComponent
  37. }
  38. Component {
  39. id: columnComponent
  40. Item {
  41. id: column
  42. property int stackHeight: 0
  43. property int xOffset: d.columnWidth/d.goldenBeast(index)
  44. property var pictureArray: []
  45. property var pictureQueue: []
  46. property bool full: {
  47. var fullStack = stackHeight > (1.3 + 1/globalSettings.columnCount)*root.height
  48. !d.populated && fullStack && (index === (globalSettings.columnCount - 1)) && (d.populated = true)
  49. return fullStack
  50. }
  51. function addExistingImage(image) {
  52. image.width = width
  53. image.x = image.y = index*-1000
  54. image.linearVelocity.x = image.linearVelocity.y = 0.0
  55. image.beyondThePale.connect(removeImage)
  56. stackHeight += image.height
  57. image.x = xOffset
  58. image.y = index == 0 ? root.height - stackHeight : -stackHeight - pictureArray.length*10
  59. image.world = isolatedWorld
  60. pictureArray.push(image)
  61. }
  62. function addImage() {
  63. var image = pictureDelegate.createObject(column)
  64. addExistingImage(image)
  65. globalUtil.itemCount++
  66. }
  67. function removeImage(image) {
  68. image.beyondThePale.disconnect(removeImage)
  69. if (index === (globalSettings.columnCount - 1)) {
  70. image.destroy()
  71. globalUtil.itemCount--
  72. } else {
  73. columnArray[index+1].pictureQueue.push(image)
  74. }
  75. }
  76. function shift() {
  77. var image = pictureArray.shift()
  78. image.world = image.world.limbo
  79. stackHeight -= image.height
  80. }
  81. width: {
  82. var colWidth = d.columnWidth*Math.pow(d.columnRatio, index);
  83. (index === (globalSettings.columnCount - 1)) && (globalVars.imageWidthOverride = colWidth)
  84. return colWidth
  85. }
  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. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  141. }
  142. }