811046ec0d7efb6ddf1f3304b70a7e92830cc56a
[platform/core/uifw/dali-demo.git] / examples / layouting / padding-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 <string>
19 #include "padding-example.h"
20 #include "layout-utilities.h"
21 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
22 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
23 #include <dali-toolkit/devel-api/controls/control-devel.h>
24 #include <dali-toolkit/devel-api/layouting/absolute-layout.h>
25 #include <dali-toolkit/devel-api/layouting/linear-layout.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30
31 namespace
32 {
33 const char* const TITLE = "Padding Example";
34
35 // Helper function to create ImageViews with given filename and size.
36 void CreateChildImageView( ImageView& imageView, const char* filename, Size size )
37 {
38   imageView = ImageView::New();
39   Property::Map imagePropertyMap;
40   imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
41   imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = filename;
42   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = size.width;
43   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = size.height;
44   imageView.SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
45   imageView.SetName("ImageView");
46   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
47   imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
48 }
49
50 }
51
52 namespace Demo
53 {
54
55 PaddingExample::PaddingExample()
56 : Example( TITLE )
57 {}
58
59 void PaddingExample::Create()
60 {
61   // The Init signal is received once (only) during the Application lifetime
62
63   auto stage = Stage::GetCurrent();
64
65   Control rootLayoutControl = LayoutUtilities::CreateRootContainer();
66   stage.Add( rootLayoutControl );
67
68   // Create a table view to show a pair of buttons above each image.
69   mHorizontalBox = Control::New();
70
71   // Create LinearLayout for this control.
72   auto hboxLayout = LinearLayout::New();
73   DevelControl::SetLayout( mHorizontalBox, hboxLayout );
74   mHorizontalBox.SetName("HBox");
75   mHorizontalBox.SetBackgroundColor( Color::WHITE );
76   mHorizontalBox.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
77   mHorizontalBox.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  700 );
78   mHorizontalBox.SetAnchorPoint( AnchorPoint::CENTER );
79   mHorizontalBox.SetParentOrigin( ParentOrigin::CENTER );
80
81   rootLayoutControl.Add( mHorizontalBox );
82
83   mToggleButton = Toolkit::PushButton::New();
84   mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Toggle Padding on #2" );
85   mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
86   mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
87   mToggleButton.ClickedSignal().Connect( this, &Demo::PaddingExample::ChangePaddingClicked );
88   mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
89   mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
90
91   stage.Add( mToggleButton );
92
93   for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ )
94   {
95     CreateChildImageView( mImageViews[x], DEMO_IMAGE_DIR "gallery-small-23.jpg" , Size(100.0f, 100.0f) );
96
97     // Set Padding for second ImageView
98     if( 1 == x )
99     {
100       mImageViews[x].SetProperty(Toolkit::Control::Property::PADDING, Extents( 10.0f,10.0f,5.0f, 5.0f));
101     }
102
103     // Set margin for first ImageView
104     if( 0 == x )
105     {
106       mImageViews[x].SetProperty(Toolkit::Control::Property::MARGIN, Extents( 10.0f,10.0f,0.0f, 0.0f));
107     }
108
109     mHorizontalBox.Add( mImageViews[x] );
110
111     mImageViewToggleStatus[ x ] = true;
112   }
113 }
114
115 void PaddingExample::Remove()
116 {
117   UnparentAndReset( mToggleButton );
118   UnparentAndReset( mHorizontalBox );
119 }
120
121 bool PaddingExample::ChangePaddingClicked( Toolkit::Button button )
122 {
123   if ( true == mImageViewToggleStatus[ 1 ] )
124   {
125     mImageViews[1].SetProperty(Toolkit::Control::Property::PADDING, Extents( .0f, .0f, .0f, .0f));
126     mImageViews[1].SetProperty(Actor::Property::COLOR_ALPHA, 0.5f);
127     mImageViewToggleStatus[ 1 ] = false;
128   }
129   else
130   {
131     mImageViews[1].SetProperty(Toolkit::Control::Property::PADDING, Extents( 10.0f, 10.0f, 5.0f, 5.0f));
132     mImageViews[1].SetProperty(Actor::Property::COLOR_ALPHA, 1.0f);
133     mImageViewToggleStatus[ 1 ] = true;
134   }
135
136   return true;
137 }
138
139 } // namespace Demo