[NUI] Add DoActionExtension to set dynamic property (#4467)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / LottieAnimationViewDynamicPropertyTest.cs
1
2 using Tizen.NUI.BaseComponents;
3 using Tizen.NUI;
4 using System.Collections.Generic;
5
6 namespace Tizen.NUI.Samples
7 {
8     using tlog = Tizen.Log;
9     public class LottieAnimationViewDynamicPropertyTest : IExample
10     {
11
12         const int NUM_OF_VIEW = 5;
13         const int TIMER_INTERVAL = 3000;
14         const string tag = "NUITEST";
15         Window win;
16         View root;
17         Timer timer;
18         public void Activate()
19         {
20             win = NUIApplication.GetDefaultWindow();
21
22             root = new View()
23             {
24                 Size = new Size(win.Size.Width, win.Size.Height, 0),
25                 BackgroundColor = Color.Yellow,
26                 Layout = new LinearLayout()
27                 {
28                     LinearOrientation = LinearLayout.Orientation.Horizontal,
29                 },
30             };
31             win.Add(root);
32
33             timer = new Timer(TIMER_INTERVAL);
34             timer.Tick += OnTick;
35             timer.Start();
36         }
37
38         int cnt;
39         bool OnTick(object sender, Timer.TickEventArgs e)
40         {
41             bool ret = false;
42             //ret = Test1();
43             //ret = Test2();
44             ret = Test3();
45             return ret;
46         }
47
48         //create objects => explicit dispose => create objects => implicit dispose
49         bool Test1()
50         {
51             switch (cnt % 4)
52             {
53                 case 0:
54                     MakeAll();
55                     break;
56                 case 1:
57                     DisposeAll();
58                     break;
59                 case 2:
60                     MakeAll();
61                     break;
62                 case 3:
63                     ImplicitDispose();
64                     break;
65                 default:
66                     DisposeAll();
67                     break;
68             }
69             cnt++;
70             return true;
71         }
72
73         //create objects => implicit dispose => force full GC
74         bool Test2()
75         {
76             switch (cnt % 3)
77             {
78                 case 0:
79                     MakeAll();
80                     break;
81                 case 1:
82                     ImplicitDispose();
83                     break;
84                 case 2:
85                     ForceFullGC();
86                     break;
87                 default:
88                     DisposeAll();
89                     break;
90             }
91             cnt++;
92             return true;
93         }
94
95         global::System.Random rand = new global::System.Random();
96         bool Test3()
97         {
98             var lav = new LottieAnimationView();
99             lav.Size2D = new Size2D(300, 300);
100             lav.Position2D = new Position2D(rand.Next(10, 1000), rand.Next(10, 1000));
101             if (cnt++ % 2 == 0)
102             {
103                 lav.URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "a.json";
104             }
105             else
106             {
107                 lav.URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "done.json";
108             }
109             lav.LoopCount = -1;
110             lav.BackgroundColor = Color.White;
111             win.Add(lav);
112             lav.Play();
113
114             var ret = lav.GetContentInfo();
115             tlog.Fatal(tag, $"ret.Count {ret.Count}");
116             foreach (var item in ret)
117             {
118                 tlog.Fatal(tag, $"item:({item.Item1}, {item.Item2}, {item.Item3})");
119             }
120             return true;
121         }
122
123         void ForceFullGC()
124         {
125             tlog.Debug(tag, "ForceFullGC start");
126             global::System.GC.Collect();
127             global::System.GC.WaitForPendingFinalizers();
128             global::System.GC.Collect();
129             tlog.Debug(tag, "ForceFullGC end");
130         }
131
132         void MakeAll()
133         {
134             tlog.Debug(tag, $"MakeAll() start");
135             int width = (int)(root.Size.Width / NUM_OF_VIEW);
136             for (int i = 0; i < NUM_OF_VIEW; i++)
137             {
138                 var lav = new LottieAnimationView();
139                 lav.Size2D = new Size2D(width, width);
140                 lav.URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "done.json";
141                 lav.LoopCount = -1;
142                 lav.BackgroundColor = Color.White;
143                 root.Add(lav);
144
145                 LottieAnimationViewDynamicProperty pro = new LottieAnimationViewDynamicProperty
146                 {
147                     KeyPath = "Shape Layer 1.Ellipse 1.Fill 1",
148                     Property = LottieAnimationView.VectorProperty.FillColor,
149                     Callback = new Tizen.NUI.BaseComponents.LottieAnimationView.DynamicPropertyCallbackType(OnFillColor),
150                 };
151
152                 if (i % 1 == 0)
153                 {
154                     lav.DoActionExtension(pro);
155                 }
156
157                 if (i % 2 == 0)
158                 {
159                     pro.KeyPath = "**";
160                     pro.Property = LottieAnimationView.VectorProperty.StrokeColor;
161                     pro.Callback = new Tizen.NUI.BaseComponents.LottieAnimationView.DynamicPropertyCallbackType(OnStrokColor);
162                     lav.DoActionExtension(pro);
163                 }
164
165                 if (i % 3 == 0)
166                 {
167                     pro.KeyPath = "**";
168                     pro.Property = LottieAnimationView.VectorProperty.StrokeWidth;
169                     pro.Callback = new Tizen.NUI.BaseComponents.LottieAnimationView.DynamicPropertyCallbackType(OnStrokWidth);
170                     lav.DoActionExtension(pro);
171                 }
172
173                 if (i % 4 == 0)
174                 {
175                     pro.KeyPath = "Shape Layer 2.Shape 1";
176                     pro.Property = LottieAnimationView.VectorProperty.TransformRotation;
177                     pro.Callback = new Tizen.NUI.BaseComponents.LottieAnimationView.DynamicPropertyCallbackType(OnTransformRotation);
178                     lav.DoActionExtension(pro);
179                 }
180                 lav.Play();
181             }
182             tlog.Debug(tag, $"MakeAll() end");
183         }
184
185         void DisposeAll()
186         {
187             tlog.Debug(tag, $"DisposeAll() start");
188             int childNum = (int)root.ChildCount;
189             for (int i = childNum - 1; i >= 0; i--)
190             {
191                 var child = root.GetChildAt((uint)i);
192                 if (child != null)
193                 {
194                     child.Unparent();
195                     child.Dispose();
196                 }
197             }
198             tlog.Debug(tag, $"DisposeAll() end");
199         }
200
201         void ImplicitDispose()
202         {
203             tlog.Debug(tag, $"ImplicitDispose() start");
204             int childNum = (int)root.ChildCount;
205             for (int i = childNum - 1; i >= 0; i--)
206             {
207                 var child = root.GetChildAt((uint)i);
208                 if (child != null)
209                 {
210                     child.Unparent();
211                 }
212             }
213             tlog.Debug(tag, $"ImplicitDispose() end");
214         }
215
216         private PropertyValue OnFillColor(int returnType, uint frameNumber)
217         {
218             tlog.Debug(tag, $"OnFillColor() returnType={returnType} frameNumber={frameNumber}");
219             if (frameNumber < 60)
220             {
221                 return new PropertyValue(new Vector3(0, 0, 1));
222             }
223             else
224             {
225                 return new PropertyValue(new Vector3(1, 0, 0));
226             }
227         }
228
229         private PropertyValue OnStrokColor(int returnType, uint frameNumber)
230         {
231             tlog.Debug(tag, $"OnStrokColor() returnType={returnType} frameNumber={frameNumber}");
232             if (frameNumber < 60)
233             {
234                 return new PropertyValue(new Vector3(1, 0, 1));
235             }
236             else
237             {
238                 return new PropertyValue(new Vector3(1, 1, 0));
239             }
240         }
241
242         private PropertyValue OnStrokWidth(int returnType, uint frameNumber)
243         {
244             tlog.Debug(tag, $"OnStrokWidth() returnType={returnType} frameNumber={frameNumber}");
245
246             if (frameNumber < 60)
247             {
248                 return new PropertyValue(2.0f);
249             }
250             else
251             {
252                 return new PropertyValue(5.0f);
253             }
254         }
255
256         private PropertyValue OnTransformRotation(int returnType, uint frameNumber)
257         {
258             tlog.Debug(tag, $"OnTransformRotation() returnType={returnType} frameNumber={frameNumber}");
259
260             return new PropertyValue(frameNumber * 20.0f);
261         }
262
263         public void Deactivate()
264         {
265             root.Unparent();
266             timer.Stop();
267             DisposeAll();
268             root.Dispose();
269         }
270     }
271 }