Switch.qml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import QtQuick 2.0
  2. import QtQuick.Particles 2.0
  3. Item {
  4. id: root
  5. property alias text: textItem.text
  6. property bool checked: false
  7. property string textOn: "On"
  8. property string textOff: "Off"
  9. QtObject {
  10. id: priv
  11. property alias checkedPriv: root.checked
  12. onCheckedPrivChanged: {
  13. if (checkedPriv) switchEffectAnimation.restart();
  14. }
  15. function releaseSwitch() {
  16. if (knob.x == 48) switchEffectAnimation.restart();
  17. // Don't switch if we are in correct side
  18. if ((knob.x == -2 && !checked) || (knob.x == 48 && checked)) {
  19. return;
  20. }
  21. checked = !checked;
  22. }
  23. }
  24. width: parent ? parent.width : 200
  25. height: 80
  26. MouseArea {
  27. width: parent.width
  28. height: parent.height
  29. onClicked: {
  30. root.checked = !root.checked
  31. }
  32. }
  33. Text {
  34. id: textItem
  35. anchors.verticalCenter: parent.verticalCenter
  36. anchors.left: parent.left
  37. anchors.leftMargin: 22
  38. anchors.right: switchBackgroundImage.left
  39. elide: Text.ElideRight
  40. font.pixelSize: 20
  41. color: "#ffffff"
  42. }
  43. Image {
  44. id: switchBackgroundImage
  45. source: "images/switch_background.png"
  46. anchors.verticalCenter: parent.verticalCenter
  47. anchors.right: parent.right
  48. anchors.rightMargin: 22
  49. }
  50. Image {
  51. id: switchFrameImage
  52. source: "images/switch_frame.png"
  53. anchors.verticalCenter: parent.verticalCenter
  54. anchors.right: parent.right
  55. anchors.rightMargin: 21
  56. z: 10
  57. }
  58. Item {
  59. id: switchItem
  60. anchors.fill: switchBackgroundImage
  61. SequentialAnimation {
  62. id: switchEffectAnimation
  63. PropertyAction { target: particleSystem; property: "paused"; value: "false" }
  64. ScriptAction { script: particleEmitter.pulse(3000) }
  65. }
  66. Image {
  67. id: switchOnImage
  68. anchors.right: knob.right
  69. anchors.rightMargin: 2
  70. source: "images/switch_on.png"
  71. opacity: knob.x / 48
  72. // Stars effect
  73. ParticleSystem {
  74. id: particleSystem
  75. anchors.fill: parent
  76. paused: true
  77. onEmptyChanged: if (empty) particleSystem.pause();
  78. ImageParticle {
  79. source: "images/star.png"
  80. rotationVariation: 180
  81. color:"#ffffff"
  82. }
  83. Emitter {
  84. id: particleEmitter
  85. width: parent.width
  86. height: 8
  87. emitRate: 16
  88. lifeSpan: 2000
  89. size: 32
  90. sizeVariation: 16
  91. endSize: 8
  92. velocity: PointDirection{ y: 20; x:-2; xVariation: 5; yVariation: 10 }
  93. enabled: false
  94. }
  95. Turbulence {
  96. width: parent.width
  97. height: (parent.height / 2)
  98. strength: 8
  99. }
  100. }
  101. }
  102. Text {
  103. anchors.verticalCenter: parent.verticalCenter
  104. anchors.right: knob.left
  105. anchors.rightMargin: 6
  106. color: "#000000"
  107. font.pixelSize: 18
  108. font.bold: true
  109. text: textOn
  110. }
  111. Text {
  112. anchors.verticalCenter: parent.verticalCenter
  113. anchors.left: knob.right
  114. anchors.leftMargin: 4
  115. color: "#ffffff"
  116. font.pixelSize: 18
  117. font.bold: true
  118. text: textOff
  119. }
  120. Image {
  121. id: knob
  122. source: "images/switch_thumb.png"
  123. x: checked ? 48 : -2
  124. opacity: 0.4
  125. MouseArea {
  126. anchors.fill: parent
  127. drag.target: knob; drag.axis: Drag.XAxis; drag.minimumX: -2; drag.maximumX: 48
  128. onClicked: checked = !checked
  129. onReleased: priv.releaseSwitch();
  130. }
  131. Behavior on x {
  132. NumberAnimation { duration: 250; easing.type: Easing.InOutQuad }
  133. }
  134. }
  135. }
  136. // Mask out switch parts which should be hidden
  137. ShaderEffect {
  138. id: shaderItem
  139. property variant source: ShaderEffectSource { sourceItem: switchItem; hideSource: true }
  140. property variant maskSource: ShaderEffectSource { sourceItem: switchBackgroundImage; hideSource: true }
  141. anchors.fill: switchBackgroundImage
  142. fragmentShader: "
  143. varying highp vec2 qt_TexCoord0;
  144. uniform highp float qt_Opacity;
  145. uniform sampler2D source;
  146. uniform sampler2D maskSource;
  147. void main(void) {
  148. gl_FragColor = texture2D(source, qt_TexCoord0.st) * (texture2D(maskSource, qt_TexCoord0.st).a) * qt_Opacity;
  149. }
  150. "
  151. }
  152. }