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-toolkit/dali-toolkit.h>
19 #include <dali-toolkit/devel-api/controls/page-turn-view/page-factory.h>
20 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-landscape-view.h>
21 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-portrait-view.h>
22 #include <dali-toolkit/devel-api/controls/page-turn-view/page-turn-view.h>
23 #include <dali/dali.h>
26 using namespace Dali::Toolkit;
30 const char* BOOK_COVER_PORTRAIT = (DEMO_IMAGE_DIR "book-portrait-cover.jpg");
31 const char* BOOK_COVER_LANDSCAPE = (DEMO_IMAGE_DIR "book-landscape-cover.jpg");
32 const char* BOOK_COVER_BACK_LANDSCAPE = (DEMO_IMAGE_DIR "book-landscape-cover-back.jpg");
34 const char* PAGE_IMAGES_PORTRAIT[] =
36 DEMO_IMAGE_DIR "book-portrait-p1.jpg",
37 DEMO_IMAGE_DIR "book-portrait-p2.jpg",
38 DEMO_IMAGE_DIR "book-portrait-p3.jpg",
39 DEMO_IMAGE_DIR "book-portrait-p4.jpg",
40 DEMO_IMAGE_DIR "book-portrait-p5.jpg",
41 DEMO_IMAGE_DIR "book-portrait-p6.jpg",
42 DEMO_IMAGE_DIR "book-portrait-p7.jpg",
43 DEMO_IMAGE_DIR "book-portrait-p8.jpg"};
44 const unsigned int NUMBER_OF_PORTRAIT_IMAGE(sizeof(PAGE_IMAGES_PORTRAIT) / sizeof(PAGE_IMAGES_PORTRAIT[0]));
46 const char* PAGE_IMAGES_LANDSCAPE[] =
48 DEMO_IMAGE_DIR "book-landscape-p1.jpg",
49 DEMO_IMAGE_DIR "book-landscape-p2.jpg",
50 DEMO_IMAGE_DIR "book-landscape-p3.jpg",
51 DEMO_IMAGE_DIR "book-landscape-p4.jpg",
52 DEMO_IMAGE_DIR "book-landscape-p5.jpg",
53 DEMO_IMAGE_DIR "book-landscape-p6.jpg",
54 DEMO_IMAGE_DIR "book-landscape-p7.jpg",
55 DEMO_IMAGE_DIR "book-landscape-p8.jpg"};
56 const unsigned int NUMBER_OF_LANDSCAPE_IMAGE(sizeof(PAGE_IMAGES_LANDSCAPE) / sizeof(PAGE_IMAGES_LANDSCAPE[0]));
67 class PortraitPageFactory : public PageFactory
70 * Query the number of pages available from the factory.
71 * The maximum available page has an ID of GetNumberOfPages()-1.
73 virtual unsigned int GetNumberOfPages()
75 return 5 * NUMBER_OF_PORTRAIT_IMAGE + 1;
79 * Create texture to represent a page.
80 * @param[in] pageId The ID of the page to create.
81 * @return The texture.
83 virtual Texture NewPage(unsigned int pageId)
90 pixels = SyncImageLoader::Load(BOOK_COVER_PORTRAIT);
94 pixels = SyncImageLoader::Load(PAGE_IMAGES_PORTRAIT[(pageId - 1) % NUMBER_OF_PORTRAIT_IMAGE]);
99 texture = Texture::New(TextureType::TEXTURE_2D, pixels.GetPixelFormat(), pixels.GetWidth(), pixels.GetHeight());
100 texture.Upload(pixels, 0, 0, 0, 0, pixels.GetWidth(), pixels.GetHeight());
107 class LandscapePageFactory : public PageFactory
110 * Query the number of pages available from the factory.
111 * The maximum available page has an ID of GetNumberOfPages()-1.
113 virtual unsigned int GetNumberOfPages()
115 return 5 * NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
119 * Create texture to represent a page.
120 * @param[in] pageId The ID of the page to create.
121 * @return The texture.
123 virtual Texture NewPage(unsigned int pageId)
127 PixelData pixelsFront;
128 PixelData pixelsBack;
132 pixelsFront = SyncImageLoader::Load(BOOK_COVER_LANDSCAPE);
133 pixelsBack = SyncImageLoader::Load(BOOK_COVER_BACK_LANDSCAPE);
137 unsigned int imageId = (pageId - 1) * 2;
138 pixelsFront = SyncImageLoader::Load(PAGE_IMAGES_LANDSCAPE[imageId % NUMBER_OF_LANDSCAPE_IMAGE]);
139 pixelsBack = SyncImageLoader::Load(PAGE_IMAGES_LANDSCAPE[(imageId + 1) % NUMBER_OF_LANDSCAPE_IMAGE]);
142 if(pixelsFront && pixelsBack)
144 texture = Texture::New(TextureType::TEXTURE_2D, pixelsFront.GetPixelFormat(), pixelsFront.GetWidth() * 2, pixelsFront.GetHeight());
145 texture.Upload(pixelsFront, 0, 0, 0, 0, pixelsFront.GetWidth(), pixelsFront.GetHeight());
146 texture.Upload(pixelsBack, 0, 0, pixelsFront.GetWidth(), 0, pixelsBack.GetWidth(), pixelsBack.GetHeight());
154 * This example shows how to use the PageTurnView UI control
156 class PageTurnExample : public ConnectionTracker
159 PageTurnExample(Application& app);
163 void OnInit(Application& app);
166 void OnWindowResized(Window window, Window::WindowSize size);
168 void Rotate(DemoOrientation orientation);
170 void OnKeyEvent(const KeyEvent& event);
173 Application& mApplication;
175 PageTurnView mPageTurnPortraitView;
176 PageTurnView mPageTurnLandscapeView;
177 PortraitPageFactory mPortraitPageFactory;
178 LandscapePageFactory mLandscapePageFactory;
180 DemoOrientation mOrientation;
183 PageTurnExample::PageTurnExample(Application& app)
185 mOrientation(UNKNOWN)
187 app.InitSignal().Connect(this, &PageTurnExample::OnInit);
190 PageTurnExample::~PageTurnExample()
195 * The Init signal is received once (only) during the Application lifetime
197 void PageTurnExample::OnInit(Application& app)
199 Window window = app.GetWindow();
200 window.KeyEventSignal().Connect(this, &PageTurnExample::OnKeyEvent);
202 window.AddAvailableOrientation(WindowOrientation::PORTRAIT);
203 window.AddAvailableOrientation(WindowOrientation::LANDSCAPE);
204 window.AddAvailableOrientation(WindowOrientation::PORTRAIT_INVERSE);
205 window.AddAvailableOrientation(WindowOrientation::LANDSCAPE_INVERSE);
206 window.ResizeSignal().Connect(this, &PageTurnExample::OnWindowResized);
208 Window::WindowSize size = window.GetSize();
209 Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
212 void PageTurnExample::OnWindowResized(Window window, Window::WindowSize size)
214 Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
217 void PageTurnExample::Rotate(DemoOrientation orientation)
219 Window window = mApplication.GetWindow();
220 Vector2 windowSize = window.GetSize();
222 if(mOrientation != orientation)
224 mOrientation = orientation;
226 if(PORTRAIT == orientation)
228 if(!mPageTurnPortraitView)
230 mPageTurnPortraitView = PageTurnPortraitView::New(mPortraitPageFactory, windowSize);
231 mPageTurnPortraitView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
234 if(mPageTurnLandscapeView)
236 window.Remove(mPageTurnLandscapeView);
238 window.Add(mPageTurnPortraitView);
240 else if(LANDSCAPE == orientation)
242 if(!mPageTurnLandscapeView)
244 mPageTurnLandscapeView = PageTurnLandscapeView::New(mLandscapePageFactory, Vector2(windowSize.x * 0.5f, windowSize.y));
245 mPageTurnLandscapeView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
248 if(mPageTurnPortraitView)
250 window.Remove(mPageTurnPortraitView);
253 window.Add(mPageTurnLandscapeView);
259 * Main key event handler
261 void PageTurnExample::OnKeyEvent(const KeyEvent& event)
263 if(event.GetState() == KeyEvent::DOWN)
265 if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
272 // Entry point for applications
273 int DALI_EXPORT_API main(int argc, char** argv)
275 Application app = Application::New(&argc, &argv);
276 PageTurnExample test(app);