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