1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2015, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 // This class is the sample plugin for a ScreenSaver animation
8 //===========================================
9 #ifndef _LUMINA_DESKTOP_SCREEN_SAVER_FIREFLIES_ANIMATION_H
10 #define _LUMINA_DESKTOP_SCREEN_SAVER_FIREFLIES_ANIMATION_H
11 
12 #include "global-includes.h"
13 #include "BaseAnimGroup.h"
14 #include <QSequentialAnimationGroup>
15 #include <QGraphicsOpacityEffect>
16 
17 class Firefly : public QSequentialAnimationGroup{
18 	Q_OBJECT
19 private:
20 	QWidget *fly;
21 	QPropertyAnimation *movement, *flash;
22 	int maxX, maxY; //maximum jitter in X/Y directions
23 	QSize range;
24 private slots:
LoopChanged()25 	void LoopChanged(){
26 	    //Adjust the movement animation a bit
27 	    movement->setStartValue(movement->endValue()); //start at the previous end point
28 	    QPoint pt = movement->startValue().toPoint();
29 	    QPoint diff( (qrand()% maxX) - (maxX/2), (qrand()% maxY) - (maxY/2) );
30 	    //Need to ensure it stays in the current box
31 	    if( (pt.x()+diff.x()) < 0 || (pt.x()+diff.x())>range.width()){ pt.setX(pt.x() - diff.x()); } //reverse the direction - otherwise will go out of bounds
32 	    else{ pt.setX( pt.x() + diff.x() ); }
33 	    if( (pt.y()+diff.y()) < 0 || (pt.y()+diff.y())>range.height()){ pt.setY(pt.y() - diff.y()); } //reverse the direction - otherwise will go out of bounds
34 	    else{ pt.setY( pt.y() + diff.y() ); }
35 	    movement->setEndValue(pt);
36 	    movement->setDuration( qrand() %500 + 1000); //between 1000->1500 ms animations for movements
37 	    //Adjust the flash duration/size a bit
38 	    flash->setDuration(qrand() %200 + 500); //500-700 ms
39 	    int sz = qrand()%4 + 4; //6-10 pixel square
40 	    //flash->setKeyValueAt(0.5, (qrand()%50) /100.0);
41 	    //fly->resize(sz,sz);
42 	    flash->setKeyValueAt(0.5, QSize(sz,sz)); //half-way point for the flash
43 
44 	  fly->show();
45 	}
stopped()46 	void stopped(){ fly->hide(); }
47 
48 public:
Firefly(QWidget * parent)49 	Firefly(QWidget *parent) : QSequentialAnimationGroup(parent){
50 	  fly = new QWidget(parent);
51 	  range = parent->size();
52 	  maxX = range.width()/4;  maxY = range.height()/4;
53 	  QString B = QString::number(qrand()%70);
54           QString RY = QString::number(qrand()%200+50);
55           QString style = "background-color: qradialgradient(spread:pad, cx:0.5, cy:0.5, radius:0.5, fx:0.5, fy:0.5, stop:0 rgba(245, 245, 143, 200), stop:0.83871 rgba(%1, %1, %2, 140), stop:0.99 rgba(0, 0, 0, 255), stop:1 transparent);";
56 	  fly->setStyleSheet(style.arg(RY, B) );
57 	  //setup the movement animation
58 	  movement = new QPropertyAnimation(fly);
59 	  movement->setTargetObject(fly);
60 	  movement->setPropertyName("pos");
61 	  movement->setEndValue( QPoint( qrand() % range.width(), qrand()%range.height()) ); //on anim start, this will become the starting point
62 	  //setup the flashing animation
63 	  /*QGraphicsOpacityEffect *eff = new QGraphicsOpacityEffect(parent);
64 	  fly->setGraphicsEffect(eff);
65 	  flash = new QPropertyAnimation(eff, "opacity");*/
66 	  flash = new QPropertyAnimation(this);
67 	  flash->setTargetObject(fly);
68 	  flash->setPropertyName("size");
69 	  flash->setStartValue(QSize(0,0));
70 	  flash->setEndValue(flash->startValue());
71 	  //fly->setProperty("opacity",0);
72 	  //flash->setPropertyName("opacity");
73 	  //flash->setStartValue(0);
74 	  //flash->setEndValue(0);
75 	  //now setup the order of the animations
76 	  this->setLoopCount(100); //do this 100 times
77 	  //Roughly half the number of fireflies with start with movement/flash
78           if(qrand()%2 == 1){
79 	    this->addAnimation(movement);
80 	    this->addAnimation(flash);
81 	  }else{
82 	    this->addAnimation(flash);
83 	    this->addAnimation(movement);
84 	  }
85 	  //Start up this firefly
86 	  LoopChanged();  //load initial values
87 
88 	  fly->setGeometry( QRect(movement->startValue().toPoint(), flash->startValue().toSize()) );
89 	  connect(this, SIGNAL(currentLoopChanged(int)), this, SLOT(LoopChanged()) );
90 	  connect(this, SIGNAL(finished()), this, SLOT(stopped()) );
91 	}
~Firefly()92 	~Firefly(){}
93 
94 };
95 
96 class FirefliesAnimation : public BaseAnimGroup{
97 	Q_OBJECT
98 private:
99 	QList<Firefly*> fireflies;
100 
101 public:
FirefliesAnimation(QWidget * parent)102 	FirefliesAnimation(QWidget *parent) : BaseAnimGroup(parent){}
~FirefliesAnimation()103 	~FirefliesAnimation(){
104 	  this->stop();
105 	  //while(fireflies.length()>0){ fireflies.takeAt(0)->deleteLater(); }
106 	}
107 
LoadAnimations()108 	void LoadAnimations(){
109 	  while(fireflies.length()>0){ fireflies.takeAt(0)->deleteLater(); }
110 	  canvas->setStyleSheet("background: black;");
111 	  int number = readSetting("number",qrand()%30 + 50).toInt();
112 	  for(int i=0; i<number; i++){
113             if(fireflies.length()>number){ continue; }
114 	    Firefly *tmp = new Firefly(canvas);
115 	    this->addAnimation(tmp);
116 	    fireflies << tmp;
117 	  }
118 
119 	}
120 
121 };
122 #endif
123