Release 12.0.0.18314
[platform/core/csapi/tizenfx.git] / src / Tizen.AIAvatar / src / public / Avatar / Animations / AnimationModule / EyeBlinker.cs
1 /*
2  * Copyright(c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 using System;
19 using System.ComponentModel;
20 using Tizen.NUI;
21 using Tizen.NUI.Scene3D;
22
23 using static Tizen.AIAvatar.AIAvatar;
24
25 namespace Tizen.AIAvatar
26 {
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     internal class EyeBlinker : AnimationModule
29     {
30         private const int blinkIntervalMinimum = 800;
31         private const int blinkIntervalMaximum = 3000;
32         private Animation eyeAnimation;
33
34         private Timer blinkTimer;
35
36         private bool isPlaying = false;
37         private const int blinkDuration = 200;
38
39         [EditorBrowsable(EditorBrowsableState.Never)]
40         internal EyeBlinker()
41         {
42
43         }
44
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         public override void Init(Animation eyeAnimation)
47         {
48             this.eyeAnimation = eyeAnimation;
49         }
50
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public override void Play(IAnimationModuleData data)
53         {
54             //data
55             StartEyeBlink();
56         }
57
58         [EditorBrowsable(EditorBrowsableState.Never)]
59         public override void Stop()
60         {
61             StopEyeBlink();
62         }
63
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         public override void Pause()
66         {
67             eyeAnimation?.Pause();
68         }
69
70         [EditorBrowsable(EditorBrowsableState.Never)]
71         public override void Destroy()
72         {
73             DestroyAnimation();
74         }
75
76         private void StartEyeBlink()
77         {
78             DestroyBlinkTimer();
79
80             blinkTimer = new Timer(blinkDuration);
81             if (blinkTimer != null)
82             {
83                 blinkTimer.Tick += OnBlinkTimer;
84                 blinkTimer?.Start();
85                 isPlaying = true;
86             }
87         }
88
89         private void PauseEyeBlink()
90         {
91             blinkTimer?.Stop();
92             isPlaying = false;
93         }
94
95         private void StopEyeBlink()
96         {
97             blinkTimer?.Stop();
98             isPlaying = false;
99         }
100
101         private void DestroyAnimation()
102         {
103             DestroyBlinkTimer();
104             if (eyeAnimation != null)
105             {
106                 eyeAnimation.Stop();
107                 eyeAnimation.Dispose();
108                 eyeAnimation = null;
109             }
110             isPlaying = false;
111         }
112
113         private bool OnBlinkTimer(object source, Timer.TickEventArgs e)
114         {
115             if (eyeAnimation == null)
116             {
117                 Log.Error(LogTag, "eye animation is not ready");
118                 return false;
119             }
120             eyeAnimation?.Play();
121
122             var random = new Random();
123             var fortimerinterval = (uint)random.Next(blinkIntervalMinimum, blinkIntervalMaximum);
124             blinkTimer.Interval = fortimerinterval;
125             return true;
126         }
127
128         private void DestroyBlinkTimer()
129         {
130             if (blinkTimer != null)
131             {
132                 blinkTimer.Tick -= OnBlinkTimer;
133                 blinkTimer.Stop();
134                 blinkTimer.Dispose();
135                 blinkTimer = null;
136             }
137         }
138     }
139 }