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