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