[NUI] Fix comments according to document review
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / Size.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
18 using System;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// A three-dimensional size.
26     /// </summary>
27     /// <since_tizen> 5 </since_tizen>
28     [Tizen.NUI.Binding.TypeConverter(typeof(SizeTypeConverter))]
29     public class Size : Disposable, ICloneable
30     {
31
32         /// <summary>
33         /// The constructor.
34         /// </summary>
35         /// <remarks>
36         /// Size2D and Size 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.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
39         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
40         /// view.MinimumSize = new Size(10, 10, 0); <br />
41         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
42         /// </remarks>
43         /// <since_tizen> 5 </since_tizen>
44         public Size() : this(Interop.Vector3.NewVector3(), true)
45         {
46             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
47         }
48
49         /// <summary>
50         /// The constructor.
51         /// </summary>
52         /// <param name="width">The width component.</param>
53         /// <param name="height">The height component.</param>
54         /// <param name="depth">The depth component(optional).</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> 5 </since_tizen>
64         public Size(float width, float height, float depth = 0.0f) : this(Interop.Vector3.NewVector3(width, height, depth), true)
65         {
66             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
67         }
68
69         /// <summary>
70         /// The constructor.
71         /// </summary>
72         /// <param name="size2d">Size2D with width and height.</param>
73         /// <since_tizen> 5 </since_tizen>
74         public Size(Size2D size2d) : this(Interop.Vector3.NewVector3WithVector2(Size2D.getCPtr(size2d)), true)
75         {
76             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
77         }
78
79         /// <summary>
80         /// The Zero constant, (0.0f, 0.0f, 0.0f).
81         /// </summary>
82         /// <since_tizen> 5 </since_tizen>
83         public static Size Zero
84         {
85             get
86             {
87                 global::System.IntPtr cPtr = Interop.Vector3.ZeroGet();
88                 Size ret = (cPtr == global::System.IntPtr.Zero) ? null : new Size(cPtr, false);
89                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
90                 return ret;
91             }
92         }
93
94         /// <summary>
95         /// The Width property for the width component of size
96         /// </summary>
97         /// <remarks>
98         /// The setter is deprecated in API8 and will be removed in API10. Use new Size(...) constructor.
99         /// </remarks>
100         /// <code>
101         /// // DO NOT use like the followings!
102         /// Size size = new Size();
103         /// size.Width = 0.1f; 
104         /// // USE like this
105         /// float width = 0.1f, height = 0.5f, depth = 0.9f;
106         /// Size size = new Size(width, height, depth);
107         /// </code>
108         /// <since_tizen> 5 </since_tizen>
109         public float Width
110         {
111             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Size(...) constructor")]
112             set
113             {
114                 Interop.Vector3.WidthSet(SwigCPtr, value);
115                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116
117                 callback?.Invoke(value, null, null);
118             }
119             get
120             {
121                 float ret = Interop.Vector3.WidthGet(SwigCPtr);
122                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
123                 return ret;
124             }
125         }
126
127         /// <summary>
128         /// The Height property for the height component of size.
129         /// </summary>
130         /// <remarks>
131         /// The setter is deprecated in API8 and will be removed in API10. Use new Size(...) constructor.
132         /// </remarks>
133         /// <code>
134         /// // DO NOT use like the followings!
135         /// Size size = new Size();
136         /// size.Height = 0.5f; 
137         /// // USE like this
138         /// float width = 0.1f, height = 0.5f, depth = 0.9f;
139         /// Size size = new Size(width, height, depth);
140         /// </code>
141         /// <since_tizen> 5 </since_tizen>
142         public float Height
143         {
144             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Size(...) constructor")]
145             set
146             {
147                 Interop.Vector3.HeightSet(SwigCPtr, value);
148                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
149
150                 callback?.Invoke(null, value, null);
151             }
152             get
153             {
154                 float ret = Interop.Vector3.HeightGet(SwigCPtr);
155                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
156                 return ret;
157             }
158         }
159
160         /// <summary>
161         /// The Depth property for the depth component of size.
162         /// </summary>
163         /// <remarks>
164         /// The setter is deprecated in API8 and will be removed in API10. Use new Size(...) constructor.
165         /// </remarks>
166         /// <code>
167         /// // DO NOT use like the followings!
168         /// Size size = new Size();
169         /// size.Depth = 0.9f; 
170         /// // USE like this
171         /// float width = 0.1f, height = 0.5f, depth = 0.9f;
172         /// Size size = new Size(width, height, depth);
173         /// </code>
174         /// <since_tizen> 5 </since_tizen>
175         public float Depth
176         {
177             [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Size(...) constructor")]
178             set
179             {
180                 Interop.Vector3.DepthSet(SwigCPtr, value);
181                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182
183                 callback?.Invoke(null, null, value);
184             }
185             get
186             {
187                 float ret = Interop.Vector3.DepthGet(SwigCPtr);
188                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
189                 return ret;
190             }
191         }
192
193         /// <summary>
194         /// The addition operator for A+B.
195         /// </summary>
196         /// <param name="arg1">Size to assign A.</param>
197         /// <param name="arg2">Size to assign B.</param>
198         /// <returns>A size containing the result of the addition.</returns>
199         /// <since_tizen> 5 </since_tizen>
200         public static Size operator +(Size arg1, Size arg2)
201         {
202             return arg1?.Add(arg2);
203         }
204
205         /// <summary>
206         /// The subtraction operator for A-B.
207         /// </summary>
208         /// <param name="arg1">Size to subtract A.</param>
209         /// <param name="arg2">Size to subtract B.</param>
210         /// <returns>The size containing the result of the subtraction.</returns>
211         /// <since_tizen> 5 </since_tizen>
212         public static Size operator -(Size arg1, Size arg2)
213         {
214             return arg1?.Subtract(arg2);
215         }
216
217         /// <summary>
218         /// The unary negation operator.
219         /// </summary>
220         /// <param name="arg1">Size for unary negation.</param>
221         /// <returns>A size containing the negation.</returns>
222         /// <since_tizen> 5 </since_tizen>
223         public static Size operator -(Size arg1)
224         {
225             return arg1?.Subtract();
226         }
227
228         /// <summary>
229         /// The multiplication operator.
230         /// </summary>
231         /// <param name="arg1">Size for multiplication.</param>
232         /// <param name="arg2">The size to multiply.</param>
233         /// <returns>A size containing the result of the multiplication.</returns>
234         /// <since_tizen> 5 </since_tizen>
235         public static Size operator *(Size arg1, Size arg2)
236         {
237             return arg1?.Multiply(arg2);
238         }
239
240         /// <summary>
241         /// The multiplication operator.
242         /// </summary>
243         /// <param name="arg1">Size for multiplication.</param>
244         /// <param name="arg2">The float value to scale the size.</param>
245         /// <returns>A size containing the result of the scaling.</returns>
246         /// <since_tizen> 5 </since_tizen>
247         public static Size operator *(Size arg1, float arg2)
248         {
249             return arg1?.Multiply(arg2);
250         }
251
252         /// <summary>
253         /// The division operator.
254         /// </summary>
255         /// <param name="arg1">Size for division.</param>
256         /// <param name="arg2">The size to divide.</param>
257         /// <returns>A size containing the result of the division.</returns>
258         /// <since_tizen> 5 </since_tizen>
259         public static Size operator /(Size arg1, Size arg2)
260         {
261             return arg1?.Divide(arg2);
262         }
263
264         /// <summary>
265         /// The division operator.
266         /// </summary>
267         /// <param name="arg1">Size for division.</param>
268         /// <param name="arg2">The float value to scale the size by.</param>
269         /// <returns>A Size containing the result of the scaling.</returns>
270         /// <since_tizen> 5 </since_tizen>
271         public static Size operator /(Size arg1, float arg2)
272         {
273             return arg1?.Divide(arg2);
274         }
275
276         /// <summary>
277         /// The array subscript operator.
278         /// </summary>
279         /// <param name="index">Subscript index.</param>
280         /// <returns>The float at the given index.</returns>
281         /// <since_tizen> 5 </since_tizen>
282         public float this[uint index]
283         {
284             get
285             {
286                 return ValueOfIndex(index);
287             }
288         }
289
290         /// <summary>
291         /// Determines whether the specified object is equal to the current object.
292         /// </summary>
293         /// <param name="obj">The object to compare with the current object.</param>
294         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
295         public override bool Equals(System.Object obj)
296         {
297             Size size = obj as Size;
298             bool equal = false;
299             if (Width == size?.Width && Height == size?.Height && Depth == size?.Depth)
300             {
301                 equal = true;
302             }
303             return equal;
304         }
305
306         /// <summary>
307         /// Gets the hash code of this Size.
308         /// </summary>
309         /// <returns>The Hash Code.</returns>
310         /// <since_tizen> 6 </since_tizen>
311         public override int GetHashCode()
312         {
313             return SwigCPtr.Handle.GetHashCode();
314         }
315
316         /// <summary>
317         /// Checks equality.<br />
318         /// Utilizes appropriate machine epsilon values.<br />
319         /// </summary>
320         /// <param name="rhs">The size to test against.</param>
321         /// <returns>True if the sizes are equal.</returns>
322         /// <since_tizen> 5 </since_tizen>
323         public bool EqualTo(Size rhs)
324         {
325             bool ret = Interop.Vector3.EqualTo(SwigCPtr, Size.getCPtr(rhs));
326             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
327             return ret;
328         }
329
330         /// <summary>
331         /// Checks inequality.<br />
332         /// Utilizes appropriate machine epsilon values.<br />
333         /// </summary>
334         /// <param name="rhs">The size to test against.</param>
335         /// <returns>True if the sizes are not equal.</returns>
336         /// <since_tizen> 5 </since_tizen>
337         public bool NotEqualTo(Size rhs)
338         {
339             bool ret = Interop.Vector3.NotEqualTo(SwigCPtr, Size.getCPtr(rhs));
340             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
341             return ret;
342         }
343
344         /// <inheritdoc/>
345         [EditorBrowsable(EditorBrowsableState.Never)]
346         public object Clone() => new Size(Width, Height, Depth);
347
348         /// <summary>
349         /// The type cast operator, Size to Vector3.
350         /// </summary>
351         /// <param name="size">The object of size type.</param>
352         /// <since_tizen> 5 </since_tizen>
353         public static implicit operator Vector3(Size size)
354         {
355             if (size == null)
356             {
357                 return null;
358             }
359             return new Vector3(size.Width, size.Height, size.Depth);
360         }
361
362         /// <summary>
363         /// The type cast operator, Vector3 to Size type.
364         /// </summary>
365         /// <param name="vec">The object of Vector3 type.</param>
366         /// <since_tizen> 5 </since_tizen>
367         public static implicit operator Size(Vector3 vec)
368         {
369             if (vec == null)
370             {
371                 return null;
372             }
373             return new Size(vec.Width, vec.Height, vec.Depth);
374         }
375
376         /// <summary>
377         /// Implicit type cast operator, Size2D to Size
378         /// </summary>
379         /// <param name="size2d">The object of Size2D type.</param>
380         /// <since_tizen> none </since_tizen>
381         /// This will be public opened in tizen_next by ACR.
382         [EditorBrowsable(EditorBrowsableState.Never)]
383         public static implicit operator Size(Size2D size2d)
384         {
385             if (size2d == null)
386             {
387                 return null;
388             }
389             return new Size(size2d.Width, size2d.Height);
390         }
391
392
393         internal static Size GetSizeFromPtr(global::System.IntPtr cPtr)
394         {
395             Size ret = new Size(cPtr, false);
396             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
397             return ret;
398         }
399
400         internal Size(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
401         {
402         }
403
404         /// This will not be public opened.
405         [EditorBrowsable(EditorBrowsableState.Never)]
406         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
407         {
408             Interop.Vector3.DeleteVector3(swigCPtr);
409         }
410
411         private Size Add(Size rhs)
412         {
413             Size ret = new Size(Interop.Vector3.Add(SwigCPtr, Size.getCPtr(rhs)), true);
414             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
415             return ret;
416         }
417
418         private Size Subtract(Size rhs)
419         {
420             Size ret = new Size(Interop.Vector3.Subtract(SwigCPtr, Size.getCPtr(rhs)), true);
421             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
422             return ret;
423         }
424
425         private Size Multiply(Size rhs)
426         {
427             Size ret = new Size(Interop.Vector3.Multiply(SwigCPtr, Size.getCPtr(rhs)), true);
428             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
429             return ret;
430         }
431
432         private Size Multiply(float rhs)
433         {
434             Size ret = new Size(Interop.Vector3.Multiply(SwigCPtr, rhs), true);
435             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
436             return ret;
437         }
438
439         private Size Divide(Size rhs)
440         {
441             Size ret = new Size(Interop.Vector3.Divide(SwigCPtr, Size.getCPtr(rhs)), true);
442             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
443             return ret;
444         }
445
446         private Size Divide(float rhs)
447         {
448             Size ret = new Size(Interop.Vector3.Divide(SwigCPtr, rhs), true);
449             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
450             return ret;
451         }
452
453         private Size Subtract()
454         {
455             Size ret = new Size(Interop.Vector3.Subtract(SwigCPtr), true);
456             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
457             return ret;
458         }
459
460         private float ValueOfIndex(uint index)
461         {
462             float ret = Interop.Vector3.ValueOfIndex(SwigCPtr, index);
463             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
464             return ret;
465         }
466
467         internal delegate void SizeChangedCallback(float? width, float? height, float? depth);
468
469         internal Size(SizeChangedCallback cb, float w, float h, float d) : this(Interop.Vector3.NewVector3(w, h, d), true)
470         {
471             callback = cb;
472             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
473         }
474
475         internal Size(SizeChangedCallback cb, Size other) : this(cb, other.Width, other.Height, other.Depth)
476         {
477         }
478
479         private SizeChangedCallback callback = null;
480     }
481 }