f1e31782086336088aa6895df9e389f6dc7df11e
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / AlphaFunction.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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
18 namespace Tizen.NUI
19 {
20
21     /// <summary>
22     /// Alpha functions are used in animations to specify the rate of change of the animation parameter over time.<br />
23     /// Understanding an animation as a parametric function over time, the alpha function is applied to the parameter of
24     /// the animation before computing the final animation value.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     public class AlphaFunction : global::System.IDisposable
28     {
29         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
30         /// <summary>swigCMemOwn.</summary>
31         /// <since_tizen> 3 </since_tizen>
32         protected bool swigCMemOwn;
33
34         internal AlphaFunction(global::System.IntPtr cPtr, bool cMemoryOwn)
35         {
36             swigCMemOwn = cMemoryOwn;
37             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
38         }
39
40         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(AlphaFunction obj)
41         {
42             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
43         }
44
45         //A Flag to check who called Dispose(). (By User or DisposeQueue)
46         private bool isDisposeQueued = false;
47         /// <summary>
48         /// A Flat to check if it is already disposed.
49         /// </summary>
50         /// <since_tizen> 3 </since_tizen>
51         protected bool disposed = false;
52
53         /// <summary>
54         /// Dispose.
55         /// </summary>
56         /// <since_tizen> 3 </since_tizen>
57         ~AlphaFunction()
58         {
59             if(!isDisposeQueued)
60             {
61                 isDisposeQueued = true;
62                 DisposeQueue.Instance.Add(this);
63             }
64         }
65         /// <summary>
66         /// To make the AlphaFunction instance be disposed.
67         /// </summary>
68         /// <since_tizen> 3 </since_tizen>
69         public void Dispose()
70         {
71             //Throw excpetion if Dispose() is called in separate thread.
72             if (!Window.IsInstalled())
73             {
74                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
75             }
76
77             if (isDisposeQueued)
78             {
79                 Dispose(DisposeTypes.Implicit);
80             }
81             else
82             {
83                 Dispose(DisposeTypes.Explicit);
84                 System.GC.SuppressFinalize(this);
85             }
86         }
87
88         /// <summary>
89         /// To make the AlphaFunction instance be disposed.
90         /// </summary>
91         /// <since_tizen> 3 </since_tizen>
92         protected virtual void Dispose(DisposeTypes type)
93         {
94             if (disposed)
95             {
96                 return;
97             }
98
99             if(type == DisposeTypes.Explicit)
100             {
101                 //Called by User
102                 //Release your own managed resources here.
103                 //You should release all of your own disposable objects here.
104             }
105
106             //Release your own unmanaged resources here.
107             //You should not access any managed member here except static instance.
108             //because the execution order of Finalizes is non-deterministic.
109
110             if (swigCPtr.Handle != global::System.IntPtr.Zero)
111             {
112                 if (swigCMemOwn)
113                 {
114                     swigCMemOwn = false;
115                     NDalicPINVOKE.delete_AlphaFunction(swigCPtr);
116                 }
117                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
118             }
119             disposed = true;
120         }
121
122         /// <summary>
123         /// The constructor.<br />
124         /// Creates an alpha function object with the user-defined alpha function.<br />
125         /// </summary>
126         /// <param name="func">User defined fuction. It must be a method formatted as float alphafunction(float progress)</param>
127         /// <since_tizen> 3 </since_tizen>
128         public AlphaFunction(System.Delegate func) : this(NDalicPINVOKE.new_AlphaFunction__SWIG_2(SWIGTYPE_p_f_float__float.getCPtr(new SWIGTYPE_p_f_float__float(System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(func), true))), true)
129         {
130             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
131         }
132
133         /// <summary>
134         /// The default constructor.<br />
135         /// Creates an alpha function object with the default built-in alpha function.<br />
136         /// </summary>
137         /// <since_tizen> 3 </since_tizen>
138         public AlphaFunction() : this(NDalicPINVOKE.new_AlphaFunction__SWIG_0(), true)
139         {
140             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
141         }
142
143         /// <summary>
144         /// The constructor.<br />
145         /// Creates an alpha function object with the built-in alpha function passed as a parameter to the constructor.<br />
146         /// </summary>
147         /// <param name="function">One of the built-in alpha functions.</param>
148         /// <since_tizen> 3 </since_tizen>
149         public AlphaFunction(AlphaFunction.BuiltinFunctions function) : this(NDalicPINVOKE.new_AlphaFunction__SWIG_1((int)function), true)
150         {
151             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
152         }
153
154         internal AlphaFunction(SWIGTYPE_p_f_float__float function) : this(NDalicPINVOKE.new_AlphaFunction__SWIG_2(SWIGTYPE_p_f_float__float.getCPtr(function)), true)
155         {
156             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
157         }
158
159         /// <summary>
160         /// The constructor.<br />
161         /// Creates a bezier alpha function. The bezier will have the first point at (0,0) and the end point at (1,1).<br />
162         /// </summary>
163         /// <remarks>The x components of the control points will be clamped to the range [0, 1] to prevent non-monotonic curves.</remarks>
164         /// <param name="controlPoint0">A Vector2 which will be used as the first control point of the curve.</param>
165         /// <param name="controlPoint1">A Vector2 which will be used as the second control point of the curve.</param>
166         /// <since_tizen> 3 </since_tizen>
167         public AlphaFunction(Vector2 controlPoint0, Vector2 controlPoint1) : this(NDalicPINVOKE.new_AlphaFunction__SWIG_3(Vector2.getCPtr(controlPoint0), Vector2.getCPtr(controlPoint1)), true)
168         {
169             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170         }
171
172         /// <summary>
173         /// Retrives the control points of the alpha function.<br />
174         /// </summary>
175         /// <param name="controlPoint0">A Vector2 which will be used as the first control point of the curve.</param>
176         /// <param name="controlPoint1">A Vector2 which will be used as the second control point of the curve.</param>
177         /// <since_tizen> 3 </since_tizen>
178         public void GetBezierControlPoints(out Vector2 controlPoint0, out Vector2 controlPoint1)
179         {
180             Vector4 ret = new Vector4(NDalicPINVOKE.AlphaFunction_GetBezierControlPoints(swigCPtr), true);
181             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182
183             controlPoint0 = new Vector2(ret.X, ret.Y);
184             controlPoint1 = new Vector2(ret.Z, ret.W);
185         }
186
187         internal SWIGTYPE_p_f_float__float GetCustomFunction()
188         {
189             global::System.IntPtr cPtr = NDalicPINVOKE.AlphaFunction_GetCustomFunction(swigCPtr);
190             SWIGTYPE_p_f_float__float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_f_float__float(cPtr, false);
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192             return ret;
193         }
194
195         /// <summary>
196         /// Returns the built-in function used by the alpha function.<br />
197         /// In case no built-in function has been specified, it will return AlphaFunction::DEFAULT.<br />
198         /// </summary>
199         /// <returns>One of the built-in alpha functions.</returns>
200         /// <since_tizen> 3 </since_tizen>
201         public AlphaFunction.BuiltinFunctions GetBuiltinFunction()
202         {
203             AlphaFunction.BuiltinFunctions ret = (AlphaFunction.BuiltinFunctions)NDalicPINVOKE.AlphaFunction_GetBuiltinFunction(swigCPtr);
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205             return ret;
206         }
207
208         /// <summary>
209         ///  Returns the functioning mode of the alpha function.
210         /// </summary>
211         /// <returns>The functioning mode of the alpha function.</returns>
212         /// <since_tizen> 3 </since_tizen>
213         public AlphaFunction.Modes GetMode()
214         {
215             AlphaFunction.Modes ret = (AlphaFunction.Modes)NDalicPINVOKE.AlphaFunction_GetMode(swigCPtr);
216             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
217             return ret;
218         }
219
220         /// <summary>
221         /// This specifies the various types of BuiltinFunctions.
222         /// </summary>
223         /// <since_tizen> 3 </since_tizen>
224         public enum BuiltinFunctions
225         {
226             /// <summary>
227             /// Linear.
228             /// </summary>
229             Default,
230             /// <summary>
231             /// No transformation.
232             /// </summary>
233             Linear,
234             /// <summary>
235             /// Reverse linear.
236             /// </summary>
237             Reverse,
238             /// <summary>
239             /// Speeds up and comes to a sudden stop (square).
240             /// </summary>
241             EaseInSquare,
242             /// <summary>
243             /// Sudden start and slows to a gradual stop (square).
244             /// </summary>
245             EaseOutSquare,
246             /// <summary>
247             /// Speeds up and comes to a sudden stop (cubic).
248             /// </summary>
249             EaseIn,
250             /// <summary>
251             /// Sudden start and slows to a gradual stop (cubic).
252             /// </summary>
253             EaseOut,
254             /// <summary>
255             /// Speeds up and slows to a gradual stop (cubic).
256             /// </summary>
257             EaseInOut,
258             /// <summary>
259             /// Speeds up and comes to a sudden stop (sinusoidal).
260             /// </summary>
261             EaseInSine,
262             /// <summary>
263             /// Sudden start and slows to a gradual stop (sinusoidal).
264             /// </summary>
265             EaseOutSine,
266             /// <summary>
267             /// Speeds up and slows to a gradual stop (sinusoidal).
268             /// </summary>
269             EaseInOutSine,
270             /// <summary>
271             /// Sudden start, loses momentum and returns to start position.
272             /// </summary>
273             Bounce,
274             /// <summary>
275             /// Single revolution.
276             /// </summary>
277             Sin,
278             /// <summary>
279             /// Sudden start, exceed end position and return to a gradual stop.
280             /// </summary>
281             EaseOutBack,
282             /// <summary>
283             /// The count of the BuiltinFunctions enum.
284             /// </summary>
285             Count
286         }
287
288         /// <summary>
289         /// This specifies which mode is set for AlphaFunction.
290         /// </summary>
291         /// <since_tizen> 3 </since_tizen>
292         public enum Modes
293         {
294             /// <summary>
295             /// The user has used a built-in function.
296             /// </summary>
297             BuiltinFunction,
298
299             /// <summary>
300             /// The user has provided a custom function.
301             /// </summary>
302             CustomFunction,
303             /// <summary>
304             /// The user has provided the control points of a bezier curve.
305             /// </summary>
306             Bezier
307         }
308
309         internal static string BuiltinToPropertyKey(BuiltinFunctions? alphaFunction)
310         {
311             string propertyKey = null;
312             if (alphaFunction != null)
313             {
314                 switch (alphaFunction)
315                 {
316                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Linear:
317                     {
318                         propertyKey = "LINEAR";
319                         break;
320                     }
321                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Reverse:
322                     {
323                         propertyKey = "REVERSE";
324                         break;
325                     }
326                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSquare:
327                     {
328                         propertyKey = "EASE_IN_SQUARE";
329                         break;
330                     }
331                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSquare:
332                     {
333                         propertyKey = "EASE_OUT_SQUARE";
334                         break;
335                     }
336                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseIn:
337                     {
338                         propertyKey = "EASE_IN";
339                         break;
340                     }
341                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOut:
342                     {
343                         propertyKey = "EASE_OUT";
344                         break;
345                     }
346                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOut:
347                     {
348                         propertyKey = "EASE_IN_OUT";
349                         break;
350                     }
351                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInSine:
352                     {
353                         propertyKey = "EASE_IN_SINE";
354                         break;
355                     }
356                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutSine:
357                     {
358                         propertyKey = "EASE_OUT_SINE";
359                         break;
360                     }
361                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseInOutSine:
362                     {
363                         propertyKey = "EASE_IN_OUT_SINE";
364                         break;
365                     }
366                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Bounce:
367                     {
368                         propertyKey = "BOUNCE";
369                         break;
370                     }
371                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.Sin:
372                     {
373                         propertyKey = "SIN";
374                         break;
375                     }
376                     case Tizen.NUI.AlphaFunction.BuiltinFunctions.EaseOutBack:
377                     {
378                         propertyKey = "EASE_OUT_BACK";
379                         break;
380                     }
381                     default:
382                     {
383                         propertyKey = "DEFAULT";
384                         break;
385                     }
386                 }
387             }
388             return propertyKey;
389         }
390     }
391 }