Cascade.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: {
  86. var colWidth = d.columnWidth*Math.pow(d.columnRatio, index)
  87. index === cascadeSettings.columnCount - 1 && (globalVars.imageWidthOverride = colWidth)
  88. return colWidth
  89. }
  90. anchors { top: parent.top; bottom: parent.bottom }
  91. World {
  92. id: isolatedWorld
  93. timeStep: d.pace
  94. running: true
  95. property var limbo: World {
  96. timeStep: isolatedWorld.timeStep
  97. running: isolatedWorld.running
  98. }
  99. }
  100. RectangleBoxBody {
  101. id: floor
  102. world: isolatedWorld
  103. height: 0
  104. width: parent.width
  105. x: xOffset
  106. anchors {
  107. top: parent.bottom
  108. }
  109. friction: 1
  110. }
  111. Timer {
  112. id: pumpTimer
  113. interval: d.feedrate
  114. repeat: true && !d.paused
  115. running: true
  116. onTriggered: {
  117. if (index === 0) {
  118. addImage()
  119. } else {
  120. pictureQueue.length && addExistingImage(pictureQueue.shift())
  121. }
  122. }
  123. }
  124. Timer {
  125. id: deathTimer
  126. running: full
  127. repeat: true
  128. interval: d.feedrate
  129. onTriggered: shift()
  130. }
  131. Connections {
  132. target: root
  133. onTogglePause: d.paused = !d.paused
  134. onNext: deathTimer.triggered()
  135. }
  136. Component.onCompleted: {
  137. columnArray.push(this)
  138. }
  139. }
  140. }
  141. Keys.onUpPressed: d.paused = !d.paused
  142. Keys.onDownPressed: root.drain()
  143. Component.onCompleted: {
  144. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  145. }
  146. }