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