2 * Copyright (c) 2017 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.
19 #include "absolute-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/absolute-layout.h>
26 using namespace Dali::Toolkit;
39 ImageDetails IMAGES[] =
41 { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f, 0.0f ), Size( 100.0f, 100.0f ) },
42 { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 100.0f, 0.0f ), Size( 100.0f, 100.0f ) },
43 { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 0.0f, 100.0f ), Size( 100.0f, 100.0f ) },
44 { DEMO_IMAGE_DIR "gallery-small-23.jpg", Vector2( 200.0f, 200.0f ), Size( 100.0f, 100.0f ) },
46 unsigned int IMAGE_COUNT=sizeof(IMAGES)/sizeof(IMAGES[0]);
48 // Helper function to create ImageViews with given filename and size.
49 void CreateChildImageView( ImageView& imageView, unsigned imageIndex )
51 imageView = ImageView::New();
52 Property::Map imagePropertyMap;
53 imagePropertyMap[ Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
54 imagePropertyMap[ ImageVisual::Property::URL ] = IMAGES[imageIndex].name;
55 imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = IMAGES[imageIndex].size.width ;
56 imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = IMAGES[imageIndex].size.height;
57 imageView.SetProperty( ImageView::Property::IMAGE , imagePropertyMap );
58 imageView.SetName("ImageView");
59 imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
60 imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
61 imageView.SetProperty( Dali::Actor::Property::POSITION, Vector3( IMAGES[imageIndex].position ) );
69 AbsoluteExample::AbsoluteExample()
70 : mRootLayoutControl(),
71 mAbsoluteLayoutContainer(),
72 mLayoutSizeToggleStatus( true ),
78 void AbsoluteExample::Create()
80 // Create a root layout, ideally Dali would have a default layout in the root layer.
81 // Without this root layer the mAbsoluteLayout (or any other layout) will not
82 // honour WIDTH_SPECIFICATION or HEIGHT_SPECIFICATION settings.
83 // It uses the default stage size and ideally should have a layout added to it.
84 auto stage = Stage::GetCurrent();
85 mRootLayoutControl = Control::New();
86 auto rootLayout = AbsoluteLayout::New();
87 DevelControl::SetLayout( mRootLayoutControl, rootLayout );
88 mRootLayoutControl.SetAnchorPoint( AnchorPoint::CENTER );
89 mRootLayoutControl.SetParentOrigin( ParentOrigin::CENTER );
90 stage.Add( mRootLayoutControl );
92 // Create an Absolute Layout to show ImageViews at explictly provided positions.
93 mAbsoluteLayoutContainer = Control::New();
94 mAbsoluteLayoutContainer.SetBackgroundColor( Color::WHITE );
95 auto absoluteLayout = AbsoluteLayout::New();
96 DevelControl::SetLayout( mAbsoluteLayoutContainer, absoluteLayout );
97 mAbsoluteLayoutContainer.SetName("AbsoluteLayout");
99 mAbsoluteLayoutContainer.SetAnchorPoint( AnchorPoint::CENTER );
100 mAbsoluteLayoutContainer.SetParentOrigin( ParentOrigin::CENTER );
102 // Initially absolute layout to use these specifications, toggle button will alter them.
103 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
104 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
106 mRootLayoutControl.Add( mAbsoluteLayoutContainer );
108 for( unsigned int x = 0; x < NUMBER_OF_IMAGE_VIEWS; x++ )
110 CreateChildImageView( mImageViews[x], x%IMAGE_COUNT );
111 mAbsoluteLayoutContainer.Add( mImageViews[x] );
114 // Button toggles the size of the layout
115 mToggleButton = PushButton::New();
116 mToggleButton.SetProperty( Toolkit::Button::Property::LABEL, "Change layout size" );
117 mToggleButton.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
118 mToggleButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
119 mToggleButton.ClickedSignal().Connect( this, &Demo::AbsoluteExample::ChangeSizeClicked );
120 mToggleButton.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
121 mToggleButton.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::HEIGHT );
123 stage.Add( mToggleButton );
127 void AbsoluteExample::Remove()
129 UnparentAndReset( mAbsoluteLayoutContainer );
130 UnparentAndReset( mToggleButton );
131 UnparentAndReset( mRootLayoutControl );
134 bool AbsoluteExample::ChangeSizeClicked( Toolkit::Button button )
136 if ( true == mLayoutSizeToggleStatus )
138 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, 480 );
139 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, 700 );
140 mLayoutSizeToggleStatus = false;
144 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
145 mAbsoluteLayoutContainer.SetProperty( LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::WRAP_CONTENT );
146 mLayoutSizeToggleStatus = true;