Fixes required after https://review.tizen.org/gerrit/#/c/189231/
[platform/core/uifw/dali-demo.git] / examples / simple-layout / custom-layout-impl.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 // CLASS HEADER
18 #include "custom-layout-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/actor.h>
22
23 namespace Demo
24 {
25
26 namespace Internal
27 {
28
29 using Dali::Actor;
30 using Dali::Toolkit::MeasuredSize;
31
32 CustomLayoutPtr CustomLayout::New()
33 {
34   CustomLayoutPtr layout( new CustomLayout() );
35   return layout;
36 }
37
38 void CustomLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
39 {
40   LayoutLength accumulatedWidth = 0;
41   LayoutLength maxHeight = 0;
42
43   // In this layout we will:
44   //  Measuring the layout with the children in a horizontal configuration, one after another
45   //  Set the required width to be the accumulated width of our children
46   //  Set the required height to be the maximum height of any of our children
47
48   for( unsigned int i=0; i<GetChildCount(); ++i )
49   {
50     auto childLayout = GetChildAt( i );
51     if( childLayout )
52     {
53       MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
54       accumulatedWidth += childLayout->GetMeasuredWidth();
55       std::max( childLayout->GetMeasuredHeight(), maxHeight );
56     }
57   }
58
59   // Finally, call this method to set the dimensions we would like
60   SetMeasuredDimensions( MeasuredSize( accumulatedWidth ), MeasuredSize( maxHeight ) );
61 }
62
63 void CustomLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
64 {
65   LayoutLength childTop( 0 );
66   LayoutLength childLeft( 0 );
67
68   // We want to vertically align the children to the middle
69   LayoutLength height = bottom - top;
70   LayoutLength middle = height / 2;
71
72   // Horizontally align the children to the middle of the space they are given too
73   LayoutLength width = right - left;
74   int count = GetChildCount();
75   LayoutLength childIncrement = width / count;
76   LayoutLength center = childIncrement / 2;
77
78   // Check layout direction
79   auto owner = GetOwner();
80   auto actor = Actor::DownCast(owner);
81   const bool isLayoutRtl = actor ? actor.GetProperty< bool >( Actor::Property::LAYOUT_DIRECTION ) : false;
82
83   for( int i = 0; i < count; i++)
84   {
85     auto itemIndex = isLayoutRtl ? count - 1 - i : i; // If RTL, then layout the last item first
86
87     Dali::Toolkit::Internal::LayoutItemPtr childLayout = GetChildAt( itemIndex );
88     if( childLayout != nullptr )
89     {
90       LayoutLength childWidth = childLayout->GetMeasuredWidth();
91       LayoutLength childHeight = childLayout->GetMeasuredHeight();
92
93       childTop = middle - (childHeight / 2);
94
95       LayoutLength left = childLeft + center - childWidth / 2;
96
97       childLayout->Layout( left, childTop, left + childWidth, childTop + childHeight );
98       childLeft += childIncrement;
99     }
100   }
101 }
102
103 } // namespace Internal
104
105 } // namespace Demo