2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
20 #include <dali/dali.h>
21 #include <dali-toolkit/dali-toolkit.h>
24 using namespace Dali::Toolkit;
29 // The content amount of one page between portrait and landscape view are different
30 // set a ratio to modify the current page number when the rotation is changed
31 const float PAGE_NUMBER_CORRESPONDING_RATIO(1.25f);
33 const char* BOOK_COVER_PORTRAIT = ( DALI_IMAGE_DIR "book-portrait-cover.jpg" );
34 const char* BOOK_COVER_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover.jpg" );
35 const char* BOOK_COVER_BACK_LANDSCAPE = ( DALI_IMAGE_DIR "book-landscape-cover-back.jpg" );
37 const char* PAGE_IMAGES_PORTRAIT[] =
39 DALI_IMAGE_DIR "book-portrait-p1.jpg",
40 DALI_IMAGE_DIR "book-portrait-p2.jpg",
41 DALI_IMAGE_DIR "book-portrait-p3.jpg",
42 DALI_IMAGE_DIR "book-portrait-p4.jpg",
43 DALI_IMAGE_DIR "book-portrait-p5.jpg"
45 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
47 const char* PAGE_IMAGES_LANDSCAPE[] =
49 DALI_IMAGE_DIR "book-landscape-p1.jpg",
50 DALI_IMAGE_DIR "book-landscape-p2.jpg",
51 DALI_IMAGE_DIR "book-landscape-p3.jpg",
52 DALI_IMAGE_DIR "book-landscape-p4.jpg",
53 DALI_IMAGE_DIR "book-landscape-p5.jpg",
54 DALI_IMAGE_DIR "book-landscape-p6.jpg",
55 DALI_IMAGE_DIR "book-landscape-p7.jpg",
56 DALI_IMAGE_DIR "book-landscape-p8.jpg"
58 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
62 class PortraitPageFactory : public PageFactory
65 * Query the number of pages available from the factory.
66 * The maximum available page has an ID of GetNumberOfPages()-1.
68 virtual unsigned int GetNumberOfPages()
70 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
73 * Create an image actor to represent a page.
74 * @param[in] pageId The ID of the page to create.
75 * @return An image actor, or an uninitialized pointer if the ID is out of range.
77 virtual Actor NewPage( unsigned int pageId )
81 return ImageActor::New( Image::New( BOOK_COVER_PORTRAIT ) );
85 return ImageActor::New( Image::New( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] ) );
90 class LandscapePageFactory : public PageFactory
93 * Query the number of pages available from the factory.
94 * The maximum available page has an ID of GetNumberOfPages()-1.
96 virtual unsigned int GetNumberOfPages()
98 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
101 * Create an image actor to represent a page.
102 * @param[in] pageId The ID of the page to create.
103 * @return An image actor, or an uninitialized pointer if the ID is out of range.
105 virtual Actor NewPage( unsigned int pageId )
107 ImageActor pageFront;
111 pageFront = ImageActor::New( Image::New( BOOK_COVER_LANDSCAPE ) );
112 pageBack = ImageActor::New( Image::New( BOOK_COVER_BACK_LANDSCAPE ) );
116 unsigned int imageId = (pageId-1)*2;
117 pageFront = ImageActor::New( Image::New( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
118 pageBack = ImageActor::New( Image::New( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] ) );
120 pageFront.Add(pageBack);
126 * This example shows how to use the page turn UI control to implement the page-turn demo
127 * The effect follows the pan gesture to animate the page
128 * Pan the image inwards, the page will bent,
129 * Depends on the distance of the panning, the image might turn over or slide back
130 * Also, in portrait view, the pan gesture outwards from position near the spine could turn the previous page back
131 * Allows to turn multiple pages one by one quickly towards the same direction, multiple animations are launched in this case
133 class PageTurnController : public ConnectionTracker
136 PageTurnController( Application &app );
137 ~PageTurnController();
139 //This method gets called once the main loop of application is up and running
140 void OnInit( Application& app );
145 * This method gets called when the screen is rotated, switch between portrait and landscape views
146 * param [in] view The view receiving the orientation change signal
147 * param [in] animation The Orientation Rotating animation
148 * param [in] orientation The current screen orientation
150 void OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation );
153 * Main key event handler
155 void OnKeyEvent(const KeyEvent& event);
158 * Callback function of page turned signal
159 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
160 * @param[in] pageIndex The index of the page turned over
161 * @param[in] isTurningForward The turning direction, forwards or backwards
163 void OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
166 * Callback function of page turned signal
167 * @param[in] pageTurnView The handle of the PageTurnPortraitView or PageTurnLandscapeView
168 * @param[in] pageIndex The index of the page turned over
169 * @param[in] isTurningForward The turning direction, forwards or backwards
171 void OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward );
174 * Callback function of page started pan signal
176 * @param[in] pageTurnView The calling page turn view
178 void OnPageStartedPan( PageTurnView pageTurnView );
181 * Callback function of page finished pan signal
183 * @param[in] pageTurnView The calling page turn view
185 void OnPageFinishedPan( PageTurnView pageTurnView );
189 Application& mApplication;
192 PageTurnView mPageTurnPortraitView;
193 PageTurnView mPageTurnLandscapeView;
194 PortraitPageFactory mPortraitPageFactory;
195 LandscapePageFactory mLandscapePageFactory;
200 PageTurnController::PageTurnController( Application &app )
201 :mApplication( app ),
204 // Connect to the Application's Init signal
205 app.InitSignal().Connect( this, &PageTurnController::OnInit );
208 PageTurnController::~PageTurnController()
213 void PageTurnController::OnInit( Application& app )
215 // The Init signal is received once ( only ) during the Application lifetime
217 Stage::GetCurrent().KeyEventSignal().Connect(this, &PageTurnController::OnKeyEvent);
219 Stage stage = Stage::GetCurrent();
220 Vector2 stageSize = stage.GetSize();
222 // Create default View.
226 Dali::Window winHandle = app.GetWindow();
227 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT );
228 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE );
229 winHandle.AddAvailableOrientation( Dali::Window::PORTRAIT_INVERSE );
230 winHandle.AddAvailableOrientation( Dali::Window::LANDSCAPE_INVERSE );
231 app.GetOrientation().ChangedSignal().Connect( &mView, &View::OrientationChanged );
232 // view will response to orientation change to display portrait or landscape views
233 mView.OrientationAnimationStartedSignal().Connect( this, &PageTurnController::OnOrientationAnimationStarted );
235 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, stageSize );
236 mPageTurnPortraitView.SetSpineShadowParameter( Vector2(70.f, 30.f) );
237 mPageTurnPortraitView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
238 mPageTurnPortraitView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
239 mPageTurnPortraitView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
240 mPageTurnPortraitView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
241 mPageTurnPortraitView.SetPositionInheritanceMode( USE_PARENT_POSITION );
243 mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(stageSize.y*0.5f, stageSize.x) );
244 mPageTurnLandscapeView.PageTurnStartedSignal().Connect( this, &PageTurnController::OnPageStartedTurn );
245 mPageTurnLandscapeView.PageTurnFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedTurn );
246 mPageTurnLandscapeView.PagePanStartedSignal().Connect( this, &PageTurnController::OnPageStartedPan );
247 mPageTurnLandscapeView.PagePanFinishedSignal().Connect( this, &PageTurnController::OnPageFinishedPan );
248 mPageTurnLandscapeView.SetPositionInheritanceMode( USE_PARENT_POSITION );
250 mView.Add(mPageTurnPortraitView);
253 void PageTurnController::OnOrientationAnimationStarted( View view, Animation& animation, const Orientation& orientation )
255 switch( orientation.GetDegrees() )
257 // portrait view, display page in the right side only
263 mView.Remove( mPageTurnLandscapeView );
264 mView.Add( mPageTurnPortraitView );
265 int currentPage = floor( static_cast<float>(mPageTurnLandscapeView.GetCurrentPage()) * PAGE_NUMBER_CORRESPONDING_RATIO );
266 mPageTurnPortraitView.GoToPage( currentPage );
271 // display pages in both sides
277 mView.Remove( mPageTurnPortraitView );
278 mView.Add( mPageTurnLandscapeView );
279 int currentPage = ceil( static_cast<float>(mPageTurnPortraitView.GetCurrentPage()) / PAGE_NUMBER_CORRESPONDING_RATIO );
280 mPageTurnLandscapeView.GoToPage( currentPage );
291 * Main key event handler
293 void PageTurnController::OnKeyEvent(const KeyEvent& event)
295 if(event.state == KeyEvent::Down)
297 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
304 void PageTurnController::OnPageStartedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
306 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
307 << " page " << pageIndex
308 << ( isTurningForward ? " is starting to turn forward" : " is starting to turn backward" )
312 void PageTurnController::OnPageFinishedTurn( PageTurnView pageTurnView, unsigned int pageIndex, bool isTurningForward )
314 std::cout<< ( ( pageTurnView == mPageTurnPortraitView ) ? " portrait: " : " Landscape: " )
315 << " page " << pageIndex
316 << ( isTurningForward ? " has finished turning forward" : " has finished turning backward" )
320 void PageTurnController::OnPageStartedPan( PageTurnView pageTurnView )
322 std::cout<< "Starting to pan" << std::endl;
325 void PageTurnController::OnPageFinishedPan( PageTurnView pageTurnView )
327 std::cout<< "Finished panning" << std::endl;
330 // Entry point for applications
331 int main( int argc, char **argv )
333 Application app = Application::New(&argc, &argv);
334 PageTurnController test ( app );