[dali_1.1.30] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / page-turn-view / page-turn-view-impl.h
1 #ifndef __DALI_TOOLKIT_INTERNAL_PAGE_TURN_VIEW_IMPL_H__
2 #define __DALI_TOOLKIT_INTERNAL_PAGE_TURN_VIEW_IMPL_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/devel-api/common/map-wrapper.h>
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/devel-api/rendering/renderer.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/public-api/controls/control-impl.h>
28 #include <dali-toolkit/public-api/controls/page-turn-view/page-turn-view.h>
29 #include <dali-toolkit/public-api/controls/page-turn-view/page-factory.h>
30 #include <dali-toolkit/devel-api/controls/shadow-view/shadow-view.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 class PageTurnView : public Control
42 {
43 protected:
44
45   /**
46    * The book page class
47    */
48   struct Page
49   {
50     /**
51      * Constructor
52      */
53     Page();
54     /**
55      * Destructor
56      */
57     ~Page(){};
58
59     /**
60      * Set the page image content
61      * @param[in] image The content of the page.
62      */
63     void SetImage( Image image );
64
65     /**
66      * Apply an effect onto the page actor.
67      * @param[in] shader The shader for rendering effect.
68      */
69     void UseEffect(Shader shader);
70
71     /**
72      * Apply an effect onto the page actor.
73      * @param[in] shader The shader for rendering effect.
74      * @param[in] geometry The geometry for rendering effect.
75      */
76     void UseEffect(Shader shader, Geometry geometry);
77
78     /**
79      * Change the page turning direction.
80      */
81     void ChangeTurnDirection();
82
83     /**
84      * Set the pan displacement property
85      * @param[in] value The property value
86      */
87     void SetPanDisplacement(float value);
88
89     /**
90      * Set the pan center property
91      * @param[in] value The property value
92      */
93     void SetPanCenter( const Vector2& value );
94
95     /**
96      * Set the original center property to be used by shader
97      * @param[in] value The property value
98      */
99     void SetOriginalCenter( const Vector2& value );
100
101     /**
102      * Set the current center property to be used by shader
103      * @param[in] value The property value
104      */
105     void SetCurrentCenter( const Vector2& value );
106
107     Actor actor;                              ///< The page actor
108     Material material;                        ///< The material of the actor
109     Renderer renderer;                        ///< The renderer of the actor
110     bool isTurnBack;                          ///< The turning direction
111     Property::Index propertyPanDisplacement;  ///< The horizontal displacement of the pan
112     Property::Index propertyPanCenter;        ///< The current pan position
113     Property::Index propertyOriginalCenter;   ///< The original center to be used by the shader
114     Property::Index propertyCurrentCenter;    ///< The current center to be used by the shader
115     Property::Index propertyTurnDirection;    ///< The turning direction property
116   };
117
118
119 protected:
120
121   /**
122    * Constructor.
123    * It initializes the PageTurnView members
124    */
125   PageTurnView( PageFactory& pageFactory, const Vector2& pageSize );
126
127   /**
128    * A reference counted object may only be deleted by calling Unreference()
129    */
130   virtual ~PageTurnView();
131
132 public:
133
134   /**
135    * Set the page size
136    * @param[in] pageSize The size of pages
137    */
138   void SetPageSize( const Vector2& pageSize );
139
140   /**
141    * Retrieve the page size.
142    * @return The page size.
143    */
144   Vector2 GetPageSize();
145
146   /**
147    * Set the spine shadow parameter to the shader effects.
148    * The two parameters are the major&minor radius (in pixels) to form an ellipse shape.
149    * The top-left quarter of this ellipse is used to calculate spine normal for simulating shadow.
150    * @param [in] spineShadowParameter The major&minor ellipse radius for the simulated spine shadow.
151    */
152   void SetSpineShadowParameter( const Vector2& spineShadowParameter );
153
154   /**
155    * Retrieve the spine shadow parameter of the shader effects.
156    * @return The spine shadow parameter.
157    */
158   Vector2 GetSpineShadowParameter();
159
160   /*
161    * Jump to a given page.
162    * @param[in] pageId The new current page id.
163    */
164   void GoToPage( unsigned int pageId );
165
166   /**
167    * Retrieve the id of the current Page.
168    * @return The current page id.
169    */
170   unsigned int GetCurrentPage();
171
172 protected:
173
174   /**
175    * This method gets a page from the factory and add to the control
176    * to keep NUMBER_OF_CACHED_PAGES_EACH_SIDE pages available in each side
177    * @param[in] pageIndex The index of the page to be added
178    */
179   void AddPage( int pageIndex );
180
181   /**
182    * This method removes a page from the control
183    * to keep only NUMBER_OF_CACHED_PAGES_EACH_SIDE pages available in each side
184    * @param[in] pageIndex The index of the page to be removed
185    */
186   void RemovePage( int pageIndex );
187
188   /**
189    * This method updates the actor and animation states after one page is turned over
190    * This method is a callback, connected when receiving the finished signal of a page turning over animation.
191    * @param [in] the page turning over animation handle
192    */
193   void TurnedOver( Animation& animation );
194
195   /**
196    * This method organize the depth of the pages on stage
197    * It is called when there is page added or removed from the control
198    */
199   void OrganizePageDepth();
200
201 private:
202
203   /**
204    * Create shader from a property map.
205    * @param[in] shaderMap The shader property map;
206    * @return The created shader.
207    */
208   Shader CreateShader( const Property::Map& shaderMap );
209
210  /**
211   * Set up the shadow view control to cast shadow
212   */
213   void SetupShadowView();
214
215   /**
216    * This method defines the processes when the pan started, gets called by OnPan( .. )
217    * @param[in] gesturePosition The current touch position in local page actor coordinates.
218    */
219   void PanStarted( const Vector2& gesturePosition );
220
221   /**
222    * This method defines the processes when the pan continuing, gets called by OnPan( .. )
223    * @param[in] gesturePosition The current touch position in local page actor coordinates.
224    */
225   void PanContinuing( const Vector2& gesturePosition );
226
227   /**
228    * This method defines the processes when the pan finished, gets called by OnPanGesture( .. )
229    * @param[in] gesturePosition The current touch position in local page actor coordinates.
230    * @param[in] gestureSpeed The speed of the pan ( in pixels per millisecond )
231    */
232   void PanFinished( const Vector2& gesturePosition, float gestureSpeed );
233
234   /**
235    * This method updates the actor and the animation states after one page is slidden back instead of turned over
236    * This method is a callback, connected when receiving the finished signal of a page sliding back animation.
237    * @param [in] the page sliding back animation handle
238    */
239   void SliddenBack( Animation& animation );
240
241   /**
242    * Stop the page turning animation and contraint.
243    * This method should be called when taking off stage or jump to a specified page.
244    */
245   void StopTurning();
246
247 private: // from Control
248
249   /**
250    * @copydoc Toolkit::Control::OnPan
251    */
252   virtual void OnPan( const PanGesture& gesture );
253
254   /**
255    * @copydoc Toolkit::Control::OnInitialize
256    */
257   virtual void OnInitialize();
258
259   /**
260    * @copydoc CustomActorImpl::OnStageConnection()
261    */
262   virtual void OnStageConnection( int depth );
263
264   /**
265    * @copydoc CustomActorImpl::OnStageDisconnection()
266    */
267   virtual void OnStageDisconnection();
268
269 private: // implemented differently by PageTurnLandscapeView and PageTurnPortraitView
270
271   /**
272    * This method is called after the pageTurnView initialization.
273    * To set the size of the control size and the parent origin of turning page layer
274    * Implemented in subclasses to provide specific behaviour.
275    */
276   virtual void OnPageTurnViewInitialize() = 0;
277
278   /**
279    * This method is called after the a new page is added to the stage.
280    * Could be re-implemented in subclasses to provide specific behaviour
281    * @param[in] newPage The added page actor
282    * @param[in] isLeftSide Which side the new page is added to
283    */
284   virtual void OnAddPage( Actor newPage, bool isLeftSide ) { }
285
286   /**
287    * This method is called when pan started or continuing
288    * to calculate the pan position in local page actor coordinate given the pan position in control coordinate
289    * Implemented in subclasses to provide specific behaviour.
290    * @param[in] gesturePosition The pan position in the control coordinate
291    * @return The pan position in the page actor local coordinate
292    */
293   virtual Vector2 SetPanPosition( const Vector2& gesturePosition ) = 0;
294
295   /**
296    * This method is called when pan started to determined which page is panned given the pan position in control coordinate
297    * Implemented in subclasses to provide specific behaviour.
298    * @param[in] gesturePosition The pan position in the control coordinate
299    */
300   virtual void SetPanActor( const Vector2& panPosition ) = 0;
301
302   /**
303    * This method is called when pan finished to detect outwards flick
304    * In portrait view, start an animation of turning previous page back when outwards flick is detected
305    * In landscape view, nothing to do
306    * @param[in] panPosition The pan position in the page actor local coordinate
307    * @param[in] gestureSpeed The speed of the pan gesture( in pixels per millisecond )
308    */
309   virtual void OnPossibleOutwardsFlick( const Vector2& panPosition, float gestureSpeed ) { }
310
311   /**
312    * This method is called when page is turned over
313    * In portrait view, the page on the left side is not rendered
314    * @param[in] actor The page actor
315    * @param[in] isLeftSide Which side the page is turned to
316    */
317   virtual void OnTurnedOver( Actor actor, bool isLeftSide ) { }
318
319 public: //signal and property
320
321   /**
322    * @copydoc Toolkit::PageTurnView::PageTurnStartedSignal()
323    */
324   Toolkit::PageTurnView::PageTurnSignal& PageTurnStartedSignal();
325
326   /**
327    * @copydoc Toolkit::PageTurnView::PageTurnFinishedSignal()
328    */
329   Toolkit::PageTurnView::PageTurnSignal& PageTurnFinishedSignal();
330
331   /**
332    * @copydoc Toolkit::PageTurnView::PagePanStartSignal()
333    */
334   Toolkit::PageTurnView::PagePanSignal& PagePanStartedSignal();
335
336   /**
337    * @copydoc Toolkit::PageTurnView::PagePanFinishedSignal()
338    */
339   Toolkit::PageTurnView::PagePanSignal& PagePanFinishedSignal();
340
341   /**
342     * Connects a callback function with the object's signals.
343     * @param[in] object The object providing the signal.
344     * @param[in] tracker Used to disconnect the signal.
345     * @param[in] signalName The signal to connect to.
346     * @param[in] functor A newly allocated FunctorDelegate.
347     * @return True if the signal was connected.
348     * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
349     */
350   static bool DoConnectSignal( BaseObject* object, ConnectionTrackerInterface* tracker, const std::string& signalName, FunctorDelegate* functor );
351
352    // Properties
353
354   /**
355    * Called when a property of an object of this type is set.
356    * @param[in] object The object whose property is set.
357    * @param[in] index The property index.
358    * @param[in] value The new property value.
359    */
360   static void SetProperty( BaseObject* object, Property::Index index, const Property::Value& value );
361
362   /**
363    * Called to retrieve a property of an object of this type.
364    * @param[in] object The object whose property is to be retrieved.
365    * @param[in] index The property index.
366    * @return The current value of the property.
367    */
368   static Property::Value GetProperty( BaseObject* object, Property::Index index );
369
370 private:
371
372   //Undefined
373   PageTurnView( const PageTurnView& );
374
375   //undefined
376   PageTurnView& operator=(const PageTurnView& rhs);
377
378 protected:
379
380   Layer                          mTurningPageLayer;        ///< The layer for the turning page, to avoid possible depth conflict
381   Toolkit::ShadowView            mShadowView;              ///< The shadow view control for shadow casting
382   Actor                          mShadowPlaneBackground;   ///< The plane for the shadow to cast on
383   Actor                          mPointLight;              ///< The point light used for shadow casting
384
385   PageFactory* const             mPageFactory;             ///< The factory which provides the page actors
386   Shader                         mTurnEffectShader;        ///< The group of PageTurnEffects
387   Shader                         mSpineEffectShader;       ///< The book spine shader effect
388   Geometry                       mGeometry;                ///< The grid geometry for pages
389
390   std::vector<Page>              mPages;                   ///< The vector of pages on stage
391   std::map<Animation,int>        mAnimationPageIdPair;     ///< The map to keep track which page actor is the animation act on
392
393   Vector2                        mPageSize;                ///< The page size
394   Vector2                        mControlSize;             ///< The size of the control, it is decided by the page size, the SetSize from application can not change it
395   Vector2                        mSpineShadowParameter;    ///< The spine shadow parameter for all the above shader effects
396   Vector2                        mOriginalCenter;          ///< The original center set to the PageTurnEffect
397   Vector2                        mCurrentCenter;           ///< The current center set to the PageTurnEffect
398   Vector2                        mPressDownPosition;       ///< The first press down position of the pan gesture
399
400   float                          mDistanceUpCorner;        ///< The distance between the original center of PageTurnEffect and the top-left corner of the page
401   float                          mDistanceBottomCorner;    ///< The distance between the original center of PageTurnEffect and the bottom-left corner of the page
402   float                          mPanDisplacement;         ///< The displacement of the pan after the constrains are applied
403
404   int                            mTotalPageCount;          ///< The total number of pages provided by the page factory
405   int                            mCurrentPageIndex;        ///< The index of the current page, between 0 ~ mTotalPageCount-1
406   int                            mTurningPageIndex;        ///< The index of the turning page
407   int                            mIndex;                   ///< The index to keep track which PanDisplacementProperty, CurrentCenterProperty is used for the current panning page
408   int                            mSlidingCount;            ///< The boolean vector to keep track whether there are animating pages sliding back
409   int                            mAnimatingCount;          ///< The boolean vector to keep track which PageTurnEffect, PanDisplacementProperty, CurrentCenterProperty is available for using
410
411   bool                           mConstraints;             ///< The boolean to keep track the constrains are applied or not
412   bool                           mPress;                   ///< The boolean to keep track the state of the pageTurnEffect is activated or not
413   bool                           mPageUpdated;             ///< The boolean to keep track whether is page is updated after any turning activity
414
415   Toolkit::PageTurnView::PageTurnSignal   mPageTurnStartedSignal;   ///< The signal to notify that a page has started turning
416   Toolkit::PageTurnView::PageTurnSignal   mPageTurnFinishedSignal;  ///< The signal to notify that a page has finished turning
417   Toolkit::PageTurnView::PagePanSignal    mPagePanStartedSignal;    ///< The signal to notify that a page has started panning
418   Toolkit::PageTurnView::PagePanSignal    mPagePanFinishedSignal;   ///< The signal to notify that a page has finished panning
419
420   static const char * const PROPERTY_TEXTURE_WIDTH;     ///< The uniform name of texture width
421   static const char * const PROPERTY_ORIGINAL_CENTER;   ///< The property name of original center, which is used to constrain the uniforms
422   static const char * const PROPERTY_CURRENT_CENTER;    ///< The property name of current center, which is used to constrain the uniforms
423
424   static const int               MAXIMUM_TURNING_NUM;                  ///< How many pages are allowed to animating in the same time
425   static const int               NUMBER_OF_CACHED_PAGES_EACH_SIDE;     ///< The maximum number of pages kept, (MAXIMUM_ANIMATION_NUM+1) pages for each side
426   static const int               NUMBER_OF_CACHED_PAGES;               ///< The maximum number of pages kept, (MAXIMUM_ANIMATION_NUM+1)*2 pages in total
427   static const float             STATIC_PAGE_INTERVAL_DISTANCE;        ///< The depth interval between stacked pages (static pages)
428 };
429
430 } // namespace Internal
431
432
433 // Helpers for public-api forwarding methods
434
435 inline Toolkit::Internal::PageTurnView& GetImplementation(Toolkit::PageTurnView& pub)
436 {
437   DALI_ASSERT_ALWAYS(pub);
438
439   Dali::RefObject& handle = pub.GetImplementation();
440
441   return static_cast<Toolkit::Internal::PageTurnView&>(handle);
442 }
443
444 inline const Toolkit::Internal::PageTurnView& GetImplementation(const Toolkit::PageTurnView& pub)
445 {
446   DALI_ASSERT_ALWAYS(pub);
447
448   const Dali::RefObject& handle = pub.GetImplementation();
449
450   return static_cast<const Toolkit::Internal::PageTurnView&>(handle);
451 }
452
453 } // namespace Toolkit
454
455 } // namespace Dali
456 #endif /* __DALI_TOOLKIT_INTERNAL_PAGE_TURN_VIEW_IMPL_H__ */