Updated demos to use DALi clang-format
[platform/core/uifw/dali-demo.git] / examples / page-turn-view / page-turn-view-example.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
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>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
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");
33
34 const char* PAGE_IMAGES_PORTRAIT[] =
35   {
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]));
45
46 const char* PAGE_IMAGES_LANDSCAPE[] =
47   {
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]));
57
58 enum DemoOrientation
59 {
60   PORTRAIT,
61   LANDSCAPE,
62   UNKNOWN
63 };
64
65 } // namespace
66
67 class PortraitPageFactory : public PageFactory
68 {
69   /**
70    * Query the number of pages available from the factory.
71    * The maximum available page has an ID of GetNumberOfPages()-1.
72    */
73   virtual unsigned int GetNumberOfPages()
74   {
75     return 5 * NUMBER_OF_PORTRAIT_IMAGE + 1;
76   }
77
78   /**
79    * Create texture to represent a page.
80    * @param[in] pageId The ID of the page to create.
81    * @return The texture.
82    */
83   virtual Texture NewPage(unsigned int pageId)
84   {
85     Texture texture;
86
87     PixelData pixels;
88     if(pageId == 0)
89     {
90       pixels = SyncImageLoader::Load(BOOK_COVER_PORTRAIT);
91     }
92     else
93     {
94       pixels = SyncImageLoader::Load(PAGE_IMAGES_PORTRAIT[(pageId - 1) % NUMBER_OF_PORTRAIT_IMAGE]);
95     }
96
97     if(pixels)
98     {
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());
101     }
102
103     return texture;
104   }
105 };
106
107 class LandscapePageFactory : public PageFactory
108 {
109   /**
110    * Query the number of pages available from the factory.
111    * The maximum available page has an ID of GetNumberOfPages()-1.
112    */
113   virtual unsigned int GetNumberOfPages()
114   {
115     return 5 * NUMBER_OF_LANDSCAPE_IMAGE / 2 + 1;
116   }
117
118   /**
119    * Create texture to represent a page.
120    * @param[in] pageId The ID of the page to create.
121    * @return The texture.
122    */
123   virtual Texture NewPage(unsigned int pageId)
124   {
125     Texture texture;
126
127     PixelData pixelsFront;
128     PixelData pixelsBack;
129
130     if(pageId == 0)
131     {
132       pixelsFront = SyncImageLoader::Load(BOOK_COVER_LANDSCAPE);
133       pixelsBack  = SyncImageLoader::Load(BOOK_COVER_BACK_LANDSCAPE);
134     }
135     else
136     {
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]);
140     }
141
142     if(pixelsFront && pixelsBack)
143     {
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());
147     }
148
149     return texture;
150   }
151 };
152
153 /**
154  * This example shows how to use the PageTurnView UI control
155  */
156 class PageTurnExample : public ConnectionTracker
157 {
158 public:
159   PageTurnExample(Application& app);
160
161   ~PageTurnExample();
162
163   void OnInit(Application& app);
164
165 private:
166   void OnWindowResized(Window window, Window::WindowSize size);
167
168   void Rotate(DemoOrientation orientation);
169
170   void OnKeyEvent(const KeyEvent& event);
171
172 private:
173   Application& mApplication;
174
175   PageTurnView         mPageTurnPortraitView;
176   PageTurnView         mPageTurnLandscapeView;
177   PortraitPageFactory  mPortraitPageFactory;
178   LandscapePageFactory mLandscapePageFactory;
179
180   DemoOrientation mOrientation;
181 };
182
183 PageTurnExample::PageTurnExample(Application& app)
184 : mApplication(app),
185   mOrientation(UNKNOWN)
186 {
187   app.InitSignal().Connect(this, &PageTurnExample::OnInit);
188 }
189
190 PageTurnExample::~PageTurnExample()
191 {
192 }
193
194 /**
195  * The Init signal is received once (only) during the Application lifetime
196  */
197 void PageTurnExample::OnInit(Application& app)
198 {
199   Window window = app.GetWindow();
200   window.KeyEventSignal().Connect(this, &PageTurnExample::OnKeyEvent);
201
202   window.AddAvailableOrientation(Window::PORTRAIT);
203   window.AddAvailableOrientation(Window::LANDSCAPE);
204   window.AddAvailableOrientation(Window::PORTRAIT_INVERSE);
205   window.AddAvailableOrientation(Window::LANDSCAPE_INVERSE);
206   window.ResizeSignal().Connect(this, &PageTurnExample::OnWindowResized);
207
208   Window::WindowSize size = window.GetSize();
209   Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
210 }
211
212 void PageTurnExample::OnWindowResized(Window window, Window::WindowSize size)
213 {
214   Rotate(size.GetWidth() > size.GetHeight() ? LANDSCAPE : PORTRAIT);
215 }
216
217 void PageTurnExample::Rotate(DemoOrientation orientation)
218 {
219   Window  window     = mApplication.GetWindow();
220   Vector2 windowSize = window.GetSize();
221
222   if(mOrientation != orientation)
223   {
224     mOrientation = orientation;
225
226     if(PORTRAIT == orientation)
227     {
228       if(!mPageTurnPortraitView)
229       {
230         mPageTurnPortraitView = PageTurnPortraitView::New(mPortraitPageFactory, windowSize);
231         mPageTurnPortraitView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
232       }
233
234       if(mPageTurnLandscapeView)
235       {
236         window.Remove(mPageTurnLandscapeView);
237       }
238       window.Add(mPageTurnPortraitView);
239     }
240     else if(LANDSCAPE == orientation)
241     {
242       if(!mPageTurnLandscapeView)
243       {
244         mPageTurnLandscapeView = PageTurnLandscapeView::New(mLandscapePageFactory, Vector2(windowSize.x * 0.5f, windowSize.y));
245         mPageTurnLandscapeView.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
246       }
247
248       if(mPageTurnPortraitView)
249       {
250         window.Remove(mPageTurnPortraitView);
251       }
252
253       window.Add(mPageTurnLandscapeView);
254     }
255   }
256 }
257
258 /**
259  * Main key event handler
260  */
261 void PageTurnExample::OnKeyEvent(const KeyEvent& event)
262 {
263   if(event.GetState() == KeyEvent::DOWN)
264   {
265     if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
266     {
267       mApplication.Quit();
268     }
269   }
270 }
271
272 // Entry point for applications
273 int DALI_EXPORT_API main(int argc, char** argv)
274 {
275   Application     app = Application::New(&argc, &argv);
276   PageTurnExample test(app);
277
278   app.MainLoop();
279
280   return 0;
281 }