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