DALi signals refactor to remove V2 naming
[platform/core/uifw/dali-toolkit.git] / optional / dali-toolkit / internal / transition-effects / cube-transition-effect-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_H__
2 #define __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_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/animation/animation.h>
23 #include <dali/public-api/images/bitmap-image.h>
24 #include <dali/public-api/images/frame-buffer-image.h>
25 #include <dali/public-api/object/base-object.h>
26 #include <dali/public-api/render-tasks/render-task.h>
27 #include <dali/public-api/shader-effects/shader-effect.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/public-api/transition-effects/cube-transition-effect.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 class CubeTransitionEffect;
39
40 namespace Internal
41 {
42
43 /**
44  * Create a image with size of viewAreaSize
45  * with the effect image as its center part and (0,0,0,1) at other parts
46  */
47 class FullAreaImageCreator : public ShaderEffect
48 {
49
50 public:
51
52   /**
53    * Create an uninitialized FullAreaImageCreator
54    * this can be initialized with FullAreaImageCreator::New()
55    */
56   FullAreaImageCreator(){}
57
58   /**
59    * @brief Destructor
60    *
61    * This is non-virtual since derived Handle types must not contain data or virtual methods.
62    */
63   ~FullAreaImageCreator(){}
64
65   /**
66    * Create an initialized FullAreaImageCreator.
67    * @return A handle to a newly allocated Dali resource.
68    */
69   static FullAreaImageCreator New()
70   {
71     std::string vertexShader(
72       "uniform mediump vec4 uRegion; \n"
73        "void main() \n"
74       "{\n"
75       "  gl_Position = uProjection * uModelView * vec4(aPosition, 1.0);\n"
76       "  vTexCoord.s = (aTexCoord.s - uRegion.s) / uRegion.p;"
77       "  vTexCoord.t = ( 1.0 - aTexCoord.t - uRegion.t) / uRegion.q;"
78       "}\n"
79     );
80
81     std::string fragmentShader(
82       "uniform mediump vec4 uRegion; \n"
83       "void main() \n"
84       "{\n"
85       "  if( vTexCoord.s > 0.0 && vTexCoord.s < 1.0 && vTexCoord.t > 0.0 && vTexCoord.t < 1.0) \n"
86       "  { \n"
87       "    gl_FragColor = texture2D( sEffect, vTexCoord ) * uColor ; \n"
88       "  } \n"
89       "  else \n"
90       "  { \n"
91       "    gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); \n"
92       "  } \n"
93       "}\n"
94     );
95
96     ShaderEffect shaderEffectCustom = ShaderEffect::New(vertexShader, fragmentShader);
97     FullAreaImageCreator handle( shaderEffectCustom );
98
99     return handle;
100   }
101
102   /**
103    * Set up the position and size of the effect texture
104    * @param[in] viewArea the size of full-area image to create
105    * @param[in] size the size of effect texture
106    */
107   void SetRegionSize( const Vector2& viewArea, const Vector2& size )
108   {
109     Vector2 sizeRatio( std::min(1.f, size.x / viewArea.x), std::min(1.f, size.y / viewArea.y) );
110     Vector4 region( (1.f-sizeRatio.x)*0.5f,
111                     (1.f-sizeRatio.y)*0.5f,
112                     sizeRatio.x,
113                     sizeRatio.y  );
114     SetUniform( "uRegion", region );
115   }
116
117 private:
118
119   FullAreaImageCreator( ShaderEffect handle )
120   : ShaderEffect( handle )
121   {}
122
123 };
124
125
126
127 /**
128  * CubeTransitionEffect implementation class
129  */
130 class CubeTransitionEffect : public Dali::BaseObject, public ConnectionTracker
131 {
132
133 public:
134
135   /**
136    * Destructor
137    */
138   ~CubeTransitionEffect();
139
140   /**
141    * @copydoc Toolkit::CubeTransitionEffect::SetTransitionDuration
142    */
143   void SetTransitionDuration( float duration );
144
145   /**
146    * @copydoc Toolkit::CubeTransitionEffect::GetTransitionDuration
147    */
148   float GetTransitionDuration() const;
149
150   /**
151    * @copydoc Toolkit::CubeTransitionEffect::SetCubeDisplacement
152    */
153   void SetCubeDisplacement( float displacement );
154
155   /**
156    * @copydoc Toolkit::CubeTransitionEffect::GetCubeDisplacement
157    */
158   float GetCubeDisplacement() const;
159
160   /**
161    * @copydoc Toolkit::CubeTransitionEffect::GetRoot
162    */
163   Actor GetRoot();
164
165   /**
166    * @copydoc Toolkit::CubeTransitionEffect::IsTransiting
167    */
168   bool IsTransiting();
169
170   /**
171    * @copydoc Toolkit::CubeTransitionEffect::SetFirstImage
172    */
173   void SetCurrentImage(ImageActor imageActor);
174
175   /**
176    * @copydoc Toolkit::CubeTransitionEffect::SetTargetImage
177    */
178   void SetTargetImage(ImageActor imageActor);
179
180   /**
181    * @copydoc Toolkit::CubeTransitionEffect::StartTransition(bool)
182    */
183   void StartTransition( bool toNextImage = true );
184
185   /**
186    * @copydoc Toolkit::CubeTransitionEffect::StartTransition(Vector2, Vector2)
187    */
188   void StartTransition( Vector2 panPosition, Vector2 panDisplacement );
189
190   /**
191    * @copydoc Toolkit::CubeTransitionEffect::PauseTransition()
192    */
193   void PauseTransition();
194
195   /**
196    * @copydoc Toolkit::CubeTransitionEffect::ResumeTransition()
197    */
198   void ResumeTransition();
199
200   /**
201    * @copydoc Toolkit::CubeTransitionEffect::StopTransition()
202    */
203   void StopTransition();
204
205 public: //Signal
206
207   /**
208    * @copydoc Toolkit::CubeTransitionEffect::TransitionCompletedSignal()
209    */
210   Toolkit::CubeTransitionEffect::TransitionCompletedSignalType& TransitionCompletedSignal();
211
212 protected:
213
214   /**
215    * Construct a new CubeTransitionEffect object
216    * Called in the constructor of subclasses
217    * @param[in] numRows How many rows of cubes
218    * @param[in] numColumns How many columns of cubes
219    * @param[in] viewAreaSize The size of view area for this transition effect
220    */
221   CubeTransitionEffect( unsigned int numRows, unsigned int numColumns, Size viewAreaSize );
222
223   /**
224    * Initialization steps: creating a layer, two groups of tiles,
225    * and one group of actors (cubes) serving as parents of every two tiles (one from each image).
226    */
227   void Initialize();
228
229 private:
230
231   /**
232    * Create an image actor to serve as a face of the cube
233    * @param[in] image The image to display.
234    * @param[in] color The color to set to the actor
235    * @return The tile actor created
236    */
237   ImageActor CreateTile( Image image, const Vector4& color );
238
239   /**
240    * Set Image content to tiles
241    * As only when the image ready, can we get correct image attributes
242    * so inside this function, the process needs to be passed to callBack of image resource loading succeed.
243    * @param[in] imageActor The imageActor whose image content will be set to the tiles
244    */
245   void SetImage(ImageActor imageActor);
246
247   /**
248    * Callback function of image resource loading succeed
249    * Set image and pixelArea to tiles
250    * @param[in] image The image content of the imageActor for transition
251    */
252   void OnImageLoaded(Image image);
253
254   /**
255    * Callback function of transition animation finished
256    * Hide transition layer, show current imageActor, and set isAnimating flag to false
257    * @param[in] source The cube transition animation
258    */
259   void OnTransitionFinished(Animation& source);
260
261   /**
262    * This method is called after the CubeTransitionEffect has been initialized.  Derived classes should do
263    * any second phase initialization by overriding this method.
264    */
265   virtual void OnInitialize() { }
266
267   /**
268    * This method is called after the a new transition is activated.
269    * Derived classes should do any specialized transition process by overriding this method.
270    * @param[in] panPosition The press down position of panGesture
271    * @param[in] panDisplacement The displacement vector of panGesture
272    */
273   virtual void OnStartTransition( Vector2 panPosition, Vector2 panDisplacement ) {}
274
275   /**
276    * This method is called when the transition is forced stop in the middle of animation.
277    * Derived classed should set the rotation status of the cubes to the same as the final state when the animation is finished completely.
278    * So that the next transition would be started correctly.
279    */
280   virtual void OnStopTransition() {}
281
282
283 protected:
284
285   unsigned int               mNumRows;
286   unsigned int               mNumColumns;
287   Size                       mViewAreaSize;
288   ActorContainer             mBoxes;
289   std::vector< ImageActor >  mTiles[2];
290   int                        mRotateIndex;
291   Size                       mTileSize;
292   Actor                      mRoot;
293
294   ImageActor                 mCurrentImage;
295   unsigned int               mContainerIndex;           //have the value 0 or 1, refer to mTiles[0] or mTiles[1]
296
297   bool                       mChangeTurningDirection;
298   bool                       mIsToNextImage;            //if true, cubes rotate counter-clockwise; else clockwise
299   bool                       mIsImageLoading;
300
301   float                      mAnimationDuration;
302   Animation                  mAnimation;
303   bool                       mIsAnimating;
304   bool                       mIsPaused;
305
306   float                      mCubeDisplacement;
307
308   bool                       mFirstTransition;
309
310   RenderTask                 mOffScreenTask;
311   FrameBufferImage           mOffScreenBuffer[2];
312   ImageActor                 mEmptyImage;
313   FullAreaImageCreator       mFullImageCreator;
314   unsigned int               mBufferIndex;
315
316   static const Vector4       FULL_BRIGHTNESS;
317   static const Vector4       HALF_BRIGHTNESS;
318
319 private:
320
321   Toolkit::CubeTransitionEffect::TransitionCompletedSignalType mTransitionCompletedSignal;
322
323 };
324
325 } // namespace Internal
326
327 // Helpers for public-api forwarding methods
328
329 inline Internal::CubeTransitionEffect& GetImpl(Dali::Toolkit::CubeTransitionEffect& obj)
330 {
331   DALI_ASSERT_ALWAYS(obj);
332
333   Dali::BaseObject& handle = obj.GetBaseObject();
334
335   return static_cast<Internal::CubeTransitionEffect&>(handle);
336 }
337
338 inline const Internal::CubeTransitionEffect& GetImpl(const Dali::Toolkit::CubeTransitionEffect& obj)
339 {
340   DALI_ASSERT_ALWAYS(obj);
341
342   const Dali::BaseObject& handle = obj.GetBaseObject();
343
344   return static_cast<const Internal::CubeTransitionEffect&>(handle);
345 }
346
347 } // namespace Toolkit
348
349 } // namespace Dali
350
351 #endif /* __DALI_TOOLKIT_INTERNAL_CUBE_TRANSITION_EFFECT_H__ */