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