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