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