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