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