2 * Copyright (c) 2020 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>
26 using namespace Dali::Toolkit;
31 const char* BOOK_COVER_PORTRAIT = ( DEMO_IMAGE_DIR "book-portrait-cover.jpg" );
32 const char* BOOK_COVER_LANDSCAPE = ( DEMO_IMAGE_DIR "book-landscape-cover.jpg" );
33 const char* BOOK_COVER_BACK_LANDSCAPE = ( DEMO_IMAGE_DIR "book-landscape-cover-back.jpg" );
35 const char* PAGE_IMAGES_PORTRAIT[] =
37 DEMO_IMAGE_DIR "book-portrait-p1.jpg",
38 DEMO_IMAGE_DIR "book-portrait-p2.jpg",
39 DEMO_IMAGE_DIR "book-portrait-p3.jpg",
40 DEMO_IMAGE_DIR "book-portrait-p4.jpg",
41 DEMO_IMAGE_DIR "book-portrait-p5.jpg",
42 DEMO_IMAGE_DIR "book-portrait-p6.jpg",
43 DEMO_IMAGE_DIR "book-portrait-p7.jpg",
44 DEMO_IMAGE_DIR "book-portrait-p8.jpg"
46 const unsigned int NUMBER_OF_PORTRAIT_IMAGE( sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]) );
48 const char* PAGE_IMAGES_LANDSCAPE[] =
50 DEMO_IMAGE_DIR "book-landscape-p1.jpg",
51 DEMO_IMAGE_DIR "book-landscape-p2.jpg",
52 DEMO_IMAGE_DIR "book-landscape-p3.jpg",
53 DEMO_IMAGE_DIR "book-landscape-p4.jpg",
54 DEMO_IMAGE_DIR "book-landscape-p5.jpg",
55 DEMO_IMAGE_DIR "book-landscape-p6.jpg",
56 DEMO_IMAGE_DIR "book-landscape-p7.jpg",
57 DEMO_IMAGE_DIR "book-landscape-p8.jpg"
59 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE( sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]) );
70 class PortraitPageFactory : public PageFactory
73 * Query the number of pages available from the factory.
74 * The maximum available page has an ID of GetNumberOfPages()-1.
76 virtual unsigned int GetNumberOfPages()
78 return 5*NUMBER_OF_PORTRAIT_IMAGE + 1;
82 * Create texture to represent a page.
83 * @param[in] pageId The ID of the page to create.
84 * @return The texture.
86 virtual Texture NewPage( unsigned int pageId )
93 pixels = SyncImageLoader::Load( BOOK_COVER_PORTRAIT );
97 pixels = SyncImageLoader::Load( PAGE_IMAGES_PORTRAIT[ (pageId-1) % NUMBER_OF_PORTRAIT_IMAGE ] );
102 texture = Texture::New( TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight() );
103 texture.Upload( pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight() );
110 class LandscapePageFactory : public PageFactory
113 * Query the number of pages available from the factory.
114 * The maximum available page has an ID of GetNumberOfPages()-1.
116 virtual unsigned int GetNumberOfPages()
118 return 5*NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
122 * Create texture to represent a page.
123 * @param[in] pageId The ID of the page to create.
124 * @return The texture.
126 virtual Texture NewPage( unsigned int pageId )
130 PixelData pixelsFront;
131 PixelData pixelsBack;
135 pixelsFront = SyncImageLoader::Load( BOOK_COVER_LANDSCAPE );
136 pixelsBack = SyncImageLoader::Load( BOOK_COVER_BACK_LANDSCAPE );
140 unsigned int imageId = (pageId-1)*2;
141 pixelsFront = SyncImageLoader::Load( PAGE_IMAGES_LANDSCAPE[ imageId % NUMBER_OF_LANDSCAPE_IMAGE ] );
142 pixelsBack = SyncImageLoader::Load( PAGE_IMAGES_LANDSCAPE[ (imageId+1) % NUMBER_OF_LANDSCAPE_IMAGE ] );
145 if( pixelsFront && pixelsBack )
147 texture = Texture::New( TextureType::TEXTURE_2D, pixelsFront.GetPixelFormat(), pixelsFront.GetWidth()*2, pixelsFront.GetHeight() );
148 texture.Upload( pixelsFront, 0, 0, 0, 0, pixelsFront.GetWidth(), pixelsFront.GetHeight() );
149 texture.Upload( pixelsBack, 0, 0, pixelsFront.GetWidth(), 0, pixelsBack.GetWidth(), pixelsBack.GetHeight() );
157 * This example shows how to use the PageTurnView UI control
159 class PageTurnExample : public ConnectionTracker
163 PageTurnExample( Application &app );
167 void OnInit( Application& app );
172 void OnWindowResized( Window window, Window::WindowSize size );
174 void Rotate( DemoOrientation orientation );
176 void OnKeyEvent(const KeyEvent& event);
180 Application& mApplication;
182 PageTurnView mPageTurnPortraitView;
183 PageTurnView mPageTurnLandscapeView;
184 PortraitPageFactory mPortraitPageFactory;
185 LandscapePageFactory mLandscapePageFactory;
187 DemoOrientation mOrientation;
190 PageTurnExample::PageTurnExample( Application &app )
191 : mApplication( app ),
192 mOrientation( UNKNOWN )
194 app.InitSignal().Connect( this, &PageTurnExample::OnInit );
197 PageTurnExample::~PageTurnExample()
202 * The Init signal is received once (only) during the Application lifetime
204 void PageTurnExample::OnInit( Application& app )
206 Window window = app.GetWindow();
207 window.KeyEventSignal().Connect(this, &PageTurnExample::OnKeyEvent);
209 window.AddAvailableOrientation( Window::PORTRAIT );
210 window.AddAvailableOrientation( Window::LANDSCAPE );
211 window.AddAvailableOrientation( Window::PORTRAIT_INVERSE );
212 window.AddAvailableOrientation( Window::LANDSCAPE_INVERSE );
213 window.ResizeSignal().Connect( this, &PageTurnExample::OnWindowResized );
215 Window::WindowSize size = window.GetSize();
216 Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
219 void PageTurnExample::OnWindowResized( Window window, Window::WindowSize size )
221 Rotate( size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT );
224 void PageTurnExample::Rotate( DemoOrientation orientation )
226 Window window = mApplication.GetWindow();
227 Vector2 windowSize = window.GetSize();
229 if( mOrientation != orientation )
231 mOrientation = orientation;
233 if( PORTRAIT == orientation )
235 if( !mPageTurnPortraitView )
237 mPageTurnPortraitView = PageTurnPortraitView::New( mPortraitPageFactory, windowSize );
238 mPageTurnPortraitView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
241 if( mPageTurnLandscapeView )
243 window.Remove( mPageTurnLandscapeView );
245 window.Add( mPageTurnPortraitView );
247 else if( LANDSCAPE == orientation )
249 if( !mPageTurnLandscapeView )
251 mPageTurnLandscapeView = PageTurnLandscapeView::New( mLandscapePageFactory, Vector2(windowSize.x*0.5f, windowSize.y) );
252 mPageTurnLandscapeView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
255 if( mPageTurnPortraitView )
257 window.Remove( mPageTurnPortraitView );
260 window.Add( mPageTurnLandscapeView );
266 * Main key event handler
268 void PageTurnExample::OnKeyEvent(const KeyEvent& event)
270 if(event.GetState() == KeyEvent::Down)
272 if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
279 // Entry point for applications
280 int DALI_EXPORT_API main( int argc, char **argv )
282 Application app = Application::New(&argc, &argv);
283 PageTurnExample test ( app );