2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <dali-toolkit/dali-toolkit.h>
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>
24 #include "custom-layout.h"
27 using namespace Dali::Toolkit;
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",
39 const unsigned int NUMBER_OF_IMAGES = sizeof( IMAGE_PATH ) / sizeof( char* );
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
47 ImageView CreateChildImageView( const char* filename, Size size )
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 );
62 } // unnamed namespace
65 * @brief Demonstrates how to create a very simple layout and apply that to any Control.
67 class SimpleLayoutExample : public ConnectionTracker
73 * @param[in] application A reference to the Application class.
75 SimpleLayoutExample( Application& application )
76 : mApplication( application )
78 // Connect to the Application's Init signal
79 mApplication.InitSignal().Connect( this, &SimpleLayoutExample::Create );
85 * @brief Called to initialise the application content
86 * @param[in] application A reference to the Application class.
88 void Create( Application& application )
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
98 // Create a new control
99 Control control = Control::New();
100 control.SetParentOrigin( ParentOrigin::CENTER );
101 control.SetAnchorPoint( AnchorPoint::CENTER );
104 // Set our Custom Layout on the control
105 auto layout = Demo::CustomLayout::New();
106 DevelControl::SetLayout( control, layout );
108 // Add child image-views to the created control
109 for( auto i = 0u; i < NUMBER_OF_IMAGES; ++i )
111 control.Add( CreateChildImageView( IMAGE_PATH[ i ], Size( 100.0f, 100.0f ) ) );
116 * @brief Called when the stage is touched.
118 * We will use this to quit the application.
120 bool OnTouch( Actor /* actor */, const TouchData& /* touch */ )
127 * @brief Called when any key event is received.
129 * Will use this to quit the application if Back or the Escape key is received
130 * @param[in] event The key event information
132 void OnKeyEvent( const KeyEvent& event )
134 if( event.state == KeyEvent::Down )
136 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
144 Application& mApplication; ///< A reference to the application object.
147 int DALI_EXPORT_API main( int argc, char **argv )
149 Application application = Application::New( &argc, &argv );
150 SimpleLayoutExample test( application );
151 application.MainLoop();