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>
20 #include <dali-toolkit/devel-api/controls/page-turn-view/page-factory.h>
21 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-landscape-view.h>
22 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-portrait-view.h>
23 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-view.h>
30 #include "shared/view.h"
33 using namespace Dali::Toolkit;
38 // The content amount of one page between portrait and landscape view are different
39 // set a ratio to modify the current page number when the rotation is changed
40 const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
42 const char* BOOK_COVER_PORTRAIT = ( DALI_IMAGE_DIR "book-portrait-cover.jpg" );
43 const char* BOOK_COVER_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover.jpg" );
44 const char* BOOK_COVER_BACK_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover-back.jpg" );
46 const char* PAGE_IMAGES_PORTRAIT[] =
48 DALI_IMAGE_DIR "book-portrait-p1.jpg",
49 DALI_IMAGE_DIR "book-portrait-p2.jpg",
50 DALI_IMAGE_DIR "book-portrait-p3.jpg",
51 DALI_IMAGE_DIR "book-portrait-p4.jpg",
52 DALI_IMAGE_DIR "book-portrait-p5.jpg"
54 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
56 const char* PAGE_IMAGES_LANDSCAPE[] =
58 DALI_IMAGE_DIR "book-landscape-p1.jpg",
59 DALI_IMAGE_DIR "book-landscape-p2.jpg",
60 DALI_IMAGE_DIR "book-landscape-p3.jpg",
61 DALI_IMAGE_DIR "book-landscape-p4.jpg",
62 DALI_IMAGE_DIR "book-landscape-p5.jpg",
63 DALI_IMAGE_DIR "book-landscape-p6.jpg",
64 DALI_IMAGE_DIR "book-landscape-p7.jpg",
65 DALI_IMAGE_DIR "book-landscape-p8.jpg"
67 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
71 class PortraitPageFactory : public PageFactory
74 * Query the number of pages available from the factory.
75 * The maximum available page has an ID of GetNumberOfPages()-1.
77 virtual unsigned int GetNumberOfPages()
79 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
82 * Create an image actor to represent a page.
83 * @param[in] pageId The ID of the page to create.
84 * @return An image actor, or an uninitialized pointer if the ID is out of range.
86 virtual Actor NewPage( unsigned int pageId )
92 page = ImageActor::New( ResourceImage::New( BOOK_COVER_PORTRAIT ) );
96 page = ImageActor::New( ResourceImage::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
103 class LandscapePageFactory : public PageFactory
106 * Query the number of pages available from the factory.
107 * The maximum available page has an ID of GetNumberOfPages()-1.
109 virtual unsigned int GetNumberOfPages()
111 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
114 * Create an image actor to represent a page.
115 * @param[in] pageId The ID of the page to create.
116 * @return An image actor, or an uninitialized pointer if the ID is out of range.
118 virtual Actor NewPage( unsigned int pageId )
120 ImageActor pageFront;
124 pageFront = ImageActor::New( ResourceImage::New( BOOK_COVER_LANDSCAPE ) );
125 pageBack = ImageActor::New( ResourceImage::New( BOOK_COVER_BACK_LANDSCAPE ) );
129 unsigned int imageId = (pageId-1)*2;
130 pageFront = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
131 pageBack = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
133 pageFront.Add(pageBack);
140 * This example shows how to use the page turn UI control to implement the page-turn demo
141 * The effect follows the pan gesture to animate the page
142 * Pan the image inwards, the page will bent,
143 * Depends on the distance of the panning, the image might turn over or slide back
144 * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
145 * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
147 class PageTurnController : public ConnectionTracker
150 PageTurnController( Application &app );
151 ~PageTurnController();
153 //This method gets called once the main loop of application is up and running
154 void OnInit( Application& app );
159 * This method gets called when the screen is rotated, switch between portrait and landscape views
160 * param [in] orientation The current screen orientation
162 void OnOrientationAnimationStarted( 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.
235 mView = Actor::New();
236 mView.SetAnchorPoint( Dali::AnchorPoint::CENTER );
237 mView.SetParentOrigin( Dali::ParentOrigin::CENTER );
238 mView.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
241 Dali::Window winHandle = app.GetWindow();
242 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
243 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
244 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
245 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
247 // view will response to orientation change to display portrait or landscape views
248 //app.GetWindow().GetOrientation().ChangedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
250 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
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.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
260 mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
261 mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
262 mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
263 mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
265 mView.Add(mPageTurnPortraitView);
268 void PageTurnController::OnOrientationAnimationStarted( Orientation orientation )
270 switch( orientation.GetDegrees() )
272 // portrait view, display page in the right side only
278 mView.Remove( mPageTurnLandscapeView );
279 mView.Add( mPageTurnPortraitView );
280 int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
281 mPageTurnPortraitView.GoToPage( currentPage );
286 // display pages in both sides
292 mView.Remove( mPageTurnPortraitView );
293 mView.Add( mPageTurnLandscapeView );
294 int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
295 mPageTurnLandscapeView.GoToPage( currentPage );
306 * Main key event handler
308 void PageTurnController::OnKeyEvent(const KeyEvent& event)
310 if(event.state == KeyEvent::Down)
312 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
319 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
321 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
322 << " page " << pageIndex
323 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
327 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
329 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
330 << " page " << pageIndex
331 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
335 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
337 std::cout<< "Starting to pan" << std::endl;
340 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
342 std::cout<< "Finished panning" << std::endl;
345 // Entry point for applications
346 int main( int argc, char **argv )
348 Application app = Application::New(&argc, &argv, DALI_DEMO_THEME_PATH);
349 PageTurnController test ( app );