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>
25 #include "shared/view.h"
28 using namespace Dali::Toolkit;
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);
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" );
41 const char* PAGE_IMAGES_PORTRAIT[] =
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"
49 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
51 const char* PAGE_IMAGES_LANDSCAPE[] =
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"
62 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
66 class PortraitPageFactory : public PageFactory
69 * Query the number of pages available from the factory.
70 * The maximum available page has an ID of GetNumberOfPages()-1.
72 virtual unsigned int GetNumberOfPages()
74 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
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.
81 virtual Actor NewPage( unsigned int pageId )
87 page = ImageActor::New( ResourceImage::New( BOOK_COVER_PORTRAIT ) );
91 page = ImageActor::New( ResourceImage::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
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);
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
142 class PageTurnController : public ConnectionTracker
145 PageTurnController( Application &app );
146 ~PageTurnController();
148 //This method gets called once the main loop of application is up and running
149 void OnInit( Application& app );
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
159 void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
162 * Main key event handler
164 void OnKeyEvent(const KeyEvent& event);
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 OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
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
180 void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
183 * Callback function of page started pan signal
185 * @param[in] pageTurnView The calling page turn view
187 void OnPageStartedPan( PageTurnView pageTurnView );
190 * Callback function of page finished pan signal
192 * @param[in] pageTurnView The calling page turn view
194 void OnPageFinishedPan( PageTurnView pageTurnView );
198 Application& mApplication;
201 PageTurnView mPageTurnPortraitView;
202 PageTurnView mPageTurnLandscapeView;
203 PortraitPageFactory mPortraitPageFactory;
204 LandscapePageFactory mLandscapePageFactory;
209 PageTurnController::PageTurnController( Application &app )
210 :mApplication( app ),
213 // Connect to the Application's Init signal
214 app.InitSignal().Connect( this, &PageTurnController::OnInit );
217 PageTurnController::~PageTurnController()
222 void PageTurnController::OnInit( Application& app )
224 // The Init signal is received once ( only ) during the Application lifetime
226 DemoHelper::RequestThemeChange();
228 Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
230 Stage stage = Stage::GetCurrent();
231 Vector2 stageSize = stage.GetSize();
233 // Create default View.
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 );
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 );
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 );
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 );
262 mView.Add(mPageTurnPortraitView);
265 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
267 switch( orientation.GetDegrees() )
269 // portrait view, display page in the right side only
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 );
283 // display pages in both sides
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 );
303 * Main key event handler
305 void PageTurnController::OnKeyEvent(const KeyEvent& event)
307 if(event.state == KeyEvent::Down)
309 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
316 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
318 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
319 << " page " << pageIndex
320 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
324 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
326 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
327 << " page " << pageIndex
328 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
332 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
334 std::cout<< "Starting to pan" << std::endl;
337 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
339 std::cout<< "Finished panning" << std::endl;
342 // Entry point for applications
343 int main( int argc, char **argv )
345 Application app = Application::New(&argc, &argv);
346 PageTurnController test ( app );