Reel.qml 4.7 KB

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