Reel.qml 4.8 KB

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