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