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