30ed0a09ea93af526e226260fc148c945b3793f5
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / controls / bubble-effect / bubble-emitter-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H__
2 #define __DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H__
3
4 /*
5  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/camera-actor.h>
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/actors/mesh-actor.h>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/images/frame-buffer-image.h>
27 #include <dali/public-api/geometry/mesh.h>
28 #include <dali/public-api/render-tasks/render-task.h>
29
30 // INTERNAL INCLUDES
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/controls/bubble-effect/bubble-emitter.h>
33 #include <dali-toolkit/public-api/shader-effects/bubble-effect/bubble-effect.h>
34
35 #include <map>
36
37 namespace Dali
38 {
39
40 namespace Toolkit
41 {
42
43 namespace Internal
44 {
45
46 /**
47  * BubbleEmitter implementation class.
48  */
49 class BubbleEmitter : public Control
50 {
51 public:
52
53   /**
54    * Destructor
55    */
56   ~BubbleEmitter();
57
58   /**
59    * @copydoc Toolkit::BubbleEmitter::New
60    */
61   static Toolkit::BubbleEmitter New( const Vector2& winSize,
62                                      Image shapeImage,
63                                      unsigned int maximumNumberOfBubble,
64                                      const Vector2& bubbleSizeRange );
65
66   /**
67    * @copydoc Toolkit::BubbleEmitter::GetRootActor
68    */
69   Actor GetRootActor();
70
71   /**
72    * @copydoc Toolkit::BubbleEmitter::SetBackground
73    */
74   void SetBackground( Image bgImage, const Vector3& hsvDelta );
75
76   /**
77    * @copydoc Toolkit::BubbleEmitter::SetShapeImage
78    */
79   void SetShapeImage( Image shapeImage );
80
81   /**
82    * @copydoc Toolkit::BubbleEmiter::SetBubbleScale
83    */
84   void SetBubbleScale( float scale );
85
86   /**
87    * @copydoc Toolkit::BubbleEmitter::SetBubbleDensity
88    */
89   void SetBubbleDensity( unsigned int density );
90
91   /**
92    * @copydoc Toolkit::BubbleEmitter::SetBlendMode
93    */
94   void SetBlendMode( bool enable );
95
96   /**
97    * @copydoc Toolkit::BubbleEmitter::EmitBubble
98    */
99   void EmitBubble( Animation& animation, const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
100
101   /**
102    * @copydoc Toolkit::BubbleEmitter::StartExplosion
103    */
104   void StartExplosion( float duration, float multiple );
105
106   /**
107    * @copydoc Toolkit::BubbleEmitter::Restore
108    */
109   void Restore();
110
111 private:
112
113   /**
114    * Construct a new BubbleEmitter object.
115    * @param[in] movementArea The size of the bubble moving area
116    * @param[in] shapeImage The alpha channnel of this texture defines the bubble shape.
117    * @param[in] maximumNumberOfBubble The maximum number of bubble needed.
118    * @param[in] bubbleSizeRange The size range of the bubbles; x component is the minimal size, and y component is the maximum size.
119    */
120   BubbleEmitter( const Vector2& movementArea,
121                  Image shapeImage,
122                  unsigned int maximumNumberOfBubble,
123                  const Vector2& bubbleSizeRange );
124
125   /**
126    * This method is called after the CubeTransitionEffect has been initialized.
127    * The meshActors and BubbleEffects are created here.
128    */
129   void OnInitialize();
130
131   /**
132    * Callback function of the finished signal of off-screen render task.
133    * @param[in] source The render task used to create the color adjusted background image.
134    */
135   void OnRenderFinished(RenderTask& source);
136
137   /**
138    * Callback function from Stage to tell us if the context has been regained.
139    */
140   void OnContextRegained();
141
142   /**
143    * Generate the material object which is attached to the meshActor to describe its color, texture, texture mapping mode etc.
144    */
145   void GenMaterial();
146
147   /**
148    * Add a vertex to the mesh data.
149    * @param[in] vertices The collection of vertices.
150    * @param[in] XYZ The vertex position coordinates.
151    * @param[in] UV The vertex texture coordinate.
152    */
153   void AddVertex(MeshData::VertexContainer& vertices, Vector3 XYZ, Vector2 UV);
154
155   /**
156    * Add a triangle to the mesh data.
157    * @param[in] faces The collection od FaceIndex items.
158    * @param[in] v0 The index of the first point of the triangle.
159    * @param[in] v1 The index of the second point of the triangle.
160    * @param[in] v3 The index of the first point of the triangle.
161    */
162   void AddTriangle(MeshData::FaceIndices& faces,size_t v0, size_t v1, size_t v2);
163
164   /**
165    * Create a new mesh.
166    * @param[in] meshData The MeshData object which encompasses all the data required to describe and render the 3D mesh.
167    * @param[in] numberOfBubble The triangle number in the meshData is 2*numOfBubble; two triangles for each bubble
168    */
169   void ConstructBubbleMesh( MeshData& meshData, unsigned int numOfBubble);
170
171   /**
172    * Set the uniform values to the shader effect to emit a bubble
173    * @param[in] effect The BubbleEffect to render the current bubble
174    * @param[in] curUniform The index of the uniform array in the shader
175    * @param[in] emitPosition The start position of the bubble movement.
176    * @param[in] displacement The displacement used to bound the moving distance of the bubble.
177    */
178   void SetBubbleParameter( BubbleEffect& effect, unsigned int curUniform,
179                            const Vector2& emitPosition, const Vector2& displacement );
180
181   /**
182    * Set the uniform values to the shader effect to emit a bubble
183    * @param[in] effect The BubbleEffect to render the current bubble
184    * @param[in] curUniform The index of the uniform array in the shader
185    * @param[in] emitPosition The start position of the bubble movement.
186    * @param[in] direction The direction used to constrain the bubble to move in an adjacent direction around it.
187    * @param[in] displacement The displacement used to bound the moving distance of the bubble.
188    */
189   void SetBubbleParameter( BubbleEffect& effect, unsigned int curUniform,
190                            const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
191
192   /**
193    * Callback function of the explosion animation finished signal to reset the shader parameters
194    * @param[in] source The explosion animation.
195    */
196   void OnExplosionFinished( Animation& source );
197
198   /**
199    * Return a random value between the given interval.
200    * @param[in] f0 The low bound
201    * @param[in] f1 The up bound
202    * @return A random value between the given interval
203    */
204   float RandomRange(float f0, float f1);
205
206 private:
207
208   Vector2                     mMovementArea;        ///< The size of the bubble moving area, usually the same size as the background image actor.
209   Image                       mShapeImage;          ///< The alpha channnel of this texture defines the bubble shape.
210   Actor                       mBubbleRoot;          ///<The bubble root actor. Need to add it to stage to get the bubbles rendered.
211
212   unsigned int                mNumBubblePerShader;  ///< How many bubbles for each BubbleEffect shader.
213   unsigned int                mNumShader;           ///< How many BubbleEffect shaders are used.
214   unsigned int                mTotalNumOfBubble;    ///< mNumBubblePerShader*mNumShader.
215   bool                        mRenderTaskRunning;   ///< If the background render task is currently running
216
217   Vector2                     mBubbleSizeRange;     ///< The bubble size range.
218
219   std::vector<Mesh>           mMesh;                ///< The mesh vector, each mesh is used to create a meshActor which applies a BubbleEffect.
220   std::vector<MeshActor>      mMeshActor;           ///< The meshActor vector, its size is mNumShader.
221   MeshActor                   mMeshActorForNoise;   ///< An Extra mesh data to emit bubbles which emit bubble in totally random angle.
222   Material                    mCustomMaterial;      ///< The material object which is attached to the meshActor to describe its color, texture, texture mapping mode etc.
223
224   std::vector<BubbleEffect>   mEffect;              ///< The bubbleEffect vector, corresponding to the mMeshActoe vector.
225   BubbleEffect                mEffectForNoise;      ///< The extra bubbleEffect, corresponding to the mMeshActorForNoise.
226
227   unsigned int                mCurrentUniform;      ///< Keep track of the uniform index for the newly emitted bubble
228
229   Vector3                     mHSVDelta;            ///< The HSV difference used to adjust the background image color.
230   Image                       mBackgroundImage;     ///< The original background image
231   FrameBufferImage            mEffectImage;         ///< The image stores the adjusted color of the background image.The bubbles pick color from this image.
232   CameraActor                 mCameraActor;         ///< The render task views the scene from the perspective of this actor.
233
234   unsigned int                mDensity;             ///< How many bubbles will emit at each time, they are controlled by same uniforms in the shader.
235
236 };
237
238 } // namespace Internal
239
240 // Helpers for public-api forwarding methods
241 inline Internal::BubbleEmitter& GetImpl(Dali::Toolkit::BubbleEmitter& obj)
242 {
243   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
244   Dali::RefObject& handle = obj.GetImplementation();
245   return static_cast<Toolkit::Internal::BubbleEmitter&>(handle);
246 }
247
248 inline const Internal::BubbleEmitter& GetImpl(const Dali::Toolkit::BubbleEmitter& obj)
249 {
250   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
251   const Dali::RefObject& handle = obj.GetImplementation();
252   return static_cast<const Toolkit::Internal::BubbleEmitter&>(handle);
253 }
254
255 } // namespace Toolkit
256
257 } // namespace Dali
258
259 #endif /* __DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H__ */