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