Merge "Reduced complexity of dali-table-view" into devel/master
[platform/core/uifw/dali-demo.git] / examples / particles / particle-view.h
1 #ifndef PARTICLES_PARTICLE_VIEW_H_
2 #define PARTICLES_PARTICLE_VIEW_H_
3 /*
4  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include "particle-field.h"
21 #include "dali/public-api/actors/camera-actor.h"
22 #include "dali/public-api/animation/animation.h"
23 #include "dali/public-api/object/weak-handle.h"
24 #include "dali/public-api/rendering/shader.h"
25 #include <vector>
26 #include <functional>
27
28 struct ColorRange
29 {
30   Dali::Vector3 rgb0;
31   Dali::Vector3 rgb1;
32 };
33
34 class ParticleView: public Dali::ConnectionTracker
35 {
36 public:
37   ParticleView(const ParticleField& field, Dali::Actor world, Dali::CameraActor camera,
38     Dali::Geometry particleGeom = Dali::Geometry());
39   ~ParticleView();
40
41   void SetColorRange(const ColorRange& range);
42
43   void SetPopulation(float percentage);
44   void SetFocalLength(float f);
45   void SetAperture(float a);
46   void SetAlphaTestRefValue(float rv);
47   void SetFadeRange(float near, float far);
48   void SetAngularVelocity(float v);
49   void SetLinearVelocity(float v);
50
51   ///@brief Starts a scatter & regroup animation, cancelling any previously played animation
52   /// of the same kind. Bigger particles, and those further away from the Z axis are affected
53   /// less.
54   ///@param radius the normalised radius, within which particles are affected.
55   ///@param amount the amount of displacement applied to particles at the peak of the animation.
56   ///@param durationOut the duration of scattering, in seconds.
57   ///@param durationIn the duration of regrouping, in seconds.
58   void Scatter(float radius, float amount, float durationOut, float durationIn);
59
60   void SetScatterRay(Dali::Vector3 rayDir);
61
62   ///@brief Starts an animation to change the opacity of the particles to @a target.
63   ///@param duration Number of seconds to complete transition in.
64   ///@param target Target opacity in the 0..1.f range.
65   void Fade(float duration, float target, Dali::AlphaFunction alphaFn = Dali::AlphaFunction::DEFAULT,
66     std::function<void(Dali::Animation&)> onFinished = nullptr);
67
68   ///@brief Starts an animation to change the opacity of the particles to @a target.
69   ///@param duration Number of seconds to complete transition in.
70   ///@param target Target opacity in the 0..1.f range.
71   ///@param from The value to set the opacity to prior to the animation.
72   void Fade(float duration, float target, float from, Dali::AlphaFunction alphaFn = Dali::AlphaFunction::DEFAULT,
73     std::function<void(Dali::Animation&)> onFinished = nullptr);
74
75 private: // DATA
76   struct ScatterProps
77   {
78     Dali::Property::Index mPropRadius;
79     Dali::Property::Index mPropAmount;
80     Dali::Property::Index mPropRay;
81
82     Dali::Animation mAnim;
83   };
84
85   Dali::WeakHandle<Dali::Actor> mWorld;
86   Dali::Vector3 mParticleBoxSize;
87
88   Dali::Shader mParticleShader;
89   Dali::Property::Index mPropPopulation;
90   Dali::Property::Index mPropFocalLength;
91   Dali::Property::Index mPropAperture;
92   Dali::Property::Index mPropAlphaTestRefValue;
93   Dali::Property::Index mPropFadeRange;
94
95   ScatterProps mScatterProps[6];
96   int mActiveScatter = 0;
97
98   Dali::Actor mMasterParticles;
99   Dali::Property::Index mPropSecondaryColor;
100
101   Dali::Actor mSlaveParticles;
102
103   Dali::Animation mAngularAnim;
104   Dali::Animation mLinearAnim;
105   Dali::Animation mFadeAnim;
106 };
107
108 #endif //PARTICLES_PARTICLE_VIEW_H_