[NUI] Fixing the emtpy finalizers(CA1821)
[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.NewVector2(), 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.NewVector2WithVector3(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.NewVector2((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.NewVector2((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         /// <remarks>
90         /// The setter is deprecated in API8 and will be removed in API10. Please use new Position2D(...) constructor.
91         /// </remarks>
92         /// <code>
93         /// // DO NOT use like the followings!
94         /// Position2D position2d = new Position2D();
95         /// position2d.X = 1; 
96         /// // Please USE like this
97         /// int x = 1, y = 2;
98         /// Position2D position2d = new Position2D(x, y);
99         /// </code>
100         /// <since_tizen> 3 </since_tizen>
101         public int X
102         {
103             set
104             {
105                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor");
106
107                 Interop.Vector2.XSet(SwigCPtr, (float)value);
108                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109
110                 callback?.Invoke(X, Y);
111             }
112             get
113             {
114                 float ret = Interop.Vector2.XGet(SwigCPtr);
115                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
116                 return (int)ret;
117             }
118         }
119
120         /// <summary>
121         /// The y component.
122         /// </summary>
123         /// <remarks>
124         /// The setter is deprecated in API8 and will be removed in API10. Please use new Position2D(...) constructor.
125         /// </remarks>
126         /// <code>
127         /// // DO NOT use like the followings!
128         /// Position2D position2d = new Position2D();
129         /// position2d.Y = 2; 
130         /// // Please USE like this
131         /// int x = 1, y = 2;
132         /// Position2D position2d = new Position2D(x, y);
133         /// </code>
134         /// <since_tizen> 3 </since_tizen>
135         public int Y
136         {
137             set
138             {
139                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Position2D(...) constructor");
140
141                 Interop.Vector2.YSet(SwigCPtr, (float)value);
142                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
143
144                 callback?.Invoke(X, Y);
145             }
146             get
147             {
148                 float ret = Interop.Vector2.YGet(SwigCPtr);
149                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
150                 return (int)ret;
151             }
152         }
153
154         /// <summary>
155         /// The const array subscript operator overload. Should be 0, or 1.
156         /// </summary>
157         /// <param name="index">The subscript index.</param>
158         /// <returns>The float at the given index.</returns>
159         /// <since_tizen> 3 </since_tizen>
160         public float this[uint index]
161         {
162             get
163             {
164                 return ValueOfIndex(index);
165             }
166         }
167
168         /// <summary>
169         /// Convert a string to Position2D.
170         /// </summary>
171         /// <param name="value">The string to convert.</param>
172         /// <returns>The converted value.</returns>
173         static public Position2D ConvertFromString(System.String value)
174         {
175             if (value != null)
176             {
177                 string[] parts = value.Split(',');
178                 if (parts.Length == 2)
179                 {
180                     int x = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[0].Trim());
181                     int y = (int)GraphicsTypeManager.Instance.ConvertScriptToPixel(parts[1].Trim());
182                     return new Position2D(x, y);
183                 }
184             }
185
186             throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(Position2D)}");
187         }
188
189         /// <summary>
190         /// Constructor a Position2D from a stirng.
191         /// </summary>
192         public static implicit operator Position2D(System.String value)
193         {
194             return ConvertFromString(value);
195         }
196
197         /// <summary>
198         /// The addition operator.
199         /// </summary>
200         /// <param name="arg1">The vector to add.</param>
201         /// <param name="arg2">The vector to add.</param>
202         /// <returns>The vector containing the result of the addition.</returns>
203         /// <since_tizen> 3 </since_tizen>
204         public static Position2D operator +(Position2D arg1, Position2D arg2)
205         {
206             return arg1?.Add(arg2);
207         }
208
209         /// <summary>
210         /// The subtraction operator.
211         /// </summary>
212         /// <param name="arg1">The vector to subtract.</param>
213         /// <param name="arg2">The vector to subtract.</param>
214         /// <returns>The vector containing the result of the subtraction.</returns>
215         /// <since_tizen> 3 </since_tizen>
216         public static Position2D operator -(Position2D arg1, Position2D arg2)
217         {
218             return arg1?.Subtract(arg2);
219         }
220
221         /// <summary>
222         /// The unary negation operator.
223         /// </summary>
224         /// <param name="arg1">The vector to negate.</param>
225         /// <returns>The vector containing the negation.</returns>
226         /// <since_tizen> 3 </since_tizen>
227         public static Position2D operator -(Position2D arg1)
228         {
229             return arg1?.Subtract();
230         }
231
232         /// <summary>
233         /// The multiplication operator.
234         /// </summary>
235         /// <param name="arg1">The vector to multiply.</param>
236         /// <param name="arg2">The vector to multiply.</param>
237         /// <returns>The vector containing the result of the multiplication.</returns>
238         /// <since_tizen> 3 </since_tizen>
239         public static Position2D operator *(Position2D arg1, Position2D arg2)
240         {
241             return arg1?.Multiply(arg2);
242         }
243
244         /// <summary>
245         /// The multiplication operator.
246         /// </summary>
247         /// <param name="arg1">The vector to multiply.</param>
248         /// <param name="arg2">The integer value to scale the vector.</param>
249         /// <returns>The vector containing the result of the multiplication.</returns>
250         /// <since_tizen> 3 </since_tizen>
251         public static Position2D operator *(Position2D arg1, int arg2)
252         {
253             return arg1?.Multiply(arg2);
254         }
255
256         /// <summary>
257         /// The division operator.
258         /// </summary>
259         /// <param name="arg1">The vector to divide.</param>
260         /// <param name="arg2">The vector to divide.</param>
261         /// <returns>The vector containing the result of the division.</returns>
262         /// <since_tizen> 3 </since_tizen>
263         public static Position2D operator /(Position2D arg1, Position2D arg2)
264         {
265             return arg1?.Divide(arg2);
266         }
267
268         /// <summary>
269         /// The division operator.
270         /// </summary>
271         /// <param name="arg1">The vector to divide.</param>
272         /// <param name="arg2">The integer value to scale the vector by.</param>
273         /// <returns>The vector containing the result of the division.</returns>
274         /// <since_tizen> 3 </since_tizen>
275         public static Position2D operator /(Position2D arg1, int arg2)
276         {
277             return arg1?.Divide(arg2);
278         }
279
280         /// <summary>
281         /// Determines whether the specified object is equal to the current object.
282         /// </summary>
283         /// <param name="obj">The object to compare with the current object.</param>
284         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
285         public override bool Equals(System.Object obj)
286         {
287             Position2D position2D = obj as Position2D;
288             bool equal = false;
289             if (X == position2D?.X && Y == position2D?.Y)
290             {
291                 equal = true;
292             }
293             return equal;
294         }
295
296         /// <summary>
297         /// Gets the the hash code of this Position2D.
298         /// </summary>
299         /// <returns>The Hash Code.</returns>
300         /// <since_tizen> 6 </since_tizen>
301         public override int GetHashCode()
302         {
303             return SwigCPtr.Handle.GetHashCode();
304         }
305
306         /// <summary>
307         /// Compares if the rhs is equal to.
308         /// </summary>
309         /// <param name="rhs">The vector to compare</param>
310         /// <returns>Returns true if the two vectors are equal, otherwise false</returns>
311         /// <since_tizen> 3 </since_tizen>
312         public bool EqualTo(Position2D rhs)
313         {
314             bool ret = Interop.Vector2.EqualTo(SwigCPtr, Position2D.getCPtr(rhs));
315             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
316             return ret;
317         }
318
319         /// <summary>
320         /// Compares if the rhs is not equal to.
321         /// </summary>
322         /// <param name="rhs">The vector to compare.</param>
323         /// <returns>Returns true if the two vectors are not equal, otherwise false.</returns>
324         /// <since_tizen> 3 </since_tizen>
325         public bool NotEqualTo(Position2D rhs)
326         {
327             bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Position2D.getCPtr(rhs));
328             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
329             return ret;
330         }
331
332         /// <summary>
333         /// Converts a Position2D instance to a Vector2 instance.
334         /// </summary>
335         /// <param name="position2d">An object of the Position2D type.</param>
336         /// <returns>return an object of the Vector2 type</returns>
337         /// <since_tizen> 3 </since_tizen>
338         public static implicit operator Vector2(Position2D position2d)
339         {
340             return new Vector2((float)position2d?.X, (float)position2d.Y);
341         }
342
343         /// <summary>
344         /// Converts a Vector2 instance to a Position2D instance.
345         /// </summary>
346         /// <param name="vec">An object of the Vector2 type.</param>
347         /// <returns>return an object of the Position2D type</returns>
348         /// <since_tizen> 3 </since_tizen>
349         public static implicit operator Position2D(Vector2 vec)
350         {
351             return new Position2D((int)vec?.X, (int)vec.Y);
352         }
353
354         /// <summary>
355         /// Implicit type cast operator, Position to Position2D
356         /// </summary>
357         /// <param name="position">The object of Position type.</param>
358         /// <since_tizen> none </since_tizen>
359         /// This will be public opened in tizen_next by ACR.
360         [EditorBrowsable(EditorBrowsableState.Never)]
361         public static implicit operator Position2D(Position position)
362         {
363             return new Position2D((int)position?.X, (int)position.Y);
364         }
365
366         /// <inheritdoc/>
367         [EditorBrowsable(EditorBrowsableState.Never)]
368         public object Clone() => new Position2D(this);
369
370         internal static Position2D GetPosition2DFromPtr(global::System.IntPtr cPtr)
371         {
372             Position2D ret = new Position2D(cPtr, false);
373             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
374             return ret;
375         }
376
377         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Position2D obj)
378         {
379             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
380         }
381
382         /// This will not be public opened.
383         [EditorBrowsable(EditorBrowsableState.Never)]
384         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
385         {
386             Interop.Vector2.DeleteVector2(swigCPtr);
387         }
388
389         private Position2D Add(Position2D rhs)
390         {
391             Position2D ret = new Position2D(Interop.Vector2.Add(SwigCPtr, Position2D.getCPtr(rhs)), true);
392             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
393             return ret;
394         }
395
396         private Position2D Subtract(Position2D rhs)
397         {
398             Position2D ret = new Position2D(Interop.Vector2.Subtract(SwigCPtr, Position2D.getCPtr(rhs)), true);
399             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
400             return ret;
401         }
402
403         private Position2D Multiply(Position2D rhs)
404         {
405             Position2D ret = new Position2D(Interop.Vector2.Multiply(SwigCPtr, Position2D.getCPtr(rhs)), true);
406             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
407             return ret;
408         }
409
410         private Position2D Multiply(int rhs)
411         {
412             Position2D ret = new Position2D(Interop.Vector2.Multiply(SwigCPtr, (float)rhs), true);
413             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
414             return ret;
415         }
416
417         private Position2D Divide(Position2D rhs)
418         {
419             Position2D ret = new Position2D(Interop.Vector2.Divide(SwigCPtr, Position2D.getCPtr(rhs)), true);
420             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
421             return ret;
422         }
423
424         private Position2D Divide(int rhs)
425         {
426             Position2D ret = new Position2D(Interop.Vector2.Divide(SwigCPtr, (float)rhs), true);
427             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
428             return ret;
429         }
430
431         private Position2D Subtract()
432         {
433             Position2D ret = new Position2D(Interop.Vector2.Subtract(SwigCPtr), true);
434             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
435             return ret;
436         }
437
438         private int ValueOfIndex(uint index)
439         {
440             int ret = (int)Interop.Vector2.ValueOfIndex(SwigCPtr, index);
441             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
442             return ret;
443         }
444     }
445 }