Revert "[NUI] Dialog and AlertDialog code refactoring with adding DialogPage"
[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
21         public void Activate()
22         {
23             rand = new Random();
24             win = NUIApplication.GetDefaultWindow();
25             win.BackgroundColor = Color.Green;
26
27             root = new View()
28             {
29                 Size = new Size(1000, 1000),
30                 BackgroundColor = Color.Yellow,
31                 PositionUsesPivotPoint = true,
32                 ParentOrigin = ParentOrigin.Center,
33                 PivotPoint = PivotPoint.Center,
34             };
35             root.TouchEvent += OnRootTouchEvent;
36             root.Name = "root";
37             win.Add(root);
38
39             makeParentsAndChildrenToAdd(root);
40
41             makeButtons(root);
42         }
43
44         private void Button_Clicked(object sender, ClickedEventArgs e)
45         {
46             TextLabel currentChild;
47             bool isCurrentParent1 = (sender as Button) == button1;
48             View currentParent = isCurrentParent1 ? parent1 : parent2;
49             if (isCurrentParent1)
50             {
51                 currentChild = currentParent.FindChildByID(removeTargetID1) as TextLabel;
52             }
53             else
54             {
55                 currentChild = currentParent.FindChildByID(removeTargetID2) as TextLabel;
56             }
57
58             if (currentChild != null && currentChild.Name.Contains("ID"))
59             {
60                 tlog.Debug(tag, $"found child : Name={currentChild.Name}, Id={currentChild.Id}, ID={currentChild.ID} and unparent the child!");
61                 currentChild.Unparent();
62
63                 if (isCurrentParent1)
64                 {
65                     removeTargetID1 = currentParent.GetChildAt(0).ID;
66                 }
67                 else
68                 {
69                     removeTargetID2 = currentParent.GetChildAt(0).ID;
70                 }
71             }
72             else
73             {
74                 tlog.Debug(tag, $"couldn't find the child of ID {(isCurrentParent1 ? removeTargetID1 : removeTargetID2)}, or this is not a target of this test!");
75             }
76
77             currentChild = new TextLabel();
78             currentChild.Size = new Size(100, 100);
79             currentChild.PositionUsesPivotPoint = true;
80             currentChild.ParentOrigin = ParentOrigin.TopLeft;
81             currentChild.PivotPoint = PivotPoint.Center;
82             currentChild.BackgroundColor = new Color((float)rand.NextDouble(), (float)rand.NextDouble(), (float)rand.NextDouble(), 0.8f);
83             currentChild.Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
84             currentChild.Name = currentChild.Text = $"ID-{currentChild.ID}";
85             currentChild.TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
86             currentParent.Add(currentChild);
87             tlog.Debug(tag, $"Add new child.ID={currentChild.ID}, Id={currentChild.Id}, Name={currentChild.Name}");
88         }
89
90         private bool OnRootTouchEvent(object source, View.TouchEventArgs e)
91         {
92             if (e.Touch.GetState(0) == PointStateType.Down)
93             {
94                 printChildrenIDs(parent1);
95                 printChildrenIDs(parent2);
96             }
97             return false;
98         }
99
100         private void printChildrenIDs(View parent)
101         {
102             uint numChild = parent.ChildCount;
103             tlog.Debug(tag, $"=============================================");
104             tlog.Debug(tag, $"parent: Name={parent.Name}, Id={parent.Id}, ID={parent.ID}");
105             for (int i = (int)numChild - 1; i >= 0; i--)
106             {
107                 View child = parent.GetChildAt((uint)i);
108                 tlog.Debug(tag, $" + child index[{i}]: Name={child.Name}, Id={child.Id}, ID={child.ID}");
109             }
110             tlog.Debug(tag, $"=============================================");
111         }
112
113         private void makeButtons(View root)
114         {
115             button1 = new Button();
116             button1.Text = "Remove & Add in parent1";
117             button1.Size = new Size(200, 200);
118             button1.BackgroundColor = Color.Cyan;
119             button1.Position = new Position(10, 600);
120             root.Add(button1);
121             button1.Clicked += Button_Clicked;
122
123             button2 = new Button();
124             button2.Text = "Remove & Add in parent2";
125             button2.Size = new Size(200, 200);
126             button2.BackgroundColor = Color.Cyan;
127             button2.Position = new Position(510, 600);
128             root.Add(button2);
129             button2.Clicked += Button_Clicked;
130         }
131
132         private void makeParentsAndChildrenToAdd(View root)
133         {
134             parent1 = new View()
135             {
136                 Size = new Size(500, 500),
137                 Position = new Position(0, 10),
138                 BackgroundColor = Color.Blue,
139                 Name = "parent1",
140             };
141             root.Add(parent1);
142
143             childList1 = new TextLabel[numberOfChildren];
144
145             for (int i = 0; i < numberOfChildren; i++)
146             {
147                 childList1[i] = new TextLabel();
148                 childList1[i].Size = new Size(100, 100);
149                 childList1[i].PositionUsesPivotPoint = true;
150                 childList1[i].ParentOrigin = ParentOrigin.TopLeft;
151                 childList1[i].PivotPoint = PivotPoint.Center;
152                 childList1[i].BackgroundColor = new Color(0.1f, 0.1f, (float)rand.NextDouble(), 0.8f);
153                 childList1[i].Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
154                 childList1[i].Name = childList1[i].Text = $"ID={childList1[i].ID}";
155                 childList1[i].TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
156                 parent1.Add(childList1[i]);
157             }
158             removeTargetID1 = childList1[0].ID;
159
160             tlog.Debug(tag, $" \n");
161             tlog.Debug(tag, $" parent1 Id={parent1.Id} ID={parent1.ID}");
162             for (int i = 0; i < numberOfChildren; i++)
163             {
164                 tlog.Debug(tag, $" child1({childList1[i].Text}) Id={childList1[i].Id}, ID={childList1[i].ID}");
165             }
166
167             parent2 = new View()
168             {
169                 Size = new Size(500, 500),
170                 Position = new Position(500, 10),
171                 BackgroundColor = Color.Red,
172                 Name = "parent2",
173             };
174             root.Add(parent2);
175
176             childList2 = new TextLabel[numberOfChildren];
177
178             for (int i = 0; i < numberOfChildren; i++)
179             {
180                 childList2[i] = new TextLabel();
181                 childList2[i].Size = new Size(100, 100);
182                 childList2[i].PositionUsesPivotPoint = true;
183                 childList2[i].ParentOrigin = ParentOrigin.TopLeft;
184                 childList2[i].PivotPoint = PivotPoint.Center;
185                 childList2[i].BackgroundColor = new Color((float)rand.NextDouble(), 0.1f, 0.1f, 1.0f);
186                 childList2[i].Position = new Position(rand.Next(100, 400), rand.Next(100, 400));
187                 childList2[i].Name = childList2[i].Text = $"ID={childList2[i].ID}";
188                 childList2[i].TextFit = new PropertyMap().Add("enable", new PropertyValue(true)).Add("minSize", new PropertyValue(5.0f)).Add("maxSize", new PropertyValue(50.0f));
189                 parent2.Add(childList2[i]);
190             }
191             removeTargetID2 = childList2[0].ID;
192
193             tlog.Debug(tag, $" \n");
194             tlog.Debug(tag, $" parent2 Id={parent2.Id} ID={parent2.ID}");
195             for (int i = 0; i < numberOfChildren; i++)
196             {
197                 tlog.Debug(tag, $" child2({childList2[i].Text}) Id={childList2[i].Id}, ID={childList2[i].ID}");
198             }
199         }
200
201         public void Deactivate()
202         {
203             for (int i = (int)(parent1.ChildCount - 1); i >= 0; i--)
204             {
205                 View child = parent1.GetChildAt((uint)i);
206                 child.Unparent();
207                 child.Dispose();
208             }
209             parent1.Unparent();
210             parent1.Dispose();
211
212             for (int i = (int)(parent2.ChildCount - 1); i >= 0; i--)
213             {
214                 View child = parent2.GetChildAt((uint)i);
215                 child.Unparent();
216                 child.Dispose();
217             }
218             parent2.Unparent();
219             parent2.Dispose();
220
221             root.Unparent();
222             root.Dispose();
223         }
224     }
225 }