Release 4.0.0-preview1-00235
[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         public static Position2D operator +(Position2D arg1, Position2D arg2)
110         {
111             return arg1.Add(arg2);
112         }
113
114         /// <summary>
115         /// The subtraction operator.
116         /// </summary>
117         /// <param name="arg1">The vector to subtract.</param>
118         /// <param name="arg2">The vector to subtract.</param>
119         /// <returns>The vector containing the result of the subtraction.</returns>
120         public static Position2D operator -(Position2D arg1, Position2D arg2)
121         {
122             return arg1.Subtract(arg2);
123         }
124
125         /// <summary>
126         /// The unary negation operator.
127         /// </summary>
128         /// <param name="arg1">The vector to negate.</param>
129         /// <returns>The vector containing the negation.</returns>
130         public static Position2D operator -(Position2D arg1)
131         {
132             return arg1.Subtract();
133         }
134
135         /// <summary>
136         /// The multiplication operator.
137         /// </summary>
138         /// <param name="arg1">The vector to multiply.</param>
139         /// <param name="arg2">The vector to multiply.</param>
140         /// <returns>The vector containing the result of the multiplication.</returns>
141         public static Position2D operator *(Position2D arg1, Position2D arg2)
142         {
143             return arg1.Multiply(arg2);
144         }
145
146         /// <summary>
147         /// The multiplication operator.
148         /// </summary>
149         /// <param name="arg1">The vector to multiply.</param>
150         /// <param name="arg2">The integer value to scale the vector.</param>
151         /// <returns>The vector containing the result of the multiplication.</returns>
152         public static Position2D operator *(Position2D arg1, int arg2)
153         {
154             return arg1.Multiply(arg2);
155         }
156
157         /// <summary>
158         /// The division operator.
159         /// </summary>
160         /// <param name="arg1">The vector to divide.</param>
161         /// <param name="arg2">The vector to divide.</param>
162         /// <returns>The vector containing the result of the division.</returns>
163         public static Position2D operator /(Position2D arg1, Position2D arg2)
164         {
165             return arg1.Divide(arg2);
166         }
167
168         /// <summary>
169         /// The division operator.
170         /// </summary>
171         /// <param name="arg1">The vector to divide.</param>
172         /// <param name="arg2">The integer value to scale the vector by.</param>
173         /// <returns>The vector containing the result of the division.</returns>
174         public static Position2D operator /(Position2D arg1, int arg2)
175         {
176             return arg1.Divide(arg2);
177         }
178
179         /// <summary>
180         /// The const array subscript operator overload. Should be 0, or 1.
181         /// </summary>
182         /// <param name="index">The subscript index.</param>
183         /// <returns>The float at the given index.</returns>
184         public float this[uint index]
185         {
186             get
187             {
188                 return ValueOfIndex(index);
189             }
190         }
191
192         internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
193         {
194             Position2D ret = new Position2D(cPtr, false);
195             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
196             return ret;
197         }
198
199         /// <summary>
200         /// The constructor.
201         /// </summary>
202         public Position2D() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
203         {
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205         }
206
207         /// <summary>
208         /// The constructor.
209         /// </summary>
210         /// <param name="x">x component</param>
211         /// <param name="y">y component</param>
212         public Position2D(int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
213         {
214             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
215         }
216
217         /// <summary>
218         /// The constructor.
219         /// </summary>
220         /// <param name="position">Position to create this vector from</param>
221         public Position2D(Position position) : this(NDalicPINVOKE.new_Vector2__SWIG_3(Position.getCPtr(position)), true)
222         {
223             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224         }
225
226         private Position2D Add(Position2D rhs)
227         {
228             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Add(swigCPtr, Position2D.getCPtr(rhs)), true);
229             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             return ret;
231         }
232
233         private Position2D Subtract(Position2D rhs)
234         {
235             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240
241         private Position2D Multiply(Position2D rhs)
242         {
243             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
244             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
245             return ret;
246         }
247
248         private Position2D Multiply(int rhs)
249         {
250             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
251             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
252             return ret;
253         }
254
255
256         private Position2D Divide(Position2D rhs)
257         {
258             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
259             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
260             return ret;
261         }
262
263         private Position2D Divide(int rhs)
264         {
265             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
266             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
267             return ret;
268         }
269
270         private Position2D Subtract()
271         {
272             Position2D ret = new Position2D(NDalicPINVOKE.Vector2_Subtract__SWIG_1(swigCPtr), true);
273             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
274             return ret;
275         }
276
277         /// <summary>
278         /// Compares if the rhs is equal to.
279         /// </summary>
280         /// <param name="rhs">The vector to compare</param>
281         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
282         public bool EqualTo(Position2D rhs)
283         {
284             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, Position2D.getCPtr(rhs));
285             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
286             return ret;
287         }
288
289         /// <summary>
290         /// Compares if the rhs is not equal to.
291         /// </summary>
292         /// <param name="rhs">The vector to compare.</param>
293         /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
294         public bool NotEqualTo(Position2D rhs)
295         {
296             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, Position2D.getCPtr(rhs));
297             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
298             return ret;
299         }
300
301         private int ValueOfIndex(uint index)
302         {
303             int ret = (int)NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
304             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             return ret;
306         }
307
308         /// <summary>
309         /// The x component.
310         /// </summary>
311         public int X
312         {
313             set
314             {
315                 NDalicPINVOKE.Vector2_X_set(swigCPtr, (float)value);
316                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
317             }
318             get
319             {
320                 float ret = NDalicPINVOKE.Vector2_X_get(swigCPtr);
321                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
322                 return (int)ret;
323             }
324         }
325
326         /// <summary>
327         /// The y component.
328         /// </summary>
329         public int Y
330         {
331             set
332             {
333                 NDalicPINVOKE.Vector2_Y_set(swigCPtr, (float)value);
334                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
335             }
336             get
337             {
338                 float ret = NDalicPINVOKE.Vector2_Y_get(swigCPtr);
339                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
340                 return (int)ret;
341             }
342         }
343
344         /// <summary>
345         /// Converts a Position2D instance to a Vector2 instance.
346         /// </summary>
347         public static implicit operator Vector2(Position2D position2d)
348         {
349             return new Vector2((float)position2d.X, (float)position2d.Y);
350         }
351
352         /// <summary>
353         /// Converts a Vector2 instance to a Position2D instance.
354         /// </summary>
355         public static implicit operator Position2D(Vector2 vec)
356         {
357             return new Position2D((int)vec.X, (int)vec.Y);
358         }
359
360         /// <summary>
361         /// Converts a Position2D instance to a Uint16Pair instance.
362         /// </summary>
363         public static implicit operator Uint16Pair(Position2D position2d)
364         {
365             return new Uint16Pair((uint)position2d.X, (uint)position2d.Y);
366         }
367
368         /// <summary>
369         /// Converts a Uint16Pair instance to a Position2D instance.
370         /// </summary>
371         public static implicit operator Position2D(Uint16Pair pair)
372         {
373             return new Position2D((int)pair.GetX(), (int)pair.GetY());
374         }
375
376     }
377
378 }
379
380