Reel.qml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import QtQuick 2.5
  2. import Qt.labs.settings 1.0
  3. // Forgive me
  4. import "../.."
  5. View {
  6. id: root
  7. property var pictureDelegate: Component {
  8. ArtImage {}
  9. }
  10. Settings {
  11. id: reelSettings
  12. category: "Reel"
  13. property int deathPeriod: 10000
  14. property real restingVelocity: 4
  15. property real velocityAccelIncrements: 0.3
  16. }
  17. QtObject {
  18. id: d
  19. property real t: 0
  20. property var priorImage
  21. property real velocity: 0
  22. property bool initialized: false
  23. property int imageBuffer: 1
  24. property real columnRatio: globalSettings.useGoldenRatio ? globalVars.goldenRatio : globalSettings.lessGoldenRatio
  25. property real columnWidth: root.width*globalUtil.columnWidthRatio(d.columnRatio, globalSettings.columnCount)
  26. function animationStep() {
  27. columnArray.forEach(function(column) { column.animationStep(); })
  28. }
  29. function killLastImage() {
  30. if(!!priorImage) {
  31. priorImage.destroy()
  32. globalUtil.itemCount--
  33. }
  34. var col = columnArray[globalSettings.columnCount - 1]
  35. priorImage = col.imageArray.shift()
  36. }
  37. NumberAnimation on t { from: 0; to: 1; duration: 1000; loops: -1 }
  38. onTChanged: { animationStep(); }
  39. }
  40. Component {
  41. id: columnComponent
  42. Item {
  43. id: column
  44. property int columnIndex: index
  45. property var imageArray: []
  46. property var imageQueue: []
  47. property bool lastColumn: columnIndex === (globalSettings.columnCount - 1)
  48. function stackHeight(imageIndex) {
  49. var height = 0
  50. for(var i = 0; i < imageIndex; i++) {
  51. height += imageArray[i].height
  52. }
  53. return height
  54. }
  55. function receptive() {
  56. return !d.initialized || imageQueue.length < d.imageBuffer
  57. }
  58. function addNewImage() {
  59. globalUtil.itemCount++
  60. addImage(pictureDelegate.createObject())
  61. imageArray.push(imageQueue.pop())
  62. }
  63. function addImage(image) {
  64. image.parent = column
  65. image.y = - image.height
  66. imageQueue.push(image)
  67. }
  68. function animationStep() {
  69. if (!imageArray.length || imageArray[imageArray.length - 1].y > -1) {
  70. if (imageQueue.length) {
  71. imageArray.push(imageQueue.pop())
  72. } else if (columnIndex === 0) {
  73. if (!(globalSettings.itemLimit > 0 && globalSettings.itemLimit <= globalUtil.itemCount)) {
  74. addNewImage()
  75. }
  76. }
  77. }
  78. for (var i = 0; i < imageArray.length; i++) {
  79. var image = imageArray[i]
  80. var restingY = root.height - image.height - stackHeight(i)
  81. var prospectiveY = image.y + d.velocity
  82. var nextColumn = columnArray[columnIndex+1]
  83. if (image.y > root.height) {
  84. imageArray.shift()
  85. nextColumn.addImage(image)
  86. } else if ((lastColumn || !nextColumn.receptive()) && prospectiveY >= restingY) {
  87. image.y = restingY
  88. if (lastColumn) {
  89. deathTimer.start()
  90. if(!d.initialized) {
  91. d.initialized = true
  92. d.velocity = reelSettings.restingVelocity
  93. }
  94. }
  95. } else {
  96. image.y = prospectiveY
  97. }
  98. }
  99. }
  100. Component.onCompleted: columnArray.push(this)
  101. x: d.columnWidth/globalUtil.columnWidthRatio(d.columnRatio, index)
  102. width: {
  103. var colWidth = d.columnWidth*Math.pow(d.columnRatio, index);
  104. lastColumn && (globalVars.imageWidthOverride = colWidth)
  105. return colWidth
  106. }
  107. anchors { top: parent.top; bottom: parent.bottom }
  108. }
  109. }
  110. // accel
  111. Timer {
  112. repeat: true
  113. running: !d.initialized
  114. interval: 100
  115. onTriggered: {
  116. d.velocity += reelSettings.velocityAccelIncrements
  117. }
  118. }
  119. // death
  120. Timer {
  121. id: deathTimer
  122. repeat: false
  123. running: false
  124. interval: reelSettings.deathPeriod
  125. onTriggered: {
  126. d.killLastImage()
  127. }
  128. }
  129. Keys.onDownPressed: {
  130. d.killLastImage()
  131. }
  132. }