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