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