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