[TFM] Update TizenFX TFM to net6.0 (#5360)
[platform/core/csapi/tizenfx.git] / test / AIAgentLayer / AIAgentLayer.cs
1 using System;
2 using Tizen.NUI;
3 using Tizen.NUI.Components;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Extension;
6
7 namespace AIAgentLayer
8 {
9     class Program : NUIApplication
10     {
11         //Define RiveAnimation Variable as Member Variable
12         Tizen.NUI.Extension.RiveAnimationView rav;
13
14         //Control RiveAnimationState Buttons
15         Button inButton, outButton, thinkingButton, okButton, listenButton;
16
17         //Control Position Animation for RiveAnimationView
18         Animation moveAnimation;
19         AlphaFunction alphaFunction;
20         Button topButton, bottomButton, leftButton, centerButton, rightButton;
21         Button leftTopButton, rightTopButton, leftBottomButton, rightBottomButton;
22
23         //In and Out Animation Timer
24         Animation showAnimation;
25         Animation inAnimation;
26         Animation outAnimation;
27         Animation resetEyeAnimation;
28         Animation resetOkAnimation;
29         Animation resetThinkingAnimation;
30         Animation resetListenAnimation;
31
32         void resetEyeFinished(object sender, EventArgs e)
33         {
34             rav.SetAnimationElapsedTime("reset", -1.0f);
35         }
36
37         void resetOkFinished(object sender, EventArgs e)
38         {
39             rav.SetAnimationElapsedTime("thinking", -1.0f);
40             rav.SetAnimationElapsedTime("listen", -1.0f);
41         }
42
43         void resetThinkingFinished(object sender, EventArgs e)
44         {
45             rav.SetAnimationElapsedTime("ok", -1.0f);
46             rav.SetAnimationElapsedTime("listen", -1.0f);
47         }
48
49         void resetListenFinished(object sender, EventArgs e)
50         {
51             rav.SetAnimationElapsedTime("ok", -1.0f);
52             rav.SetAnimationElapsedTime("thinking", -1.0f);
53         }
54
55         void moveAnimationFinished(object sender, EventArgs e)
56         {
57             rav.SetAnimationElapsedTime("reset", 0.0f);
58             rav.SetAnimationElapsedTime("eye 360", -1.0f);
59
60             if (resetEyeAnimation)
61             {
62                 resetEyeAnimation.Stop();
63             }
64             else
65             {
66                 resetEyeAnimation = new Animation(100);
67                 resetEyeAnimation.Finished += resetEyeFinished;
68             }
69             resetEyeAnimation.Play();
70         }
71
72         void showAnimationFinished(object sender, EventArgs e)
73         {
74             if (inAnimation)
75             {
76                 inAnimation.Stop();
77             }
78             rav.SetAnimationElapsedTime("reset", -1.0f);
79             rav.SetAnimationElapsedTime("eye 360", -1.0f);
80             rav.SetAnimationElapsedTime("out", 0.0f);
81
82             rav.SetAnimationElapsedTime("thinking", 0.0f);
83             rav.SetAnimationElapsedTime("listen", 0.0f);
84             rav.SetAnimationElapsedTime("ok", 0.0f);
85             rav.EnableAnimation("thinking", false);
86             rav.EnableAnimation("listen", false);
87             rav.EnableAnimation("ok", false);
88
89             rav.EnableAnimation("out", false);
90             rav.EnableAnimation("in", true);
91
92             inAnimation.Play();
93         }
94
95         void inAnimationFinished(object sender, EventArgs e)
96         {
97             rav.SetAnimationElapsedTime("reset", -1.0f);
98             rav.SetAnimationElapsedTime("eye 360", -1.0f);
99             rav.SetAnimationElapsedTime("out", -1.0f);
100             rav.EnableAnimation("in", false);
101             rav.EnableAnimation("idle", true);
102         }
103
104         void outAnimationFinished(object sender, EventArgs e)
105         {
106             rav.SetAnimationElapsedTime("reset", 0.0f);
107             rav.SetAnimationElapsedTime("eye 360", -1.0f);
108             rav.SetAnimationElapsedTime("out", -1.0f);
109             rav.EnableAnimation("out", false);
110             rav.EnableAnimation("idle", false);
111             rav.Stop();
112         }
113         void InitializeTimer()
114         {
115             showAnimation = new Animation(100);
116             showAnimation.Finished += showAnimationFinished;
117             inAnimation = new Animation(1100);
118             inAnimation.Finished += inAnimationFinished;
119             outAnimation = new Animation(900);
120             outAnimation.Finished += outAnimationFinished;
121         }
122
123         protected override void OnCreate()
124         {
125             base.OnCreate();
126             InitializeTimer();
127             Initialize();
128         }
129
130         protected override void OnTerminate()
131         {
132             base.OnTerminate();
133             Destory();
134         }
135         
136         private double CalculateEyeAngle(Vector2 targetPos)
137         {
138             double ravCenterX = rav.PositionX + (rav.Size.Width / 2);
139             double ravCenterY = rav.PositionY + (rav.Size.Height / 2);
140
141             double angle = Math.Atan2(targetPos.Y - ravCenterY, targetPos.X - ravCenterX) * (180.0f / Math.PI);
142             angle += 180.0f;
143             if (angle < 0.0f)
144             {
145                 angle = 360.0f + angle;
146             }
147
148             double angleToTime = angle / 180.0f;
149             return angleToTime;
150         }
151
152         private void OnTouchEvent(object source, Window.TouchEventArgs e)
153         {
154             if (e.Touch.GetState(0) == PointStateType.Motion)
155             {
156                 //Calibrate Cursor Posiiton to Animation Time
157                 Vector2 pos = e.Touch.GetScreenPosition(0);
158                 double angleToTime = CalculateEyeAngle(pos);
159
160                 //Control eye 360 animation using cursor position
161                 rav.SetAnimationElapsedTime("reset", -1.0f);
162                 rav.SetAnimationElapsedTime("eye 360", (float)angleToTime);
163             }
164         }
165
166         void ResetMoveAnimation()
167         {
168             //Reset previous moving animation
169             if (moveAnimation)
170             {
171                 moveAnimation.Stop();
172                 moveAnimation.Dispose();
173             }
174
175             moveAnimation = new Animation(800);
176             var c1 = new Vector2(0.5f, 0.0f);
177             var c2 = new Vector2(0.5f, 1.0f);
178
179             alphaFunction = new AlphaFunction(c1, c2);
180             moveAnimation.DefaultAlphaFunction = alphaFunction;
181             moveAnimation.Finished += moveAnimationFinished;
182         }
183
184         void ApplyMoveAnimation(int X, int Y)
185         {
186             ResetMoveAnimation();
187             Vector2 pos = new Vector2(X, Y);
188             double angleToTime = CalculateEyeAngle(pos);
189             rav.SetAnimationElapsedTime("reset", -1.0f);
190             rav.SetAnimationElapsedTime("eye 360", (float)angleToTime);
191
192             moveAnimation.AnimateTo(rav, "PositionX", pos.X);
193             moveAnimation.AnimateTo(rav, "PositionY", pos.Y);
194             moveAnimation.Play();
195         }
196
197         void Initialize()
198         {
199             Window.Instance.WindowSize = new Size(1920, 1080);
200
201             //Make Window Transparency
202             Window.Instance.BackgroundColor = Color.Transparent;
203             Window.Instance.SetTransparency(true);
204
205             //Ignore Key Events for Receiving remote controller event
206             Window.Instance.SetAcceptFocus(false);
207
208             //Set Window Notification Type
209             Window.Instance.Type = WindowType.Notification;
210
211             //Make Window Notification Level Top
212             Window.Instance.SetNotificationLevel(NotificationLevel.Top);
213
214             Window.Instance.TouchEvent += OnTouchEvent;
215
216             //Create RiveAnimation
217             rav = new Tizen.NUI.Extension.RiveAnimationView(Tizen.Applications.Application.Current.DirectoryInfo.Resource + "mini_a.riv")
218             {
219                 Size = new Size(250, 250),
220                 Position = new Position( 750, 300)
221             };
222
223             //Enable In Animation
224             rav.EnableAnimation("in", true);
225
226             //Play RiveAnimation 
227             rav.Play();
228             inAnimation.Play();
229
230             inButton = new Button()
231             {
232                 Size = new Size(120, 100),
233                 Position = new Position(10, 10),
234                 Text = "in"
235             };
236             inButton.Clicked += (object source, ClickedEventArgs args) =>
237             {
238                 if (showAnimation)
239                 {
240                     showAnimation.Stop();
241                 }
242                 rav.Play();
243                 showAnimation.Play();
244             };
245
246             outButton = new Button()
247             {
248                 Size = new Size(120, 100),
249                 Position = new Position(140, 10),
250                 Text = "out"
251             };
252             outButton.Clicked += (object source, ClickedEventArgs args) =>
253             {
254                 if (outAnimation)
255                 {
256                     outAnimation.Stop();
257                 }
258                 rav.EnableAnimation("in", false);
259                 rav.EnableAnimation("out", true);
260                 outAnimation.Play();
261             };
262
263             okButton = new Button()
264             {
265                 Size = new Size(120, 100),
266                 Position = new Position(10, 120),
267                 Text = "ok"
268             };
269             okButton.Clicked += (object source, ClickedEventArgs args) =>
270             {
271                 rav.SetAnimationElapsedTime("reset", -1.0f);
272                 rav.SetAnimationElapsedTime("eye 360", -1.0f);
273                 //Enable ok animation only
274                 rav.SetAnimationElapsedTime("thinking", 0.0f);
275                 rav.SetAnimationElapsedTime("listen", 0.0f);
276
277                 if (resetOkAnimation)
278                 {
279                     resetOkAnimation.Stop();
280                 }
281                 else
282                 {
283                     resetOkAnimation = new Animation(100);
284                     resetOkAnimation.Finished += resetOkFinished;
285                 }
286                 resetOkAnimation.Play();
287
288                 rav.EnableAnimation("thinking", false);
289                 rav.EnableAnimation("listen", false);
290                 rav.EnableAnimation("ok", true);
291             };
292
293             listenButton = new Button()
294             {
295                 Size = new Size(120, 100),
296                 Position = new Position(140, 120),
297                 Text = "listen"
298             };
299             listenButton.Clicked += (object source, ClickedEventArgs args) =>
300             {
301                 rav.SetAnimationElapsedTime("reset", -1.0f);
302                 rav.SetAnimationElapsedTime("eye 360", -1.0f);
303                 //Enable listen animation only
304                 rav.SetAnimationElapsedTime("ok", 0.0f);
305                 rav.SetAnimationElapsedTime("thinking", 0.0f);
306
307                 if (resetListenAnimation)
308                 {
309                     resetListenAnimation.Stop();
310                 }
311                 else
312                 {
313                     resetListenAnimation = new Animation(100);
314                     resetListenAnimation.Finished += resetListenFinished;
315                 }
316                 resetListenAnimation.Play();
317
318                 rav.EnableAnimation("ok", false);
319                 rav.EnableAnimation("thinking", false);
320                 rav.EnableAnimation("listen", true);
321             };
322
323             thinkingButton = new Button()
324             {
325                 Size = new Size(120, 100),
326                 Position = new Position(270, 120),
327                 Text = "thinking"
328             };
329             thinkingButton.Clicked += (object source, ClickedEventArgs args) =>
330             {
331                 rav.SetAnimationElapsedTime("reset", -1.0f);
332                 rav.SetAnimationElapsedTime("eye 360", -1.0f);
333                 //Enable ok animation only
334                 rav.SetAnimationElapsedTime("ok", 0.0f);
335                 rav.SetAnimationElapsedTime("listen", 0.0f);
336
337                 if (resetThinkingAnimation)
338                 {
339                     resetThinkingAnimation.Stop();
340                 }
341                 else
342                 {
343                     resetThinkingAnimation = new Animation(100);
344                     resetThinkingAnimation.Finished += resetThinkingFinished;
345                 }
346                 resetThinkingAnimation.Play();
347
348                 rav.EnableAnimation("ok", false);
349                 rav.EnableAnimation("listen", false);
350                 rav.EnableAnimation("thinking", true);
351             };
352
353             topButton = new Button()
354             {
355                 Size = new Size(160, 100),
356                 Position = new Position(180, 250),
357                 Text = "Move Top"
358             };
359             topButton.Clicked += (object source, ClickedEventArgs args) =>
360             {
361                 ApplyMoveAnimation(750, 0);
362             };
363
364             bottomButton = new Button()
365             {
366                 Size = new Size(160, 100),
367                 Position = new Position(180, 470),
368                 Text = "Move Bottom"
369             };
370             bottomButton.Clicked += (object source, ClickedEventArgs args) =>
371             {
372                 ApplyMoveAnimation(750, 600);
373             };
374
375             leftButton = new Button()
376             {
377                 Size = new Size(160, 100),
378                 Position = new Position(10, 360),
379                 Text = "Move Left"
380             };
381             leftButton.Clicked += (object source, ClickedEventArgs args) =>
382             {
383                 ApplyMoveAnimation(300, 300);
384             };
385
386             centerButton = new Button()
387             {
388                 Size = new Size(160, 100),
389                 Position = new Position(180, 360),
390                 Text = "Move Center"
391             };
392             centerButton.Clicked += (object source, ClickedEventArgs args) =>
393             {
394                 ApplyMoveAnimation(750, 300);
395             };
396
397             rightButton = new Button()
398             {
399                 Size = new Size(160, 100),
400                 Position = new Position(350, 360),
401                 Text = "Move Right"
402             };
403             rightButton.Clicked += (object source, ClickedEventArgs args) =>
404             {
405                 ApplyMoveAnimation(1400, 300);
406             };
407
408             leftTopButton = new Button()
409             {
410                 Size = new Size(160, 100),
411                 Position = new Position(10, 250),
412                 Text = "Left Top"
413             };
414             leftTopButton.Clicked += (object source, ClickedEventArgs args) =>
415             {
416                 ApplyMoveAnimation(300, 0);
417             };
418
419             rightTopButton = new Button()
420             {
421                 Size = new Size(160, 100),
422                 Position = new Position(350, 250),
423                 Text = "Move Right"
424             };
425             rightTopButton.Clicked += (object source, ClickedEventArgs args) =>
426             {
427                 ApplyMoveAnimation(1400, 0);
428             };
429
430             leftBottomButton = new Button()
431             {
432                 Size = new Size(160, 100),
433                 Position = new Position(10, 470),
434                 Text = "Move Right"
435             };
436             leftBottomButton.Clicked += (object source, ClickedEventArgs args) =>
437             {
438                 ApplyMoveAnimation(300, 750);
439             };
440
441             rightBottomButton = new Button()
442             {
443                 Size = new Size(160, 100),
444                 Position = new Position(350, 470),
445                 Text = "Move Right"
446             };
447             rightBottomButton.Clicked += (object source, ClickedEventArgs args) =>
448             {
449                 ApplyMoveAnimation(1400, 750);
450             };
451
452             //Add RiveAnimationView and NUI Buttons to Main Window
453             Window.Instance.GetDefaultLayer().Add(rav);
454             Window.Instance.GetDefaultLayer().Add(inButton);
455             Window.Instance.GetDefaultLayer().Add(outButton);
456             Window.Instance.GetDefaultLayer().Add(thinkingButton);
457             Window.Instance.GetDefaultLayer().Add(listenButton);
458             Window.Instance.GetDefaultLayer().Add(okButton);
459             Window.Instance.GetDefaultLayer().Add(topButton);
460             Window.Instance.GetDefaultLayer().Add(bottomButton);
461             Window.Instance.GetDefaultLayer().Add(leftButton);
462             Window.Instance.GetDefaultLayer().Add(centerButton);
463             Window.Instance.GetDefaultLayer().Add(rightButton);
464             Window.Instance.GetDefaultLayer().Add(leftTopButton);
465             Window.Instance.GetDefaultLayer().Add(rightTopButton);
466             Window.Instance.GetDefaultLayer().Add(leftBottomButton);
467             Window.Instance.GetDefaultLayer().Add(rightBottomButton);
468         }
469
470         void Destory()
471         {
472             //Remove RiveAnimationView and NUI Buttons from Main Window
473             Window.Instance.GetDefaultLayer().Remove(rav);
474             Window.Instance.GetDefaultLayer().Remove(inButton);
475             Window.Instance.GetDefaultLayer().Remove(outButton);
476             Window.Instance.GetDefaultLayer().Remove(thinkingButton);
477             Window.Instance.GetDefaultLayer().Remove(listenButton);
478             Window.Instance.GetDefaultLayer().Remove(okButton);
479             Window.Instance.GetDefaultLayer().Remove(topButton);
480             Window.Instance.GetDefaultLayer().Remove(bottomButton);
481             Window.Instance.GetDefaultLayer().Remove(leftButton);
482             Window.Instance.GetDefaultLayer().Remove(centerButton);
483             Window.Instance.GetDefaultLayer().Remove(rightButton);
484             Window.Instance.GetDefaultLayer().Remove(leftTopButton);
485             Window.Instance.GetDefaultLayer().Remove(rightTopButton);
486             Window.Instance.GetDefaultLayer().Remove(leftBottomButton);
487             Window.Instance.GetDefaultLayer().Remove(rightBottomButton);
488         }
489
490         static void Main(string[] args)
491         {
492             var app = new Program();
493             app.Run(args);
494         }
495     }
496 }