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 ] ) );
94 page.SetRelayoutEnabled( false );
100 class LandscapePageFactory : public PageFactory
103 * Query the number of pages available from the factory.
104 * The maximum available page has an ID of GetNumberOfPages()-1.
106 virtual unsigned int GetNumberOfPages()
108 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
111 * Create an image actor to represent a page.
112 * @param[in] pageId The ID of the page to create.
113 * @return An image actor, or an uninitialized pointer if the ID is out of range.
115 virtual Actor NewPage( unsigned int pageId )
117 ImageActor pageFront;
121 pageFront = ImageActor::New( ResourceImage::New( BOOK_COVER_LANDSCAPE ) );
122 pageBack = ImageActor::New( ResourceImage::New( BOOK_COVER_BACK_LANDSCAPE ) );
126 unsigned int imageId = (pageId-1)*2;
127 pageFront = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
128 pageBack = ImageActor::New( ResourceImage::New( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
130 pageFront.Add(pageBack);
132 pageFront.SetRelayoutEnabled( false );
133 pageBack.SetRelayoutEnabled( false );
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] view The view receiving the orientation change signal
161 * param [in] animation The Orientation Rotating animation
162 * param [in] orientation The current screen orientation
164 void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
167 * Main key event handler
169 void OnKeyEvent(const KeyEvent& event);
172 * Callback function of page turned signal
173 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
174 * @param[in] pageIndex The index of the page turned over
175 * @param[in] isTurningForward The turning direction, forwards or backwards
177 void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
180 * Callback function of page turned signal
181 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
182 * @param[in] pageIndex The index of the page turned over
183 * @param[in] isTurningForward The turning direction, forwards or backwards
185 void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
188 * Callback function of page started pan signal
190 * @param[in] pageTurnView The calling page turn view
192 void OnPageStartedPan( PageTurnView pageTurnView );
195 * Callback function of page finished pan signal
197 * @param[in] pageTurnView The calling page turn view
199 void OnPageFinishedPan( PageTurnView pageTurnView );
203 Application& mApplication;
206 PageTurnView mPageTurnPortraitView;
207 PageTurnView mPageTurnLandscapeView;
208 PortraitPageFactory mPortraitPageFactory;
209 LandscapePageFactory mLandscapePageFactory;
214 PageTurnController::PageTurnController( Application &app )
215 :mApplication( app ),
218 // Connect to the Application's Init signal
219 app.InitSignal().Connect( this, &PageTurnController::OnInit );
222 PageTurnController::~PageTurnController()
227 void PageTurnController::OnInit( Application& app )
229 // The Init signal is received once ( only ) during the Application lifetime
231 DemoHelper::RequestThemeChange();
233 Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
235 Stage stage = Stage::GetCurrent();
236 Vector2 stageSize = stage.GetSize();
238 // Create default View.
240 mView.SetRelayoutEnabled( false );
243 Dali::Window winHandle = app.GetWindow();
244 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
245 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
246 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
247 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
249 //app.GetOrientation().ChangedSignal().Connect( &mView, &View::OrientationChanged );
250 // view will response to orientation change to display portrait or landscape views
251 mView.OrientationAnimationStartedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
253 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
254 mPageTurnPortraitView.SetRelayoutEnabled( false );
255 mPageTurnPortraitView.SetSpineShadowParameter( Vector2(70.f, 30.f) );
256 mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
257 mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
258 mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
259 mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
260 mPageTurnPortraitView.SetPositionInheritanceMode( USE_PARENT_POSITION );
262 mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.y*0.5f, stageSize.x) );
263 mPageTurnLandscapeView.SetRelayoutEnabled( false );
264 mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
265 mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
266 mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
267 mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
268 mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
270 mView.Add(mPageTurnPortraitView);
273 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
275 switch( orientation.GetDegrees() )
277 // portrait view, display page in the right side only
283 mView.Remove( mPageTurnLandscapeView );
284 mView.Add( mPageTurnPortraitView );
285 int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
286 mPageTurnPortraitView.GoToPage( currentPage );
291 // display pages in both sides
297 mView.Remove( mPageTurnPortraitView );
298 mView.Add( mPageTurnLandscapeView );
299 int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
300 mPageTurnLandscapeView.GoToPage( currentPage );
311 * Main key event handler
313 void PageTurnController::OnKeyEvent(const KeyEvent& event)
315 if(event.state == KeyEvent::Down)
317 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
324 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
326 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
327 << " page " << pageIndex
328 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
332 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
334 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
335 << " page " << pageIndex
336 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
340 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
342 std::cout<< "Starting to pan" << std::endl;
345 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
347 std::cout<< "Finished panning" << std::endl;
350 // Entry point for applications
351 int main( int argc, char **argv )
353 Application app = Application::New(&argc, &argv);
354 PageTurnController test ( app );