[NUI] Correct the meaning of ExcludeLayouting. (#2257)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Layouting / AbsoluteLayout.cs
1 /* Copyright (c) 2020 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16 using Tizen.NUI.BaseComponents;
17
18 namespace Tizen.NUI
19 {
20     /// <summary>
21     /// [Draft] This class implements a absolute layout, allowing explicit positioning of children.
22     ///  Positions are from the top left of the layout and can be set using the View.Position and alike.
23     /// </summary>
24     public class AbsoluteLayout : LayoutGroup
25     {
26         /// <summary>
27         /// [Draft] Constructor
28         /// </summary>
29         /// <since_tizen> 6 </since_tizen>
30         public AbsoluteLayout()
31         {
32         }
33
34         /// <summary>
35         /// Measure the layout and its content to determine the measured width and the measured height.<br />
36         /// </summary>
37         /// <param name="widthMeasureSpec">horizontal space requirements as imposed by the parent.</param>
38         /// <param name="heightMeasureSpec">vertical space requirements as imposed by the parent.</param>
39         /// <since_tizen> 6 </since_tizen>
40         protected override void OnMeasure(MeasureSpecification widthMeasureSpec, MeasureSpecification heightMeasureSpec)
41         {
42             // Ensure layout respects it's given minimum size
43             float maxWidth = SuggestedMinimumWidth.AsDecimal();
44             float maxHeight = SuggestedMinimumHeight.AsDecimal();
45
46             MeasuredSize.StateType childWidthState = MeasuredSize.StateType.MeasuredSizeOK;
47             MeasuredSize.StateType childHeightState = MeasuredSize.StateType.MeasuredSizeOK;
48
49             for (int i = 0; i < LayoutChildren.Count; i++)
50             {
51                 LayoutItem childLayout = LayoutChildren[i];
52                 if (childLayout != null)
53                 {
54                     // Get size of child with no padding, no margin. we won't support margin, padding for AbsolutLayout.
55                     MeasureChildWithoutPadding(childLayout, widthMeasureSpec, heightMeasureSpec);
56
57                     if (childLayout.Owner.ExcludeLayouting)
58                     {
59                         continue;
60                     }
61
62                     // Determine the width and height needed by the children using their given position and size.
63                     // Children could overlap so find the right most child.
64                     Position2D childPosition = childLayout.Owner.Position2D;
65                     float childRight = childLayout.MeasuredWidth.Size.AsDecimal() + childPosition.X;
66                     float childBottom = childLayout.MeasuredHeight.Size.AsDecimal() + childPosition.Y;
67
68                     if (maxWidth < childRight)
69                         maxWidth = childRight;
70
71                     if (maxHeight < childBottom)
72                         maxHeight = childBottom;
73
74                     if (childLayout.MeasuredWidth.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
75                     {
76                         childWidthState = MeasuredSize.StateType.MeasuredSizeTooSmall;
77                     }
78                     if (childLayout.MeasuredHeight.State == MeasuredSize.StateType.MeasuredSizeTooSmall)
79                     {
80                         childHeightState = MeasuredSize.StateType.MeasuredSizeTooSmall;
81                     }
82                 }
83             }
84
85             SetMeasuredDimensions(ResolveSizeAndState(new LayoutLength(maxWidth), widthMeasureSpec, childWidthState),
86                                   ResolveSizeAndState(new LayoutLength(maxHeight), heightMeasureSpec, childHeightState));
87         }
88
89         /// <summary>
90         /// Assign a size and position to each of its children.<br />
91         /// </summary>
92         /// <param name="changed">This is a new size or position for this layout.</param>
93         /// <param name="left">Left position, relative to parent.</param>
94         /// <param name="top"> Top position, relative to parent.</param>
95         /// <param name="right">Right position, relative to parent.</param>
96         /// <param name="bottom">Bottom position, relative to parent.</param>
97         /// <since_tizen> 6 </since_tizen>
98         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
99         {
100             // Absolute layout positions it's children at their Actor positions.
101             // Children could overlap or spill outside the parent, as is the nature of absolute positions.
102             for (int i = 0; i < LayoutChildren.Count; i++)
103             {
104                 LayoutItem childLayout = LayoutChildren[i];
105                 if (childLayout != null)
106                 {
107                     LayoutLength childWidth = childLayout.MeasuredWidth.Size;
108                     LayoutLength childHeight = childLayout.MeasuredHeight.Size;
109
110                     Position2D childPosition = childLayout.Owner.Position2D;
111
112                     LayoutLength childLeft = new LayoutLength(childPosition.X);
113                     LayoutLength childTop = new LayoutLength(childPosition.Y);
114
115                     childLayout.Layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight, true);
116                 }
117             }
118         }
119     }
120
121 } // namespace