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