aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/particles/followemitter.cpp
blob: 825b9ea69f43522f77e9c35c6145d025ad4a13d3 (plain)
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
192
193
194
195
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Declarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "followemitter.h"
#include "particle.h"
#include <cmath>
QT_BEGIN_NAMESPACE

FollowEmitter::FollowEmitter(QSGItem *parent) :
    ParticleEmitter(parent)
  , m_lastTimeStamp(0)
  , m_emitterXVariation(0)
  , m_emitterYVariation(0)
  , m_followCount(0)
  , m_emissionExtruder(0)
  , m_defaultEmissionExtruder(new ParticleExtruder(this))
{
    connect(this, SIGNAL(followChanged(QString)),
            this, SLOT(recalcParticlesPerSecond()));
    connect(this, SIGNAL(particleDurationChanged(int)),
            this, SLOT(recalcParticlesPerSecond()));
    connect(this, SIGNAL(particlesPerParticlePerSecondChanged(int)),
            this, SLOT(recalcParticlesPerSecond()));
}

void FollowEmitter::recalcParticlesPerSecond(){
    if(!m_system)
        return;
    m_followCount = m_system->m_groupData[m_system->m_groupIds[m_follow]]->size;
    if(!m_followCount){
        setParticlesPerSecond(1000);//XXX: Fix this horrendous hack, needed so they aren't turned off from start
    }else{
        setParticlesPerSecond(m_particlesPerParticlePerSecond * m_followCount);
        m_lastEmission.resize(m_followCount);
        m_lastEmission.fill(0);
    }
}

void FollowEmitter::reset()
{
    m_followCount = 0;
}

void FollowEmitter::emitWindow(int timeStamp)
{
    if (m_system == 0)
        return;
    if(!m_emitting && !m_burstLeft && !m_emitLeft)
        return;
    if(m_followCount != m_system->m_groupData[m_system->m_groupIds[m_follow]]->size){
        qreal oldPPS = m_particlesPerSecond;
        recalcParticlesPerSecond();
        if(m_particlesPerSecond != oldPPS)
            return;//system may need to update
    }

    if(m_burstLeft){
        m_burstLeft -= timeStamp - m_lastTimeStamp * 1000.;
        if(m_burstLeft < 0){
            timeStamp += m_burstLeft;
            m_burstLeft = 0;
        }
    }

    qreal time = timeStamp / 1000.;
    qreal particleRatio = 1. / m_particlesPerParticlePerSecond;
    qreal pt;

    //Have to map it into this system, because particlesystem automaps it back
    QPointF offset = m_system->mapFromItem(this, QPointF(0, 0));
    qreal sizeAtEnd = m_particleEndSize >= 0 ? m_particleEndSize : m_particleSize;

    int gId = m_system->m_groupIds[m_follow];
    int gId2 = m_system->m_groupIds[m_particle];
    for(int i=0; i<m_system->m_groupData[gId]->size; i++){
        pt = m_lastEmission[i];
        ParticleData* d = m_system->m_data[i + m_system->m_groupData[gId]->start];
        if(!d || !d->stillAlive())
            continue;
        if(pt < d->pv.t)
            pt = d->pv.t;

        if(!effectiveExtruder()->contains(QRectF(offset.x(), offset.y(), width(), height()),QPointF(d->curX(), d->curY()))){
            m_lastEmission[i] = time;//jump over this time period without emitting, because it's outside
            continue;
        }
        while(pt < time || m_emitLeft){
            ParticleData* datum = m_system->newDatum(gId2);
            if(!datum){//skip this emission
                if(m_emitLeft)
                    --m_emitLeft;
                else
                    pt += particleRatio;
                continue;
            }
            datum->e = this;//###useful?
            ParticleVertex &p = datum->pv;

            // Particle timestamp
            p.t = pt;
            p.lifeSpan =
                    (m_particleDuration
                     + ((rand() % ((m_particleDurationVariation*2) + 1)) - m_particleDurationVariation))
                    / 1000.0;

            // Particle position
            qreal followT =  pt - d->pv.t;
            qreal followT2 = followT * followT * 0.5;
            qreal sizeOffset = d->pv.size/2;//TODO: Current size? As an option
            //TODO: Set variations
            //Subtract offset, because PS expects this in emitter coordinates
            QRectF boundsRect(d->pv.x - offset.x() + d->pv.sx * followT + d->pv.ax * followT2 - m_emitterXVariation/2,
                              d->pv.y - offset.y() + d->pv.sy * followT + d->pv.ay * followT2 - m_emitterYVariation/2,
                              m_emitterXVariation,
                              m_emitterYVariation);
//            QRectF boundsRect(d->pv.x + d->pv.sx * followT + d->pv.ax * followT2 + offset.x() - sizeOffset,
//                              d->pv.y + d->pv.sy * followT + d->pv.ay * followT2 + offset.y() - sizeOffset,
//                              sizeOffset*2,
//                              sizeOffset*2);

            ParticleExtruder* effectiveEmissionExtruder = m_emissionExtruder ? m_emissionExtruder : m_defaultEmissionExtruder;
            const QPointF &newPos = effectiveEmissionExtruder->extrude(boundsRect);
            p.x = newPos.x();
            p.y = newPos.y();

            // Particle speed
            const QPointF &speed = m_speed->sample(newPos);
            p.sx = speed.x();
            p.sy = speed.y();

            // Particle acceleration
            const QPointF &accel = m_acceleration->sample(newPos);
            p.ax = accel.x();
            p.ay = accel.y();

            // Particle size
            float sizeVariation = -m_particleSizeVariation
                    + rand() / float(RAND_MAX) * m_particleSizeVariation * 2;

            float size = qMax((qreal)0.0, m_particleSize + sizeVariation);
            float endSize = qMax((qreal)0.0, sizeAtEnd + sizeVariation);

            p.size = size * float(m_emitting);
            p.endSize = endSize * float(m_emitting);

            if(m_emitLeft)
                --m_emitLeft;
            else
                pt += particleRatio;
            m_system->emitParticle(datum);
        }
        m_lastEmission[i] = pt;
    }

    m_lastTimeStamp = time;
}
QT_END_NAMESPACE