Merge branch 'tizen' into devel/new_mesh
[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) 2015 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/geometry/geometry.h>
26 #include <dali/public-api/images/frame-buffer-image.h>
27 #include <dali/public-api/render-tasks/render-task.h>
28 #include <dali/public-api/shader-effects/sampler.h>
29 #include <dali/public-api/shader-effects/material.h>
30
31 // INTERNAL INCLUDES
32 #include <dali-toolkit/public-api/controls/control-impl.h>
33 #include <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
34
35 namespace Dali
36 {
37
38 namespace Toolkit
39 {
40
41 namespace Internal
42 {
43
44 class BubbleActor;
45 typedef IntrusivePtr<BubbleActor> BubbleActorPtr;
46
47 /**
48  * BubbleEmitter implementation class.
49  */
50 class BubbleEmitter : public Control
51 {
52 public:
53
54   /**
55    * Destructor
56    */
57   ~BubbleEmitter();
58
59   /**
60    * @copydoc Toolkit::BubbleEmitter::New
61    */
62   static Toolkit::BubbleEmitter New( const Vector2& winSize,
63                                      Image shapeImage,
64                                      unsigned int maximumNumberOfBubble,
65                                      const Vector2& bubbleSizeRange );
66
67   /**
68    * @copydoc Toolkit::BubbleEmitter::GetRootActor
69    */
70   Actor GetRootActor();
71
72   /**
73    * @copydoc Toolkit::BubbleEmitter::SetBackground
74    */
75   void SetBackground( Image bgImage, const Vector3& hsvDelta );
76
77   /**
78    * @copydoc Toolkit::BubbleEmitter::SetShapeImage
79    */
80   void SetShapeImage( Image shapeImage );
81
82   /**
83    * @copydoc Toolkit::BubbleEmiter::SetBubbleScale
84    */
85   void SetBubbleScale( float scale );
86
87   /**
88    * @copydoc Toolkit::BubbleEmitter::SetBubbleDensity
89    */
90   void SetBubbleDensity( unsigned int density );
91
92   /**
93    * @copydoc Toolkit::BubbleEmitter::SetBlendMode
94    */
95   void SetBlendMode( bool enable );
96
97   /**
98    * @copydoc Toolkit::BubbleEmitter::EmitBubble
99    */
100   void EmitBubble( Animation& animation, const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
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    * Create the geometry of a mesh.
129    * @param[in] numOfPatch The triangle number in the mesh is 2*numOfPatch; two triangles for each bubble.
130    * @return The mesh geometry.
131    */
132   Geometry CreateGeometry( unsigned int numOfPatch );
133
134   /**
135    * Callback function of the finished signal of off-screen render task.
136    * @param[in] source The render task used to create the color adjusted background image.
137    */
138   void OnRenderFinished(RenderTask& source);
139
140   /**
141    * Callback function from Stage to tell us if the context has been regained.
142    */
143   void OnContextRegained();
144
145   /**
146    * Set the uniform values to the shader effect to emit a bubble
147    * @param[in] bubbleActor The BubbleActor to render the current bubble
148    * @param[in] curUniform The index of the uniform array in the shader
149    * @param[in] emitPosition The start position of the bubble movement.
150    * @param[in] direction The direction used to constrain the bubble to move in an adjacent direction around it.
151    * @param[in] displacement The displacement used to bound the moving distance of the bubble.
152    */
153   void SetBubbleParameter( BubbleActorPtr bubbleActor, unsigned int curUniform,
154                            const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
155
156 private:
157
158   Actor                       mBubbleRoot;          ///<The bubble root actor. Need to add it to stage to get the bubbles rendered.
159   Image                       mShapeImage;          ///< The alpha channnel of this texture defines the bubble shape.
160   Image                       mBackgroundImage;     ///< The original background image
161   FrameBufferImage            mEffectImage;         ///< The image stores the adjusted color of the background image.The bubbles pick color from this image.
162   CameraActor                 mCameraActor;         ///< The render task views the scene from the perspective of this actor.
163
164   Sampler                     mSamplerBackground;    ///< The sampler which provides the background image to material
165   Sampler                     mSamplerBubbleShape;   ///< The sampler which provides the bubble shape image to material
166   Geometry                    mMeshGeometry;         ///< The mesh geometry which contains the vertices and indices data
167   Material                    mMaterial;             ///< The material which controls the bubble display
168   std::vector<BubbleActorPtr> mBubbleActors;         ///< The meshActor vector, its size is mNumShader.
169
170   Vector2                     mMovementArea;        ///< The size of the bubble moving area, usually the same size as the background image actor.
171   Vector2                     mBubbleSizeRange;     ///< The size range of the bubbles; x component is the low bound, and y component is the up bound.
172   Vector3                     mHSVDelta;            ///< The HSV difference used to adjust the background image color.
173
174   unsigned int                mNumBubblePerActor;   ///< How many bubbles for each BubbleActor.
175   unsigned int                mNumActor;            ///< How many BubbleActors are used.
176   unsigned int                mDensity;             ///< How many bubbles will emit at each time, they are controlled by same uniforms in the shader.
177   unsigned int                mTotalNumOfBubble;    ///< mNumBubblePerShader*mNumShader.
178   unsigned int                mCurrentBubble;       ///< Keep track of the index for the newly emitted bubble
179
180   bool                        mRenderTaskRunning;   ///< If the background render task is currently running
181
182 };
183
184 } // namespace Internal
185
186 // Helpers for public-api forwarding methods
187 inline Internal::BubbleEmitter& GetImpl(Dali::Toolkit::BubbleEmitter& obj)
188 {
189   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
190   Dali::RefObject& handle = obj.GetImplementation();
191   return static_cast<Toolkit::Internal::BubbleEmitter&>(handle);
192 }
193
194 inline const Internal::BubbleEmitter& GetImpl(const Dali::Toolkit::BubbleEmitter& obj)
195 {
196   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
197   const Dali::RefObject& handle = obj.GetImplementation();
198   return static_cast<const Toolkit::Internal::BubbleEmitter&>(handle);
199 }
200
201 } // namespace Toolkit
202
203 } // namespace Dali
204
205 #endif /* __DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H__ */