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