custom-layout-impl.cpp
3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright (c) 2018 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// CLASS HEADER
#include "custom-layout-impl.h"
// EXTERNAL INCLUDES
#include <dali/public-api/actors/actor.h>
namespace Demo
{
namespace Internal
{
using Dali::Actor;
using Dali::Toolkit::MeasuredSize;
CustomLayoutPtr CustomLayout::New()
{
CustomLayoutPtr layout( new CustomLayout() );
return layout;
}
void CustomLayout::OnMeasure( MeasureSpec widthMeasureSpec, MeasureSpec heightMeasureSpec )
{
LayoutLength accumulatedWidth = 0;
LayoutLength maxHeight = 0;
// In this layout we will:
// Measuring the layout with the children in a horizontal configuration, one after another
// Set the required width to be the accumulated width of our children
// Set the required height to be the maximum height of any of our children
for( unsigned int i=0; i<GetChildCount(); ++i )
{
auto childLayout = GetChildAt( i );
if( childLayout )
{
MeasureChild( childLayout, widthMeasureSpec, heightMeasureSpec );
accumulatedWidth += childLayout->GetMeasuredWidth();
std::max( childLayout->GetMeasuredHeight(), maxHeight );
}
}
// Finally, call this method to set the dimensions we would like
SetMeasuredDimensions( MeasuredSize( accumulatedWidth ), MeasuredSize( maxHeight ) );
}
void CustomLayout::OnLayout( bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom )
{
LayoutLength childTop( 0 );
LayoutLength childLeft( 0 );
// We want to vertically align the children to the middle
LayoutLength middle = (bottom - top) / 2;
// Horizontally align the children to the middle of the space they are given too
LayoutLength width = right - left;
int count = GetChildCount();
LayoutLength childIncrement = width / count;
LayoutLength center = childIncrement / 2;
// Check layout direction
auto owner = GetOwner();
auto actor = Actor::DownCast(owner);
const bool isLayoutRtl = actor ? actor.GetProperty< bool >( Actor::Property::LAYOUT_DIRECTION ) : false;
for( int i = 0; i < count; i++)
{
auto itemIndex = isLayoutRtl ? count - 1 - i : i; // If RTL, then layout the last item first
Dali::Toolkit::Internal::LayoutItemPtr childLayout = GetChildAt( itemIndex );
if( childLayout != nullptr )
{
LayoutLength childWidth = childLayout->GetMeasuredWidth();
LayoutLength childHeight = childLayout->GetMeasuredHeight();
childTop = middle - childHeight / 2;
LayoutLength left = childLeft + center - childWidth / 2;
childLayout->Layout( left, childTop, left + childWidth, childTop + childHeight );
childLeft += childIncrement;
}
}
}
} // namespace Internal
} // namespace Demo