[NUI] Sync with dalihub & API5 branch (#631)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / RelativeVector2.cs
1 /*
2  * Copyright (c) 2018 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  using System;
18  using Tizen.NUI.Binding;
19
20 namespace Tizen.NUI
21 {
22     /// <summary>
23     /// RelativeVector2 is a two-dimensional vector.
24     /// Both values (x and y) should be between [0, 1].
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     [TypeConverter(typeof(RelativeVector2TypeConverter))]
28     public class RelativeVector2 : global::System.IDisposable
29     {
30         /// <summary>
31         /// swigCMemOwn
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         protected bool swigCMemOwn;
35
36         /// <summary>
37         /// A Flat to check if it is already disposed.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         protected bool disposed = false;
41
42         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
43
44         //A Flag to check who called Dispose(). (By User or DisposeQueue)
45         private bool isDisposeQueued = false;
46
47         /// <summary>
48         /// The constructor.
49         /// </summary>
50         /// <since_tizen> 3 </since_tizen>
51         public RelativeVector2() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
52         {
53             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54         }
55
56         /// <summary>
57         /// The constructor.
58         /// </summary>
59         /// <param name="x">The x component.</param>
60         /// <param name="y">The y component.</param>
61         /// <since_tizen> 3 </since_tizen>
62         public RelativeVector2(float x, float y) : this(NDalicPINVOKE.new_Vector2__SWIG_1(ValueCheck(x), ValueCheck(y)), true)
63         {
64             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
65         }
66
67         /// <summary>
68         /// The constructor.
69         /// </summary>
70         /// <param name="relativeVector3">The RelativeVector3 to create this vector from.</param>
71         /// <since_tizen> 3 </since_tizen>
72         public RelativeVector2(RelativeVector3 relativeVector3) : this(NDalicPINVOKE.new_Vector2__SWIG_3(RelativeVector3.getCPtr(relativeVector3)), true)
73         {
74             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
75         }
76
77         /// <summary>
78         /// The constructor.
79         /// </summary>
80         /// <param name="relativeVector4">The RelativeVector4 to create this vector from.</param>
81         /// <since_tizen> 3 </since_tizen>
82         public RelativeVector2(RelativeVector4 relativeVector4) : this(NDalicPINVOKE.new_Vector2__SWIG_4(RelativeVector4.getCPtr(relativeVector4)), true)
83         {
84             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
85         }
86
87         internal RelativeVector2(global::System.IntPtr cPtr, bool cMemoryOwn)
88         {
89             swigCMemOwn = cMemoryOwn;
90             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
91         }
92
93         /// <summary>
94         /// Dispose.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         ~RelativeVector2()
98         {
99             if (!isDisposeQueued)
100             {
101                 isDisposeQueued = true;
102                 DisposeQueue.Instance.Add(this);
103             }
104         }
105
106         /// <summary>
107         /// The x component.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         public float X
111         {
112             set
113             {
114                 NDalicPINVOKE.Vector2_X_set(swigCPtr, ValueCheck(value));
115                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116             }
117             get
118             {
119                 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
120                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121                 return ret;
122             }
123         }
124
125         /// <summary>
126         /// The y component.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         public float Y
130         {
131             set
132             {
133                 NDalicPINVOKE.Vector2_Y_set(swigCPtr, ValueCheck(value));
134                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
135             }
136             get
137             {
138                 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
139                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
140                 return ret;
141             }
142         }
143
144         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(RelativeVector2 obj)
145         {
146             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
147         }
148
149         /// <summary>
150         /// The addition operator.
151         /// </summary>
152         /// <param name="arg1">The vector to add.</param>
153         /// <param name="arg2">The vector to add.</param>
154         /// <returns>The vector containing the result of the addition.</returns>
155         /// <since_tizen> 3 </since_tizen>
156         public static RelativeVector2 operator +(RelativeVector2 arg1, RelativeVector2 arg2)
157         {
158             RelativeVector2 result = arg1.Add(arg2);
159             return ValueCheck(result);
160         }
161
162         /// <summary>
163         /// The subtraction operator.
164         /// </summary>
165         /// <param name="arg1">The vector to subtract.</param>
166         /// <param name="arg2">The vector to subtract.</param>
167         /// <returns>The vector containing the result of the subtraction.</returns>
168         /// <since_tizen> 3 </since_tizen>
169         public static RelativeVector2 operator -(RelativeVector2 arg1, RelativeVector2 arg2)
170         {
171             RelativeVector2 result = arg1.Subtract(arg2);
172             return ValueCheck(result);
173         }
174
175         /// <summary>
176         /// The multiplication operator.
177         /// </summary>
178         /// <param name="arg1">The vector to multiply.</param>
179         /// <param name="arg2">The vector to multiply.</param>
180         /// <returns>The vector containing the result of the multiplication.</returns>
181         /// <since_tizen> 3 </since_tizen>
182         public static RelativeVector2 operator *(RelativeVector2 arg1, RelativeVector2 arg2)
183         {
184             RelativeVector2 result = arg1.Multiply(arg2);
185             return ValueCheck(result);
186         }
187
188         /// <summary>
189         /// The multiplication operator.
190         /// </summary>
191         /// <param name="arg1">The vector to multiply.</param>
192         /// <param name="arg2">The float value to scale the vector.</param>
193         /// <returns>The vector containing the result of the scaling.</returns>
194         /// <since_tizen> 3 </since_tizen>
195         public static RelativeVector2 operator *(RelativeVector2 arg1, float arg2)
196         {
197             RelativeVector2 result = arg1.Multiply(arg2);
198             return ValueCheck(result);
199         }
200
201         /// <summary>
202         /// The division operator.
203         /// </summary>
204         /// <param name="arg1">The vector to divide.</param>
205         /// <param name="arg2">The vector to divide.</param>
206         /// <returns>The vector containing the result of the division.</returns>
207         /// <since_tizen> 3 </since_tizen>
208         public static RelativeVector2 operator /(RelativeVector2 arg1, RelativeVector2 arg2)
209         {
210             RelativeVector2 result = arg1.Divide(arg2);
211             return ValueCheck(result);
212         }
213
214         /// <summary>
215         /// The division operator.
216         /// </summary>
217         /// <param name="arg1">The vector to divide.</param>
218         /// <param name="arg2">The float value to scale the vector by.</param>
219         /// <returns>The vector containing the result of the scaling.</returns>
220         /// <since_tizen> 3 </since_tizen>
221         public static RelativeVector2 operator /(RelativeVector2 arg1, float arg2)
222         {
223             RelativeVector2 result = arg1.Divide(arg2);
224             return ValueCheck(result);
225         }
226
227         /// <summary>
228         /// </summary>
229         /// <since_tizen> 3 </since_tizen>
230         public static implicit operator Vector2(RelativeVector2 relativeVector2)
231         {
232             return new Vector2(relativeVector2.X, relativeVector2.Y);
233         }
234
235         /// <summary>
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         public static implicit operator RelativeVector2(Vector2 vec)
239         {
240             return new RelativeVector2(ValueCheck(vec.X), ValueCheck(vec.Y));
241         }
242
243         /// <summary>
244         /// Dispose.
245         /// </summary>
246         /// <since_tizen> 3 </since_tizen>
247         public void Dispose()
248         {
249             //Throw excpetion if Dispose() is called in separate thread.
250             if (!Window.IsInstalled())
251             {
252                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
253             }
254
255             if (isDisposeQueued)
256             {
257                 Dispose(DisposeTypes.Implicit);
258             }
259             else
260             {
261                 Dispose(DisposeTypes.Explicit);
262                 System.GC.SuppressFinalize(this);
263             }
264         }
265
266         /// <summary>
267         /// The const array subscript operator overload. Should be 0, 1.
268         /// </summary>
269         /// <param name="index">The subscript index.</param>
270         /// <returns>The float at the given index.</returns>
271         /// <since_tizen> 3 </since_tizen>
272         public float this[uint index]
273         {
274             get
275             {
276                 return ValueOfIndex(index);
277             }
278         }
279
280         /// <summary>
281         /// Determines whether the specified object is equal to the current object.
282         /// </summary>
283         /// <param name="obj">The object to compare with the current object.</param>
284         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
285         public override bool Equals(System.Object obj)
286         {
287             RelativeVector2 relativeRector2 = obj as RelativeVector2;
288             bool equal = false;
289             if (X == relativeRector2?.X && Y == relativeRector2?.Y)
290             {
291                 equal = true;
292             }
293             return equal;
294         }
295
296         /// <summary>
297         /// Gets the the hash code of this RelativeVector2.
298         /// </summary>
299         /// <returns>The Hash Code.</returns>
300         /// <since_tizen> 5 </since_tizen>
301         public override int GetHashCode()
302         {
303             return base.GetHashCode();
304         }
305
306         /// <summary>
307         /// Compares if the rhs is equal to.
308         /// </summary>
309         /// <param name="rhs">The vector to compare.</param>
310         /// <returns>Returns true if the two vectors are equal, otherwise false.</returns>
311         /// <since_tizen> 3 </since_tizen>
312         public bool EqualTo(RelativeVector2 rhs)
313         {
314             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
315             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
316             return ret;
317         }
318
319         /// <summary>
320         /// Compares if the rhs is not equal to.
321         /// </summary>
322         /// <param name="rhs">The vector to compare.</param>
323         /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
324         /// <since_tizen> 3 </since_tizen>
325         public bool NotEqualTo(RelativeVector2 rhs)
326         {
327             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
328             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
329             return ret;
330         }
331
332         /// <summary>
333         /// </summary>
334         internal static RelativeVector2 GetRelativeVector2FromPtr(global::System.IntPtr cPtr)
335         {
336             RelativeVector2 ret = new RelativeVector2(cPtr, false);
337             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
338             return ret;
339         }
340
341         internal static RelativeVector2 ValueCheck(RelativeVector2 relativeVector2)
342         {
343             if (relativeVector2.X < 0.0f)
344             {
345                 relativeVector2.X = 0.0f;
346                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
347             }
348             else if (relativeVector2.X > 1.0f)
349             {
350                 relativeVector2.X = 1.0f;
351                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
352             }
353             if (relativeVector2.Y < 0.0f)
354             {
355                 relativeVector2.Y = 0.0f;
356                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
357             }
358             else if (relativeVector2.Y > 1.0f)
359             {
360                 relativeVector2.Y = 1.0f;
361                 NUILog.Error("The value of Result is invalid! Should be between [0, 1].");
362             }
363             return relativeVector2;
364         }
365
366         internal static float ValueCheck(float value)
367         {
368             if (value < 0.0f)
369             {
370                 value = 0.0f;
371                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
372             }
373             else if (value > 1.0f)
374             {
375                 value = 1.0f;
376                 NUILog.Error("The value of Parameters is invalid! Should be between [0, 1].");
377             }
378             return value;
379         }
380
381         /// <summary>
382         /// Dispose.
383         /// </summary>
384         /// <since_tizen> 3 </since_tizen>
385         protected virtual void Dispose(DisposeTypes type)
386         {
387             if (disposed)
388             {
389                 return;
390             }
391
392             if (type == DisposeTypes.Explicit)
393             {
394                 //Called by User
395                 //Release your own managed resources here.
396                 //You should release all of your own disposable objects here.
397             }
398
399             //Release your own unmanaged resources here.
400             //You should not access any managed member here except static instance.
401             //because the execution order of Finalizes is non-deterministic.
402
403             if (swigCPtr.Handle != global::System.IntPtr.Zero)
404             {
405                 if (swigCMemOwn)
406                 {
407                     swigCMemOwn = false;
408                     NDalicPINVOKE.delete_Vector2(swigCPtr);
409                 }
410                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
411             }
412             disposed = true;
413         }
414
415         private RelativeVector2 Add(RelativeVector2 rhs)
416         {
417             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Add(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
418             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419             return ret;
420         }
421
422         private RelativeVector2 Subtract(RelativeVector2 rhs)
423         {
424             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429         private RelativeVector2 Multiply(RelativeVector2 rhs)
430         {
431             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
432             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433             return ret;
434         }
435
436         private RelativeVector2 Multiply(float rhs)
437         {
438             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, rhs), true);
439             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
440             return ret;
441         }
442
443         private RelativeVector2 Divide(RelativeVector2 rhs)
444         {
445             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
446             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447             return ret;
448         }
449
450         private RelativeVector2 Divide(float rhs)
451         {
452             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, rhs), true);
453             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454             return ret;
455         }
456
457         private float ValueOfIndex(uint index)
458         {
459             float ret = NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
460             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
461             return ret;
462         }
463     }
464 }
465
466