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