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