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