Cascade.qml 5.1 KB

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