qparticles.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef QPARTICLES_H
  2. #define QPARTICLES_H
  3. #include <QGLShaderProgram>
  4. #include <QDeclarativeItem>
  5. #include <QtCore/QAbstractAnimation>
  6. #include <QVarLengthArray>
  7. #include <QPen>
  8. #include "qparticle.h"
  9. #include "vertexpositioncolor.h"
  10. #define KPARTICLES 100
  11. #define KCOLORS 256
  12. //an animation that just gives a tick
  13. template<class T, void (T::*method)(int)>
  14. class TickAnimationProxy : public QAbstractAnimation
  15. {
  16. public:
  17. TickAnimationProxy(T *p, QObject *parent = 0) : QAbstractAnimation(parent), m_p(p) {}
  18. virtual int duration() const { return -1; }
  19. protected:
  20. virtual void updateCurrentTime(int msec) { (m_p->*method)(msec); }
  21. private:
  22. T *m_p;
  23. };
  24. class QParticles : public QDeclarativeItem
  25. {
  26. Q_OBJECT
  27. Q_PROPERTY(int particles READ particles WRITE setParticles)
  28. Q_PROPERTY(float particleSize READ particleSize WRITE setParticleSize)
  29. Q_PROPERTY(bool squareParticle READ squareParticle WRITE setSquareParticle)
  30. public:
  31. explicit QParticles(QDeclarativeItem *parent = 0);
  32. void tick(int time);
  33. void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *);
  34. Q_INVOKABLE void onActivated();
  35. TickAnimationProxy<QParticles, &QParticles::tick> clock;
  36. protected:
  37. bool sceneEvent(QEvent *event);
  38. // bool event(QEvent *event);
  39. public:
  40. void setParticles(int aNewParticlesCount);
  41. int particles() const;
  42. void setParticleSize(float aParticleSize);
  43. float particleSize() const;
  44. // void setdrawOnUI(bool aDrawOnUI);
  45. // bool drawOnUI() const;
  46. void setSquareParticle(bool aSquareParticle);
  47. bool squareParticle() const;
  48. private:
  49. int randInt(int low, int high) const;
  50. bool updateParticles(qreal deltaTime, const QRectF & boundingRect, bool dontNull);
  51. void applyTouchGravity(const QPointF & center);
  52. void generateParticles(const QRectF & boundingRect);
  53. void generateParticles(int aCount);
  54. void generateColors();
  55. void stopRedraw();
  56. void startRedraw();
  57. void appendParticle();
  58. signals:
  59. void particlesChanged();
  60. protected:
  61. virtual void componentComplete();
  62. // virtual void geometryChanged(const QRectF &newGeometry,
  63. // const QRectF &oldGeometry);
  64. private:
  65. int m_lastTickTime;
  66. int m_upTime;
  67. float m_particleSize;
  68. bool m_SquareParticle;
  69. QVector< QParticle> m_particles;
  70. QList<QPointF> m_touchpos;
  71. QVector<TVertexPositionColor> data;
  72. QVector< QVector4D > m_colors;
  73. QPen m_pen;
  74. int m_MatrixLoc;
  75. int m_VertexAtribLoc;
  76. int m_ColorAttribLoc;
  77. QGLShaderProgram * m_BaseShader;
  78. void paintParticles(qreal aOpacity, qreal aParticleSize);
  79. void InitShaders();
  80. private:
  81. // init status
  82. bool m_InitShader;
  83. bool iColorInit;
  84. bool iParticleInit;
  85. private:
  86. QVector3D convertColor(int aColor) const;
  87. };
  88. #endif // QPARTICLES_H