Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / imports / particles / particleemitter.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Declarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "particleemitter.h"
43 QT_BEGIN_NAMESPACE
44 ParticleEmitter::ParticleEmitter(QSGItem *parent) :
45     QSGItem(parent)
46   , m_particlesPerSecond(10)
47   , m_particleDuration(1000)
48   , m_particleDurationVariation(0)
49   , m_emitting(true)
50   , m_system(0)
51   , m_extruder(0)
52   , m_defaultExtruder(0)
53   , m_speed(&m_nullVector)
54   , m_acceleration(&m_nullVector)
55   , m_particleSize(16)
56   , m_particleEndSize(-1)
57   , m_particleSizeVariation(0)
58   , m_maxParticleCount(-1)
59   , m_burstLeft(0)
60   , m_emitLeft(0)
61
62 {
63     //TODO: Reset speed/acc back to null vector? Or allow null pointer?
64     connect(this, SIGNAL(maxParticleCountChanged(int)),
65             this, SIGNAL(particleCountChanged()));
66     connect(this, SIGNAL(particlesPerSecondChanged(qreal)),
67             this, SIGNAL(particleCountChanged()));
68     connect(this, SIGNAL(particleDurationChanged(int)),
69             this, SIGNAL(particleCountChanged()));
70 }
71
72 ParticleEmitter::~ParticleEmitter()
73 {
74     if(m_defaultExtruder)
75         delete m_defaultExtruder;
76 }
77
78 void ParticleEmitter::componentComplete()
79 {
80     if(!m_system)
81         qWarning() << "Emitter created without a particle system specified";//TODO: useful QML warnings, like line number?
82     QSGItem::componentComplete();
83 }
84 void ParticleEmitter::emitWindow(int timeStamp)
85 {
86     Q_UNUSED(timeStamp);
87 }
88
89
90 void ParticleEmitter::setEmitting(bool arg)
91 {
92     if (m_emitting != arg) {
93         m_emitting = arg;
94         emit emittingChanged(arg);
95     }
96 }
97
98
99 ParticleExtruder* ParticleEmitter::effectiveExtruder()
100 {
101     if(m_extruder)
102         return m_extruder;
103     if(!m_defaultExtruder)
104         m_defaultExtruder = new ParticleExtruder;
105     return m_defaultExtruder;
106 }
107
108 void ParticleEmitter::pulse(qreal seconds)
109 {
110     if(!particleCount())
111         qWarning() << "pulse called on an emitter with a particle count of zero";
112     if(!m_emitting)
113         m_burstLeft = seconds*1000.0;//TODO: Change name to match
114 }
115
116 void ParticleEmitter::burst(int num)
117 {
118     if(!particleCount())
119         qWarning() << "burst called on an emitter with a particle count of zero";
120     m_emitLeft += num;
121 }
122
123 void ParticleEmitter::setMaxParticleCount(int arg)
124 {
125     if (m_maxParticleCount != arg) {
126         if(arg < 0 && m_maxParticleCount >= 0){
127             connect(this, SIGNAL(particlesPerSecondChanged(qreal)),
128                     this, SIGNAL(particleCountChanged()));
129             connect(this, SIGNAL(particleDurationChanged(int)),
130                     this, SIGNAL(particleCountChanged()));
131         }else if(arg >= 0 && m_maxParticleCount < 0){
132             disconnect(this, SIGNAL(particlesPerSecondChanged(qreal)),
133                     this, SIGNAL(particleCountChanged()));
134             disconnect(this, SIGNAL(particleDurationChanged(int)),
135                     this, SIGNAL(particleCountChanged()));
136         }
137         m_maxParticleCount = arg;
138         emit maxParticleCountChanged(arg);
139     }
140 }
141
142 int ParticleEmitter::particleCount() const
143 {
144     if(m_maxParticleCount >= 0)
145         return m_maxParticleCount;
146     return m_particlesPerSecond*((m_particleDuration+m_particleDurationVariation)/1000.0);
147 }
148
149 QT_END_NAMESPACE