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