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