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