Well.qml 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import QtQuick 2.5
  2. import Box2D 2.0
  3. import Qt.labs.settings 1.0
  4. import ".."
  5. View {
  6. id: root
  7. signal togglePause
  8. signal toggleChaos
  9. signal next
  10. property var pictureDelegate: Component {
  11. WellDelegate {}
  12. }
  13. Settings {
  14. id: wellSettings
  15. category: "Well"
  16. property int feedRate: 100
  17. // 0 is abutting
  18. property bool graduatedColumns: true
  19. property int verticalOffset: 5
  20. property real pace: 3
  21. property real density: 1.0
  22. property real friction: 1.0
  23. property bool globalWorld: false
  24. property bool fixedRotation: true
  25. // Very computationally heavy: 40% vs 20% for 0.1 vs 0
  26. property real restitution: 0
  27. }
  28. QtObject {
  29. id: d
  30. property real pace: wellSettings.pace/60.0
  31. property bool paused: false
  32. }
  33. World {
  34. id: world
  35. timeStep: d.pace
  36. running: wellSettings.globalWorld && globalUtil.primed
  37. property var limbo: World {
  38. timeStep: world.timeStep
  39. running: world.running
  40. }
  41. }
  42. Component {
  43. id: columnComponent
  44. Item {
  45. id: column
  46. property int stackHeight: 0
  47. property bool full: false
  48. property int cappedXOffset: index ? parent.width*graduationFactor : 0
  49. property int xOffset: wellSettings.graduatedColumns ? cappedXOffset : width * index
  50. property var pictureArray: []
  51. property real graduationFactor: {
  52. var cappedPositionalWeight = index ? index : 1
  53. var graduationFactor=1.0/(Math.pow(2,(globalSettings.columnCount - cappedPositionalWeight)))
  54. return graduationFactor
  55. }
  56. function considerImage() {
  57. if (stackHeight < (1.3 + 1/globalSettings.columnCount)*root.height) {
  58. addImage()
  59. }
  60. }
  61. function addImage() {
  62. var image = pictureDelegate.createObject(column, { x: -1000, y: -1000 })
  63. image.beyondThePale.connect(removeImage)
  64. image.world = wellSettings.globalWorld ? world : isolatedWorld
  65. image.x = xOffset
  66. stackHeight += image.height
  67. image.y = floor.y - stackHeight - wellSettings.verticalOffset
  68. pictureArray.push(image)
  69. globalUtil.itemCount++
  70. }
  71. function removeImage(image) {
  72. stackHeight -= image.height
  73. image.destroy()
  74. globalUtil.itemCount--
  75. }
  76. function shift() {
  77. if (pictureArray.length > 0) {
  78. var image = pictureArray.shift()
  79. image.world = image.world.limbo
  80. addImage()
  81. }
  82. }
  83. onStackHeightChanged: {
  84. if (!column.full && (stackHeight > root.height)) {
  85. globalUtil.registerColumnPrimed()
  86. column.full = true
  87. }
  88. }
  89. width: wellSettings.graduatedColumns ? parent.width*graduationFactor : parent.width/globalSettings.columnCount
  90. anchors { top: parent.top; bottom: parent.bottom }
  91. World {
  92. id: isolatedWorld
  93. timeStep: d.pace
  94. running: !wellSettings.globalWorld && globalUtil.primed
  95. property var limbo: World {
  96. timeStep: isolatedWorld.timeStep
  97. running: isolatedWorld.running
  98. }
  99. }
  100. RectangleBoxBody {
  101. id: floor
  102. world: isolatedWorld
  103. height: 0
  104. width: parent.width
  105. x: xOffset
  106. anchors {
  107. top: parent.bottom
  108. }
  109. friction: 1
  110. }
  111. Timer {
  112. id: pumpTimer
  113. interval: Math.abs(wellSettings.feedRate)
  114. repeat: true
  115. running: true
  116. onTriggered: considerImage()
  117. }
  118. Timer {
  119. id: deathTimer
  120. running: !globalSettings.commonFeed && globalUtil.primed && d.paused
  121. repeat: true
  122. interval: globalUtil.adjustedInterval
  123. onTriggered: shift()
  124. }
  125. Connections {
  126. target: root
  127. onTogglePause: d.paused = !d.paused
  128. onNext: deathTimer.triggered()
  129. onToggleChaos: wellSettings.fixedRotation = !wellSettings.fixedRotation
  130. }
  131. Component.onCompleted: {
  132. columnArray.push(this)
  133. }
  134. }
  135. }
  136. // floor
  137. RectangleBoxBody {
  138. id: globalFloor
  139. world: world
  140. height: 0
  141. anchors {
  142. left: parent.left
  143. right: parent.right
  144. top: parent.bottom
  145. }
  146. friction: 1
  147. }
  148. DebugDraw {
  149. id: debugDraw
  150. enabled: false
  151. z: 1
  152. world: world
  153. anchors.fill: parent
  154. opacity: 0.75
  155. visible: enabled
  156. }
  157. Keys.onUpPressed: root.togglePause()
  158. Keys.onDownPressed: root.toggleChaos() //root.next()
  159. Component.onCompleted: {
  160. pictureDelegate.status !== Component.Ready && console.log('Component failed with:' + pictureDelegate.errorString())
  161. }
  162. }