particle-system-example.cpp
6.82 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
192
193
194
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <dali-toolkit/dali-toolkit.h>
#include <dali-toolkit/public-api/particle-system/particle-domain.h>
#include <dali-toolkit/public-api/particle-system/particle-emitter.h>
#include <dali-toolkit/public-api/particle-system/particle-list.h>
#include <dali-toolkit/public-api/particle-system/particle-renderer.h>
#include <dali-toolkit/public-api/particle-system/particle-source.h>
#include <dali-toolkit/public-api/particle-system/particle-types.h>
#include <dali-toolkit/public-api/particle-system/particle-modifier.h>
#include "effects/particle-effect.h"
using namespace Dali;
using namespace Dali::Toolkit::ParticleSystem;
using namespace Dali::Toolkit;
using Dali::Toolkit::TextLabel;
using namespace Dali::ParticleEffect;
constexpr uint16_t NUMBER_OF_EFFECTS = 3;
constexpr float TEXT_LABEL_ANIMATION_TIME = 5.0f;
const TimePeriod TEXT_LABEL_ANIMATION_TIME_PERIOD(3.0, 2.0f);
/**
* This example shows Particle System feature
*/
class ParticleEffectController : public ConnectionTracker
{
public:
ParticleEffectController(Application& application)
: mApplication(application)
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect(this, &ParticleEffectController::Create);
}
~ParticleEffectController() = default; // Nothing to do in destructor
private:
// The Init signal is received once (only) during the Application lifetime
void Create(Application& application)
{
Window window = application.GetWindow();
window.SetBackgroundColor(Color::BLACK);
auto windowSize = window.GetSize();
// Set up emitter actor to be the full size of the window as some particles may be outside a particular size
mEmitterActor = Handle::New<Actor>({{Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER},
{Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER},
{Actor::Property::POSITION, Vector2::ZERO},
{Actor::Property::SIZE, Vector2(windowSize.GetWidth(), windowSize.GetHeight())}});
window.Add(mEmitterActor);
// Create a tap gesture detector, attach the actor & connect
mTapDetector = TapGestureDetector::New();
mTapDetector.Attach(mEmitterActor);
mTapDetector.DetectedSignal().Connect(this, [&](Actor actor, const TapGesture& tap) { NextEffect(); });
// Respond to key events
window.KeyEventSignal().Connect(this, &ParticleEffectController::OnKeyEvent);
// Create a Text Label at the bottom of the screen
mTextLabel = Handle::New<TextLabel>({{Actor::Property::ANCHOR_POINT, AnchorPoint::BOTTOM_CENTER},
{Actor::Property::PARENT_ORIGIN, ParentOrigin::BOTTOM_CENTER},
{TextLabel::Property::MULTI_LINE, true},
{TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER},
{TextLabel::Property::TEXT_COLOR, Color::WHITE}});
window.Add(mTextLabel);
// Create a fade out animation for the text label after a few seconds
mTextLabelAnimation = Animation::New(TEXT_LABEL_ANIMATION_TIME);
mTextLabelAnimation.AnimateTo(Property(mTextLabel, Actor::Property::COLOR_ALPHA), 0.0f, TEXT_LABEL_ANIMATION_TIME_PERIOD);
mTextLabelAnimation.Play();
// Start the default effect
StartEffect(EffectType(mCurrentEffectType));
// Add extra line to text for further instructions only the first time
std::string label = mTextLabel[TextLabel::Property::TEXT];
label += "\nTap to change particle effect";
mTextLabel[TextLabel::Property::TEXT] = label;
}
void StartEffect(EffectType effectType)
{
if(mCurrentEmitter)
{
mCurrentEmitter.Stop();
mCurrentEmitter.Reset();
}
ParticleEffectParams params{};
std::string effectName;
switch(effectType)
{
case EffectType::FIRE_RING:
params.particleCount = 5000;
params.emissionRate = 1000;
params.initialParticleCount = 0;
params.sourceSize = Vector2(200, 10);
params.strTexture = "sparkle-part1.png";
effectName = "Fire Effect";
break;
case EffectType::SPARKLES:
params.particleCount = 10000;
params.emissionRate = 500;
params.initialParticleCount = 0;
params.sourceSize = Vector2(10, 10);
params.strTexture = "blue-part2.png";
effectName = "Sparkle Effect";
break;
case EffectType::IMAGE_EXPLOSION:
params.particleCount = 20000;
params.emissionRate = 0;
params.initialParticleCount = 10;
params.sourceSize = Vector2(64, 64);
params.strImageSourceName = "particle-image-source.jpg";
effectName = "Image Source Effect";
break;
}
mCurrentEmitter = mParticleSystem->CreateEffectEmitter(effectType, mEmitterActor, params);
mCurrentEmitter.Start();
// Set text and reset TextLabel properties and animation
mTextLabel[Toolkit::TextLabel::Property::TEXT] = effectName;
mTextLabel[Actor::Property::COLOR_ALPHA] = 1.0f;
mTextLabelAnimation.SetCurrentProgress(0.0f);
mTextLabelAnimation.Play();
}
void NextEffect()
{
StartEffect(EffectType(++mCurrentEffectType %= NUMBER_OF_EFFECTS));
}
void OnKeyEvent(const KeyEvent& event)
{
if(event.GetState() == KeyEvent::DOWN)
{
if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
{
mApplication.Quit();
}
else
{
NextEffect();
}
}
}
private:
using ParticleEffectPtr = std::unique_ptr<Dali::ParticleEffect::ParticleEffect>;
Application& mApplication;
ParticleEffectPtr mParticleSystem;
ParticleEmitter mCurrentEmitter;
Actor mEmitterActor;
TapGestureDetector mTapDetector;
uint32_t mCurrentEffectType{0u};
Actor mTextLabel;
Animation mTextLabelAnimation;
};
int DALI_EXPORT_API main(int argc, char** argv)
{
Application application = Application::New(&argc, &argv);
ParticleEffectController test(application);
application.MainLoop();
return 0;
}