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.
18 #include <dali/dali.h>
19 #include <dali-toolkit/dali-toolkit.h>
26 using namespace Dali::Toolkit;
31 // The content amount of one page between portrait and landscape view are different
32 // set a ratio to modify the current page number when the rotation is changed
33 const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
35 const char* BOOK_COVER_PORTRAIT = ( DALI_IMAGE_DIR "book-portrait-cover.jpg" );
36 const char* BOOK_COVER_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover.jpg" );
37 const char* BOOK_COVER_BACK_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover-back.jpg" );
39 const char* PAGE_IMAGES_PORTRAIT[] =
41 DALI_IMAGE_DIR "book-portrait-p1.jpg",
42 DALI_IMAGE_DIR "book-portrait-p2.jpg",
43 DALI_IMAGE_DIR "book-portrait-p3.jpg",
44 DALI_IMAGE_DIR "book-portrait-p4.jpg",
45 DALI_IMAGE_DIR "book-portrait-p5.jpg"
47 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
49 const char* PAGE_IMAGES_LANDSCAPE[] =
51 DALI_IMAGE_DIR "book-landscape-p1.jpg",
52 DALI_IMAGE_DIR "book-landscape-p2.jpg",
53 DALI_IMAGE_DIR "book-landscape-p3.jpg",
54 DALI_IMAGE_DIR "book-landscape-p4.jpg",
55 DALI_IMAGE_DIR "book-landscape-p5.jpg",
56 DALI_IMAGE_DIR "book-landscape-p6.jpg",
57 DALI_IMAGE_DIR "book-landscape-p7.jpg",
58 DALI_IMAGE_DIR "book-landscape-p8.jpg"
60 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
64 class PortraitPageFactory : public PageFactory
67 * Query the number of pages available from the factory.
68 * The maximum available page has an ID of GetNumberOfPages()-1.
70 virtual unsigned int GetNumberOfPages()
72 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
75 * Create an image actor to represent a page.
76 * @param[in] pageId The ID of the page to create.
77 * @return An image actor, or an uninitialized pointer if the ID is out of range.
79 virtual Actor NewPage( unsigned int pageId )
85 page = ImageActor::New( ResourceImage::New( BOOK_COVER_PORTRAIT ) );
89 page = ImageActor::New( ResourceImage::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
92 page.SetRelayoutEnabled( false );
98 class LandscapePageFactory : public PageFactory
101 * Query the number of pages available from the factory.
102 * The maximum available page has an ID of GetNumberOfPages()-1.
104 virtual unsigned int GetNumberOfPages()
106 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
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.
113 virtual Actor NewPage( unsigned int pageId )
115 ImageActor pageFront;
119 pageFront = ImageActor::New( ResourceImage::New( BOOK_COVER_LANDSCAPE ) );
120 pageBack = ImageActor::New( ResourceImage::New( BOOK_COVER_BACK_LANDSCAPE ) );
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 ] ) );
128 pageFront.Add(pageBack);
130 pageFront.SetRelayoutEnabled( false );
131 pageBack.SetRelayoutEnabled( false );
138 * This example shows how to use the page turn UI control to implement the page-turn demo
139 * The effect follows the pan gesture to animate the page
140 * Pan the image inwards, the page will bent,
141 * Depends on the distance of the panning, the image might turn over or slide back
142 * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
143 * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
145 class PageTurnController : public ConnectionTracker
148 PageTurnController( Application &app );
149 ~PageTurnController();
151 //This method gets called once the main loop of application is up and running
152 void OnInit( Application& app );
157 * This method gets called when the screen is rotated, switch between portrait and landscape views
158 * param [in] view The view receiving the orientation change signal
159 * param [in] animation The Orientation Rotating animation
160 * param [in] orientation The current screen orientation
162 void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
165 * Main key event handler
167 void OnKeyEvent(const KeyEvent& event);
170 * Callback function of page turned signal
171 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
172 * @param[in] pageIndex The index of the page turned over
173 * @param[in] isTurningForward The turning direction, forwards or backwards
175 void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
178 * Callback function of page turned signal
179 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
180 * @param[in] pageIndex The index of the page turned over
181 * @param[in] isTurningForward The turning direction, forwards or backwards
183 void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
186 * Callback function of page started pan signal
188 * @param[in] pageTurnView The calling page turn view
190 void OnPageStartedPan( PageTurnView pageTurnView );
193 * Callback function of page finished pan signal
195 * @param[in] pageTurnView The calling page turn view
197 void OnPageFinishedPan( PageTurnView pageTurnView );
201 Application& mApplication;
204 PageTurnView mPageTurnPortraitView;
205 PageTurnView mPageTurnLandscapeView;
206 PortraitPageFactory mPortraitPageFactory;
207 LandscapePageFactory mLandscapePageFactory;
212 PageTurnController::PageTurnController( Application &app )
213 :mApplication( app ),
216 // Connect to the Application's Init signal
217 app.InitSignal().Connect( this, &PageTurnController::OnInit );
220 PageTurnController::~PageTurnController()
225 void PageTurnController::OnInit( Application& app )
227 // The Init signal is received once ( only ) during the Application lifetime
229 Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
231 Stage stage = Stage::GetCurrent();
232 Vector2 stageSize = stage.GetSize();
234 // Create default View.
236 mView.SetRelayoutEnabled( false );
239 Dali::Window winHandle = app.GetWindow();
240 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
241 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
242 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
243 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
245 //app.GetOrientation().ChangedSignal().Connect( &mView, &View::OrientationChanged );
246 // view will response to orientation change to display portrait or landscape views
247 mView.OrientationAnimationStartedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
249 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
250 mPageTurnPortraitView.SetRelayoutEnabled( false );
251 mPageTurnPortraitView.SetSpineShadowParameter( Vector2(70.f, 30.f) );
252 mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
253 mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
254 mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
255 mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
256 mPageTurnPortraitView.SetPositionInheritanceMode( USE_PARENT_POSITION );
258 mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.y*0.5f, stageSize.x) );
259 mPageTurnLandscapeView.SetRelayoutEnabled( false );
260 mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
261 mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
262 mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
263 mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
264 mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
266 mView.Add(mPageTurnPortraitView);
269 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
271 switch( orientation.GetDegrees() )
273 // portrait view, display page in the right side only
279 mView.Remove( mPageTurnLandscapeView );
280 mView.Add( mPageTurnPortraitView );
281 int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
282 mPageTurnPortraitView.GoToPage( currentPage );
287 // display pages in both sides
293 mView.Remove( mPageTurnPortraitView );
294 mView.Add( mPageTurnLandscapeView );
295 int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
296 mPageTurnLandscapeView.GoToPage( currentPage );
307 * Main key event handler
309 void PageTurnController::OnKeyEvent(const KeyEvent& event)
311 if(event.state == KeyEvent::Down)
313 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
320 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
322 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
323 << " page " << pageIndex
324 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
328 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
330 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
331 << " page " << pageIndex
332 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
336 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
338 std::cout<< "Starting to pan" << std::endl;
341 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
343 std::cout<< "Finished panning" << std::endl;
346 // Entry point for applications
347 int main( int argc, char **argv )
349 Application app = Application::New(&argc, &argv);
350 PageTurnController test ( app );