-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfxwidget.cpp
More file actions
191 lines (154 loc) · 4.17 KB
/
fxwidget.cpp
File metadata and controls
191 lines (154 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "fxwidget.h"
#include <QTimer>
#include <QPainter>
#include <QKeyEvent>
#include "doomfire.h"
#include "starfield.h"
#include "tunnel.h"
#include "recursiontree.h"
#include "sierpinski.h"
#include "fractal.h"
#include "plasma.h"
#include "fire.h"
typedef QPair<QString,Effect*> FxElement;
class FxWidget::Priv
{
public:
Priv(int _w, int _h) : currentFx(nullptr), w(_w), h(_h), isRunning(true), isCreated(true) {
effects.push_back(FxElement("Doom Fire", new DoomFire(w, h)));
effects.push_back(FxElement("Star Field", new StarField(w, h)));
effects.push_back(FxElement("Tunnel", new Tunnel(w, h)));
effects.push_back(FxElement("Recursion Tree", new RecursionTree(w, h)));
effects.push_back(FxElement("Sierpinski", new Sierpinski(w, h)));
effects.push_back(FxElement("Fractal", new Fractal(w, h)));
effects.push_back(FxElement("Plasma", new Plasma(w, h)));
effects.push_back(FxElement("Fire", new Fire(w, h)));
selectFx(0);
}
~Priv() {
foreach (FxElement fx, effects)
delete fx.second;
}
void selectFx(int i) {
currentFx = effects[i].second;
updateInterval = currentFx->defaultRefreshRate();
timer.setInterval(updateInterval);
}
QVector<FxElement> effects;
Effect *currentFx;
QTimer timer;
int w;
int h;
int updateInterval;
bool isRunning;
bool isCreated;
};
FxWidget::FxWidget(QWidget *parent) : QWidget(parent)
{
setMinimumSize(400, 320);
setFocusPolicy(Qt::StrongFocus);
d = new Priv(width(), height());
connect(&d->timer, &QTimer::timeout, this, &FxWidget::onTimerUpdate);
if (d->updateInterval > 0)
d->timer.start();
}
FxWidget::~FxWidget()
{
delete d;
}
QVector<QString> FxWidget::fxList()
{
QVector<QString> v;
foreach (FxElement fx, d->effects)
v.append(fx.first);
return v;
}
QPair<int, QVector<QString>> FxWidget::fxKindList()
{
return d->currentFx->fxKindList();
}
int FxWidget::updateInterval()
{
return d->updateInterval;
}
bool FxWidget::isRunning()
{
return d->isRunning;
}
bool FxWidget::isCreated()
{
return d->isCreated;
}
//#include <QDebug>
void FxWidget::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
//qDebug() << "Paint event" << d->updateInterval;
QPainter p(this);
d->currentFx->paint(&p);
}
void FxWidget::keyPressEvent(QKeyEvent *event)
{
bool needUpdate = false;
switch (event->key()) {
case Qt::Key_Plus: needUpdate = d->currentFx->keyPressed(KEY_PLUS); break;
case Qt::Key_Minus: needUpdate = d->currentFx->keyPressed(KEY_MINUS); break;
case Qt::Key_Left: needUpdate = d->currentFx->keyPressed(KEY_LEFT); break;
case Qt::Key_Right: needUpdate = d->currentFx->keyPressed(KEY_RIGHT); break;
case Qt::Key_Up: needUpdate = d->currentFx->keyPressed(KEY_UP); break;
case Qt::Key_Down: needUpdate = d->currentFx->keyPressed(KEY_DOWN); break;
}
QWidget::keyPressEvent(event);
if (needUpdate)
update();
}
void FxWidget::onTimerUpdate()
{
d->currentFx->update();
update();
}
void FxWidget::onEffectSelected(int fx)
{
d->selectFx(fx);
d->timer.stop();
if (d->isRunning && d->updateInterval > 0)
d->timer.start();
update();
emit statusUpdated();
emit paramsUpdated();
}
void FxWidget::onPlayPausePressed()
{
if (d->isRunning)
d->timer.stop();
else if (d->updateInterval > 0)
d->timer.start();
d->isRunning = !d->isRunning;
emit statusUpdated();
}
void FxWidget::onIncreaseIntervalPressed()
{
if (d->updateInterval < 500) {
if (d->updateInterval >= 100)
d->updateInterval += 20;
else
d->updateInterval += 5;
d->timer.setInterval(d->updateInterval);
emit statusUpdated();
}
}
void FxWidget::onDecreaseIntervalPressed()
{
if (d->updateInterval > 5) {
if (d->updateInterval <= 100)
d->updateInterval -= 5;
else
d->updateInterval -= 20;
d->timer.setInterval(d->updateInterval);
emit statusUpdated();
}
}
void FxWidget::onFxKindSelected(int kind)
{
d->currentFx->setFxKind(kind);
}