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