Reel.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. ReelImage {}
  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. priorImage.bowOut()
  37. }
  38. NumberAnimation on t { from: 0; to: 1; duration: 1000; loops: -1 }
  39. onTChanged: { animationStep(); }
  40. }
  41. Component {
  42. id: columnComponent
  43. Item {
  44. id: column
  45. property int columnIndex: index
  46. property var imageArray: []
  47. property var imageQueue: []
  48. property bool lastColumn: columnIndex === (globalSettings.columnCount - 1)
  49. function stackHeight(imageIndex) {
  50. var height = 0
  51. for(var i = 0; i < imageIndex; i++) {
  52. height += imageArray[i].height
  53. }
  54. return height
  55. }
  56. function receptive() {
  57. return !d.initialized || imageQueue.length < d.imageBuffer
  58. }
  59. function addNewImage() {
  60. globalUtil.itemCount++
  61. addImage(pictureDelegate.createObject())
  62. imageArray.push(imageQueue.pop())
  63. }
  64. function addImage(image) {
  65. image.parent = column
  66. image.y = - image.height
  67. imageQueue.push(image)
  68. }
  69. function animationStep() {
  70. if (!imageArray.length || imageArray[imageArray.length - 1].y > -1) {
  71. if (imageQueue.length) {
  72. imageArray.push(imageQueue.pop())
  73. } else if (columnIndex === 0) {
  74. if (!(globalSettings.itemLimit > 0 && globalSettings.itemLimit <= globalUtil.itemCount)) {
  75. addNewImage()
  76. }
  77. }
  78. }
  79. for (var i = 0; i < imageArray.length; i++) {
  80. var image = imageArray[i]
  81. var restingY = root.height - image.height - stackHeight(i)
  82. var prospectiveY = image.y + d.velocity
  83. var nextColumn = columnArray[columnIndex+1]
  84. if (image.y > root.height) {
  85. imageArray.shift()
  86. nextColumn.addImage(image)
  87. } else if ((lastColumn || !nextColumn.receptive()) && prospectiveY >= restingY) {
  88. image.y = restingY
  89. if (lastColumn) {
  90. deathTimer.start()
  91. if(!d.initialized) {
  92. d.initialized = true
  93. d.velocity = reelSettings.restingVelocity
  94. }
  95. }
  96. } else {
  97. image.y = prospectiveY
  98. }
  99. }
  100. }
  101. Component.onCompleted: columnArray.push(this)
  102. x: d.columnWidth/globalUtil.columnWidthRatio(d.columnRatio, index)
  103. width: {
  104. var colWidth = d.columnWidth*Math.pow(d.columnRatio, index);
  105. lastColumn && (globalVars.imageWidthOverride = colWidth)
  106. return colWidth
  107. }
  108. anchors { top: parent.top; bottom: parent.bottom }
  109. }
  110. }
  111. // accel
  112. Timer {
  113. repeat: true
  114. running: !d.initialized
  115. interval: 100
  116. onTriggered: {
  117. d.velocity += reelSettings.velocityAccelIncrements
  118. }
  119. }
  120. // death
  121. Timer {
  122. id: deathTimer
  123. repeat: false
  124. running: false
  125. interval: reelSettings.deathPeriod
  126. onTriggered: {
  127. d.killLastImage()
  128. }
  129. }
  130. Keys.onDownPressed: {
  131. d.killLastImage()
  132. }
  133. }