Release 4.0.0-preview1-00249
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreTimelineAnimator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4
5 namespace ElmSharp
6 {
7     public class EcoreTimelineAnimator
8     {
9         double _runtime;
10         IntPtr _animator;
11         Action _timelineCallback;
12         Interop.Ecore.EcoreTimelineCallback _nativeCallback;
13         double _position;
14
15         public event EventHandler Finished;
16
17
18         public EcoreTimelineAnimator(double runtime, Action timelineCallback)
19         {
20             _runtime = runtime;
21             _nativeCallback = NativeCallback;
22             _timelineCallback = timelineCallback;
23             _position = 0;
24         }
25
26         public bool IsRunning { get; private set; }
27         public double Position => _position;
28
29         public void Start()
30         {
31             IsRunning = true;
32             _animator = Interop.Ecore.ecore_animator_timeline_add(_runtime, _nativeCallback, IntPtr.Zero);
33         }
34
35         public void Stop()
36         {
37             IsRunning = false;
38             _animator = IntPtr.Zero;
39         }
40
41         public void Freeze()
42         {
43             Interop.Ecore.ecore_animator_freeze(_animator);
44         }
45
46         public void Thaw()
47         {
48             Interop.Ecore.ecore_animator_thaw(_animator);
49         }
50
51         protected void OnTimeline()
52         {
53             _timelineCallback();
54         }
55
56         bool NativeCallback(IntPtr data, double pos)
57         {
58             _position = pos;
59             if (!IsRunning) return false;
60             OnTimeline();
61             if (pos == 1.0)
62             {
63                 IsRunning = false;
64                 _animator = IntPtr.Zero;
65                 Finished?.Invoke(this, EventArgs.Empty);
66             }
67             return true;
68         }
69     }
70 }