[3.0] Version downgrade (1.2.0 to 1.1.45)
[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   Stage stage = Stage::GetCurrent();
246   Vector2 stageSize =  stage.GetSize();
247
248   mButtonLayer = Layer::New();
249   mButtonLayer.SetAnchorPoint( Dali::AnchorPoint::CENTER );
250   mButtonLayer.SetParentOrigin( Dali::ParentOrigin::CENTER );
251   mButtonLayer.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
252   Toolkit::PushButton button = Toolkit::PushButton::New();
253   button.SetAnchorPoint( AnchorPoint::TOP_RIGHT );
254   button.SetParentOrigin( ParentOrigin::TOP_RIGHT );
255   button.SetUnselectedImage( CHANGE_IMAGE_ICON  );
256   button.SetSelectedImage( CHANGE_IMAGE_ICON_SELECTED );
257   button.SetLeaveRequired( true );
258   button.SetScale(1.5f);
259   button.PressedSignal().Connect( this, &PageTurnController::OnButtonClicked );
260   stage.Add( mButtonLayer );
261   mButtonLayer.Add(button);
262
263   Vector2 bookSize( stageSize.x > stageSize.y ? stageSize.y : stageSize.x,
264                     stageSize.x > stageSize.y ? stageSize.x : stageSize.y );
265
266   mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, bookSize );
267   mPageTurnPortraitView.SetParentOrigin( ParentOrigin::CENTER );
268   mPageTurnPortraitView.SetAnchorPoint( AnchorPoint::CENTER );
269   mPageTurnPortraitView.SetProperty( PageTurnView::Property::SPINE_SHADOW, Vector2(70.f, 30.f) );
270   mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
271   mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
272   mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
273   mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
274
275   mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(bookSize.y*0.5f, bookSize.x) );
276   mPageTurnLandscapeView.SetParentOrigin( ParentOrigin::CENTER );
277   mPageTurnLandscapeView.SetAnchorPoint( AnchorPoint::CENTER );
278   mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
279   mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
280   mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
281   mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
282
283   if( stageSize.x > stageSize.y )
284   {
285     stage.Add(mPageTurnLandscapeView);
286     mPageTurnPortraitView.SetOrientation(Degree(90.f), Vector3::ZAXIS);
287     mIsPortrait = false;
288   }
289   else
290   {
291     stage.Add(mPageTurnPortraitView);
292     mPageTurnLandscapeView.SetOrientation(Degree(90.f), Vector3::ZAXIS);
293     mIsPortrait = true;
294   }
295
296   mButtonLayer.RaiseToTop();
297 }
298
299 bool PageTurnController::OnButtonClicked(Toolkit::Button button)
300 {
301   if( mIsPortrait )
302   {
303     mPageTurnPortraitView.Unparent();
304     Stage::GetCurrent().Add( mPageTurnLandscapeView );
305     int pageId = mPageTurnPortraitView.GetProperty( PageTurnView::Property::CURRENT_PAGE_ID ).Get<int>();
306     int currentPage = ceil( static_cast<float>(pageId) / PAGE_NUMBER_CORRESPONDING_RATIO );
307     mPageTurnLandscapeView.SetProperty(PageTurnView::Property::CURRENT_PAGE_ID, currentPage );
308   }
309   else
310   {
311     mPageTurnLandscapeView.Unparent();
312     Stage::GetCurrent().Add( mPageTurnPortraitView );
313     int pageId = mPageTurnLandscapeView.GetProperty( PageTurnView::Property::CURRENT_PAGE_ID ).Get<int>();
314     int currentPage = floor(pageId * PAGE_NUMBER_CORRESPONDING_RATIO );
315     mPageTurnPortraitView.SetProperty(PageTurnView::Property::CURRENT_PAGE_ID, currentPage );
316   }
317
318   mIsPortrait = !mIsPortrait;
319   mButtonLayer.RaiseToTop();
320   return true;
321 }
322
323 /**
324  * Main key event handler
325  */
326 void PageTurnController::OnKeyEvent(const KeyEvent& event)
327 {
328   if(event.state == KeyEvent::Down)
329   {
330     if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
331     {
332       mApplication.Quit();
333     }
334   }
335 }
336
337 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
338 {
339   std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
340            << " page " << pageIndex
341            << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
342            << std::endl;
343 }
344
345 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
346 {
347   std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
348            << " page " << pageIndex
349            << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
350            << std::endl;
351 }
352
353 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
354 {
355   std::cout<< "Starting to pan" << std::endl;
356 }
357
358 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
359 {
360   std::cout<< "Finished panning" << std::endl;
361 }
362
363 // Entry point for applications
364 int DALI_EXPORT_API main( int argc, char **argv )
365 {
366   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
367   PageTurnController test ( app );
368
369   app.MainLoop();
370
371   return 0;
372 }