2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali/dali.h>
22 #include <dali-toolkit/dali-toolkit.h>
25 using namespace Dali::Toolkit;
30 // The content amount of one page between portrait and landscape view are different
31 // set a ratio to modify the current page number when the rotation is changed
32 const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
34 const char* BOOK_COVER_PORTRAIT = ( DALI_IMAGE_DIR "book-portrait-cover.jpg" );
35 const char* BOOK_COVER_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover.jpg" );
36 const char* BOOK_COVER_BACK_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover-back.jpg" );
38 const char* PAGE_IMAGES_PORTRAIT[] =
40 DALI_IMAGE_DIR "book-portrait-p1.jpg",
41 DALI_IMAGE_DIR "book-portrait-p2.jpg",
42 DALI_IMAGE_DIR "book-portrait-p3.jpg",
43 DALI_IMAGE_DIR "book-portrait-p4.jpg",
44 DALI_IMAGE_DIR "book-portrait-p5.jpg"
46 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
48 const char* PAGE_IMAGES_LANDSCAPE[] =
50 DALI_IMAGE_DIR "book-landscape-p1.jpg",
51 DALI_IMAGE_DIR "book-landscape-p2.jpg",
52 DALI_IMAGE_DIR "book-landscape-p3.jpg",
53 DALI_IMAGE_DIR "book-landscape-p4.jpg",
54 DALI_IMAGE_DIR "book-landscape-p5.jpg",
55 DALI_IMAGE_DIR "book-landscape-p6.jpg",
56 DALI_IMAGE_DIR "book-landscape-p7.jpg",
57 DALI_IMAGE_DIR "book-landscape-p8.jpg"
59 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
63 class PortraitPageFactory : public PageFactory
66 * Query the number of pages available from the factory.
67 * The maximum available page has an ID of GetNumberOfPages()-1.
69 virtual unsigned int GetNumberOfPages()
71 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
74 * Create an image actor to represent a page.
75 * @param[in] pageId The ID of the page to create.
76 * @return An image actor, or an uninitialized pointer if the ID is out of range.
78 virtual Actor NewPage( unsigned int pageId )
82 return ImageActor::New( Image::New( BOOK_COVER_PORTRAIT ) );
86 return ImageActor::New( Image::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
91 class LandscapePageFactory : public PageFactory
94 * Query the number of pages available from the factory.
95 * The maximum available page has an ID of GetNumberOfPages()-1.
97 virtual unsigned int GetNumberOfPages()
99 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
102 * Create an image actor to represent a page.
103 * @param[in] pageId The ID of the page to create.
104 * @return An image actor, or an uninitialized pointer if the ID is out of range.
106 virtual Actor NewPage( unsigned int pageId )
108 ImageActor pageFront;
112 pageFront = ImageActor::New( Image::New( BOOK_COVER_LANDSCAPE ) );
113 pageBack = ImageActor::New( Image::New( BOOK_COVER_BACK_LANDSCAPE ) );
117 unsigned int imageId = (pageId-1)*2;
118 pageFront = ImageActor::New( Image::New( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
119 pageBack = ImageActor::New( Image::New( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
121 pageFront.Add(pageBack);
127 * This example shows how to use the page turn UI control to implement the page-turn demo
128 * The effect follows the pan gesture to animate the page
129 * Pan the image inwards, the page will bent,
130 * Depends on the distance of the panning, the image might turn over or slide back
131 * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
132 * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
134 class PageTurnController : public ConnectionTracker
137 PageTurnController( Application &app );
138 ~PageTurnController();
140 //This method gets called once the main loop of application is up and running
141 void OnInit( Application& app );
146 * This method gets called when the screen is rotated, switch between portrait and landscape views
147 * param [in] view The view receiving the orientation change signal
148 * param [in] animation The Orientation Rotating animation
149 * param [in] orientation The current screen orientation
151 void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
154 * Main key event handler
156 void OnKeyEvent(const KeyEvent& event);
159 * Callback function of page turned signal
160 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
161 * @param[in] pageIndex The index of the page turned over
162 * @param[in] isTurningForward The turning direction, forwards or backwards
164 void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
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
172 void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
175 * Callback function of page started pan signal
177 * @param[in] pageTurnView The calling page turn view
179 void OnPageStartedPan( PageTurnView pageTurnView );
182 * Callback function of page finished pan signal
184 * @param[in] pageTurnView The calling page turn view
186 void OnPageFinishedPan( PageTurnView pageTurnView );
190 Application& mApplication;
193 PageTurnView mPageTurnPortraitView;
194 PageTurnView mPageTurnLandscapeView;
195 PortraitPageFactory mPortraitPageFactory;
196 LandscapePageFactory mLandscapePageFactory;
201 PageTurnController::PageTurnController( Application &app )
202 :mApplication( app ),
205 // Connect to the Application's Init signal
206 app.InitSignal().Connect( this, &PageTurnController::OnInit );
209 PageTurnController::~PageTurnController()
214 void PageTurnController::OnInit( Application& app )
216 // The Init signal is received once ( only ) during the Application lifetime
218 Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
220 Stage stage = Stage::GetCurrent();
221 Vector2 stageSize = stage.GetSize();
223 // Create default View.
227 Dali::Window winHandle = app.GetWindow();
228 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
229 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
230 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
231 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
232 app.GetOrientation().ChangedSignal().Connect( &mView, &View::OrientationChanged );
233 // view will response to orientation change to display portrait or landscape views
234 mView.OrientationAnimationStartedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
236 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
237 mPageTurnPortraitView.SetSpineShadowParameter( Vector2(70.f, 30.f) );
238 mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
239 mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
240 mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
241 mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
242 mPageTurnPortraitView.SetPositionInheritanceMode( USE_PARENT_POSITION );
244 mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.y*0.5f, stageSize.x) );
245 mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
246 mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
247 mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
248 mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
249 mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
251 mView.Add(mPageTurnPortraitView);
254 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
256 switch( orientation.GetDegrees() )
258 // portrait view, display page in the right side only
264 mView.Remove( mPageTurnLandscapeView );
265 mView.Add( mPageTurnPortraitView );
266 int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
267 mPageTurnPortraitView.GoToPage( currentPage );
272 // display pages in both sides
278 mView.Remove( mPageTurnPortraitView );
279 mView.Add( mPageTurnLandscapeView );
280 int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
281 mPageTurnLandscapeView.GoToPage( currentPage );
292 * Main key event handler
294 void PageTurnController::OnKeyEvent(const KeyEvent& event)
296 if(event.state == KeyEvent::Down)
298 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
305 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
307 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
308 << " page " << pageIndex
309 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
313 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
315 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
316 << " page " << pageIndex
317 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
321 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
323 std::cout<< "Starting to pan" << std::endl;
326 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
328 std::cout<< "Finished panning" << std::endl;
331 // Entry point for applications
332 int main( int argc, char **argv )
334 Application app = Application::New(&argc, &argv);
335 PageTurnController test ( app );