Cascade.qml 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. anchors.fill: parent
  14. Settings {
  15. id: cascadeSettings
  16. category: "Cascade"
  17. property int columnCount: 5
  18. property int feedRate: 1000
  19. }
  20. QtObject {
  21. id: d
  22. property real goldenRatio: 1.61803398875
  23. property real pace: 1.0/20.0
  24. property bool paused: false
  25. function goldenBeast(col) {
  26. return (1 - d.goldenRatio)/(1 - Math.pow(d.goldenRatio, col))
  27. }
  28. property real columnWidth: {
  29. var foo = root.width*goldenBeast(cascadeSettings.columnCount)
  30. console.log('Column width is:', foo)
  31. return foo
  32. }
  33. }
  34. Repeater {
  35. model: cascadeSettings.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 bool full: stackHeight > (1.3 + 1/cascadeSettings.columnCount)*root.height
  46. function addExistingImage(image) {
  47. // make sure there is no spacial conflict in limbo, or shit goes tits up
  48. image.width = width
  49. image.x = image.y = index*-1000
  50. image.linearVelocity.x = image.linearVelocity.y = 0.0
  51. image.beyondThePale.connect(removeImage)
  52. stackHeight += image.height
  53. image.x = xOffset
  54. image.y = - stackHeight
  55. image.world = isolatedWorld
  56. pictureArray.push(image)
  57. }
  58. function addImage() {
  59. var image = pictureDelegate.createObject(column)
  60. addExistingImage(image)
  61. globalUtil.itemCount++
  62. }
  63. function removeImage(image) {
  64. image.beyondThePale.disconnect(removeImage)
  65. if (index === cascadeSettings.columnCount-1) {
  66. image.destroy()
  67. globalUtil.itemCount--
  68. } else {
  69. columnArray[index+1].addExistingImage(image)
  70. }
  71. }
  72. function shift() {
  73. var image = pictureArray.shift()
  74. image.world = image.world.limbo
  75. stackHeight -= image.height
  76. }
  77. width: d.columnWidth*Math.pow(d.goldenRatio, index)
  78. anchors { top: parent.top; bottom: parent.bottom }
  79. World {
  80. id: isolatedWorld
  81. timeStep: d.pace
  82. running: true
  83. property var limbo: World {
  84. timeStep: isolatedWorld.timeStep
  85. running: isolatedWorld.running
  86. }
  87. }
  88. RectangleBoxBody {
  89. id: floor
  90. world: isolatedWorld
  91. height: 0
  92. width: parent.width
  93. x: xOffset
  94. anchors {
  95. top: parent.bottom
  96. }
  97. friction: 1
  98. }
  99. Timer {
  100. id: pumpTimer
  101. interval: full ? cascadeSettings.feedRate : 10
  102. repeat: true
  103. running: index === 0
  104. onTriggered: addImage()
  105. }
  106. Timer {
  107. id: deathTimer
  108. running: full
  109. repeat: true
  110. interval: cascadeSettings.feedRate
  111. onTriggered: {
  112. shift()
  113. }
  114. }
  115. Connections {
  116. target: root
  117. onTogglePause: d.paused = !d.paused
  118. onNext: deathTimer.triggered()
  119. }
  120. Component.onCompleted: {
  121. columnArray.push(this)
  122. }
  123. }
  124. }
  125. Keys.onUpPressed: root.togglePause()
  126. Keys.onDownPressed: root.toggleChaos() //root.next()
  127. Component.onCompleted: {
  128. globalVars.loadFullImage = true
  129. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  130. }
  131. }