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