Well.qml 5.4 KB

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