Button.qml 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import QtQuick 2.0
  2. import QtQuick.Particles 2.0
  3. Item {
  4. id: root
  5. property alias text: buttonTextItem.text
  6. property bool effectsOn: true
  7. signal clicked
  8. width: buttonBackgroundImage.width
  9. height: buttonBackgroundImage.height
  10. scale: mouseArea.pressed && mouseArea.containsMouse ? 0.9 : 1.0
  11. opacity: mouseArea.pressed && mouseArea.containsMouse ? 0.8 : 1.0
  12. Behavior on scale {
  13. NumberAnimation { duration: 150; easing.type: Easing.OutQuad }
  14. }
  15. Behavior on opacity {
  16. NumberAnimation { duration: 150; easing.type: Easing.OutQuad }
  17. }
  18. BorderImage {
  19. id: buttonBackgroundImage
  20. source: "images/button.png"
  21. border.left: 24
  22. border.right: 24
  23. border.top: 20
  24. border.bottom: 20
  25. width: 140
  26. }
  27. // Stars effect
  28. ParticleSystem {
  29. id: particleSystem
  30. anchors.fill: buttonBackgroundImage
  31. running: root.effectsOn
  32. ImageParticle {
  33. source: "images/star.png"
  34. rotationVariation: 180
  35. color:"#ffffff"
  36. }
  37. Emitter {
  38. width: parent.width
  39. height: 8
  40. emitRate: 16
  41. lifeSpan: 2000
  42. size: 32
  43. sizeVariation: 16
  44. endSize: 8
  45. velocity: PointDirection{ y: 20; x:-2; xVariation: 5; yVariation: 10 }
  46. }
  47. Turbulence {
  48. width: parent.width
  49. height: (parent.height / 2)
  50. strength: 8
  51. }
  52. }
  53. // Button background
  54. ShaderEffectSource {
  55. id: shaderSource
  56. anchors.fill: buttonBackgroundImage
  57. sourceItem: buttonBackgroundImage
  58. hideSource: true
  59. visible: false
  60. }
  61. // Particles
  62. ShaderEffectSource {
  63. id: shaderSource2
  64. anchors.fill: particleSystem
  65. sourceItem: particleSystem
  66. hideSource: true
  67. visible: false
  68. }
  69. // Mask particles inside the button
  70. ShaderEffect {
  71. id: shaderEffectItem
  72. anchors.fill: shaderSource
  73. property variant source: shaderSource
  74. property variant source2: shaderSource2
  75. fragmentShader: "
  76. uniform sampler2D source;
  77. uniform sampler2D source2;
  78. uniform lowp float qt_Opacity;
  79. varying highp vec2 qt_TexCoord0;
  80. void main() {
  81. lowp vec4 pix = texture2D(source, qt_TexCoord0);
  82. lowp vec4 pix2 = texture2D(source2, qt_TexCoord0);
  83. gl_FragColor = qt_Opacity * (pix + pix.a * pix2);
  84. }"
  85. }
  86. Text {
  87. id: buttonTextItem
  88. anchors.centerIn: parent
  89. color: "#000000"
  90. font.pixelSize: 28
  91. }
  92. MouseArea {
  93. id: mouseArea
  94. anchors.fill: parent
  95. anchors.margins: -16
  96. onClicked: {
  97. root.clicked();
  98. }
  99. }
  100. }