Merge remote-tracking branch 'widget-application/tizen'
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Vector2.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI
18 {
19
20     /// <summary>
21     /// A two dimensional vector.
22     /// </summary>
23     public class Vector2 : global::System.IDisposable
24     {
25         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
26         protected bool swigCMemOwn;
27
28         internal Vector2(global::System.IntPtr cPtr, bool cMemoryOwn)
29         {
30             swigCMemOwn = cMemoryOwn;
31             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
32         }
33
34         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Vector2 obj)
35         {
36             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
37         }
38
39         //A Flag to check who called Dispose(). (By User or DisposeQueue)
40         private bool isDisposeQueued = false;
41         //A Flat to check if it is already disposed.
42         protected bool disposed = false;
43
44         ~Vector2()
45         {
46             if(!isDisposeQueued)
47             {
48                 isDisposeQueued = true;
49                 DisposeQueue.Instance.Add(this);
50             }
51         }
52
53         public void Dispose()
54         {
55             //Throw excpetion if Dispose() is called in separate thread.
56             if (!Window.IsInstalled())
57             {
58                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
59             }
60
61             if (isDisposeQueued)
62             {
63                 Dispose(DisposeTypes.Implicit);
64             }
65             else
66             {
67                 Dispose(DisposeTypes.Explicit);
68                 System.GC.SuppressFinalize(this);
69             }
70         }
71
72         protected virtual void Dispose(DisposeTypes type)
73         {
74             if (disposed)
75             {
76                 return;
77             }
78
79             if(type == DisposeTypes.Explicit)
80             {
81                 //Called by User
82                 //Release your own managed resources here.
83                 //You should release all of your own disposable objects here.
84             }
85
86             //Release your own unmanaged resources here.
87             //You should not access any managed member here except static instance.
88             //because the execution order of Finalizes is non-deterministic.
89
90             if (swigCPtr.Handle != global::System.IntPtr.Zero)
91             {
92                 if (swigCMemOwn)
93                 {
94                     swigCMemOwn = false;
95                     NDalicPINVOKE.delete_Vector2(swigCPtr);
96                 }
97                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
98             }
99             disposed = true;
100         }
101
102         /// <summary>
103         /// Addition operator.
104         /// </summary>
105         /// <param name="arg1">First value</param>
106         /// <param name="arg2">Second value</param>
107         /// <returns>A vector containing the result of the addition</returns>
108         public static Vector2 operator +(Vector2 arg1, Vector2 arg2)
109         {
110             return arg1.Add(arg2);
111         }
112
113         /// <summary>
114         /// Subtraction operator.
115         /// </summary>
116         /// <param name="arg1">First value</param>
117         /// <param name="arg2">Second value</param>
118         /// <returns>A vector containing the result of the subtraction</returns>
119         public static Vector2 operator -(Vector2 arg1, Vector2 arg2)
120         {
121             return arg1.Subtract(arg2);
122         }
123
124         /// <summary>
125         /// Unary negation operator.
126         /// </summary>
127         /// <param name="arg1">Target Value</param>
128         /// <returns>A vector containg the negation</returns>
129         public static Vector2 operator -(Vector2 arg1)
130         {
131             return arg1.Subtract();
132         }
133
134         /// <summary>
135         /// Multiplication operator.
136         /// </summary>
137         /// <param name="arg1">First Value</param>
138         /// <param name="arg2">Second Value</param>
139         /// <returns>A vector containing the result of the multiplication</returns>
140         public static Vector2 operator *(Vector2 arg1, Vector2 arg2)
141         {
142             return arg1.Multiply(arg2);
143         }
144
145         /// <summary>
146         /// Multiplication operator.
147         /// </summary>
148         /// <param name="arg1">First Value</param>
149         /// <param name="arg2">The float value to scale the vector</param>
150         /// <returns>A vector containing the result of the scaling</returns>
151         public static Vector2 operator *(Vector2 arg1, float arg2)
152         {
153             return arg1.Multiply(arg2);
154         }
155
156         /// <summary>
157         /// Division operator.
158         /// </summary>
159         /// <param name="arg1">First Value</param>
160         /// <param name="arg2">Second Value</param>
161         /// <returns>A vector containing the result of the division</returns>
162         public static Vector2 operator /(Vector2 arg1, Vector2 arg2)
163         {
164             return arg1.Divide(arg2);
165         }
166
167         /// <summary>
168         /// Division operator.
169         /// </summary>
170         /// <param name="arg1">First Value</param>
171         /// <param name="arg2">The float value to scale the vector by</param>
172         /// <returns>A vector containing the result of the scaling</returns>
173         public static Vector2 operator /(Vector2 arg1, float arg2)
174         {
175             return arg1.Divide(arg2);
176         }
177
178         /// <summary>
179         /// Array subscript operator overload.
180         /// </summary>
181         /// <param name="index">Subscript index</param>
182         /// <returns>The float at the given index</returns>
183         public float this[uint index]
184         {
185             get
186             {
187                 return ValueOfIndex(index);
188             }
189         }
190
191         internal static Vector2 GetVector2FromPtr(global::System.IntPtr cPtr)
192         {
193             Vector2 ret = new Vector2(cPtr, false);
194             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
195             return ret;
196         }
197
198         /// <summary>
199         /// Default constructor, initializes the vector to 0.
200         /// </summary>
201         public Vector2() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
202         {
203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204         }
205
206         /// <summary>
207         /// Constructor.
208         /// </summary>
209         /// <param name="x">x or width component</param>
210         /// <param name="y">y or height component</param>
211         public Vector2(float x, float y) : this(NDalicPINVOKE.new_Vector2__SWIG_1(x, y), true)
212         {
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214         }
215
216         /// <summary>
217         /// Conversion constructor from an array of two floats.
218         /// </summary>
219         /// <param name="array">Array of xy</param>
220         public Vector2(float[] array) : this(NDalicPINVOKE.new_Vector2__SWIG_2(array), true)
221         {
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223         }
224
225         /// <summary>
226         /// Constructor.
227         /// </summary>
228         /// <param name="vec3">Vector3 to create this vector from</param>
229         public Vector2(Vector3 vec3) : this(NDalicPINVOKE.new_Vector2__SWIG_3(Vector3.getCPtr(vec3)), true)
230         {
231             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
232         }
233
234         /// <summary>
235         /// Constructor.
236         /// </summary>
237         /// <param name="vec4">Vector4 to create this vector from</param>
238         public Vector2(Vector4 vec4) : this(NDalicPINVOKE.new_Vector2__SWIG_4(Vector4.getCPtr(vec4)), true)
239         {
240             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
241         }
242
243         /// <summary>
244         /// (1.0f,1.0f)
245         /// </summary>
246         public static Vector2 One
247         {
248             get
249             {
250                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_ONE_get();
251                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
252                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
253                 return ret;
254             }
255         }
256
257         /// <summary>
258         /// Vector representing the X axis
259         /// </summary>
260         public static Vector2 XAxis
261         {
262             get
263             {
264                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_XAXIS_get();
265                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
266                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267                 return ret;
268             }
269         }
270
271         /// <summary>
272         /// Vector representing the Y axis
273         /// </summary>
274         public static Vector2 YAxis
275         {
276             get
277             {
278                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_YAXIS_get();
279                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
280                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
281                 return ret;
282             }
283         }
284
285         /// <summary>
286         /// Vector representing the negative X axis
287         /// </summary>
288         public static Vector2 NegativeXAxis
289         {
290             get
291             {
292                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_NEGATIVE_XAXIS_get();
293                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
294                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
295                 return ret;
296             }
297         }
298
299         /// <summary>
300         /// Vector representing the negative Y axis
301         /// </summary>
302         public static Vector2 NegativeYAxis
303         {
304             get
305             {
306                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_NEGATIVE_YAXIS_get();
307                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
308                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
309                 return ret;
310             }
311         }
312
313         /// <summary>
314         /// (0.0f, 0.0f)
315         /// </summary>
316         public static Vector2 Zero
317         {
318             get
319             {
320                 global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_ZERO_get();
321                 Vector2 ret = (cPtr == global::System.IntPtr.Zero) ? null : new Vector2(cPtr, false);
322                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
323                 return ret;
324             }
325         }
326
327         private Vector2 Add(Vector2 rhs)
328         {
329             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Add(swigCPtr, Vector2.getCPtr(rhs)), true);
330             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331             return ret;
332         }
333
334         private Vector2 AddAssign(Vector2 rhs)
335         {
336             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_AddAssign(swigCPtr, Vector2.getCPtr(rhs)), false);
337             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
338             return ret;
339         }
340
341         private Vector2 Subtract(Vector2 rhs)
342         {
343             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, Vector2.getCPtr(rhs)), true);
344             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345             return ret;
346         }
347
348         private Vector2 SubtractAssign(Vector2 rhs)
349         {
350             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_SubtractAssign(swigCPtr, Vector2.getCPtr(rhs)), false);
351             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352             return ret;
353         }
354
355         private Vector2 Multiply(Vector2 rhs)
356         {
357             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, Vector2.getCPtr(rhs)), true);
358             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
359             return ret;
360         }
361
362         private Vector2 Multiply(float rhs)
363         {
364             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, rhs), true);
365             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
366             return ret;
367         }
368
369         private Vector2 MultiplyAssign(Vector2 rhs)
370         {
371             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_MultiplyAssign__SWIG_0(swigCPtr, Vector2.getCPtr(rhs)), false);
372             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373             return ret;
374         }
375
376         private Vector2 MultiplyAssign(float rhs)
377         {
378             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_MultiplyAssign__SWIG_1(swigCPtr, rhs), false);
379             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
380             return ret;
381         }
382
383         private Vector2 Divide(Vector2 rhs)
384         {
385             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, Vector2.getCPtr(rhs)), true);
386             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387             return ret;
388         }
389
390         private Vector2 Divide(float rhs)
391         {
392             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, rhs), true);
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394             return ret;
395         }
396
397         private Vector2 DivideAssign(Vector2 rhs)
398         {
399             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_DivideAssign__SWIG_0(swigCPtr, Vector2.getCPtr(rhs)), false);
400             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401             return ret;
402         }
403
404         private Vector2 DivideAssign(float rhs)
405         {
406             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_DivideAssign__SWIG_1(swigCPtr, rhs), false);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408             return ret;
409         }
410
411         private Vector2 Subtract()
412         {
413             Vector2 ret = new Vector2(NDalicPINVOKE.Vector2_Subtract__SWIG_1(swigCPtr), true);
414             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415             return ret;
416         }
417
418         private bool EqualTo(Vector2 rhs)
419         {
420             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, Vector2.getCPtr(rhs));
421             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422             return ret;
423         }
424
425         private bool NotEqualTo(Vector2 rhs)
426         {
427             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, Vector2.getCPtr(rhs));
428             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429             return ret;
430         }
431
432         private float ValueOfIndex(uint index)
433         {
434             float ret = NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
435             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
436             return ret;
437         }
438
439         /// <summary>
440         /// Returns the length of the vector.
441         /// </summary>
442         /// <returns>The length of the vector</returns>
443         public float Length()
444         {
445             float ret = NDalicPINVOKE.Vector2_Length(swigCPtr);
446             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447             return ret;
448         }
449
450         /// <summary>
451         /// Returns the length of the vector squared.<br>
452         /// This is more efficient than Length() for threshold
453         /// testing as it avoids the use of a square root.<br>
454         /// </summary>
455         /// <returns>The length of the vector squared</returns>
456         public float LengthSquared()
457         {
458             float ret = NDalicPINVOKE.Vector2_LengthSquared(swigCPtr);
459             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
460             return ret;
461         }
462
463         /// <summary>
464         /// Sets the vector to be unit length, whilst maintaining its direction.
465         /// </summary>
466         public void Normalize()
467         {
468             NDalicPINVOKE.Vector2_Normalize(swigCPtr);
469             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470         }
471
472         /// <summary>
473         /// Clamps the vector between minimum and maximum vectors.
474         /// </summary>
475         /// <param name="min">The minimum vector</param>
476         /// <param name="max">The maximum vector</param>
477         public void Clamp(Vector2 min, Vector2 max)
478         {
479             NDalicPINVOKE.Vector2_Clamp(swigCPtr, Vector2.getCPtr(min), Vector2.getCPtr(max));
480             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
481         }
482
483         internal SWIGTYPE_p_float AsFloat()
484         {
485             global::System.IntPtr cPtr = NDalicPINVOKE.Vector2_AsFloat__SWIG_0(swigCPtr);
486             SWIGTYPE_p_float ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_float(cPtr, false);
487             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
488             return ret;
489         }
490
491         /// <summary>
492         /// x component
493         /// </summary>
494         public float X
495         {
496             set
497             {
498                 NDalicPINVOKE.Vector2_X_set(swigCPtr, value);
499                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
500             }
501             get
502             {
503                 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
504                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
505                 return ret;
506             }
507         }
508
509         /// <summary>
510         /// width
511         /// </summary>
512         public float Width
513         {
514             set
515             {
516                 NDalicPINVOKE.Vector2_Width_set(swigCPtr, value);
517                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
518             }
519             get
520             {
521                 float ret = NDalicPINVOKE.Vector2_Width_get(swigCPtr);
522                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
523                 return ret;
524             }
525         }
526
527         /// <summary>
528         /// y component
529         /// </summary>
530         public float Y
531         {
532             set
533             {
534                 NDalicPINVOKE.Vector2_Y_set(swigCPtr, value);
535                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
536             }
537             get
538             {
539                 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
540                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
541                 return ret;
542             }
543         }
544
545         /// <summary>
546         /// height
547         /// </summary>
548         public float Height
549         {
550             set
551             {
552                 NDalicPINVOKE.Vector2_Height_set(swigCPtr, value);
553                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
554             }
555             get
556             {
557                 float ret = NDalicPINVOKE.Vector2_Height_get(swigCPtr);
558                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
559                 return ret;
560             }
561         }
562
563     }
564
565 }