e2fcd0f827f4be407aecf184c27a62d31c628a06
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Position2D.cs
1 /*
2  * Copyright (c) 2018 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 using System.Globalization;
19 using Tizen.NUI.Binding;
20
21 namespace Tizen.NUI
22 {
23
24     /// <summary>
25     /// Position2D is a two-dimensional vector.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     [Tizen.NUI.Binding.TypeConverter(typeof(Position2DTypeConverter))]
29     public class Position2D : global::System.IDisposable
30     {
31         /// <summary>
32         /// swigCMemOwn
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         protected bool swigCMemOwn;
36         /// <summary>
37         /// A Flat to check if it is already disposed.
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         protected bool disposed = false;
41
42         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
43         //A Flag to check who called Dispose(). (By User or DisposeQueue)
44         private bool isDisposeQueued = false;
45
46         private Position2DChangedCallback callback = null;
47
48         /// <summary>
49         /// The constructor.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         public Position2D() : this(Interop.Vector2.new_Vector2__SWIG_0(), true)
53         {
54             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
55         }
56
57         /// <summary>
58         /// The constructor.
59         /// </summary>
60         /// <param name="position">Position to create this vector from</param>
61         /// <since_tizen> 3 </since_tizen>
62         public Position2D(Position position) : this(Interop.Vector2.new_Vector2__SWIG_3(Position.getCPtr(position)), true)
63         {
64             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
65         }
66
67         /// <summary>
68         /// The constructor.
69         /// </summary>
70         /// <param name="x">x component</param>
71         /// <param name="y">y component</param>
72         /// <since_tizen> 3 </since_tizen>
73         public Position2D(int x, int y) : this(Interop.Vector2.new_Vector2__SWIG_1((float)x, (float)y), true)
74         {
75             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
76         }
77
78         internal Position2D(global::System.IntPtr cPtr, bool cMemoryOwn)
79         {
80             swigCMemOwn = cMemoryOwn;
81             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
82         }
83
84         internal Position2D(Position2DChangedCallback cb, int x, int y) : this(Interop.Vector2.new_Vector2__SWIG_1((float)x, (float)y), true)
85         {
86             callback = cb;
87             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
88         }
89
90         /// <summary>
91         /// Dispose.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         ~Position2D()
95         {
96             if (!isDisposeQueued)
97             {
98                 isDisposeQueued = true;
99                 DisposeQueue.Instance.Add(this);
100             }
101         }
102
103         internal delegate void Position2DChangedCallback(int x, int y);
104
105         /// <summary>
106         /// The x component.
107         /// </summary>
108         /// <since_tizen> 3 </since_tizen>
109         public int X
110         {
111             set
112             {
113                 Interop.Vector2.Vector2_X_set(swigCPtr, (float)value);
114                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
115
116                 callback?.Invoke(X, Y);
117             }
118             get
119             {
120                 float ret = Interop.Vector2.Vector2_X_get(swigCPtr);
121                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122                 return (int)ret;
123             }
124         }
125
126         /// <summary>
127         /// The y component.
128         /// </summary>
129         /// <since_tizen> 3 </since_tizen>
130         public int Y
131         {
132             set
133             {
134                 Interop.Vector2.Vector2_Y_set(swigCPtr, (float)value);
135                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
136
137                 callback?.Invoke(X, Y);
138             }
139             get
140             {
141                 float ret = Interop.Vector2.Vector2_Y_get(swigCPtr);
142                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
143                 return (int)ret;
144             }
145         }
146
147         /// <summary>
148         /// The const array subscript operator overload. Should be 0, or 1.
149         /// </summary>
150         /// <param name="index">The subscript index.</param>
151         /// <returns>The float at the given index.</returns>
152         /// <since_tizen> 3 </since_tizen>
153         public float this[uint index]
154         {
155             get
156             {
157                 return ValueOfIndex(index);
158             }
159         }
160
161         /// <summary>
162         /// Convert a string to Position2D.
163         /// </summary>
164         /// <param name="value">The string to convert.</param>
165         /// <returns>The converted value.</returns>
166         static public Position2D ConvertFromString(System.String value)
167         {
168             if (value != null)
169             {
170                 string[] parts = value.Split(',');
171                 if (parts.Length == 2)
172                 {
173                     return new Position2D(int.Parse(parts[0].Trim()), int.Parse(parts[1].Trim()));
174                 }
175             }
176
177             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Position2D)}");
178         }
179
180         /// <summary>
181         /// Constructor a Position2D from a stirng.
182         /// </summary>
183         public static implicit operator Position2D(System.String value)
184         {
185             return ConvertFromString(value);
186         }
187
188         /// <summary>
189         /// Dispose.
190         /// </summary>
191         /// <since_tizen> 3 </since_tizen>
192         public void Dispose()
193         {
194             //Throw excpetion if Dispose() is called in separate thread.
195             if (!Window.IsInstalled())
196             {
197                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
198             }
199
200             if (isDisposeQueued)
201             {
202                 Dispose(DisposeTypes.Implicit);
203             }
204             else
205             {
206                 Dispose(DisposeTypes.Explicit);
207                 System.GC.SuppressFinalize(this);
208             }
209         }
210
211         /// <summary>
212         /// The addition operator.
213         /// </summary>
214         /// <param name="arg1">The vector to add.</param>
215         /// <param name="arg2">The vector to add.</param>
216         /// <returns>The vector containing the result of the addition.</returns>
217         /// <since_tizen> 3 </since_tizen>
218         public static Position2D operator +(Position2D arg1, Position2D arg2)
219         {
220             return arg1.Add(arg2);
221         }
222
223         /// <summary>
224         /// The subtraction operator.
225         /// </summary>
226         /// <param name="arg1">The vector to subtract.</param>
227         /// <param name="arg2">The vector to subtract.</param>
228         /// <returns>The vector containing the result of the subtraction.</returns>
229         /// <since_tizen> 3 </since_tizen>
230         public static Position2D operator -(Position2D arg1, Position2D arg2)
231         {
232             return arg1.Subtract(arg2);
233         }
234
235         /// <summary>
236         /// The unary negation operator.
237         /// </summary>
238         /// <param name="arg1">The vector to negate.</param>
239         /// <returns>The vector containing the negation.</returns>
240         /// <since_tizen> 3 </since_tizen>
241         public static Position2D operator -(Position2D arg1)
242         {
243             return arg1.Subtract();
244         }
245
246         /// <summary>
247         /// The multiplication operator.
248         /// </summary>
249         /// <param name="arg1">The vector to multiply.</param>
250         /// <param name="arg2">The vector to multiply.</param>
251         /// <returns>The vector containing the result of the multiplication.</returns>
252         /// <since_tizen> 3 </since_tizen>
253         public static Position2D operator *(Position2D arg1, Position2D arg2)
254         {
255             return arg1.Multiply(arg2);
256         }
257
258         /// <summary>
259         /// The multiplication operator.
260         /// </summary>
261         /// <param name="arg1">The vector to multiply.</param>
262         /// <param name="arg2">The integer value to scale the vector.</param>
263         /// <returns>The vector containing the result of the multiplication.</returns>
264         /// <since_tizen> 3 </since_tizen>
265         public static Position2D operator *(Position2D arg1, int arg2)
266         {
267             return arg1.Multiply(arg2);
268         }
269
270         /// <summary>
271         /// The division operator.
272         /// </summary>
273         /// <param name="arg1">The vector to divide.</param>
274         /// <param name="arg2">The vector to divide.</param>
275         /// <returns>The vector containing the result of the division.</returns>
276         /// <since_tizen> 3 </since_tizen>
277         public static Position2D operator /(Position2D arg1, Position2D arg2)
278         {
279             return arg1.Divide(arg2);
280         }
281
282         /// <summary>
283         /// The division operator.
284         /// </summary>
285         /// <param name="arg1">The vector to divide.</param>
286         /// <param name="arg2">The integer value to scale the vector by.</param>
287         /// <returns>The vector containing the result of the division.</returns>
288         /// <since_tizen> 3 </since_tizen>
289         public static Position2D operator /(Position2D arg1, int arg2)
290         {
291             return arg1.Divide(arg2);
292         }
293
294         /// <summary>
295         /// Determines whether the specified object is equal to the current object.
296         /// </summary>
297         /// <param name="obj">The object to compare with the current object.</param>
298         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
299         public override bool Equals(System.Object obj)
300         {
301             Position2D position2D = obj as Position2D;
302             bool equal = false;
303             if (X == position2D?.X && Y == position2D?.Y)
304             {
305                 equal = true;
306             }
307             return equal;
308         }
309
310         /// <summary>
311         /// Gets the the hash code of this Position2D.
312         /// </summary>
313         /// <returns>The Hash Code.</returns>
314         /// <since_tizen> 6 </since_tizen>
315         public override int GetHashCode()
316         {
317             return swigCPtr.Handle.GetHashCode();
318         }
319
320         /// <summary>
321         /// Compares if the rhs is equal to.
322         /// </summary>
323         /// <param name="rhs">The vector to compare</param>
324         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
325         /// <since_tizen> 3 </since_tizen>
326         public bool EqualTo(Position2D rhs)
327         {
328             bool ret = Interop.Vector2.Vector2_EqualTo(swigCPtr, Position2D.getCPtr(rhs));
329             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
330             return ret;
331         }
332
333         /// <summary>
334         /// Compares if the rhs is not equal to.
335         /// </summary>
336         /// <param name="rhs">The vector to compare.</param>
337         /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
338         /// <since_tizen> 3 </since_tizen>
339         public bool NotEqualTo(Position2D rhs)
340         {
341             bool ret = Interop.Vector2.Vector2_NotEqualTo(swigCPtr, Position2D.getCPtr(rhs));
342             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
343             return ret;
344         }
345
346         /// <summary>
347         /// Converts a Position2D instance to a Vector2 instance.
348         /// </summary>
349         /// <param name="position2d">An object of the Position2D type.</param>
350         /// <returns>return an object of the Vector2 type</returns>
351         /// <since_tizen> 3 </since_tizen>
352         public static implicit operator Vector2(Position2D position2d)
353         {
354             return new Vector2((float)position2d.X, (float)position2d.Y);
355         }
356
357         /// <summary>
358         /// Converts a Vector2 instance to a Position2D instance.
359         /// </summary>
360         /// <param name="vec">An object of the Vector2 type.</param>
361         /// <returns>return an object of the Position2D type</returns>
362         /// <since_tizen> 3 </since_tizen>
363         public static implicit operator Position2D(Vector2 vec)
364         {
365             return new Position2D((int)vec.X, (int)vec.Y);
366         }
367
368         internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
369         {
370             Position2D ret = new Position2D(cPtr, false);
371             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
372             return ret;
373         }
374
375         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Position2D obj)
376         {
377             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
378         }
379
380         /// <summary>
381         /// Dispose.
382         /// </summary>
383         /// <param name="type">The dispose type.</param>
384         /// <since_tizen> 3 </since_tizen>
385         protected virtual void Dispose(DisposeTypes type)
386         {
387             if (disposed)
388             {
389                 return;
390             }
391
392             if (type == DisposeTypes.Explicit)
393             {
394                 //Called by User
395                 //Release your own managed resources here.
396                 //You should release all of your own disposable objects here.
397             }
398
399             //Release your own unmanaged resources here.
400             //You should not access any managed member here except static instance.
401             //because the execution order of Finalizes is non-deterministic.
402
403             if (swigCPtr.Handle != global::System.IntPtr.Zero)
404             {
405                 if (swigCMemOwn)
406                 {
407                     swigCMemOwn = false;
408                     Interop.Vector2.delete_Vector2(swigCPtr);
409                 }
410                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
411             }
412             disposed = true;
413         }
414
415         private Position2D Add(Position2D rhs)
416         {
417             Position2D ret = new Position2D(Interop.Vector2.Vector2_Add(swigCPtr, Position2D.getCPtr(rhs)), true);
418             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419             return ret;
420         }
421
422         private Position2D Subtract(Position2D rhs)
423         {
424             Position2D ret = new Position2D(Interop.Vector2.Vector2_Subtract__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429
430         private Position2D Multiply(Position2D rhs)
431         {
432             Position2D ret = new Position2D(Interop.Vector2.Vector2_Multiply__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
433             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
434             return ret;
435         }
436
437         private Position2D Multiply(int rhs)
438         {
439             Position2D ret = new Position2D(Interop.Vector2.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
440             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
441             return ret;
442         }
443
444
445         private Position2D Divide(Position2D rhs)
446         {
447             Position2D ret = new Position2D(Interop.Vector2.Vector2_Divide__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
448             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
449             return ret;
450         }
451
452         private Position2D Divide(int rhs)
453         {
454             Position2D ret = new Position2D(Interop.Vector2.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
455             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
456             return ret;
457         }
458
459         private Position2D Subtract()
460         {
461             Position2D ret = new Position2D(Interop.Vector2.Vector2_Subtract__SWIG_1(swigCPtr), true);
462             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
463             return ret;
464         }
465
466         private int ValueOfIndex(uint index)
467         {
468             int ret = (int)Interop.Vector2.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
469             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
470             return ret;
471         }
472
473     }
474
475 }
476