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