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