Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EcoreAnimator.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 using System.Collections.Generic;
19
20 namespace ElmSharp
21 {
22     /// <summary>
23     /// EcoreAnimator is a helper class, it provides functions to manager animations.
24     /// </summary>
25     public static class EcoreAnimator
26     {
27         static readonly Dictionary<int, Func<bool>> _taskMap = new Dictionary<int, Func<bool>>();
28         static readonly Object _taskLock = new Object();
29         static int _newTaskId = 0;
30
31         static Interop.Ecore.EcoreTaskCallback _nativeHandler;
32
33         static EcoreAnimator()
34         {
35             _nativeHandler = NativeHandler;
36         }
37
38         /// <summary>
39         /// Gets current system time as a floating point value in seconds.
40         /// </summary>
41         /// <returns>Current system time</returns>
42         public static double GetCurrentTime()
43         {
44             return Interop.Ecore.ecore_time_get();
45         }
46
47         /// <summary>
48         /// Adds an animator to call <paramref name="handler"/> at every animation tick during main loop execution.
49         /// </summary>
50         /// <param name="handler">The function to call when it ticks off</param>
51         /// <returns>A handle to the new animator</returns>
52         public static IntPtr AddAnimator(Func<bool> handler)
53         {
54             int id = RegistHandler(handler);
55             return Interop.Ecore.ecore_animator_add(_nativeHandler, (IntPtr)id);
56         }
57
58         /// <summary>
59         /// Removes the specified animator from the animator list.
60         /// </summary>
61         /// <param name="anim">The specified animator handle</param>
62         public static void RemoveAnimator(IntPtr anim)
63         {
64             int taskId = (int)Interop.Ecore.ecore_animator_del(anim);
65             _taskMap.Remove(taskId);
66         }
67
68         static int RegistHandler(Func<bool> task)
69         {
70             int taskId;
71             lock (_taskLock)
72             {
73                 taskId = _newTaskId++;
74             }
75             _taskMap[taskId] = task;
76             return taskId;
77         }
78
79         static bool NativeHandler(IntPtr userData)
80         {
81             int task_id = (int)userData;
82             Func<bool> userAction = null;
83             _taskMap.TryGetValue(task_id, out userAction);
84             return (userAction != null) ? userAction() : false;
85         }
86
87     }
88 }