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