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