[NUI] Remove duplicate getCPtr from Disposable/View subclasses (#3544)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / Size2D.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     /// A two-dimensional size.
25     /// </summary>
26     /// <since_tizen> 3 </since_tizen>
27     [Tizen.NUI.Binding.TypeConverter(typeof(Size2DTypeConverter))]
28     public class Size2D : Disposable, ICloneable
29     {
30
31         private Size2DChangedCallback callback = null;
32
33         /// <summary>
34         /// The constructor.
35         /// </summary>
36         /// <remarks>
37         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
38         /// For example, the followings are possible. <br />
39         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
40         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
41         /// view.MinimumSize = new Size(10, 10, 0); <br />
42         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
43         /// </remarks>
44         /// <since_tizen> 3 </since_tizen>
45         public Size2D() : this(Interop.Vector2.NewVector2(), true)
46         {
47             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
48         }
49
50         /// <summary>
51         /// The constructor.
52         /// </summary>
53         /// <param name="width">The width component.</param>
54         /// <param name="height">The height component.</param>
55         /// <remarks>
56         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
57         /// For example, the followings are possible. <br />
58         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
59         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
60         /// view.MinimumSize = new Size(10, 10, 0); <br />
61         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
62         /// </remarks>
63         /// <since_tizen> 3 </since_tizen>
64         public Size2D(int width, int height) : this(Interop.Vector2.NewVector2((float)width, (float)height), true)
65         {
66             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67         }
68
69         internal delegate void Size2DChangedCallback(int? width, int? height);
70
71         /// <summary>
72         /// The property for the width component of a size.
73         /// </summary>
74         /// <remarks>
75         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
76         /// </remarks>
77         /// <code>
78         /// // DO NOT use like the followings!
79         /// Size2D size2d = new Size2D();
80         /// size2d.Width = 1; 
81         /// // Please USE like this
82         /// int width = 1, height = 2;
83         /// Size2D size2d = new Size2D(width, height);
84         /// </code>
85         /// <since_tizen> 3 </since_tizen>
86         public int Width
87         {
88             [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
89             set
90             {
91                 Interop.Vector2.WidthSet(SwigCPtr, (float)value);
92                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93
94                 callback?.Invoke(value, null);
95             }
96             get
97             {
98                 float ret = Interop.Vector2.WidthGet(SwigCPtr);
99                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
100                 return (int)ret;
101             }
102         }
103
104         /// <summary>
105         /// The property for the height component of a size.
106         /// </summary>
107         /// <remarks>
108         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
109         /// </remarks>
110         /// <code>
111         /// // DO NOT use like the followings!
112         /// Size2D size2d = new Size2D();
113         /// size2d.Height = 2; 
114         /// // Please USE like this
115         /// int width = 1, height = 2;
116         /// Size2D size2d = new Size2D(width, height);
117         /// </code>
118         /// <since_tizen> 3 </since_tizen>
119         public int Height
120         {
121             [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
122             set
123             {
124                 Interop.Vector2.HeightSet(SwigCPtr, (float)value);
125                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
126
127                 callback?.Invoke(null, value);
128             }
129             get
130             {
131                 float ret = Interop.Vector2.HeightGet(SwigCPtr);
132                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
133                 return (int)ret;
134             }
135         }
136
137         /// <summary>
138         /// The addition operator for A+B.
139         /// </summary>
140         /// <param name="arg1">Size A.</param>
141         /// <param name="arg2">Size to assign B.</param>
142         /// <returns>A size containing the result of the addition.</returns>
143         /// <since_tizen> 3 </since_tizen>
144         public static Size2D operator +(Size2D arg1, Size2D arg2)
145         {
146             return arg1?.Add(arg2);
147         }
148
149         /// <summary>
150         /// The subtraction operator for A-B.
151         /// </summary>
152         /// <param name="arg1">Size A.</param>
153         /// <param name="arg2">Size to subtract B.</param>
154         /// <returns>A size containing the result of the subtraction.</returns>
155         /// <since_tizen> 3 </since_tizen>
156         public static Size2D operator -(Size2D arg1, Size2D arg2)
157         {
158             return arg1?.Subtract(arg2);
159         }
160
161         /// <summary>
162         /// The unary negation operator.
163         /// </summary>
164         /// <param name="arg1">Size for unary negation.</param>
165         /// <returns>A size containing the negation.</returns>
166         /// <since_tizen> 3 </since_tizen>
167         public static Size2D operator -(Size2D arg1)
168         {
169             return arg1?.Subtract();
170         }
171
172         /// <summary>
173         /// The multiplication operator.
174         /// </summary>
175         /// <param name="arg1">Size for multiplication.</param>
176         /// <param name="arg2">Size to multiply.</param>
177         /// <returns>A size containing the result of the multiplication.</returns>
178         /// <since_tizen> 3 </since_tizen>
179         public static Size2D operator *(Size2D arg1, Size2D arg2)
180         {
181             return arg1?.Multiply(arg2);
182         }
183
184         /// <summary>
185         /// The multiplication operator.
186         /// </summary>
187         /// <param name="arg1">Size for multiplication</param>
188         /// <param name="arg2">The integer value to scale the size.</param>
189         /// <returns>A size containing the result of the scaling.</returns>
190
191         /// <since_tizen> 3 </since_tizen>
192         public static Size2D operator *(Size2D arg1, int arg2)
193         {
194             return arg1?.Multiply(arg2);
195         }
196
197         /// <summary>
198         /// The division operator.
199         /// </summary>
200         /// <param name="arg1">Size for division.</param>
201         /// <param name="arg2">Size to divide.</param>
202         /// <returns>A size containing the result of the division.</returns>
203         /// <since_tizen> 3 </since_tizen>
204         public static Size2D operator /(Size2D arg1, Size2D arg2)
205         {
206             return arg1?.Divide(arg2);
207         }
208
209         /// <summary>
210         /// The division operator.
211         /// </summary>
212         /// <param name="arg1">Size for division.</param>
213         /// <param name="arg2">The integer value to scale the size by.</param>
214         /// <returns>A size containing the result of the scaling.</returns>
215         /// <since_tizen> 3 </since_tizen>
216         public static Size2D operator /(Size2D arg1, int arg2)
217         {
218             return arg1?.Divide(arg2);
219         }
220
221         /// <summary>
222         /// The type cast operator, Size2D to Vector2.
223         /// </summary>
224         /// <param name="size">An object of the Size2D type.</param>
225         /// <returns>return a Vector2 instance</returns>
226         /// <since_tizen> 3 </since_tizen>
227         public static implicit operator Vector2(Size2D size)
228         {
229             if (size == null)
230             {
231                 return null;
232             }
233             return new Vector2(size.Width, size.Height);
234         }
235
236         /// <summary>
237         /// The type cast operator, Vector2 to Size2D type.
238         /// </summary>
239         /// <param name="vector2">An object of the Vector2 type.</param>
240         /// <returns>return a Size2D instance</returns>
241         /// <since_tizen> 3 </since_tizen>
242         public static implicit operator Size2D(Vector2 vector2)
243         {
244             if (vector2 == null)
245             {
246                 return null;
247             }
248             return new Size2D((int)vector2.X, (int)vector2.Y);
249         }
250
251         /// <summary>
252         /// Implicit type cast operator, Size to Size2D
253         /// </summary>
254         /// <param name="size">The object of Size type.</param>
255         /// <since_tizen> none </since_tizen>
256         /// This will be public opened in tizen_next by ACR.
257         [EditorBrowsable(EditorBrowsableState.Never)]
258         public static implicit operator Size2D(Size size)
259         {
260             if (size == null)
261             {
262                 return null;
263             }
264             return new Size2D((int)size.Width, (int)size.Height);
265         }
266
267
268         /// <summary>
269         /// The array subscript operator.
270         /// </summary>
271         /// <param name="index">The subscript index.</param>
272         /// <returns>The float at the given index.</returns>
273         /// <since_tizen> 3 </since_tizen>
274         public float this[uint index]
275         {
276             get
277             {
278                 return ValueOfIndex(index);
279             }
280         }
281
282         /// <summary>
283         /// Determines whether the specified object is equal to the current object.
284         /// </summary>
285         /// <param name="obj">The object to compare with the current object.</param>
286         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
287         public override bool Equals(System.Object obj)
288         {
289             Size2D size2D = obj as Size2D;
290             bool equal = false;
291             if (Width == size2D?.Width && Height == size2D?.Height)
292             {
293                 equal = true;
294             }
295             return equal;
296         }
297
298         /// <summary>
299         /// Gets the hash code of this Size2D.
300         /// </summary>
301         /// <returns>The Hash Code.</returns>
302         /// <since_tizen> 6 </since_tizen>
303         public override int GetHashCode()
304         {
305             return SwigCPtr.Handle.GetHashCode();
306         }
307
308         /// <summary>
309         /// Checks equality.<br />
310         /// Utilizes appropriate machine epsilon values.<br />
311         /// </summary>
312         /// <param name="rhs">The size to test against.</param>
313         /// <returns>True if the sizes are equal.</returns>
314         /// <since_tizen> 3 </since_tizen>
315         public bool EqualTo(Size2D rhs)
316         {
317             bool ret = Interop.Vector2.EqualTo(SwigCPtr, Size2D.getCPtr(rhs));
318             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
319             return ret;
320         }
321
322         /// <summary>
323         /// Checks inequality.<br />
324         /// Utilizes appropriate machine epsilon values.<br />
325         /// </summary>
326         /// <param name="rhs">The size to test against.</param>
327         /// <returns>True if the sizes are not equal.</returns>
328         /// <since_tizen> 3 </since_tizen>
329         public bool NotEqualTo(Size2D rhs)
330         {
331             bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Size2D.getCPtr(rhs));
332             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
333             return ret;
334         }
335
336         /// <inheritdoc/>
337         [EditorBrowsable(EditorBrowsableState.Never)]
338         public object Clone() => new Size2D(Width, Height);
339
340         /// <summary>
341         /// Gets the size from the pointer.
342         /// </summary>
343         /// <param name="cPtr">The pointer of the size.</param>
344         /// <returns>Size</returns>
345         internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
346         {
347             Size2D ret = new Size2D(cPtr, false);
348             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
349             return ret;
350         }
351
352         internal Size2D(Size2DChangedCallback cb, int x, int y) : this(Interop.Vector2.NewVector2((float)x, (float)y), true)
353         {
354             callback = cb;
355             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
356         }
357
358         internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
359         {
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.DeleteVector2(swigCPtr);
367         }
368
369         private Size2D Add(Size2D rhs)
370         {
371             Size2D ret = new Size2D(Interop.Vector2.Add(SwigCPtr, Size2D.getCPtr(rhs)), true);
372             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373             return ret;
374         }
375
376         private Size2D Subtract(Size2D rhs)
377         {
378             Size2D ret = new Size2D(Interop.Vector2.Subtract(SwigCPtr, Size2D.getCPtr(rhs)), true);
379             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
380             return ret;
381         }
382
383         private Size2D Multiply(Size2D rhs)
384         {
385             Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, Size2D.getCPtr(rhs)), true);
386             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
387             return ret;
388         }
389
390         private Size2D Multiply(int rhs)
391         {
392             Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, (float)rhs), true);
393             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394             return ret;
395         }
396
397         private Size2D Divide(Size2D rhs)
398         {
399             Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, Size2D.getCPtr(rhs)), true);
400             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
401             return ret;
402         }
403
404         private Size2D Divide(int rhs)
405         {
406             Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, (float)rhs), true);
407             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
408             return ret;
409         }
410
411         private Size2D Subtract()
412         {
413             Size2D ret = new Size2D(Interop.Vector2.Subtract(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.ValueOfIndex(SwigCPtr, index);
421             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422             return ret;
423         }
424     }
425 }