Cascade.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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*globalUtil.columnWidthRatio(d.columnRatio, globalSettings.columnCount)
  30. }
  31. Repeater {
  32. model: globalSettings.columnCount
  33. delegate: columnComponent
  34. }
  35. Component {
  36. id: columnComponent
  37. Item {
  38. id: column
  39. property int stackHeight: 0
  40. property int xOffset: d.columnWidth/globalUtil.columnWidthRatio(d.columnRatio, index)
  41. property var pictureArray: []
  42. property var pictureQueue: []
  43. property bool full: {
  44. var fullStack = stackHeight > (1.3 + 1/globalSettings.columnCount)*root.height
  45. !d.populated && fullStack && (index === (globalSettings.columnCount - 1)) && (d.populated = true)
  46. return fullStack
  47. }
  48. function addExistingImage(image) {
  49. image.width = width
  50. image.x = image.y = index*-1000
  51. image.linearVelocity.x = image.linearVelocity.y = 0.0
  52. image.beyondThePale.connect(removeImage)
  53. stackHeight += image.height
  54. image.x = xOffset
  55. image.y = index == 0 ? root.height - stackHeight : -stackHeight - pictureArray.length*10
  56. image.world = isolatedWorld
  57. pictureArray.push(image)
  58. }
  59. function addImage() {
  60. var image = pictureDelegate.createObject(column)
  61. addExistingImage(image)
  62. globalUtil.itemCount++
  63. }
  64. function removeImage(image) {
  65. image.beyondThePale.disconnect(removeImage)
  66. if (index === (globalSettings.columnCount - 1)) {
  67. image.destroy()
  68. globalUtil.itemCount--
  69. } else {
  70. columnArray[index+1].pictureQueue.push(image)
  71. }
  72. }
  73. function shift() {
  74. var image = pictureArray.shift()
  75. image.world = image.world.limbo
  76. stackHeight -= image.height
  77. }
  78. width: {
  79. var colWidth = d.columnWidth*Math.pow(d.columnRatio, index);
  80. (index === (globalSettings.columnCount - 1)) && (globalVars.imageWidthOverride = colWidth)
  81. return colWidth
  82. }
  83. anchors { top: parent.top; bottom: parent.bottom }
  84. World {
  85. id: isolatedWorld
  86. timeStep: d.pace
  87. running: true
  88. property var limbo: World {
  89. timeStep: isolatedWorld.timeStep
  90. running: isolatedWorld.running
  91. }
  92. }
  93. RectangleBoxBody {
  94. id: floor
  95. world: isolatedWorld
  96. height: 0
  97. width: parent.width
  98. x: xOffset
  99. anchors {
  100. top: parent.bottom
  101. }
  102. friction: 1
  103. }
  104. Timer {
  105. id: pumpTimer
  106. interval: d.feedrate
  107. repeat: true && !d.paused
  108. running: true
  109. onTriggered: {
  110. if (index === 0) {
  111. addImage()
  112. } else {
  113. pictureQueue.length && addExistingImage(pictureQueue.shift())
  114. }
  115. }
  116. }
  117. Timer {
  118. id: deathTimer
  119. running: full
  120. repeat: true
  121. interval: d.feedrate
  122. onTriggered: shift()
  123. }
  124. Connections {
  125. target: root
  126. onTogglePause: d.paused = !d.paused
  127. onNext: deathTimer.triggered()
  128. }
  129. Component.onCompleted: {
  130. columnArray.push(this)
  131. }
  132. }
  133. }
  134. Keys.onUpPressed: d.paused = !d.paused
  135. Keys.onDownPressed: root.drain()
  136. Component.onCompleted: {
  137. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  138. }
  139. }