Changed some examples to use initializer list for Maps & Arrays
[platform/core/uifw/dali-demo.git] / examples / layouting / absolute-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 "absolute-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
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29
30 namespace
31 {
32 const char* const TITLE = "Absolute Example";
33
34 struct ImageDetails
35 {
36   const char * name;
37   Vector2 position;
38   Size size;
39 };
40
41 ImageDetails IMAGES[] =
42 {
43   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f,   0.0f ), Size( 100.0f, 100.0f ) },
44   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 100.0f,   0.0f ), Size( 100.0f, 100.0f ) },
45   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f, 100.0f ), Size( 100.0f, 100.0f ) },
46   { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 200.0f, 200.0f ), Size( 100.0f, 100.0f ) },
47 };
48 unsigned int IMAGE_COUNT=sizeof(IMAGES)/sizeof(IMAGES[0]);
49
50 // Helper function to create ImageViews with given filename and size.
51 void CreateChildImageView( ImageView& imageView, unsigned imageIndex )
52 {
53   imageView = ImageView::New();
54   Property::Map imagePropertyMap;
55   imagePropertyMap[ Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
56   imagePropertyMap[ ImageVisual::Property::URL ] = IMAGES[imageIndex].name;
57   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = IMAGES[imageIndex].size.width ;
58   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = IMAGES[imageIndex].size.height;
59   imageView.SetProperty( ImageView::Property::IMAGE , imagePropertyMap );
60   imageView.SetName("ImageView");
61   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
62   imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
63   imageView.SetProperty( Dali::Actor::Property::POSITION, Vector3( IMAGES[imageIndex].position ) );
64 }
65
66 } // namespace
67
68 namespace Demo
69 {
70
71 AbsoluteExample::AbsoluteExample()
72 : Example( TITLE ),
73   mRootLayoutControl(),
74   mAbsoluteLayoutContainer(),
75   mLayoutSizeToggleStatus( true ),
76   mToggleButton()
77 {
78
79 }
80
81 void AbsoluteExample::Create()
82 {
83   auto stage = Stage::GetCurrent();
84   // This layout will be the size of the stage but allows subsequent layouts to be any size.
85   mRootLayoutControl = LayoutUtilities::CreateRootContainer();
86   stage.Add( mRootLayoutControl );
87
88   // Create an Absolute Layout to show ImageViews at explictly provided positions.
89   mAbsoluteLayoutContainer = Control::New();
90   mAbsoluteLayoutContainer.SetBackgroundColor( Color::WHITE );
91   auto absoluteLayout = AbsoluteLayout::New();
92   DevelControl::SetLayout( mAbsoluteLayoutContainer, absoluteLayout );
93   mAbsoluteLayoutContainer.SetName("AbsoluteLayout");
94
95   mAbsoluteLayoutContainer.SetAnchorPoint( AnchorPoint::CENTER );
96   mAbsoluteLayoutContainer.SetParentOrigin( ParentOrigin::CENTER );
97
98   // Initially absolute layout to use these specifications, toggle button will alter them.
99   mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
100   mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
101
102   mRootLayoutControl.Add( mAbsoluteLayoutContainer );
103
104   for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ )
105   {
106     CreateChildImageView( mImageViews[x], x%IMAGE_COUNT );
107     mAbsoluteLayoutContainer.Add( mImageViews[x] );
108   }
109
110   // Button toggles the size of the layout
111   mToggleButton = PushButton::New();
112   mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Change layout size" );
113   mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
114   mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
115   mToggleButton.ClickedSignal().Connect( this, &Demo::AbsoluteExample::ChangeSizeClicked );
116   mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
117   mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
118
119   stage.Add( mToggleButton );
120
121 }
122
123 void AbsoluteExample::Remove()
124 {
125   UnparentAndReset( mAbsoluteLayoutContainer );
126   UnparentAndReset( mToggleButton );
127   UnparentAndReset( mRootLayoutControl );
128 }
129
130 bool AbsoluteExample::ChangeSizeClicked( Toolkit::Button button )
131 {
132   if ( true == mLayoutSizeToggleStatus )
133   {
134     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
135     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  700 );
136     mLayoutSizeToggleStatus = false;
137   }
138   else
139   {
140     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
141     mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION,  ChildLayoutData::WRAP_CONTENT );
142     mLayoutSizeToggleStatus = true;
143   }
144
145   return true;
146 }
147
148 } // namespace Demo