QUItBattery.qml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import QtQuick 2.0
  2. import QtQuick.Particles 2.0
  3. Item {
  4. id: root
  5. // Value between 0 (0%) and 1 (100%)
  6. property real value: 0.5
  7. // Battery is being charged
  8. property bool charging: false
  9. // Setting a max rotation, 0 means non-liquid
  10. property real maxLiquidRotation: 50
  11. /* Private */
  12. property real __smoothedValue: value
  13. // True when battery is running low
  14. property bool __batteryLow: root.value <= 0.2
  15. // Battery liquid rotation
  16. property real __bRotation: Math.max(-maxLiquidRotation, Math.min(root.rotation+90, maxLiquidRotation))
  17. width: batteryImage.width
  18. height: batteryImage.height
  19. Behavior on __smoothedValue {
  20. SmoothedAnimation {
  21. velocity: 0.1
  22. duration: 1200
  23. }
  24. }
  25. Behavior on __bRotation {
  26. SmoothedAnimation {
  27. velocity: 3.0
  28. duration: 500
  29. }
  30. }
  31. Image {
  32. id: batteryBar
  33. source: "images/battery_bar.png"
  34. visible: false
  35. }
  36. ParticleSystem {
  37. id: particlesItem
  38. anchors.fill: batteryBar
  39. ImageParticle {
  40. source: "images/particle.png"
  41. rotationVariation: 180
  42. color:"#ffffff"
  43. colorVariation: 0.2
  44. }
  45. Emitter {
  46. width: 8
  47. height: parent.height
  48. x: 20
  49. emitRate: root.charging ? 20 + 50*root.value : 2 + 5*root.value
  50. lifeSpan: 3500
  51. size: 32
  52. sizeVariation: 24
  53. velocity: PointDirection{ x: 5+100*root.value; xVariation: x*0.5; yVariation: 6 }
  54. endSize: 8
  55. }
  56. Turbulence {
  57. width: parent.width
  58. height: parent.height
  59. strength: 32
  60. }
  61. }
  62. ShaderEffectSource {
  63. id: particlesShaderSource
  64. anchors.fill: particlesItem
  65. sourceItem: particlesItem
  66. hideSource: true
  67. visible: false
  68. }
  69. ShaderEffect {
  70. id: shaderEffectItem
  71. anchors.fill: particlesShaderSource
  72. property variant source: batteryBar
  73. property variant source2: particlesShaderSource
  74. property real value: 0.10 + root.__smoothedValue*0.78
  75. property real rot: root.__bRotation
  76. fragmentShader: "
  77. uniform sampler2D source;
  78. uniform sampler2D source2;
  79. uniform lowp float qt_Opacity;
  80. varying highp vec2 qt_TexCoord0;
  81. uniform highp float value;
  82. uniform highp float rot;
  83. void main() {
  84. lowp vec4 pix = texture2D(source, qt_TexCoord0);
  85. lowp vec4 pix2 = texture2D(source2, qt_TexCoord0);
  86. highp float r = qt_TexCoord0.y*(rot*0.008) - 0.0042*rot + abs(rot)*(value-0.5)*0.006;
  87. highp float isVis = smoothstep(qt_TexCoord0.x-0.02-r, qt_TexCoord0.x+0.02-r, value);
  88. highp vec4 color = vec4(1.0 - value, value, 0.0, 1.0);
  89. highp vec4 light = color * isVis * pix;
  90. gl_FragColor = (isVis * pix.a * pix2 + light) * qt_Opacity;
  91. }"
  92. }
  93. Item {
  94. id: clipper
  95. anchors.fill: parent
  96. anchors.leftMargin: 40
  97. anchors.rightMargin: 40
  98. clip: true
  99. Image {
  100. id: levelImage
  101. source: "images/bar_level2.png"
  102. x: shaderEffectItem.value * root.width + (Math.abs(-root.__bRotation)*(shaderEffectItem.value-0.5)*2.9) - 75
  103. visible: false
  104. }
  105. ShaderEffect {
  106. id: levelImageShaderEffect
  107. anchors.fill: levelImage
  108. opacity: 0.5
  109. transform: [
  110. Rotation {
  111. origin.x: 32
  112. origin.y: 135
  113. angle: -root.__bRotation
  114. }, Scale {
  115. origin.x: 32
  116. origin.y: 134
  117. yScale: 1 + root.__bRotation*root.__bRotation*0.00021
  118. }
  119. ]
  120. property variant source: levelImage
  121. property real value: shaderEffectItem.value
  122. fragmentShader: "
  123. uniform sampler2D source;
  124. uniform lowp float qt_Opacity;
  125. varying highp vec2 qt_TexCoord0;
  126. uniform highp float value;
  127. void main() {
  128. lowp vec4 pix = texture2D(source, qt_TexCoord0);
  129. highp vec4 color = vec4(1.0 - value, value, 0.2, 1.0);
  130. gl_FragColor = pix * color * qt_Opacity;
  131. }"
  132. }
  133. }
  134. ParticleSystem {
  135. anchors.fill: batteryBar
  136. running: root.charging || !empty
  137. ImageParticle {
  138. groups: ["first", "second"]
  139. source: "images/lightning.png"
  140. rotationVariation: 20
  141. color:"#ffffff"
  142. colorVariation: 0.1
  143. opacity: 0.5
  144. }
  145. Emitter {
  146. group: "first"
  147. width: 8
  148. height: 8
  149. anchors.verticalCenter: parent.verticalCenter
  150. emitRate: 100
  151. lifeSpan: 1400
  152. lifeSpanVariation: 500
  153. size: 32
  154. sizeVariation: 16
  155. velocity: PointDirection{ x: 200; xVariation: x*0.5; yVariation: 30 }
  156. endSize: 8
  157. enabled: root.charging
  158. }
  159. GroupGoal {
  160. groups: ["first"]
  161. x: shaderEffectItem.value * parent.width
  162. height: parent.height
  163. width: 100
  164. goalState: "second"
  165. jump: true
  166. }
  167. Gravity {
  168. groups: ["second"]
  169. magnitude: 200
  170. angle: -180
  171. }
  172. }
  173. Text {
  174. anchors.verticalCenter: parent.verticalCenter
  175. x: 60
  176. font.pixelSize: 26
  177. color: "#404040"
  178. style: Text.Outline
  179. styleColor: "#ffffff"
  180. opacity: root.charging ? 0.6 : 0
  181. text: "Charging"
  182. Behavior on opacity {
  183. NumberAnimation { duration: 500; easing.type: Easing.InOutQuad }
  184. }
  185. }
  186. Image {
  187. id: batteryImage
  188. source: "images/battery2.png"
  189. }
  190. Text {
  191. anchors.centerIn: parent
  192. y: 22
  193. font.pixelSize: 48
  194. style: Text.Outline
  195. color: root.__batteryLow && !root.charging ? "#802020" : "#404040"
  196. styleColor: "#ffffff"
  197. font.bold: true
  198. rotation: -root.rotation
  199. text: (root.value*100).toFixed(0) + "%"
  200. SequentialAnimation on opacity {
  201. running: root.__batteryLow && !root.charging
  202. loops: Animation.Infinite
  203. alwaysRunToEnd: true
  204. NumberAnimation { to: 0.5; duration: 200; easing.type: Easing.InOutQuad }
  205. NumberAnimation { to: 1.0; duration: 200; easing.type: Easing.InOutQuad }
  206. }
  207. }
  208. }