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