Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Position2D.cs
1 /*
2  * Copyright (c) 2019 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.ComponentModel;
19 using Tizen.NUI.Binding;
20
21 namespace Tizen.NUI
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 : Disposable, ICloneable
29     {
30         private Position2DChangedCallback callback = null;
31
32         /// <summary>
33         /// The constructor.
34         /// </summary>
35         /// <remarks>
36         /// Position2D and Position are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
37         /// For example, the followings are possible. <br />
38         /// view.Position2D = new Position(10.0f, 10.0f, 10.0f); // be aware that here the z value(10.0f) will be lost. <br />
39         /// view.Position = new Position2D(10, 10); // be aware that here the z value is 0.0f by default. <br />
40         /// </remarks>
41         /// <since_tizen> 3 </since_tizen>
42         public Position2D() : this(Interop.Vector2.new_Vector2__SWIG_0(), true)
43         {
44             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
45         }
46
47         /// <summary>
48         /// The constructor.
49         /// </summary>
50         /// <param name="position">Position to create this vector from</param>
51         /// <since_tizen> 3 </since_tizen>
52         public Position2D(Position position) : this(Interop.Vector2.new_Vector2__SWIG_3(Position.getCPtr(position)), true)
53         {
54             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
55         }
56
57         /// <summary>
58         /// The constructor.
59         /// </summary>
60         /// <param name="x">x component</param>
61         /// <param name="y">y component</param>
62         /// <remarks>
63         /// Position2D and Position are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
64         /// For example, the followings are possible. <br />
65         /// view.Position2D = new Position(10.0f, 10.0f, 10.0f); // be aware that here the z value(10.0f) will be lost. <br />
66         /// view.Position = new Position2D(10, 10); // be aware that here the z value is 0.0f by default. <br />
67         /// </remarks>
68         /// <since_tizen> 3 </since_tizen>
69         public Position2D(int x, int y) : this(Interop.Vector2.new_Vector2__SWIG_1((float)x, (float)y), true)
70         {
71             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72         }
73
74         internal Position2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
75         {
76         }
77
78         internal Position2D(Position2DChangedCallback cb, int x, int y) : this(Interop.Vector2.new_Vector2__SWIG_1((float)x, (float)y), true)
79         {
80             callback = cb;
81             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
82         }
83
84         internal delegate void Position2DChangedCallback(int x, int y);
85
86         /// <summary>
87         /// The x component.
88         /// </summary>
89         /// <since_tizen> 3 </since_tizen>
90         [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor")]
91         public int X
92         {
93             set
94             {
95                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor");
96
97                 Interop.Vector2.Vector2_X_set(swigCPtr, (float)value);
98                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
99
100                 callback?.Invoke(X, Y);
101             }
102             get
103             {
104                 float ret = Interop.Vector2.Vector2_X_get(swigCPtr);
105                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
106                 return (int)ret;
107             }
108         }
109
110         /// <summary>
111         /// The y component.
112         /// </summary>
113         /// <since_tizen> 3 </since_tizen>
114         [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor")]
115         public int Y
116         {
117             set
118             {
119                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor");
120
121                 Interop.Vector2.Vector2_Y_set(swigCPtr, (float)value);
122                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
123
124                 callback?.Invoke(X, Y);
125             }
126             get
127             {
128                 float ret = Interop.Vector2.Vector2_Y_get(swigCPtr);
129                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
130                 return (int)ret;
131             }
132         }
133
134         /// <summary>
135         /// The const array subscript operator overload. Should be 0, or 1.
136         /// </summary>
137         /// <param name="index">The subscript index.</param>
138         /// <returns>The float at the given index.</returns>
139         /// <since_tizen> 3 </since_tizen>
140         public float this[uint index]
141         {
142             get
143             {
144                 return ValueOfIndex(index);
145             }
146         }
147
148         /// <summary>
149         /// Convert a string to Position2D.
150         /// </summary>
151         /// <param name="value">The string to convert.</param>
152         /// <returns>The converted value.</returns>
153         static public Position2D ConvertFromString(System.String value)
154         {
155             if (value != null)
156             {
157                 string[] parts = value.Split(',');
158                 if (parts.Length == 2)
159                 {
160                     int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
161                     int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
162                     return new Position2D(x, y);
163                 }
164             }
165
166             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Position2D)}");
167         }
168
169         /// <summary>
170         /// Constructor a Position2D from a stirng.
171         /// </summary>
172         public static implicit operator Position2D(System.String value)
173         {
174             return ConvertFromString(value);
175         }
176
177         /// <summary>
178         /// The addition operator.
179         /// </summary>
180         /// <param name="arg1">The vector to add.</param>
181         /// <param name="arg2">The vector to add.</param>
182         /// <returns>The vector containing the result of the addition.</returns>
183         /// <since_tizen> 3 </since_tizen>
184         public static Position2D operator +(Position2D arg1, Position2D arg2)
185         {
186             return arg1.Add(arg2);
187         }
188
189         /// <summary>
190         /// The subtraction operator.
191         /// </summary>
192         /// <param name="arg1">The vector to subtract.</param>
193         /// <param name="arg2">The vector to subtract.</param>
194         /// <returns>The vector containing the result of the subtraction.</returns>
195         /// <since_tizen> 3 </since_tizen>
196         public static Position2D operator -(Position2D arg1, Position2D arg2)
197         {
198             return arg1.Subtract(arg2);
199         }
200
201         /// <summary>
202         /// The unary negation operator.
203         /// </summary>
204         /// <param name="arg1">The vector to negate.</param>
205         /// <returns>The vector containing the negation.</returns>
206         /// <since_tizen> 3 </since_tizen>
207         public static Position2D operator -(Position2D arg1)
208         {
209             return arg1.Subtract();
210         }
211
212         /// <summary>
213         /// The multiplication operator.
214         /// </summary>
215         /// <param name="arg1">The vector to multiply.</param>
216         /// <param name="arg2">The vector to multiply.</param>
217         /// <returns>The vector containing the result of the multiplication.</returns>
218         /// <since_tizen> 3 </since_tizen>
219         public static Position2D operator *(Position2D arg1, Position2D arg2)
220         {
221             return arg1.Multiply(arg2);
222         }
223
224         /// <summary>
225         /// The multiplication operator.
226         /// </summary>
227         /// <param name="arg1">The vector to multiply.</param>
228         /// <param name="arg2">The integer value to scale the vector.</param>
229         /// <returns>The vector containing the result of the multiplication.</returns>
230         /// <since_tizen> 3 </since_tizen>
231         public static Position2D operator *(Position2D arg1, int arg2)
232         {
233             return arg1.Multiply(arg2);
234         }
235
236         /// <summary>
237         /// The division operator.
238         /// </summary>
239         /// <param name="arg1">The vector to divide.</param>
240         /// <param name="arg2">The vector to divide.</param>
241         /// <returns>The vector containing the result of the division.</returns>
242         /// <since_tizen> 3 </since_tizen>
243         public static Position2D operator /(Position2D arg1, Position2D arg2)
244         {
245             return arg1.Divide(arg2);
246         }
247
248         /// <summary>
249         /// The division operator.
250         /// </summary>
251         /// <param name="arg1">The vector to divide.</param>
252         /// <param name="arg2">The integer value to scale the vector by.</param>
253         /// <returns>The vector containing the result of the division.</returns>
254         /// <since_tizen> 3 </since_tizen>
255         public static Position2D operator /(Position2D arg1, int arg2)
256         {
257             return arg1.Divide(arg2);
258         }
259
260         /// <summary>
261         /// Determines whether the specified object is equal to the current object.
262         /// </summary>
263         /// <param name="obj">The object to compare with the current object.</param>
264         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
265         public override bool Equals(System.Object obj)
266         {
267             Position2D position2D = obj as Position2D;
268             bool equal = false;
269             if (X == position2D?.X && Y == position2D?.Y)
270             {
271                 equal = true;
272             }
273             return equal;
274         }
275
276         /// <summary>
277         /// Gets the the hash code of this Position2D.
278         /// </summary>
279         /// <returns>The Hash Code.</returns>
280         /// <since_tizen> 6 </since_tizen>
281         public override int GetHashCode()
282         {
283             return swigCPtr.Handle.GetHashCode();
284         }
285
286         /// <summary>
287         /// Compares if the rhs is equal to.
288         /// </summary>
289         /// <param name="rhs">The vector to compare</param>
290         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
291         /// <since_tizen> 3 </since_tizen>
292         public bool EqualTo(Position2D rhs)
293         {
294             bool ret = Interop.Vector2.Vector2_EqualTo(swigCPtr, Position2D.getCPtr(rhs));
295             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296             return ret;
297         }
298
299         /// <summary>
300         /// Compares if the rhs is not equal to.
301         /// </summary>
302         /// <param name="rhs">The vector to compare.</param>
303         /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
304         /// <since_tizen> 3 </since_tizen>
305         public bool NotEqualTo(Position2D rhs)
306         {
307             bool ret = Interop.Vector2.Vector2_NotEqualTo(swigCPtr, Position2D.getCPtr(rhs));
308             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
309             return ret;
310         }
311
312         /// <summary>
313         /// Converts a Position2D instance to a Vector2 instance.
314         /// </summary>
315         /// <param name="position2d">An object of the Position2D type.</param>
316         /// <returns>return an object of the Vector2 type</returns>
317         /// <since_tizen> 3 </since_tizen>
318         public static implicit operator Vector2(Position2D position2d)
319         {
320             return new Vector2((float)position2d.X, (float)position2d.Y);
321         }
322
323         /// <summary>
324         /// Converts a Vector2 instance to a Position2D instance.
325         /// </summary>
326         /// <param name="vec">An object of the Vector2 type.</param>
327         /// <returns>return an object of the Position2D type</returns>
328         /// <since_tizen> 3 </since_tizen>
329         public static implicit operator Position2D(Vector2 vec)
330         {
331             return new Position2D((int)vec.X, (int)vec.Y);
332         }
333
334         /// <summary>
335         /// Implicit type cast operator, Position to Position2D
336         /// </summary>
337         /// <param name="position">The object of Position type.</param>
338         /// <since_tizen> none </since_tizen>
339         /// This will be public opened in tizen_next by ACR.
340         [EditorBrowsable(EditorBrowsableState.Never)]
341         public static implicit operator Position2D(Position position)
342         {
343             return new Position2D((int)position.X, (int)position.Y);
344         }
345
346         /// <inheritdoc/>
347         [EditorBrowsable(EditorBrowsableState.Never)]
348         public object Clone() => new Position2D(this);
349
350         internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
351         {
352             Position2D ret = new Position2D(cPtr, false);
353             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
354             return ret;
355         }
356
357         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Position2D obj)
358         {
359             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
360         }
361
362         /// This will not be public opened.
363         [EditorBrowsable(EditorBrowsableState.Never)]
364         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
365         {
366             Interop.Vector2.delete_Vector2(swigCPtr);
367         }
368
369         private Position2D Add(Position2D rhs)
370         {
371             Position2D ret = new Position2D(Interop.Vector2.Vector2_Add(swigCPtr, Position2D.getCPtr(rhs)), true);
372             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373             return ret;
374         }
375
376         private Position2D Subtract(Position2D rhs)
377         {
378             Position2D ret = new Position2D(Interop.Vector2.Vector2_Subtract__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
379             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
380             return ret;
381         }
382
383         private Position2D Multiply(Position2D rhs)
384         {
385             Position2D ret = new Position2D(Interop.Vector2.Vector2_Multiply__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
386             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387             return ret;
388         }
389
390         private Position2D Multiply(int rhs)
391         {
392             Position2D ret = new Position2D(Interop.Vector2.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394             return ret;
395         }
396
397         private Position2D Divide(Position2D rhs)
398         {
399             Position2D ret = new Position2D(Interop.Vector2.Vector2_Divide__SWIG_0(swigCPtr, Position2D.getCPtr(rhs)), true);
400             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401             return ret;
402         }
403
404         private Position2D Divide(int rhs)
405         {
406             Position2D ret = new Position2D(Interop.Vector2.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408             return ret;
409         }
410
411         private Position2D Subtract()
412         {
413             Position2D ret = new Position2D(Interop.Vector2.Vector2_Subtract__SWIG_1(swigCPtr), true);
414             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415             return ret;
416         }
417
418         private int ValueOfIndex(uint index)
419         {
420             int ret = (int)Interop.Vector2.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
421             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422             return ret;
423         }
424     }
425 }