[Tizen] Update demo to use LinearLayout.
[platform/core/uifw/dali-demo.git] / examples / simple-layout / simple-layout-example.cpp
1 /*
2  * Copyright (c) 2018 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
20 #include <dali-toolkit/devel-api/controls/control-devel.h>
21 #include <dali-toolkit/devel-api/layouting/layout-item-impl.h>
22
23 #include "custom-layout.h"
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
30
31 /// Child image filenames
32 const char* IMAGE_PATH[] = {
33   DEMO_IMAGE_DIR "application-icon-101.png",
34   DEMO_IMAGE_DIR "application-icon-102.png",
35   DEMO_IMAGE_DIR "application-icon-103.png",
36   DEMO_IMAGE_DIR "application-icon-104.png",
37 };
38 const unsigned int NUMBER_OF_IMAGES = sizeof( IMAGE_PATH ) / sizeof( char* );
39
40 /**
41  * @brief Helper function to create ImageViews with given filename and size.
42  * @param[in]  filename  The filename of the image to use
43  * @param[in]  size      The size that the image should be loaded at
44  * @return The created ImageView
45  */
46 ImageView CreateChildImageView( const char* filename, Size size )
47 {
48   ImageView imageView = ImageView::New();
49   Property::Map imagePropertyMap;
50   imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
51   imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = filename;
52   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = size.width;
53   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = size.height;
54   imageView.SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
55   imageView.SetName("ImageView");
56   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
57   imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
58   return imageView;
59 }
60
61 } // unnamed namespace
62
63 /**
64  * @brief Demonstrates how to create a very simple layout and apply that to any Control.
65  */
66 class SimpleLayoutExample : public ConnectionTracker
67 {
68 public:
69
70   /**
71    * @brief Constructor.
72    * @param[in]  application  A reference to the Application class.
73    */
74   SimpleLayoutExample( Application& application )
75   : mApplication( application )
76   {
77     // Connect to the Application's Init signal
78     mApplication.InitSignal().Connect( this, &SimpleLayoutExample::Create );
79   }
80
81 private:
82
83   /**
84    * @brief Called to initialise the application content
85    * @param[in] application A reference to the Application class.
86    */
87   void Create( Application& application )
88   {
89     // Get a handle to the stage, change the background color and connect to the Touch & Key signals
90     Stage stage = Stage::GetCurrent();
91     stage.SetBackgroundColor( Color::WHITE );
92     stage.GetRootLayer().TouchSignal().Connect( this, &SimpleLayoutExample::OnTouch );
93     stage.KeyEventSignal().Connect( this, &SimpleLayoutExample::OnKeyEvent );
94     stage.KeepRendering(0.5f); // TODO: Should remove after bugfix, but currently required to ensure renders are done after resources are loaded
95
96
97     // Create a new control
98     Control control = Control::New();
99     control.SetParentOrigin( ParentOrigin::CENTER );
100     control.SetAnchorPoint( AnchorPoint::CENTER );
101     stage.Add( control);
102
103     // Set our Custom Layout on the control
104     auto layout = Demo::CustomLayout::New();
105     DevelControl::SetLayout( control, layout );
106
107     // Add child image-views to the created control
108     for( auto i = 0u; i < NUMBER_OF_IMAGES; ++i )
109     {
110       control.Add( CreateChildImageView( IMAGE_PATH[ i ], Size( 100.0f, 100.0f ) ) );
111     }
112   }
113
114   /**
115    * @brief Called when the stage is touched.
116    *
117    * We will use this to quit the application.
118    */
119   bool OnTouch( Actor /* actor */, const TouchData& /* touch */ )
120   {
121     mApplication.Quit();
122     return true;
123   }
124
125   /**
126    * @brief Called when any key event is received.
127    *
128    * Will use this to quit the application if Back or the Escape key is received
129    * @param[in] event The key event information
130    */
131   void OnKeyEvent( const KeyEvent& event )
132   {
133     if( event.state == KeyEvent::Down )
134     {
135       if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
136       {
137         mApplication.Quit();
138       }
139     }
140   }
141
142 private:
143   Application&  mApplication; ///< A reference to the application object.
144 };
145
146 int DALI_EXPORT_API main( int argc, char **argv )
147 {
148   Application application = Application::New( &argc, &argv );
149   SimpleLayoutExample test( application );
150   application.MainLoop();
151   return 0;
152 }