2 * Copyright (c) 2018 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 "linear-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/linear-layout.h>
26 using namespace Dali::Toolkit;
32 const char* LTR_IMAGE( DEMO_IMAGE_DIR "icon-play.png" );
33 const char* LTR_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-play-selected.png" );
35 const char* RTL_IMAGE( DEMO_IMAGE_DIR "icon-reverse.png" );
36 const char* RTL_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-reverse-selected.png" );
38 const char* ROTATE_CLOCKWISE_IMAGE( DEMO_IMAGE_DIR "icon-reset.png" );
39 const char* ROTATE_CLOCKWISE_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-reset-selected.png" );
41 // Child image filenames
42 const char* IMAGE_PATH[] = {
43 DEMO_IMAGE_DIR "application-icon-101.png",
44 DEMO_IMAGE_DIR "application-icon-102.png",
45 DEMO_IMAGE_DIR "application-icon-103.png",
46 DEMO_IMAGE_DIR "application-icon-104.png"
49 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
51 // Helper function to create ImageViews with given filename and size.
52 void CreateChildImageView( ImageView& imageView, const char* filename, Size size )
54 imageView = ImageView::New();
55 Property::Map imagePropertyMap;
56 imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
57 imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = filename;
58 imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = size.width;
59 imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = size.height;
60 imageView.SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
61 imageView.SetName("ImageView");
62 imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
63 imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
71 LinearExample::LinearExample()
76 void LinearExample::Create()
78 auto stage = Stage::GetCurrent();
80 mDirectionButton = PushButton::New();
81 mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, RTL_IMAGE );
82 mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, RTL_SELECTED_IMAGE );
83 mDirectionButton.ClickedSignal().Connect( this, &LinearExample::OnDirectionClicked );
84 mDirectionButton.SetParentOrigin( Vector3(0.33f, 1.0f, 0.5f ) );
85 mDirectionButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
86 mDirectionButton.SetSize(75, 75);
87 stage.Add( mDirectionButton );
89 mRotateButton = PushButton::New();
90 mRotateButton.SetProperty( PushButton::Property::UNSELECTED_ICON, ROTATE_CLOCKWISE_IMAGE );
91 mRotateButton.SetProperty( PushButton::Property::SELECTED_ICON, ROTATE_CLOCKWISE_SELECTED_IMAGE );
92 mRotateButton.ClickedSignal().Connect( this, &LinearExample::OnRotateClicked );
93 mRotateButton.SetParentOrigin( Vector3(0.66f, 1.0f, 0.5f ));
94 mRotateButton.SetAnchorPoint( Vector3(0.5f, 1.0f, 0.5f));
95 mRotateButton.SetSize(75, 75);
96 stage.Add( mRotateButton );
98 // Create a linear layout
99 mLinearContainer = Control::New();
100 auto layout = LinearLayout::New();
101 layout.SetAnimateLayout(true);
102 layout.SetOrientation( LinearLayout::Orientation::VERTICAL );
103 DevelControl::SetLayout( mLinearContainer, layout );
105 mLinearContainer.SetParentOrigin( ParentOrigin::CENTER );
106 mLinearContainer.SetAnchorPoint( AnchorPoint::CENTER );
107 mLinearContainer.SetName( "LinearExample" );
108 stage.Add( mLinearContainer );
109 mLinearContainer.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
110 mLinearContainer.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
111 mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::LEFT_TO_RIGHT );
113 for( unsigned int x = 0; x < NUMBER_OF_RESOURCES; ++x )
115 Toolkit::ImageView imageView;
116 CreateChildImageView( imageView, IMAGE_PATH[ x ], Size(100.0f, 100.0f) );
117 mLinearContainer.Add( imageView );
121 // Remove controls added by this example from stage
122 void LinearExample::Remove()
124 if ( mLinearContainer )
126 UnparentAndReset( mDirectionButton );
127 UnparentAndReset( mRotateButton );
128 UnparentAndReset( mLinearContainer);
132 // Mirror items in layout
133 bool LinearExample::OnDirectionClicked( Button button )
137 mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, LTR_IMAGE );
138 mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, LTR_SELECTED_IMAGE );
139 mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::LEFT_TO_RIGHT );
143 mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, RTL_IMAGE );
144 mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, RTL_SELECTED_IMAGE );
145 mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::RIGHT_TO_LEFT );
147 mLTRDirection = !mLTRDirection;
151 // Rotate layout by changing layout
152 bool LinearExample::OnRotateClicked( Button button )
154 auto layout = LinearLayout::DownCast( DevelControl::GetLayout( mLinearContainer ) );
155 if( layout.GetOrientation() == LinearLayout::Orientation::VERTICAL )
157 layout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
161 layout.SetOrientation( LinearLayout::Orientation::VERTICAL );