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