[dali_1.9.26] Merge branch '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) 2020 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/devel-api/common/stage.h>
24 #include <dali/public-api/rendering/frame-buffer.h>
25 #include <dali/public-api/render-tasks/render-task.h>
26 #include <dali/public-api/rendering/geometry.h>
27 #include <dali/public-api/rendering/property-buffer.h>
28 #include <dali/public-api/rendering/renderer.h>
29 #include <dali/public-api/rendering/sampler.h>
30 #include <dali/public-api/rendering/shader.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/public-api/controls/control-impl.h>
34 #include <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Internal
43 {
44
45 class BubbleRenderer;
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                                      Texture shapeTexture,
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( Texture bgTexture, const Vector3& hsvDelta );
76
77   /**
78    * @copydoc Toolkit::BubbleEmitter::SetShape
79    */
80   void SetBubbleShape( Texture shapeTexture );
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::EmitBubble
94    */
95   void EmitBubble( Animation& animation, const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
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] shapeTexture 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                  Texture shapeTexture,
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    * Create the geometry of a mesh.
124    * @param[in] numOfPatch The triangle number in the mesh is 2*numOfPatch; two triangles for each bubble.
125    * @return The mesh geometry.
126    */
127   Geometry CreateGeometry( unsigned int numOfPatch );
128
129   /**
130    * Callback function of the finished signal of off-screen render task.
131    * @param[in] source The render task used to create the color adjusted background texture.
132    */
133   void OnRenderFinished(RenderTask& source);
134
135   /**
136    * Callback function from Stage to tell us if the context has been regained.
137    */
138   void OnContextRegained();
139
140   /**
141    * Set the uniform values to the shader effect to emit a bubble
142    * @param[in] bubbleRenderer The BubbleRenderer
143    * @param[in] curUniform The index of the uniform array in the shader
144    * @param[in] emitPosition The start position of the bubble movement.
145    * @param[in] direction The direction used to constrain the bubble to move in an adjacent direction around it.
146    * @param[in] displacement The displacement used to bound the moving distance of the bubble.
147    */
148   void SetBubbleParameter( BubbleRenderer& bubbleRenderer, unsigned int curUniform,
149                            const Vector2& emitPosition, const Vector2& direction, const Vector2& displacement );
150
151 private:
152
153   Actor                       mBubbleRoot;          ///<The bubble root actor. Need to add it to stage to get the bubbles rendered.
154   Texture                     mShapeTexture;        ///< The alpha channnel of this texture defines the bubble shape.
155   Texture                     mBackgroundTexture;   ///< The original background texture
156   Texture                     mEffectTexture;       ///< Texture which stores the adjusted color of the background image.The bubbles pick color from this image.
157   FrameBuffer                 mFrameBuffer;         ///< FrameBuffer used for offscreen rendering
158   CameraActor                 mCameraActor;         ///< The render task views the scene from the perspective of this actor.
159
160   Geometry                    mMeshGeometry;         ///< The mesh geometry which contains the vertices and indices data
161   TextureSet                  mTextureSet;           ///< The texture set which controls the bubble display
162   std::vector<BubbleRenderer> mBubbleRenderers;      ///< The BubbleRenderer vector, its size is mNumShader.
163
164   Vector2                     mMovementArea;        ///< The size of the bubble moving area, usually the same size as the background.
165   Vector2                     mBubbleSizeRange;     ///< The size range of the bubbles; x component is the low bound, and y component is the up bound.
166   Vector3                     mHSVDelta;            ///< The HSV difference used to adjust the background image color.
167
168   unsigned int                mNumBubblePerRenderer;   ///< How many bubbles for each BubbleRenderer.
169   unsigned int                mNumRenderer;            ///< How many BubbleRenderers are used.
170   unsigned int                mDensity;             ///< How many bubbles will emit at each time, they are controlled by same uniforms in the shader.
171   unsigned int                mTotalNumOfBubble;    ///< mNumBubblePerShader*mNumShader.
172   unsigned int                mCurrentBubble;       ///< Keep track of the index for the newly emitted bubble
173   unsigned int                mRandomSeed;          ///< Seed to generate random number.
174
175   bool                        mRenderTaskRunning;   ///< If the background render task is currently running
176
177 };
178
179 } // namespace Internal
180
181 // Helpers for public-api forwarding methods
182 inline Internal::BubbleEmitter& GetImpl(Dali::Toolkit::BubbleEmitter& obj)
183 {
184   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
185   Dali::RefObject& handle = obj.GetImplementation();
186   return static_cast<Toolkit::Internal::BubbleEmitter&>(handle);
187 }
188
189 inline const Internal::BubbleEmitter& GetImpl(const Dali::Toolkit::BubbleEmitter& obj)
190 {
191   DALI_ASSERT_ALWAYS(obj && "BubbleEmitter handle is empty");
192   const Dali::RefObject& handle = obj.GetImplementation();
193   return static_cast<const Toolkit::Internal::BubbleEmitter&>(handle);
194 }
195
196 } // namespace Toolkit
197
198 } // namespace Dali
199
200 #endif // DALI_TOOLKIT_INTERNAL_BUBBLE_EMITTER_IMPL_H