Merge remote-tracking branch 'origin/master' into tizen
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / SiblingOrderTest.cs
1 
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI.Components;
4 using System;
5
6 namespace Tizen.NUI.Samples
7 {
8     using l = Tizen.Log;
9
10     public class SiblingOrderTest : IExample
11     {
12         Window win;
13         View v;
14         Button b1, b2, b3;
15         TextLabel text;
16         const string t = "NUITEST";
17         const int W1 = 700, H1 = 800;
18         const int W2 = 230, H2 = 150;
19         public void Activate()
20         {
21             win = NUIApplication.GetDefaultWindow();
22
23             v = new View();
24             v.Size = new Size(W1, H1);
25             v.Position = new Position(10, 10);
26             v.BackgroundColor = new Color(0.8f, 0.8f, 0.8f, 1.0f);
27             win.Add(v);
28
29             b1 = new Button()
30             {
31                 Size = new Size(700, 100),
32                 Position = new Position(10, H1 + 20),
33                 Text = "add new child",
34             };
35             b1.Clicked += B1_Clicked;
36             win.Add(b1);
37
38             b2 = new Button()
39             {
40                 Size = new Size(700, 100),
41                 Position = new Position(10, H1 + 20 + 120),
42                 Text = "refresh sibling order",
43             };
44             b2.Clicked += B2_Clicked;
45             win.Add(b2);
46
47             b3 = new Button()
48             {
49                 Size = new Size(700, 100),
50                 Position = new Position(10, H1 + 20 + 120 + 120),
51                 Text = "place children to fit parent",
52             };
53             b3.Clicked += B3_Clicked;
54             win.Add(b3);
55
56             text = new TextLabel()
57             {
58                 Size = new Size(700, 100),
59                 Position = new Position(10, H1 + 20 + 120 + 120 + 120),
60                 Text = "push buttons above",
61                 BackgroundColor = Color.Yellow,
62                 Name = "status",
63             };
64             win.Add(text);
65         }
66
67         private void B1_Clicked(object sender, ClickedEventArgs e)
68         {
69             string n = "CH" + (v.ChildCount + 1);
70             var ch = new TextLabel();
71             Random r = new Random();
72             int so = r.Next(0, (int)v.ChildCount);
73
74             ch.Size = new Size(W2, H2);
75             ch.Position = new Position(100, 900);
76             ch.BackgroundColor = new Color((float)r.NextDouble(), (float)r.NextDouble(), (float)r.NextDouble(), 0.5f);
77             ch.MultiLine = true;
78             ch.Name = n;
79             v.Add(ch);
80             // SiblingOrder can set the order of mine in the sibling list owned by parent.
81             // the lower value of SiblingOrder means it goes lower to the bottom so that it will be covered by higher valued siblings. 
82             // (Vise versa, if SiblingOrder goes to high, the object will be up to the top which can be said as a screen.)
83             //if new added sibling has same value with previously existed sibling, the existed siblings, which have higer value than new sibling, will get +1 and will be rearranged. 
84             // (this means the new added sibling will be placed right under the old sibling that has same SiblingOrder value)
85             //Therefore, SiblingOrder will be automatically set as parent's ChildCount + 1 when child itself is added to parent by "Add()".
86             ch.SiblingOrder = so;
87             ch.Text = ch.Name + "  SO=" + ch.SiblingOrder;
88
89             var a = new Animation(500);
90             var targetposition = GetPositionFromSiblingOrder(ch.SiblingOrder);
91             a.AnimateTo(ch, "position", new Position((targetposition.X + W2 / 2), (targetposition.Y + H2 / 2), 0));
92             a.Play();
93             a.Finished += A_Finished;
94         }
95
96         private void B2_Clicked(object sender, ClickedEventArgs e)
97         {
98             for(int i=0; i < v.ChildCount; i++)
99             {
100                 var ch = v.GetChildAt((uint)i) as TextLabel;
101                 if(ch != null)
102                 {
103                     ch.Text = ch.Name + "  SO=" + ch.SiblingOrder;
104                 }
105             }
106         }
107
108         private void B3_Clicked(object sender, ClickedEventArgs e)
109         {
110             for (int i = 0; i < v.ChildCount; i++)
111             {
112                 var ch = v.GetChildAt((uint)i);
113                 var ani = new Animation(500);
114                 ani.AnimateTo(ch, "position", GetPositionFromSiblingOrder(i), new AlphaFunction(AlphaFunction.BuiltinFunctions.EaseOut));
115                 ani.Play();
116             }
117         }
118
119         private void A_Finished(object sender, EventArgs e)
120         {
121             var text = win.GetDefaultLayer().FindChildByName("status") as TextLabel;
122             if(text != null)
123             {
124                 int i = (int)v.ChildCount;
125                 text.Text = $"child {i} (CH{i}) is just added!";
126             }
127         }
128
129         Position GetPositionFromSiblingOrder(int so)
130         {
131             int px = (so % (W1 / W2)) * W2;
132             int py = (so / (W1 / W2)) * H2;
133             return new Position(px, py, 0);
134         }
135
136         public void Deactivate()
137         {
138             b1.Clicked -= B1_Clicked;
139             b2.Clicked -= B2_Clicked;
140             b3.Clicked -= B3_Clicked;
141
142             b1.Unparent();
143             b2.Unparent();
144             b3.Unparent();
145             b1.Dispose();
146             b2.Dispose();
147             b3.Dispose();
148
149             text.Unparent();
150             text.Dispose();
151             text = null;
152
153             v.Unparent();
154             v.Dispose();
155             v = null;
156         }
157     }
158 }