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/layout-item-impl.h>
23 #include "custom-layout.h"
26 using namespace Dali::Toolkit;
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",
38 const unsigned int NUMBER_OF_IMAGES = sizeof( IMAGE_PATH ) / sizeof( char* );
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
46 ImageView CreateChildImageView( const char* filename, Size size )
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 );
61 } // unnamed namespace
64 * @brief Demonstrates how to create a very simple layout and apply that to any Control.
66 class SimpleLayoutExample : public ConnectionTracker
72 * @param[in] application A reference to the Application class.
74 SimpleLayoutExample( Application& application )
75 : mApplication( application )
77 // Connect to the Application's Init signal
78 mApplication.InitSignal().Connect( this, &SimpleLayoutExample::Create );
84 * @brief Called to initialise the application content
85 * @param[in] application A reference to the Application class.
87 void Create( Application& application )
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
97 // Create a new control
98 Control control = Control::New();
99 control.SetParentOrigin( ParentOrigin::CENTER );
100 control.SetAnchorPoint( AnchorPoint::CENTER );
103 // Set our Custom Layout on the control
104 auto layout = Demo::CustomLayout::New();
105 DevelControl::SetLayout( control, layout );
107 // Add child image-views to the created control
108 for( auto i = 0u; i < NUMBER_OF_IMAGES; ++i )
110 control.Add( CreateChildImageView( IMAGE_PATH[ i ], Size( 100.0f, 100.0f ) ) );
115 * @brief Called when the stage is touched.
117 * We will use this to quit the application.
119 bool OnTouch( Actor /* actor */, const TouchData& /* touch */ )
126 * @brief Called when any key event is received.
128 * Will use this to quit the application if Back or the Escape key is received
129 * @param[in] event The key event information
131 void OnKeyEvent( const KeyEvent& event )
133 if( event.state == KeyEvent::Down )
135 if ( IsKey( event, Dali::DALI_KEY_ESCAPE ) || IsKey( event, Dali::DALI_KEY_BACK ) )
143 Application& mApplication; ///< A reference to the application object.
146 int DALI_EXPORT_API main( int argc, char **argv )
148 Application application = Application::New( &argc, &argv );
149 SimpleLayoutExample test( application );
150 application.MainLoop();