Extending Layout tester demo
[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/hbox-layout.h>
24 #include <dali-toolkit/devel-api/layouting/vbox-layout.h>
25
26 using namespace Dali;
27 using namespace Dali::Toolkit;
28
29 namespace
30 {
31
32 // Button file names
33 const char* LTR_IMAGE( DEMO_IMAGE_DIR "icon-play.png" );
34 const char* LTR_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-play-selected.png" );
35
36 const char* RTL_IMAGE( DEMO_IMAGE_DIR "icon-reverse.png" );
37 const char* RTL_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-reverse-selected.png" );
38
39 const char* ROTATE_CLOCKWISE_IMAGE( DEMO_IMAGE_DIR "icon-reset.png" );
40 const char* ROTATE_CLOCKWISE_SELECTED_IMAGE( DEMO_IMAGE_DIR "icon-reset-selected.png" );
41
42 // Child image filenames
43 const char* IMAGE_PATH[] = {
44   DEMO_IMAGE_DIR "application-icon-101.png",
45   DEMO_IMAGE_DIR "application-icon-102.png",
46   DEMO_IMAGE_DIR "application-icon-103.png",
47   DEMO_IMAGE_DIR "application-icon-104.png"
48 };
49
50 const unsigned int NUMBER_OF_RESOURCES = sizeof(IMAGE_PATH) / sizeof(char*);
51
52 // Helper function to create ImageViews with given filename and size.
53 void CreateChildImageView( ImageView& imageView, const char* filename, Size size )
54 {
55   imageView = ImageView::New();
56   Property::Map imagePropertyMap;
57   imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
58   imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = filename;
59   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = size.width;
60   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = size.height;
61   imageView.SetProperty(Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
62   imageView.SetName("ImageView");
63   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
64   imageView.SetResizePolicy( ResizePolicy::FIXED, Dimension::ALL_DIMENSIONS );
65 }
66
67 }  // namespace
68
69 namespace Demo
70 {
71
72 void LinearExample::Create()
73 {
74   // The Init signal is received once (only) during the Application lifetime
75   auto stage = Stage::GetCurrent();
76
77   mDirectionButton = PushButton::New();
78   mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, RTL_IMAGE );
79   mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, RTL_SELECTED_IMAGE );
80   mDirectionButton.ClickedSignal().Connect( this, &LinearExample::OnDirectionClicked );
81   mDirectionButton.SetParentOrigin( Vector3(0.33f, 1.0f, 0.5f ) );
82   mDirectionButton.SetAnchorPoint( AnchorPoint::BOTTOM_CENTER );
83   mDirectionButton.SetSize(75, 75);
84   stage.Add( mDirectionButton );
85
86   mRotateButton = PushButton::New();
87   mRotateButton.SetProperty( PushButton::Property::UNSELECTED_ICON, ROTATE_CLOCKWISE_IMAGE );
88   mRotateButton.SetProperty( PushButton::Property::SELECTED_ICON, ROTATE_CLOCKWISE_SELECTED_IMAGE );
89   mRotateButton.ClickedSignal().Connect( this, &LinearExample::OnRotateClicked );
90   mRotateButton.SetParentOrigin( Vector3(0.66f, 1.0f, 0.5f ));
91   mRotateButton.SetAnchorPoint( Vector3(0.5f, 1.0f, 0.5f));
92   mRotateButton.SetSize(75, 75);
93   stage.Add( mRotateButton );
94
95   // Create a linear layout
96   mLinearContainer = Control::New();
97   mIsHorizontal = false;
98
99   auto layout = VboxLayout::New();
100   layout.SetAnimateLayout(true);
101   DevelControl::SetLayout( mLinearContainer, layout );
102
103   mLinearContainer.SetParentOrigin( ParentOrigin::CENTER );
104   mLinearContainer.SetAnchorPoint( AnchorPoint::CENTER );
105   mLinearContainer.SetName( "LinearExample" );
106   stage.Add( mLinearContainer );
107   mLinearContainer.SetProperty( Toolkit::LayoutItem::ChildProperty::WIDTH_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
108   mLinearContainer.SetProperty( Toolkit::LayoutItem::ChildProperty::HEIGHT_SPECIFICATION, ChildLayoutData::MATCH_PARENT );
109   mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::LEFT_TO_RIGHT );
110
111   for( unsigned int x = 0; x < NUMBER_OF_RESOURCES; ++x )
112   {
113     Toolkit::ImageView imageView;
114     CreateChildImageView( imageView, IMAGE_PATH[ x ], Size(100.0f, 100.0f) );
115     mLinearContainer.Add( imageView );
116   }
117 }
118
119 // Remove controls added by this example from stage
120 void LinearExample::Remove()
121 {
122   if ( mLinearContainer )
123   {
124     UnparentAndReset( mDirectionButton );
125     UnparentAndReset( mRotateButton );
126     UnparentAndReset( mLinearContainer);
127   }
128 }
129
130 // Mirror items in layout
131 bool LinearExample::OnDirectionClicked( Button button )
132 {
133   if( mDirection )
134   {
135     mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, LTR_IMAGE );
136     mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, LTR_SELECTED_IMAGE );
137     mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::LEFT_TO_RIGHT );
138   }
139   else
140   {
141     mDirectionButton.SetProperty( PushButton::Property::UNSELECTED_ICON, RTL_IMAGE );
142     mDirectionButton.SetProperty( PushButton::Property::SELECTED_ICON, RTL_SELECTED_IMAGE );
143     mLinearContainer.SetProperty( Actor::Property::LAYOUT_DIRECTION, LayoutDirection::RIGHT_TO_LEFT );
144   }
145   mDirection = !mDirection;
146   return true;
147 }
148
149 // Rotate layout by changing layout
150 bool LinearExample::OnRotateClicked( Button button )
151 {
152   mIsHorizontal = !mIsHorizontal;
153   if( mIsHorizontal )
154   {
155     auto hboxLayout = HboxLayout::New();
156     hboxLayout.SetAnimateLayout(true);
157     DevelControl::SetLayout( mLinearContainer, hboxLayout );
158   }
159   else
160   {
161     auto vboxLayout = VboxLayout::New();
162     vboxLayout.SetAnimateLayout(true);
163     DevelControl::SetLayout( mLinearContainer, vboxLayout );
164   }
165   return true;
166 }
167
168 } // namespace Demo