d92d46372bd88f6c37b1d5fc7bc6043acd9c3b99
[platform/core/csapi/tizenfx.git] / test / NUITestSample / NUITestSample / examples / layouting / custom-layout-test.cs
1 using System;
2 using System.Threading;
3 using Tizen.NUI;
4 using Tizen.NUI.BaseComponents;
5 using System.Collections.Generic;
6
7 namespace CustomLayoutByAbsoluteLayout
8 {
9     static class Images
10     {
11         public static string resources = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
12         public static readonly string[] s_images = new string[]
13         {
14             resources + "images/gallery-1.jpg",
15             resources + "images/gallery-2.jpg",
16             resources + "images/gallery-3.jpg",
17             resources + "images/gallery-4.jpg",
18             resources + "images/image-1.jpg",
19             resources + "images/image-2.jpg",
20             resources + "images/image-3.jpg",
21         };
22     }
23
24     public class CustomLayoutHorizental : LayoutGroup
25     {
26         private static LayoutItem[] childLayouts = new LayoutItem[10];
27
28         public CustomLayoutHorizental()
29         {
30             Console.WriteLine($"CustomLayoutHorizental() constructor!");
31         }
32         protected override void OnMeasure(LayoutMeasureSpec widthMeasureSpec, LayoutMeasureSpec heightMeasureSpec)
33         {
34             Console.WriteLine($"CustomLayoutHorizental OnMeasure() START");
35
36             var accumulatedWidth = new LayoutLength(0);
37             var maxHeight = new LayoutLength(0);
38
39             // this is needed, otherwise the child's LayoutItem is garbage collected!
40             for (uint i = 0; i < ChildCount; ++i)
41             {
42                 childLayouts[i] = GetChildAt(i);
43             }
44
45             // In this layout we will:
46             //  Measuring the layout with the children in a horizontal configuration, one after another
47             //  Set the required width to be the accumulated width of our children
48             //  Set the required height to be the maximum height of any of our children
49             for (uint i = 0; i < ChildCount; ++i)
50             {
51                 var childLayout = childLayouts[i];
52
53                 Console.WriteLine($"child count={ChildCount}, i={i}");
54                 if (childLayout)
55                 {
56                     MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
57                     accumulatedWidth += childLayout.MeasuredWidth;
58                     maxHeight.Value = System.Math.Max(childLayout.MeasuredHeight.Value, maxHeight.Value);
59                     Console.WriteLine($"child layout is not NULL! accumulatedWidth={accumulatedWidth.Value}, i={i}");
60                 }
61             }
62             // Finally, call this method to set the dimensions we would like
63             SetMeasuredDimensions(new MeasuredSize(accumulatedWidth), new MeasuredSize(maxHeight));
64             Console.WriteLine($"CustomLayoutHorizental OnMeasure() accumlated width={accumulatedWidth.Value}, maxHeight={maxHeight.Value} END");
65         }
66         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
67         {
68             Console.WriteLine($"CustomLayoutHorizental OnLayout() START");
69
70             LayoutLength childTop = new LayoutLength(0);
71             LayoutLength childLeft = new LayoutLength(0);
72
73             // We want to vertically align the children to the middle
74             var height = bottom - top;
75             var middle = height / 2;
76
77             // Horizontally align the children to the middle of the space they are given too
78             var width = right - left;
79             uint count = ChildCount;
80             Console.WriteLine($"child count={count}");
81
82             var childIncrement = 0;
83             if (count > 0)
84             {
85                 childIncrement = width.Value / System.Convert.ToInt32(count);
86             }
87             var center = childIncrement / 2;
88
89             // Check layout direction
90             var view = GetOwner();
91             ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
92
93             // this is needed, otherwise the child's LayoutItem is garbage collected!
94             for (uint i = 0; i < ChildCount; ++i)
95             {
96                 childLayouts[i] = GetChildAt(i);
97             }
98
99             for (uint i = 0; i < count; i++)
100             {
101                 uint itemIndex;
102                 // If RTL, then layout the last item first
103                 if (layoutDirection == ViewLayoutDirectionType.RTL)
104                 {
105                     itemIndex = count - 1 - i;
106                 }
107                 else
108                 {
109                     itemIndex = i;
110                 }
111
112                 var childLayout = childLayouts[itemIndex];
113                 if (childLayout)
114                 {
115                     var childWidth = childLayout.MeasuredWidth;
116                     var childHeight = childLayout.MeasuredHeight;
117
118                     childTop = middle - (childHeight / 2);
119
120                     var leftPosition = childLeft + center - childWidth / 2;
121
122                     childLayout.Layout(leftPosition, childTop, leftPosition + childWidth, childTop + childHeight);
123                     childLeft += childIncrement;
124
125                     Console.WriteLine($"child layout is not NULL! childWidth={childWidth.Value}, i={i}");
126                 }
127             }
128             Console.WriteLine($"CustomLayoutHorizental OnLayout() END");
129         }
130     }
131
132     public class CustomLayoutVertical : LayoutGroup
133     {
134         public CustomLayoutVertical()
135         {
136             this.LayoutAnimate = true;
137         }
138
139         private static LayoutItem[] childLayouts = new LayoutItem[10];
140
141         protected override void OnMeasure(LayoutMeasureSpec widthMeasureSpec, LayoutMeasureSpec heightMeasureSpec)
142         {
143             var accumulatedHeight = new LayoutLength(0);
144             var maxWidth = new LayoutLength(0);
145
146             for (uint i = 0; i < ChildCount; ++i)
147             {
148                 childLayouts[i] = GetChildAt(i);
149             }
150
151             for (uint i = 0; i < ChildCount; ++i)
152             {
153                 var childLayout = childLayouts[i];
154                 if (childLayout)
155                 {
156                     MeasureChild(childLayout, widthMeasureSpec, heightMeasureSpec);
157                     accumulatedHeight += childLayout.MeasuredHeight;
158                     maxWidth.Value = System.Math.Max(childLayout.MeasuredWidth.Value, maxWidth.Value);
159                     Console.WriteLine($"CustomLayoutVertical child layout is not NULL! accumulatedHeight={accumulatedHeight.Value}, i={i}");
160                 }
161             }
162             SetMeasuredDimensions(new MeasuredSize(maxWidth), new MeasuredSize(accumulatedHeight));
163             Console.WriteLine($"CustomLayoutVertical OnMeasure() max width={maxWidth.Value}, accumulated Height={accumulatedHeight.Value}");
164         }
165
166         protected override void OnLayout(bool changed, LayoutLength left, LayoutLength top, LayoutLength right, LayoutLength bottom)
167         {
168             LayoutLength childTop = new LayoutLength(0);
169             LayoutLength childLeft = new LayoutLength(0);
170
171             var height = bottom - top;
172             var width = right - left;
173             var middle = width / 2;
174
175             uint count = ChildCount;
176             var childIncrement = 0;
177             if (count > 0)
178             {
179                 childIncrement = height.Value / System.Convert.ToInt32(count);
180             }
181             var center = childIncrement / 2;
182
183             var view = GetOwner();
184             ViewLayoutDirectionType layoutDirection = view.LayoutDirection;
185
186             for (uint i = 0; i < ChildCount; ++i)
187             {
188                 childLayouts[i] = GetChildAt(i);
189             }
190
191             for (uint i = 0; i < count; i++)
192             {
193                 uint itemIndex;
194                 if (layoutDirection == ViewLayoutDirectionType.RTL)
195                 {
196                     itemIndex = count - 1 - i;
197                 }
198                 else
199                 {
200                     itemIndex = i;
201                 }
202
203                 var childLayout = childLayouts[itemIndex];
204                 if (childLayout)
205                 {
206                     var childWidth = childLayout.MeasuredWidth;
207                     var childHeight = childLayout.MeasuredHeight;
208
209                     childLeft = middle - (childWidth / 2);
210
211                     var topPosition = childTop + center - childHeight / 2;
212
213                     childLayout.Layout(childLeft, topPosition, childLeft + childWidth, topPosition + childHeight);
214                     childTop += childIncrement;
215
216                     Console.WriteLine($"CustomLayoutVertical child layout is not NULL! childWidth={childWidth.Value}, i={i}");
217                 }
218             }
219             Console.WriteLine($"CustomLayoutVertical OnLayout() END");
220         }
221     }
222     
223     class Example : NUIApplication
224     {
225         public Example() : base()
226         {
227             Console.WriteLine("Example()!");
228         }
229
230         protected override void OnCreate()
231         {
232             base.OnCreate();
233             Initialize();
234         }
235
236         static View rootLayoutView, linearContainer;
237         const int MAX_CHILDREN = 7;
238         static ImageView[] imageViews = new ImageView[MAX_CHILDREN];
239         static CustomLayoutHorizental horizontalLayout;
240         static CustomLayoutVertical verticalLayout;
241         static AbsoluteLayout rootLayout;
242
243         private void Initialize()
244         {
245             Console.WriteLine("Initialize()!");
246             Window window = Window.Instance;
247             window.BackgroundColor = Color.Green;
248
249             rootLayoutView = new View();
250             rootLayoutView.WidthSpecificationFixed = 1900;
251             rootLayoutView.HeightSpecificationFixed = 1000;
252             rootLayoutView.Position = new Position(0, 0, 0);
253             rootLayoutView.BackgroundColor = Color.Magenta;
254
255             linearContainer = new View();
256             linearContainer.PositionUsesPivotPoint = true;
257             linearContainer.PivotPoint = PivotPoint.Center;
258             linearContainer.ParentOrigin = ParentOrigin.Center;
259             //linearContainer.BackgroundColor = Color.Yellow;
260             linearContainer.KeyEvent += OnKeyEvent;
261             linearContainer.Focusable = true;
262
263             for (int index = 0; index < MAX_CHILDREN - 3; index++)
264             {
265                 imageViews[index] = new ImageView(Images.s_images[index]);
266                 imageViews[index].WidthSpecificationFixed = 150;
267                 imageViews[index].HeightSpecificationFixed = 100;
268                 linearContainer.Add(imageViews[index]);
269             }
270             for (int index = MAX_CHILDREN - 3; index < MAX_CHILDREN; index++)
271             {
272                 imageViews[index] = new ImageView(Images.s_images[index]);
273                 imageViews[index].WidthSpecificationFixed = 150;
274                 imageViews[index].HeightSpecificationFixed = 100;
275                 imageViews[index].Name = "t_image" + (index - 3);
276             }
277
278             horizontalLayout = new CustomLayoutHorizental();
279             verticalLayout = new CustomLayoutVertical();
280             horizontalLayout.LayoutAnimate = true;
281             linearContainer.Layout = horizontalLayout;
282
283             rootLayout = new AbsoluteLayout();
284             rootLayoutView.Layout = rootLayout;
285
286             rootLayoutView.Add(linearContainer);
287             window.Add(rootLayoutView);
288             FocusManager.Instance.SetCurrentFocusView(linearContainer);
289             FocusManager.Instance.FocusIndicator = new View();
290         }
291
292         int cnt1 = 1;
293         private bool OnKeyEvent(object source, View.KeyEventArgs e)
294         {
295             if (e.Key.State == Key.StateType.Down)
296             {
297                 Console.WriteLine($"key pressed name={e.Key.KeyPressedName}");
298                 switch (e.Key.KeyPressedName)
299                 {
300                     case "Right":
301                         if (cnt1 < 4 && cnt1 > 0)
302                         {
303                             linearContainer.Add(imageViews[cnt1 + 3]);
304                             cnt1++;
305                         }
306                         break;
307
308                     case "Left":
309                         if (cnt1 - 1 < 4 && cnt1 - 1 > 0)
310                         {
311                             View tmp = linearContainer.FindChildByName("t_image" + (cnt1 - 1));
312                             if (tmp != null)
313                             {
314                                 linearContainer.Remove(tmp);
315                                 cnt1--;
316                             }
317                         }
318                         break;
319
320                     case "Up":
321                         linearContainer.Layout = verticalLayout;
322                         break;
323
324                     case "Down":
325                         linearContainer.Layout = horizontalLayout;
326                         break;
327
328                     case "Return":
329                         if (linearContainer.LayoutDirection == ViewLayoutDirectionType.LTR) { linearContainer.LayoutDirection = ViewLayoutDirectionType.RTL; }
330                         else { linearContainer.LayoutDirection = ViewLayoutDirectionType.LTR; }
331                         break;
332                 }
333             }
334             return true;
335         }
336     }
337 }