Use Window instead of Stage
[platform/core/uifw/dali-demo.git] / examples / simple-scroll-view / simple-scroll-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 // EXTERNAL INCLUDES
19 #include <dali/dali.h>
20 #include <dali-toolkit/dali-toolkit.h>
21
22 using namespace Dali;
23 using namespace Dali::Toolkit;
24
25 namespace
26 {
27 const int NUMBER_OF_PAGES = 9; ///< Number of Pages going across
28 const int ROWS_PER_PAGE   = 5; ///< Number of Images going down (rows) within a Page
29
30 const float DISTANCE_BETWEEN_IMAGES = 6.0f; ///< The distance between the images
31
32 } // unnamed namespace
33
34 /**
35  * An example showing how to create a very simple scroll view
36  */
37 class ExampleController : public ConnectionTracker
38 {
39 public:
40
41   /**
42    * Constructor
43    * @param application class, stored as reference
44    */
45   ExampleController( Application& application )
46   : mApplication( application )
47   {
48     // Connect to the Application's Init and orientation changed signal
49     mApplication.InitSignal().Connect(this, &ExampleController::OnInit);
50   }
51
52   ~ExampleController() = default;
53
54 private:
55
56   /**
57    * This method gets called once the main loop of application is up and running
58    */
59   void OnInit(Application& app)
60   {
61     Window window = app.GetWindow();
62     Vector2 windowSize = window.GetSize();
63     window.SetBackgroundColor( Color::WHITE );
64     window.KeyEventSignal().Connect(this, &ExampleController::OnKeyEvent);
65
66     // Make the scroll view's size a certain percentage of the window
67     const Vector2 pageSize = windowSize * 0.75f;
68
69     // Create a scroll view and set our desired properties
70     ScrollView scrollView = ScrollView::New();
71     scrollView.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
72     scrollView.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
73     scrollView.SetProperty( Actor::Property::SIZE, pageSize );
74     scrollView.SetAxisAutoLock( true );
75     window.Add( scrollView );
76
77     // We want to the scroll-view so only one page is shown
78     scrollView.SetProperty( Actor::Property::CLIPPING_MODE, ClippingMode::CLIP_TO_BOUNDING_BOX );
79
80     // Create rulers for the X and Y domains, we want to disable vertical scrolling but enable horizontal scrolling
81     RulerPtr rulerX = new FixedRuler( pageSize.width ); // Snaps to a multiple of this when flicking
82     RulerPtr rulerY = new DefaultRuler;
83     rulerX->SetDomain( RulerDomain( 0.0f, pageSize.width * NUMBER_OF_PAGES, true ) ); // Set the domain to equal the number of pages used
84     rulerY->Disable();
85
86     scrollView.SetRulerX( rulerX );
87     scrollView.SetRulerY( rulerY );
88
89     // Populate the Pages
90     for( int column = 0, textNumber = 0; column < NUMBER_OF_PAGES; column++ )
91     {
92       Actor page = CreatePage( pageSize, textNumber );
93       page.SetProperty( Actor::Property::POSITION, Vector2( column * pageSize.x, 0.0f ));
94       scrollView.Add( page );
95     }
96
97     // Do a little animation from the last page to the first page on load
98     scrollView.ScrollTo( NUMBER_OF_PAGES - 1, 0.0f );
99     scrollView.ScrollTo( 0 );
100   }
101
102   /**
103    * Creates a page using a source of images.
104    */
105   Actor CreatePage( const Vector2& pageSize, int& textNumber )
106   {
107     Actor page = Actor::New();
108     page.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
109     page.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
110     page.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
111
112     Window window = mApplication.GetWindow();
113
114     // Calculate the number of images going across (columns) within a page, the image size and the size of the text
115     const int imageColumns = round( ROWS_PER_PAGE * ( pageSize.width ) / ( pageSize.height ) );
116     const Vector3 imageSize( ( pageSize.width  / imageColumns ) - DISTANCE_BETWEEN_IMAGES,
117                              ( pageSize.height / ROWS_PER_PAGE) - DISTANCE_BETWEEN_IMAGES,
118                              0.0f);
119     const float textPixelSize = imageSize.width / 3.0f;
120
121     // Populate the page
122     for( int row = 0; row < ROWS_PER_PAGE; row++ )
123     {
124       for( int column = 0; column < imageColumns; column++ )
125       {
126         const Vector3 position( DISTANCE_BETWEEN_IMAGES * 0.5f + ( imageSize.x + DISTANCE_BETWEEN_IMAGES ) * column - pageSize.width  * 0.5f,
127                                 DISTANCE_BETWEEN_IMAGES * 0.5f + ( imageSize.y + DISTANCE_BETWEEN_IMAGES ) * row    - pageSize.height * 0.5f,
128                                 0.0f);
129
130         Control item = TextLabel::New( std::to_string( textNumber++ ) );
131         item.SetProperty( Actor::Property::POSITION, position + imageSize * 0.5f );
132         item.SetProperty( Actor::Property::SIZE, imageSize );
133         item.SetProperty( Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER );
134         item.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER );
135         item.SetProperty( TextLabel::Property::TEXT_COLOR, Color::BLACK );
136         item.SetProperty( TextLabel::Property::PIXEL_SIZE, textPixelSize );
137         item.SetProperty( TextLabel::Property::HORIZONTAL_ALIGNMENT, HorizontalAlignment::CENTER );
138         item.SetProperty( TextLabel::Property::VERTICAL_ALIGNMENT, VerticalAlignment::CENTER );
139         item.SetProperty( TextLabel::Property::OUTLINE, Property::Map{ { "width", 2 }, { "color", Color::WHITE } } );
140         item.SetProperty( Control::Property::BACKGROUND, Vector4( Random::Range( 0.0f, 1.0f ), Random::Range( 0.0f, 1.0f ), Random::Range( 0.0f, 1.0f ), 0.7f ) );
141         page.Add(item);
142       }
143     }
144
145     return page;
146   }
147
148   /**
149    * Main key event handler
150    */
151   void OnKeyEvent(const KeyEvent& event)
152   {
153     if(event.state == KeyEvent::Down)
154     {
155       if( IsKey( event, Dali::DALI_KEY_ESCAPE) || IsKey( event, Dali::DALI_KEY_BACK) )
156       {
157         mApplication.Quit();
158       }
159     }
160   }
161
162 private:
163   Application& mApplication; ///< Application instance
164 };
165
166 int DALI_EXPORT_API main(int argc, char **argv)
167 {
168   Application app = Application::New(&argc, &argv, DEMO_THEME_PATH);
169   ExampleController test(app);
170   app.MainLoop();
171   return 0;
172 }