Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-demo.git] / examples / page-turn-view / page-turn-view-example.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <assert.h>
21 #include <cstdlib>
22 #include <string.h>
23 #include <iostream>
24
25 #include "shared/view.h"
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 // LOCAL STUFF
31 namespace
32 {
33 // The content amount of one page between portrait and landscape view are different
34 // set a ratio to modify the current page number when the rotation is changed
35 const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
36
37 const char* BOOK_COVER_PORTRAIT = ( DALI_IMAGE_DIR "book-portrait-cover.jpg" );
38 const char* BOOK_COVER_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover.jpg" );
39 const char* BOOK_COVER_BACK_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover-back.jpg" );
40
41 const char* PAGE_IMAGES_PORTRAIT[] =
42 {
43   DALI_IMAGE_DIR "book-portrait-p1.jpg",
44   DALI_IMAGE_DIR "book-portrait-p2.jpg",
45   DALI_IMAGE_DIR "book-portrait-p3.jpg",
46   DALI_IMAGE_DIR "book-portrait-p4.jpg",
47   DALI_IMAGE_DIR "book-portrait-p5.jpg"
48 };
49 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
50
51 const char* PAGE_IMAGES_LANDSCAPE[] =
52 {
53   DALI_IMAGE_DIR "book-landscape-p1.jpg",
54   DALI_IMAGE_DIR "book-landscape-p2.jpg",
55   DALI_IMAGE_DIR "book-landscape-p3.jpg",
56   DALI_IMAGE_DIR "book-landscape-p4.jpg",
57   DALI_IMAGE_DIR "book-landscape-p5.jpg",
58   DALI_IMAGE_DIR "book-landscape-p6.jpg",
59   DALI_IMAGE_DIR "book-landscape-p7.jpg",
60   DALI_IMAGE_DIR "book-landscape-p8.jpg"
61 };
62 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
63
64 }// end LOCAL STUFF
65
66 class PortraitPageFactory : public PageFactory
67 {
68   /**
69    * Query the number of pages available from the factory.
70    * The maximum available page has an ID of GetNumberOfPages()-1.
71    */
72   virtual unsigned int GetNumberOfPages()
73   {
74     return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
75   }
76   /**
77    * Create an image actor to represent a page.
78    * @param[in] pageId The ID of the page to create.
79    * @return An image actor, or an uninitialized pointer if the ID is out of range.
80    */
81   virtual Actor NewPage( unsigned int pageId )
82   {
83     ImageActor page;
84
85     if( pageId == 0 )
86     {
87       page = ImageActor::New( ResourceImage::New( BOOK_COVER_PORTRAIT ) );
88     }
89     else
90     {
91       page = ImageActor::New( ResourceImage::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
92     }
93
94     page.SetRelayoutEnabled( false );
95
96     return page;
97   }
98 };
99
100 class LandscapePageFactory : public PageFactory
101 {
102   /**
103    * Query the number of pages available from the factory.
104    * The maximum available page has an ID of GetNumberOfPages()-1.
105    */
106   virtual unsigned int GetNumberOfPages()
107   {
108     return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
109   }
110   /**
111    * Create an image actor to represent a page.
112    * @param[in] pageId The ID of the page to create.
113    * @return An image actor, or an uninitialized pointer if the ID is out of range.
114    */
115   virtual Actor NewPage( unsigned int pageId )
116   {
117     ImageActor pageFront;
118     ImageActor pageBack;
119     if( pageId == 0 )
120     {
121        pageFront = ImageActor::New( ResourceImage::New( BOOK_COVER_LANDSCAPE ) );
122        pageBack = ImageActor::New( ResourceImage::New( BOOK_COVER_BACK_LANDSCAPE ) );
123     }
124     else
125     {
126       unsigned int imageId = (pageId-1)*2;
127       pageFront = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
128       pageBack = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
129     }
130     pageFront.Add(pageBack);
131
132     pageFront.SetRelayoutEnabled( false );
133     pageBack.SetRelayoutEnabled( false );
134
135     return pageFront;
136   }
137 };
138
139 /**
140  * This example shows how to use the page turn UI control to implement the page-turn demo
141  * The effect follows the pan gesture to animate the page
142  * Pan the image inwards, the page will bent,
143  * Depends on the distance of the panning, the image might turn over or slide back
144  * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
145  * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
146 */
147 class PageTurnController : public ConnectionTracker
148 {
149 public:
150   PageTurnController( Application &app );
151   ~PageTurnController();
152
153   //This method gets called once the main loop of application is up and running
154   void OnInit( Application& app );
155
156 private:
157
158   /**
159    * This method gets called when the screen is rotated, switch between portrait and landscape views
160    * param [in] view The view receiving the orientation change signal
161    * param [in] animation The Orientation Rotating animation
162    * param [in] orientation The current screen orientation
163    */
164   void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
165
166   /**
167    * Main key event handler
168    */
169   void OnKeyEvent(const KeyEvent& event);
170
171   /**
172    * Callback function of page turned signal
173    * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
174    * @param[in] pageIndex The index of the page turned over
175    * @param[in] isTurningForward The turning direction, forwards or backwards
176    */
177   void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
178
179   /**
180    * Callback function of page turned signal
181    * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
182    * @param[in] pageIndex The index of the page turned over
183    * @param[in] isTurningForward The turning direction, forwards or backwards
184    */
185   void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
186
187   /**
188    * Callback function of page started pan signal
189    *
190    * @param[in] pageTurnView The calling page turn view
191    */
192   void OnPageStartedPan( PageTurnView pageTurnView );
193
194   /**
195    * Callback function of page finished pan signal
196    *
197    * @param[in] pageTurnView The calling page turn view
198    */
199   void OnPageFinishedPan( PageTurnView pageTurnView );
200
201 private:
202
203   Application&                mApplication;
204   View                        mView;
205
206   PageTurnView                mPageTurnPortraitView;
207   PageTurnView                mPageTurnLandscapeView;
208   PortraitPageFactory         mPortraitPageFactory;
209   LandscapePageFactory        mLandscapePageFactory;
210
211   bool                        mIsPortrait;
212 };
213
214 PageTurnController::PageTurnController( Application &app )
215 :mApplication( app ),
216  mIsPortrait( true )
217 {
218   // Connect to the Application's Init signal
219   app.InitSignal().Connect( this, &PageTurnController::OnInit );
220 }
221
222 PageTurnController::~PageTurnController()
223 {
224 }
225
226
227 void PageTurnController::OnInit( Application& app )
228 {
229   // The Init signal is received once ( only ) during the Application lifetime
230
231   DemoHelper::RequestThemeChange();
232
233   Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
234
235   Stage stage = Stage::GetCurrent();
236   Vector2 stageSize =  stage.GetSize();
237
238   // Create default View.
239   mView = View::New();
240   mView.SetRelayoutEnabled( false );
241   stage.Add( mView );
242
243   Dali::Window winHandle = app.GetWindow();
244   winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
245   winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
246   winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE  );
247   winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
248   // FIXME
249   //app.GetOrientation().ChangedSignal().Connect( &mView, &View::OrientationChanged );
250   // view will response to orientation change to display portrait or landscape views
251   mView.OrientationAnimationStartedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
252
253   mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
254   mPageTurnPortraitView.SetRelayoutEnabled( false );
255   mPageTurnPortraitView.SetSpineShadowParameter( Vector2(70.f, 30.f) );
256   mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
257   mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
258   mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
259   mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
260   mPageTurnPortraitView.SetPositionInheritanceMode( USE_PARENT_POSITION );
261
262   mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.y*0.5f, stageSize.x) );
263   mPageTurnLandscapeView.SetRelayoutEnabled( false );
264   mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
265   mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
266   mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
267   mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
268   mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
269
270   mView.Add(mPageTurnPortraitView);
271 }
272
273 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
274 {
275   switch( orientation.GetDegrees() )
276   {
277     // portrait view, display page in the right side only
278     case 0:
279     case 180:
280     {
281       if( !mIsPortrait )
282       {
283         mView.Remove( mPageTurnLandscapeView );
284         mView.Add( mPageTurnPortraitView );
285         int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
286         mPageTurnPortraitView.GoToPage( currentPage );
287         mIsPortrait = true;
288       }
289       break;
290     }
291     // display pages in both sides
292     case 90:
293     case 270:
294     {
295       if( mIsPortrait )
296       {
297         mView.Remove( mPageTurnPortraitView );
298         mView.Add( mPageTurnLandscapeView );
299         int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
300         mPageTurnLandscapeView.GoToPage( currentPage );
301         mIsPortrait = false;
302       }
303       break;
304     }
305     default:
306     break;
307   }
308 }
309
310 /**
311  * Main key event handler
312  */
313 void PageTurnController::OnKeyEvent(const KeyEvent& event)
314 {
315   if(event.state == KeyEvent::Down)
316   {
317     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
318     {
319       mApplication.Quit();
320     }
321   }
322 }
323
324 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
325 {
326   std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
327            << " page " << pageIndex
328            << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
329            << std::endl;
330 }
331
332 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
333 {
334   std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
335            << " page " << pageIndex
336            << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
337            << std::endl;
338 }
339
340 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
341 {
342   std::cout<< "Starting to pan" << std::endl;
343 }
344
345 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
346 {
347   std::cout<< "Finished panning" << std::endl;
348 }
349
350 // Entry point for applications
351 int main( int argc, char **argv )
352 {
353   Application app = Application::New(&argc, &argv);
354   PageTurnController test ( app );
355
356   app.MainLoop();
357
358   return 0;
359 }
360