Release 4.0.0-preview1-00051
[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         /// Addition operator.
108         /// </summary>
109         /// <param name="arg1">Vector to add</param>
110         /// <param name="arg2">Vector to add</param>
111         /// <returns>A vector containing the result of the addition</returns>
112         public static RelativeVector2 operator +(RelativeVector2 arg1, RelativeVector2 arg2)
113         {
114             RelativeVector2 result = arg1.Add(arg2);
115             ValueCheck(result);
116             return result;
117         }
118
119         /// <summary>
120         /// Subtraction operator.
121         /// </summary>
122         /// <param name="arg1">Vector to subtract</param>
123         /// <param name="arg2">Vector to subtract</param>
124         /// <returns>A vector containing the result of the subtraction</returns>
125         public static RelativeVector2 operator -(RelativeVector2 arg1, RelativeVector2 arg2)
126         {
127             RelativeVector2 result = arg1.Subtract(arg2);
128             ValueCheck(result);
129             return result;
130         }
131
132         /// <summary>
133         /// Multiplication operator.
134         /// </summary>
135         /// <param name="arg1">The vector to multiply</param>
136         /// <param name="arg2">The vector to multiply</param>
137         /// <returns>A vector containing the result of the multiplication</returns>
138         public static RelativeVector2 operator *(RelativeVector2 arg1, RelativeVector2 arg2)
139         {
140             RelativeVector2 result = arg1.Multiply(arg2);
141             ValueCheck(result);
142             return result;
143         }
144
145         /// <summary>
146         /// Multiplication operator.
147         /// </summary>
148         /// <param name="arg1">The vector to multiply</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 RelativeVector2 operator *(RelativeVector2 arg1, float arg2)
152         {
153             RelativeVector2 result = arg1.Multiply(arg2);
154             ValueCheck(result);
155             return result;
156         }
157
158         /// <summary>
159         /// Division operator.
160         /// </summary>
161         /// <param name="arg1">The vector to divide</param>
162         /// <param name="arg2">The vector to divide</param>
163         /// <returns>A vector containing the result of the division</returns>
164         public static RelativeVector2 operator /(RelativeVector2 arg1, RelativeVector2 arg2)
165         {
166             RelativeVector2 result = arg1.Divide(arg2);
167             ValueCheck(result);
168             return result;
169         }
170
171         /// <summary>
172         /// Division operator.
173         /// </summary>
174         /// <param name="arg1">The vector to divide</param>
175         /// <param name="arg2">The float value to scale the vector by</param>
176         /// <returns>A vector containing the result of the scaling</returns>
177         public static RelativeVector2 operator /(RelativeVector2 arg1, float arg2)
178         {
179             RelativeVector2 result = arg1.Divide(arg2);
180             ValueCheck(result);
181             return result;
182         }
183
184
185         /// <summary>
186         /// Const array subscript operator overload. Should be 0, 1.
187         /// </summary>
188         /// <param name="index">Subscript index</param>
189         /// <returns>The float at the given index</returns>
190         public float this[uint index]
191         {
192             get
193             {
194                 return ValueOfIndex(index);
195             }
196         }
197
198         /// <summary>
199         /// </summary>
200         internal static RelativeVector2 GetRelativeVector2FromPtr(global::System.IntPtr cPtr)
201         {
202             RelativeVector2 ret = new RelativeVector2(cPtr, false);
203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
204             return ret;
205         }
206
207
208         /// <summary>
209         /// Constructor
210         /// </summary>
211         public RelativeVector2() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
212         {
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214         }
215
216         /// <summary>
217         /// Constructor
218         /// </summary>
219         /// <param name="x">x component</param>
220         /// <param name="y">y component</param>
221         public RelativeVector2(float x, float y) : this(NDalicPINVOKE.new_Vector2__SWIG_1(x, y), true)
222         {
223             ValueCheck(x);
224             ValueCheck(y);
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226         }
227
228         /// <summary>
229         /// Constructor
230         /// </summary>
231         /// <param name="relativeVector3">RelativeVector3 to create this vector from</param>
232         public RelativeVector2(RelativeVector3 relativeVector3) : this(NDalicPINVOKE.new_Vector2__SWIG_3(RelativeVector3.getCPtr(relativeVector3)), true)
233         {
234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235         }
236
237         /// <summary>
238         /// Constructor
239         /// </summary>
240         /// <param name="relativeVector4">RelativeVector4 to create this vector from</param>
241         public RelativeVector2(RelativeVector4 relativeVector4) : this(NDalicPINVOKE.new_Vector2__SWIG_4(RelativeVector4.getCPtr(relativeVector4)), true)
242         {
243             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
244         }
245
246
247         private RelativeVector2 Add(RelativeVector2 rhs)
248         {
249             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Add(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
250             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
251             return ret;
252         }
253
254         private RelativeVector2 Subtract(RelativeVector2 rhs)
255         {
256             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
257             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258             return ret;
259         }
260
261         private RelativeVector2 Multiply(RelativeVector2 rhs)
262         {
263             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
264             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
265             return ret;
266         }
267
268         private RelativeVector2 Multiply(float rhs)
269         {
270             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, rhs), true);
271             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
272             return ret;
273         }
274
275         private RelativeVector2 Divide(RelativeVector2 rhs)
276         {
277             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
278             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
279             return ret;
280         }
281
282         private RelativeVector2 Divide(float rhs)
283         {
284             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, rhs), true);
285             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
286             return ret;
287         }
288
289         private float ValueOfIndex(uint index)
290         {
291             float ret = NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
292             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
293             return ret;
294         }
295
296         /// <summary>
297         /// Compare if rhs is equal to
298         /// </summary>
299         /// <param name="rhs">The vector to compare</param>
300         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
301         public bool EqualTo(RelativeVector2 rhs)
302         {
303             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
304             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             return ret;
306         }
307
308         /// <summary>
309         /// Compare if rhs is not equal to
310         /// </summary>
311         /// <param name="rhs">The vector to compare</param>
312         /// <returns>Returns true if the two vectors are not equal, otherwise false</returns>
313         public bool NotEqualTo(RelativeVector2 rhs)
314         {
315             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
316             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317             return ret;
318         }
319
320
321         /// <summary>
322         /// x component
323         /// </summary>
324         public float X
325         {
326             set
327             {
328                 ValueCheck(value);
329                 NDalicPINVOKE.Vector2_X_set(swigCPtr, value);
330                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
331             }
332             get
333             {
334                 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
335                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
336                 return ret;
337             }
338         }
339
340         /// <summary>
341         /// y component
342         /// </summary>
343         public float Y
344         {
345             set
346             {
347                 ValueCheck(value);
348                 NDalicPINVOKE.Vector2_Y_set(swigCPtr, value);
349                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350             }
351             get
352             {
353                 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
354                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355                 return ret;
356             }
357         }
358
359         /// <summary>
360         /// </summary>
361         public static implicit operator Vector2(RelativeVector2 relativeVector2)
362         {
363             return new Vector2(relativeVector2.X, relativeVector2.Y);
364         }
365
366         /// <summary>
367         /// </summary>
368         public static implicit operator RelativeVector2(Vector2 vec)
369         {
370             ValueCheck(vec.X);
371             ValueCheck(vec.Y);
372             return new RelativeVector2(vec.X, vec.Y);
373         }
374
375         internal static void ValueCheck(RelativeVector2 relativeVector2)
376         {
377             if(relativeVector2.X < 0.0f)
378             {
379                 relativeVector2.X = 0.0f;
380                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
381             }
382             else if(relativeVector2.X > 1.0f)
383             {
384                 relativeVector2.X = 1.0f;
385                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
386             }
387             if(relativeVector2.Y < 0.0f)
388             {
389                 relativeVector2.Y = 0.0f;
390                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
391             }
392             else if(relativeVector2.Y > 1.0f)
393             {
394                 relativeVector2.Y = 1.0f;
395                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
396             }
397         }
398
399         internal static void ValueCheck(float value)
400         {
401             if(value < 0.0f)
402             {
403                 value = 0.0f;
404                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
405             }
406             else if(value > 1.0f)
407             {
408                 value = 1.0f;
409                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
410             }
411         }
412     }
413
414 }
415
416