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