Release 4.0.0-preview1-00267
[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, it provides functions to manager animations.
23     /// </summary>
24     public class EcoreTimelineAnimator
25     {
26         double _runtime;
27         IntPtr _animator;
28         Action _timelineCallback;
29         Interop.Ecore.EcoreTimelineCallback _nativeCallback;
30         double _position;
31
32         /// <summary>
33         /// It occurs when the animator is complete.
34         /// </summary>
35         public event EventHandler Finished;
36
37         /// <summary>
38         /// Creates and initializes a new instance of the EcoreTimelineAnimator class.
39         /// </summary>
40         /// <param name="runtime">The time to run in seconds</param>
41         /// <param name="timelineCallback">Functions called at each time line</param>
42         public EcoreTimelineAnimator(double runtime, Action timelineCallback)
43         {
44             _runtime = runtime;
45             _nativeCallback = NativeCallback;
46             _timelineCallback = timelineCallback;
47             _position = 0;
48         }
49
50         /// <summary>
51         /// Gets whether the animation is running.
52         /// </summary>
53         public bool IsRunning { get; private set; }
54
55         /// <summary>
56         /// Gets the current position of the animation.
57         /// </summary>
58         public double Position => _position;
59
60         /// <summary>
61         /// Starts an animator that runs for a limited time.for a limited time.
62         /// </summary>
63         public void Start()
64         {
65             IsRunning = true;
66             _animator = Interop.Ecore.ecore_animator_timeline_add(_runtime, _nativeCallback, IntPtr.Zero);
67         }
68
69         /// <summary>
70         /// Stops an animator that running.
71         /// </summary>
72         public void Stop()
73         {
74             IsRunning = false;
75             _animator = IntPtr.Zero;
76         }
77
78         /// <summary>
79         /// Suspends the specified animator.
80         /// </summary>
81         public void Freeze()
82         {
83             Interop.Ecore.ecore_animator_freeze(_animator);
84         }
85
86         /// <summary>
87         /// Restores execution of the specified animator.
88         /// </summary>
89         public void Thaw()
90         {
91             Interop.Ecore.ecore_animator_thaw(_animator);
92         }
93
94         /// <summary>
95         /// Callback that is called when ticks off
96         /// </summary>
97         protected void OnTimeline()
98         {
99             _timelineCallback();
100         }
101
102         bool NativeCallback(IntPtr data, double pos)
103         {
104             _position = pos;
105             if (!IsRunning) return false;
106             OnTimeline();
107             if (pos == 1.0)
108             {
109                 IsRunning = false;
110                 _animator = IntPtr.Zero;
111                 Finished?.Invoke(this, EventArgs.Empty);
112             }
113             return true;
114         }
115     }
116 }