9f56222ae6236347c234d45af3d9f525bda0897c
[platform/core/uifw/dali-demo.git] / examples / layouting / linear-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 "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>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 namespace
29 {
30
31 // Button file names
32 const char* LTR_IMAGE( DEMO_IMAGE_DIR "icon-play.png" );
33 const char* LTR_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-play-selected.png" );
34
35 const char* RTL_IMAGE( DEMO_IMAGE_DIR "icon-reverse.png" );
36 const char* RTL_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-reverse-selected.png" );
37
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" );
40
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"
47 };
48
49 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
50
51 // Helper function to create ImageViews with given filename and size.
52 void CreateChildImageView( ImageView& imageView, const char* filename, Size size )
53 {
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 );
64 }
65
66 }  // namespace
67
68 namespace Demo
69 {
70
71 LinearExample::LinearExample()
72 : mLTRDirection(true)
73 {
74 }
75
76 void LinearExample::Create()
77 {
78   auto stage = Stage::GetCurrent();
79
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 );
88
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 );
97
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 );
104
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 );
112
113   for( unsigned int x = 0; x < NUMBER_OF_RESOURCES; ++x )
114   {
115     Toolkit::ImageView imageView;
116     CreateChildImageView( imageView, IMAGE_PATH[ x ], Size(100.0f, 100.0f) );
117     mLinearContainer.Add( imageView );
118   }
119 }
120
121 // Remove controls added by this example from stage
122 void LinearExample::Remove()
123 {
124   if ( mLinearContainer )
125   {
126     UnparentAndReset( mDirectionButton );
127     UnparentAndReset( mRotateButton );
128     UnparentAndReset( mLinearContainer);
129   }
130 }
131
132 // Mirror items in layout
133 bool LinearExample::OnDirectionClicked( Button button )
134 {
135   if( !mLTRDirection )
136   {
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 );
140   }
141   else
142   {
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 );
146   }
147   mLTRDirection = !mLTRDirection;
148   return true;
149 }
150
151 // Rotate layout by changing layout
152 bool LinearExample::OnRotateClicked( Button button )
153 {
154   auto layout = LinearLayout::DownCast( DevelControl::GetLayout( mLinearContainer ) );
155   if( layout.GetOrientation() == LinearLayout::Orientation::VERTICAL )
156   {
157     layout.SetOrientation( LinearLayout::Orientation::HORIZONTAL );
158   }
159   else
160   {
161     layout.SetOrientation( LinearLayout::Orientation::VERTICAL );
162   }
163   return true;
164 }
165
166 } // namespace Demo