[NUI] Fix Tizen.NUI.Samples build error
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / FindChildByIDTest.cs
1 
2 using System;
3 using Tizen.NUI;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Components;
6
7 namespace Tizen.NUI.Samples
8 {
9     using tlog = Tizen.Log;
10     public class FindChildByIdTest : IExample
11     {
12         private const string tag = "NUITEST";
13         private const int numberOfChildren = 10;
14         private Window win;
15         private View root, parent1, parent2;
16         private TextLabel[] childList1, childList2;
17         private Button button1, button2;
18         private Random rand;
19         private uint removeTargetID1, removeTargetID2;
20         private Layer layer1, layer2;
21         private uint layer1ID, layer2ID;
22
23         public void Activate()
24         {
25             rand = new Random();
26             win = NUIApplication.GetDefaultWindow();
27             win.BackgroundColor = Color.Green;
28
29             root = new View()
30             {
31                 Size = new Size(1000, 1000),
32                 BackgroundColor = Color.Yellow,
33                 PositionUsesPivotPoint = true,
34                 ParentOrigin = ParentOrigin.Center,
35                 PivotPoint = PivotPoint.Center,
36             };
37             root.TouchEvent += OnRootTouchEvent;
38             root.Name = "root";
39             win.Add(root);
40
41             makeParentsAndChildrenToAdd(root);
42
43             makeButtons(root);
44
45             layer1 = new Layer();
46             layer1.Name = "layerTest1";
47             win.AddLayer(layer1);
48             layer1ID = layer1.ID;
49
50             layer2 = new Layer();
51             layer2.Name = "layerTest2";
52             win.AddLayer(layer2);
53             layer2ID = layer2.ID;
54         }
55
56         private void Button_Clicked(object sender, ClickedEventArgs e)
57         {
58             TextLabel currentChild;
59             bool isCurrentParent1 = (sender as Button) == button1;
60             View currentParent = isCurrentParent1 ? parent1 : parent2;
61             if (isCurrentParent1)
62             {
63                 currentChild = currentParent.FindDescendantByID(removeTargetID1) as TextLabel;
64             }
65             else
66             {
67                 currentChild = currentParent.FindDescendantByID(removeTargetID2) as TextLabel;
68             }
69
70             if (currentChild != null && currentChild.Name.Contains("ID"))
71             {
72                 tlog.Debug(tag, $"found child : Name={currentChild.Name}, Id={currentChild.Id}, ID={currentChild.ID} and unparent the child!");
73                 currentChild.Unparent();
74
75                 if (isCurrentParent1)
76                 {
77                     removeTargetID1 = currentParent.GetChildAt(0).ID;
78                 }
79                 else
80                 {
81                     removeTargetID2 = currentParent.GetChildAt(0).ID;
82                 }
83             }
84             else
85             {
86                 tlog.Debug(tag, $"couldn't find the child of ID {(isCurrentParent1 ? removeTargetID1 : removeTargetID2)}, or this is not a target of this test!");
87             }
88
89             currentChild = new TextLabel();
90             currentChild.Size = new Size(100, 100);
91             currentChild.PositionUsesPivotPoint = true;
92             currentChild.ParentOrigin = ParentOrigin.TopLeft;
93             currentChild.PivotPoint = PivotPoint.Center;
94             currentChild.BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 0.8f);
95             currentChild.Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
96             currentChild.Name = currentChild.Text = $"ID-{currentChild.ID}";
97             currentChild.TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
98             currentParent.Add(currentChild);
99             tlog.Debug(tag, $"Add new child.ID={currentChild.ID}, Id={currentChild.Id}, Name={currentChild.Name}");
100         }
101
102         private bool OnRootTouchEvent(object source, View.TouchEventArgs e)
103         {
104             if (e.Touch.GetState(0) == PointStateType.Down)
105             {
106                 printChildrenIDs(parent1);
107                 printChildrenIDs(parent2);
108             }
109             return false;
110         }
111
112         private void printChildrenIDs(View parent)
113         {
114             uint numChild = parent.ChildCount;
115             tlog.Debug(tag, $"=============================================");
116             tlog.Debug(tag, $"parent: Name={parent.Name}, Id={parent.Id}, ID={parent.ID}");
117             for (int i = (int)numChild - 1; i >= 0; i--)
118             {
119                 View child = parent.GetChildAt((uint)i);
120                 tlog.Debug(tag, $" + child index[{i}]: Name={child.Name}, Id={child.Id}, ID={child.ID}");
121             }
122             tlog.Debug(tag, $"=============================================");
123         }
124
125         private void makeButtons(View root)
126         {
127             button1 = new Button();
128             button1.Text = "Remove & Add in parent1";
129             button1.Size = new Size(200, 200);
130             button1.BackgroundColor = Color.Cyan;
131             button1.Position = new Position(10, 600);
132             root.Add(button1);
133             button1.Clicked += Button_Clicked;
134
135             button2 = new Button();
136             button2.Text = "Remove & Add in parent2";
137             button2.Size = new Size(200, 200);
138             button2.BackgroundColor = Color.Cyan;
139             button2.Position = new Position(510, 600);
140             root.Add(button2);
141             button2.Clicked += Button_Clicked;
142         }
143
144         private void makeParentsAndChildrenToAdd(View root)
145         {
146             parent1 = new View()
147             {
148                 Size = new Size(500, 500),
149                 Position = new Position(0, 10),
150                 BackgroundColor = Color.Blue,
151                 Name = "parent1",
152             };
153             root.Add(parent1);
154
155             childList1 = new TextLabel[numberOfChildren];
156
157             for (int i = 0; i < numberOfChildren; i++)
158             {
159                 childList1[i] = new TextLabel();
160                 childList1[i].Size = new Size(100, 100);
161                 childList1[i].PositionUsesPivotPoint = true;
162                 childList1[i].ParentOrigin = ParentOrigin.TopLeft;
163                 childList1[i].PivotPoint = PivotPoint.Center;
164                 childList1[i].BackgroundColor = new Color(0.1f, 0.1f, (float)rand.NextDouble(), 0.8f);
165                 childList1[i].Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
166                 childList1[i].Name = childList1[i].Text = $"ID={childList1[i].ID}";
167                 childList1[i].TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
168                 parent1.Add(childList1[i]);
169             }
170             removeTargetID1 = childList1[0].ID;
171
172             tlog.Debug(tag, $" \n");
173             tlog.Debug(tag, $" parent1 Id={parent1.Id} ID={parent1.ID}");
174             for (int i = 0; i < numberOfChildren; i++)
175             {
176                 tlog.Debug(tag, $" child1({childList1[i].Text}) Id={childList1[i].Id}, ID={childList1[i].ID}");
177             }
178
179             parent2 = new View()
180             {
181                 Size = new Size(500, 500),
182                 Position = new Position(500, 10),
183                 BackgroundColor = Color.Red,
184                 Name = "parent2",
185             };
186             root.Add(parent2);
187
188             childList2 = new TextLabel[numberOfChildren];
189
190             for (int i = 0; i < numberOfChildren; i++)
191             {
192                 childList2[i] = new TextLabel();
193                 childList2[i].Size = new Size(100, 100);
194                 childList2[i].PositionUsesPivotPoint = true;
195                 childList2[i].ParentOrigin = ParentOrigin.TopLeft;
196                 childList2[i].PivotPoint = PivotPoint.Center;
197                 childList2[i].BackgroundColor = new Color((float)rand.NextDouble(), 0.1f, 0.1f, 1.0f);
198                 childList2[i].Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
199                 childList2[i].Name = childList2[i].Text = $"ID={childList2[i].ID}";
200                 childList2[i].TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
201                 parent2.Add(childList2[i]);
202             }
203             removeTargetID2 = childList2[0].ID;
204
205             tlog.Debug(tag, $" \n");
206             tlog.Debug(tag, $" parent2 Id={parent2.Id} ID={parent2.ID}");
207             for (int i = 0; i < numberOfChildren; i++)
208             {
209                 tlog.Debug(tag, $" child2({childList2[i].Text}) Id={childList2[i].Id}, ID={childList2[i].ID}");
210             }
211         }
212         
213         bool toggle = false;
214         private void WindowTouchEvent(object sender, Window.TouchEventArgs e)
215         {
216             if (e.Touch.GetState(0) == PointStateType.Down)
217             {
218                 tlog.Debug(tag, $"======================");
219                 var ret = checkTest() ? "PASS" : "FAIL";
220                 tlog.Debug(tag, $"test result={ret}");
221             }
222         }
223
224         private void WindowKeyEvent(object sender, Window.KeyEventArgs e)
225         {
226             if (e.Key.State == Key.StateType.Down)
227             {
228                 if (e.Key.KeyPressedName == "Up")
229                 {
230                     tlog.Debug(tag, $"======================");
231                     var ret = checkTest() ? "PASS" : "FAIL";
232                     tlog.Debug(tag, $"test result={ret}");
233                 }
234             }
235         }
236
237         bool checkTest()
238         {
239             bool ret = true;
240             toggle = !toggle;
241
242             if (toggle)
243             {
244                 var gotten = win.FindLayerByID(layer1ID);
245                 if (gotten)
246                 {
247                     if (layer1ID == gotten.ID)
248                     {
249                         tlog.Debug(tag, $"Test#1: FindLayerByID({gotten.ID}) OK");
250                     }
251                     else
252                     {
253                         tlog.Debug(tag, $"Test#1: FindLayerByID({gotten.ID}) ERROR");
254                         ret = false;
255                     }
256                 }
257                 else
258                 {
259                     tlog.Debug(tag, $"Test#1: FindLayerByID() ERROR, gotten Layer is NULL!");
260                     ret = false;
261                 }
262             }
263             else
264             {
265                 var gotten = win.FindLayerByID(layer2ID);
266                 if (gotten)
267                 {
268                     if (layer2ID == gotten.ID)
269                     {
270                         tlog.Debug(tag, $"Test#2: FindLayerByID({gotten.ID}) OK");
271                     }
272                     else
273                     {
274                         tlog.Debug(tag, $"Test#2: FindLayerByID({gotten.ID}) ERROR");
275                         ret = false;
276                     }
277                 }
278                 else
279                 {
280                     tlog.Debug(tag, $"Test#2: FindLayerByID() ERROR, gotten Layer is NULL!");
281                     ret = false;
282                 }
283             }
284             return ret;
285         }
286
287         public void Deactivate()
288         {
289             for (int i = (int)(parent1.ChildCount - 1); i >= 0; i--)
290             {
291                 View child = parent1.GetChildAt((uint)i);
292                 child.Unparent();
293                 child.Dispose();
294             }
295             parent1.Unparent();
296             parent1.Dispose();
297
298             for (int i = (int)(parent2.ChildCount - 1); i >= 0; i--)
299             {
300                 View child = parent2.GetChildAt((uint)i);
301                 child.Unparent();
302                 child.Dispose();
303             }
304             parent2.Unparent();
305             parent2.Dispose();
306
307             root.Unparent();
308             root.Dispose();
309         }
310     }
311 }