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