[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreTimelineAnimator.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// EcoreTimelineAnimator is a helper class, which provides the functions to manage animations.
23     /// </summary>
24     /// <since_tizen> preview </since_tizen>
25     public class EcoreTimelineAnimator
26     {
27         double _runtime;
28         IntPtr _animator;
29         Action _timelineCallback;
30         Interop.Ecore.EcoreTimelineCallback _nativeCallback;
31         double _position;
32
33         /// <summary>
34         /// It occurs when the animator is complete.
35         /// </summary>
36         /// <since_tizen> preview </since_tizen>
37         public event EventHandler Finished;
38
39         /// <summary>
40         /// Creates and initializes a new instance of the EcoreTimelineAnimator class.
41         /// </summary>
42         /// <param name="runtime">The time to run in seconds.</param>
43         /// <param name="timelineCallback">Functions called at each time line.</param>
44         /// <since_tizen> preview </since_tizen>
45         public EcoreTimelineAnimator(double runtime, Action timelineCallback)
46         {
47             _runtime = runtime;
48             _nativeCallback = NativeCallback;
49             _timelineCallback = timelineCallback;
50             _position = 0;
51         }
52
53         /// <summary>
54         /// Gets whether the animation is running.
55         /// </summary>
56         /// <since_tizen> preview </since_tizen>
57         public bool IsRunning { get; private set; }
58
59         /// <summary>
60         /// Gets the current position of the animation.
61         /// </summary>
62         /// <since_tizen> preview </since_tizen>
63         public double Position => _position;
64
65         /// <summary>
66         /// Starts an animator that runs for a limited time.
67         /// </summary>
68         /// <since_tizen> preview </since_tizen>
69         public void Start()
70         {
71             IsRunning = true;
72             _animator = Interop.Ecore.ecore_animator_timeline_add(_runtime, _nativeCallback, IntPtr.Zero);
73         }
74
75         /// <summary>
76         /// Stops an animator that is running.
77         /// </summary>
78         /// <since_tizen> preview </since_tizen>
79         public void Stop()
80         {
81             IsRunning = false;
82             _animator = IntPtr.Zero;
83         }
84
85         /// <summary>
86         /// Suspends the specified animator.
87         /// </summary>
88         /// <since_tizen> preview </since_tizen>
89         public void Freeze()
90         {
91             Interop.Ecore.ecore_animator_freeze(_animator);
92         }
93
94         /// <summary>
95         /// Restores execution of the specified animator.
96         /// </summary>
97         /// <since_tizen> preview </since_tizen>
98         public void Thaw()
99         {
100             Interop.Ecore.ecore_animator_thaw(_animator);
101         }
102
103         /// <summary>
104         /// Callback is called when it ticks off.
105         /// </summary>
106         /// <since_tizen> preview </since_tizen>
107         protected void OnTimeline()
108         {
109             _timelineCallback();
110         }
111
112         bool NativeCallback(IntPtr data, double pos)
113         {
114             _position = pos;
115             if (!IsRunning) return false;
116             OnTimeline();
117             if (pos == 1.0)
118             {
119                 IsRunning = false;
120                 _animator = IntPtr.Zero;
121                 Finished?.Invoke(this, EventArgs.Empty);
122             }
123             return true;
124         }
125     }
126 }