Implement normalized Vector2/3/4 class.
[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         ~RelativeVector2()
43         {
44             DisposeQueue.Instance.Add(this);
45         }
46
47         public virtual void Dispose()
48         {
49             if (!Stage.IsInstalled())
50             {
51                 DisposeQueue.Instance.Add(this);
52                 return;
53             }
54
55             lock (this)
56             {
57                 if (swigCPtr.Handle != global::System.IntPtr.Zero)
58                 {
59                     if (swigCMemOwn)
60                     {
61                         swigCMemOwn = false;
62                         NDalicPINVOKE.delete_Vector2(swigCPtr);
63                     }
64                     swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
65                 }
66                 global::System.GC.SuppressFinalize(this);
67             }
68         }
69
70
71         /// <summary>
72         /// Addition operator.
73         /// </summary>
74         /// <param name="arg1">Vector to add</param>
75         /// <param name="arg2">Vector to add</param>
76         /// <returns>A vector containing the result of the addition</returns>
77         public static RelativeVector2 operator +(RelativeVector2 arg1, RelativeVector2 arg2)
78         {
79             RelativeVector2 result = arg1.Add(arg2);
80             ValueCheck(result);
81             return result;
82         }
83
84         /// <summary>
85         /// Subtraction operator.
86         /// </summary>
87         /// <param name="arg1">Vector to subtract</param>
88         /// <param name="arg2">Vector to subtract</param>
89         /// <returns>A vector containing the result of the subtraction</returns>
90         public static RelativeVector2 operator -(RelativeVector2 arg1, RelativeVector2 arg2)
91         {
92             RelativeVector2 result = arg1.Subtract(arg2);
93             ValueCheck(result);
94             return result;
95         }
96
97         /// <summary>
98         /// Multiplication operator.
99         /// </summary>
100         /// <param name="arg1">The vector to multiply</param>
101         /// <param name="arg2">The vector to multiply</param>
102         /// <returns>A vector containing the result of the multiplication</returns>
103         public static RelativeVector2 operator *(RelativeVector2 arg1, RelativeVector2 arg2)
104         {
105             RelativeVector2 result = arg1.Multiply(arg2);
106             ValueCheck(result);
107             return result;
108         }
109
110         /// <summary>
111         /// Multiplication operator.
112         /// </summary>
113         /// <param name="arg1">The vector to multiply</param>
114         /// <param name="arg2">The float value to scale the vector</param>
115         /// <returns>A vector containing the result of the scaling</returns>
116         public static RelativeVector2 operator *(RelativeVector2 arg1, float arg2)
117         {
118             RelativeVector2 result = arg1.Multiply(arg2);
119             ValueCheck(result);
120             return result;
121         }
122
123         /// <summary>
124         /// Division operator.
125         /// </summary>
126         /// <param name="arg1">The vector to divide</param>
127         /// <param name="arg2">The vector to divide</param>
128         /// <returns>A vector containing the result of the division</returns>
129         public static RelativeVector2 operator /(RelativeVector2 arg1, RelativeVector2 arg2)
130         {
131             RelativeVector2 result = arg1.Divide(arg2);
132             ValueCheck(result);
133             return result;
134         }
135
136         /// <summary>
137         /// Division operator.
138         /// </summary>
139         /// <param name="arg1">The vector to divide</param>
140         /// <param name="arg2">The float value to scale the vector by</param>
141         /// <returns>A vector containing the result of the scaling</returns>
142         public static RelativeVector2 operator /(RelativeVector2 arg1, float arg2)
143         {
144             RelativeVector2 result = arg1.Divide(arg2);
145             ValueCheck(result);
146             return result;
147         }
148
149
150         /// <summary>
151         /// Const array subscript operator overload. Should be 0, 1.
152         /// </summary>
153         /// <param name="index">Subscript index</param>
154         /// <returns>The float at the given index</returns>
155         public float this[uint index]
156         {
157             get
158             {
159                 return ValueOfIndex(index);
160             }
161         }
162
163         /// <summary>
164         /// </summary>
165         internal static RelativeVector2 GetRelativeVector2FromPtr(global::System.IntPtr cPtr)
166         {
167             RelativeVector2 ret = new RelativeVector2(cPtr, false);
168             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
169             return ret;
170         }
171
172
173         /// <summary>
174         /// Constructor
175         /// </summary>
176         public RelativeVector2() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
177         {
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179         }
180
181         /// <summary>
182         /// Constructor
183         /// </summary>
184         /// <param name="x">x component</param>
185         /// <param name="y">y component</param>
186         public RelativeVector2(float x, float y) : this(NDalicPINVOKE.new_Vector2__SWIG_1(x, y), true)
187         {
188             ValueCheck(x);
189             ValueCheck(y);
190             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
191         }
192
193         /// <summary>
194         /// Constructor
195         /// </summary>
196         /// <param name="relativeVector3">RelativeVector3 to create this vector from</param>
197         public RelativeVector2(RelativeVector3 relativeVector3) : this(NDalicPINVOKE.new_Vector2__SWIG_3(RelativeVector3.getCPtr(relativeVector3)), true)
198         {
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200         }
201
202         /// <summary>
203         /// Constructor
204         /// </summary>
205         /// <param name="relativeVector4">RelativeVector4 to create this vector from</param>
206         public RelativeVector2(RelativeVector4 relativeVector4) : this(NDalicPINVOKE.new_Vector2__SWIG_4(RelativeVector4.getCPtr(relativeVector4)), true)
207         {
208             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
209         }
210
211
212         private RelativeVector2 Add(RelativeVector2 rhs)
213         {
214             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Add(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216             return ret;
217         }
218
219         private RelativeVector2 Subtract(RelativeVector2 rhs)
220         {
221             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223             return ret;
224         }
225
226         private RelativeVector2 Multiply(RelativeVector2 rhs)
227         {
228             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
229             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             return ret;
231         }
232
233         private RelativeVector2 Multiply(float rhs)
234         {
235             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, rhs), true);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240         private RelativeVector2 Divide(RelativeVector2 rhs)
241         {
242             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, RelativeVector2.getCPtr(rhs)), true);
243             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
244             return ret;
245         }
246
247         private RelativeVector2 Divide(float rhs)
248         {
249             RelativeVector2 ret = new RelativeVector2(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, rhs), true);
250             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
251             return ret;
252         }
253
254         private float ValueOfIndex(uint index)
255         {
256             float ret = NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
257             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258             return ret;
259         }
260
261         /// <summary>
262         /// Compare if rhs is equal to
263         /// </summary>
264         /// <param name="rhs">The vector to compare</param>
265         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
266         public bool EqualTo(RelativeVector2 rhs)
267         {
268             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
269             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
270             return ret;
271         }
272
273         /// <summary>
274         /// Compare if rhs is not equal to
275         /// </summary>
276         /// <param name="rhs">The vector to compare</param>
277         /// <returns>Returns true if the two vectors are not equal, otherwise false</returns>
278         public bool NotEqualTo(RelativeVector2 rhs)
279         {
280             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, RelativeVector2.getCPtr(rhs));
281             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
282             return ret;
283         }
284
285
286         /// <summary>
287         /// x component
288         /// </summary>
289         public float X
290         {
291             set
292             {
293                 ValueCheck(value);
294                 NDalicPINVOKE.Vector2_X_set(swigCPtr, value);
295                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296             }
297             get
298             {
299                 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
300                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
301                 return ret;
302             }
303         }
304
305         /// <summary>
306         /// y component
307         /// </summary>
308         public float Y
309         {
310             set
311             {
312                 ValueCheck(value);
313                 NDalicPINVOKE.Vector2_Y_set(swigCPtr, value);
314                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
315             }
316             get
317             {
318                 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
319                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
320                 return ret;
321             }
322         }
323
324         /// <summary>
325         /// </summary>
326         public static implicit operator Vector2(RelativeVector2 relativeVector2)
327         {
328             return new Vector2(relativeVector2.X, relativeVector2.Y);
329         }
330
331         /// <summary>
332         /// </summary>
333         public static implicit operator RelativeVector2(Vector2 vec)
334         {
335             ValueCheck(vec.X);
336             ValueCheck(vec.Y);
337             return new RelativeVector2(vec.X, vec.Y);
338         }
339
340         internal static void ValueCheck(RelativeVector2 relativeVector2)
341         {
342             if(relativeVector2.X < 0.0f)
343             {
344                 relativeVector2.X = 0.0f;
345                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
346             }
347             else if(relativeVector2.X > 1.0f)
348             {
349                 relativeVector2.X = 1.0f;
350                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
351             }
352             if(relativeVector2.Y < 0.0f)
353             {
354                 relativeVector2.Y = 0.0f;
355                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
356             }
357             else if(relativeVector2.Y > 1.0f)
358             {
359                 relativeVector2.Y = 1.0f;
360                 Tizen.Log.Fatal("NUI", "The value of Result is invalid! Should be between [0, 1].");
361             }
362         }
363
364         internal static void ValueCheck(float value)
365         {
366             if(value < 0.0f)
367             {
368                 value = 0.0f;
369                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
370             }
371             else if(value > 1.0f)
372             {
373                 value = 1.0f;
374                 Tizen.Log.Fatal("NUI", "The value of Parameters is invalid! Should be between [0, 1].");
375             }
376         }
377     }
378
379 }
380
381