-
Notifications
You must be signed in to change notification settings - Fork 0
/
effect.cpp
92 lines (77 loc) · 1.95 KB
/
effect.cpp
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
#include "effect.h"
#include <QRandomGenerator>
#include <QQmlContext>
const static int MaxTimeToLive = 100;
Effect::Effect(std::shared_ptr<UiContext> context)
{
_timeToLive = 0;
_position = QPoint(0, 0);
_context = context;
_item = nullptr;
}
Effect::~Effect()
{
safeDelete();
}
void Effect::key(QChar ch)
{
_timeToLive = MaxTimeToLive;
QObject *obj = _context->component->create();
QQuickItem *item = qobject_cast<QQuickItem*>(obj);
int x = QRandomGenerator::global()->bounded(10, _context->root->width() - 10);
int y = QRandomGenerator::global()->bounded(10, _context->root->height() - 10);
item->setPosition(QPointF(x, y));
item->setParentItem(_context->root->contentItem());
item->setVisible(true);
qreal scale = _context->zoom * (1.0 + QRandomGenerator::global()->bounded(0.7));
item->setScale(scale);
safeDelete();
_item = item;
}
void Effect::tick()
{
if(_timeToLive < 0)
{
return;
}
_timeToLive--;
if(_timeToLive <= 0)
{
safeDelete();
}
if(_item != nullptr)
{
float r = _context->zoom * ((0.7 * MaxTimeToLive + _timeToLive) / MaxTimeToLive);
if(r < _item->scale())
{
_item->setScale(r);
}
int maxx = _context->root->width();
int maxy = _context->root->height();
int dx = (int) QRandomGenerator::global()->bounded(0.001 * maxx);
int dy = (int) QRandomGenerator::global()->bounded(0.001 * maxy);
int newX = _item->x() + dx;
int newY = _item->y() + dy;
if (newX < 0 || newX >= maxx)
{
newX = _item->x();
}
if (newY < 0 || newY >= maxy)
{
newY = _item->y();
}
_item->setPosition(QPointF(newX, newY));
}
}
bool Effect::isAlive() const
{
return _timeToLive > 0;
}
void Effect::safeDelete()
{
if(_item != nullptr)
{
delete _item;
_item = nullptr;
}
}