[NUI] Refactor dispose pattern to reduce duplication (#1112)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Size.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
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
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.new_Vector3__SWIG_0(), 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.new_Vector3__SWIG_1(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.new_Vector3__SWIG_3(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.Vector3_ZERO_get();
88                 Size ret = (cPtr == global::System.IntPtr.Zero) ? null : new Size(cPtr, false);
89                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
90                 return ret;
91             }
92         }
93
94         /// <summary>
95         /// The Width property for the width component of size
96         /// </summary>
97         /// <since_tizen> 5 </since_tizen>
98         public float Width
99         {
100             set
101             {
102                 Interop.Vector3.Vector3_Width_set(swigCPtr, value);
103                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
104
105                 callback?.Invoke(Width, Height, Depth);
106             }
107             get
108             {
109                 float ret = Interop.Vector3.Vector3_Width_get(swigCPtr);
110                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
111                 return ret;
112             }
113         }
114
115         /// <summary>
116         /// The Height property for the height component of size.
117         /// </summary>
118         /// <since_tizen> 5 </since_tizen>
119         public float Height
120         {
121             set
122             {
123                 Interop.Vector3.Vector3_Height_set(swigCPtr, value);
124                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
125
126                 callback?.Invoke(Width, Height, Depth);
127             }
128             get
129             {
130                 float ret = Interop.Vector3.Vector3_Height_get(swigCPtr);
131                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
132                 return ret;
133             }
134         }
135
136         /// <summary>
137         /// The Depth property for the depth component of size.
138         /// </summary>
139         /// <since_tizen> 5 </since_tizen>
140         public float Depth
141         {
142             set
143             {
144                 Interop.Vector3.Vector3_Depth_set(swigCPtr, value);
145                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
146
147                 callback?.Invoke(Width, Height, Depth);
148             }
149             get
150             {
151                 float ret = Interop.Vector3.Vector3_Depth_get(swigCPtr);
152                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153                 return ret;
154             }
155         }
156
157         /// <summary>
158         /// The addition operator for A+B.
159         /// </summary>
160         /// <param name="arg1">Size to assign A.</param>
161         /// <param name="arg2">Size to assign B.</param>
162         /// <returns>A size containing the result of the addition.</returns>
163         /// <since_tizen> 5 </since_tizen>
164         public static Size operator +(Size arg1, Size arg2)
165         {
166             return arg1.Add(arg2);
167         }
168
169         /// <summary>
170         /// The subtraction operator for A-B.
171         /// </summary>
172         /// <param name="arg1">Size to subtract A.</param>
173         /// <param name="arg2">Size to subtract B.</param>
174         /// <returns>The size containing the result of the subtraction.</returns>
175         /// <since_tizen> 5 </since_tizen>
176         public static Size operator -(Size arg1, Size arg2)
177         {
178             return arg1.Subtract(arg2);
179         }
180
181         /// <summary>
182         /// The unary negation operator.
183         /// </summary>
184         /// <param name="arg1">Size for unary negation.</param>
185         /// <returns>A size containing the negation.</returns>
186         /// <since_tizen> 5 </since_tizen>
187         public static Size operator -(Size arg1)
188         {
189             return arg1.Subtract();
190         }
191
192         /// <summary>
193         /// The multiplication operator.
194         /// </summary>
195         /// <param name="arg1">Size for multiplication.</param>
196         /// <param name="arg2">The size to multiply.</param>
197         /// <returns>A size containing the result of the multiplication.</returns>
198         /// <since_tizen> 5 </since_tizen>
199         public static Size operator *(Size arg1, Size arg2)
200         {
201             return arg1.Multiply(arg2);
202         }
203
204         /// <summary>
205         /// The multiplication operator.
206         /// </summary>
207         /// <param name="arg1">Size for multiplication.</param>
208         /// <param name="arg2">The float value to scale the size.</param>
209         /// <returns>A size containing the result of the scaling.</returns>
210         /// <since_tizen> 5 </since_tizen>
211         public static Size operator *(Size arg1, float arg2)
212         {
213             return arg1.Multiply(arg2);
214         }
215
216         /// <summary>
217         /// The division operator.
218         /// </summary>
219         /// <param name="arg1">Size for division.</param>
220         /// <param name="arg2">The size to divide.</param>
221         /// <returns>A size containing the result of the division.</returns>
222         /// <since_tizen> 5 </since_tizen>
223         public static Size operator /(Size arg1, Size arg2)
224         {
225             return arg1.Divide(arg2);
226         }
227
228         /// <summary>
229         /// The division operator.
230         /// </summary>
231         /// <param name="arg1">Size for division.</param>
232         /// <param name="arg2">The float value to scale the size by.</param>
233         /// <returns>A Size containing the result of the scaling.</returns>
234         /// <since_tizen> 5 </since_tizen>
235         public static Size operator /(Size arg1, float arg2)
236         {
237             return arg1.Divide(arg2);
238         }
239
240         /// <summary>
241         /// The array subscript operator.
242         /// </summary>
243         /// <param name="index">Subscript index.</param>
244         /// <returns>The float at the given index.</returns>
245         /// <since_tizen> 5 </since_tizen>
246         public float this[uint index]
247         {
248             get
249             {
250                 return ValueOfIndex(index);
251             }
252         }
253
254         /// <summary>
255         /// Determines whether the specified object is equal to the current object.
256         /// </summary>
257         /// <param name="obj">The object to compare with the current object.</param>
258         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
259         public override bool Equals(System.Object obj)
260         {
261             Size size = obj as Size;
262             bool equal = false;
263             if (Width == size?.Width && Height == size?.Height && Depth == size?.Depth)
264             {
265                 equal = true;
266             }
267             return equal;
268         }
269
270         /// <summary>
271         /// Gets the the hash code of this Size.
272         /// </summary>
273         /// <returns>The Hash Code.</returns>
274         /// <since_tizen> 6 </since_tizen>
275         public override int GetHashCode()
276         {
277             return swigCPtr.Handle.GetHashCode();
278         }
279
280         /// <summary>
281         /// Checks equality.<br />
282         /// Utilizes appropriate machine epsilon values.<br />
283         /// </summary>
284         /// <param name="rhs">The size to test against.</param>
285         /// <returns>True if the sizes are equal.</returns>
286         /// <since_tizen> 5 </since_tizen>
287         public bool EqualTo(Size rhs)
288         {
289             bool ret = Interop.Vector3.Vector3_EqualTo(swigCPtr, Size.getCPtr(rhs));
290             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
291             return ret;
292         }
293
294         /// <summary>
295         /// Checks inequality.<br />
296         /// Utilizes appropriate machine epsilon values.<br />
297         /// </summary>
298         /// <param name="rhs">The size to test against.</param>
299         /// <returns>True if the sizes are not equal.</returns>
300         /// <since_tizen> 5 </since_tizen>
301         public bool NotEqualTo(Size rhs)
302         {
303             bool ret = Interop.Vector3.Vector3_NotEqualTo(swigCPtr, Size.getCPtr(rhs));
304             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             return ret;
306         }
307
308         /// <summary>
309         /// The type cast operator, Size to Vector3.
310         /// </summary>
311         /// <param name="size">The object of size type.</param>
312         /// <since_tizen> 5 </since_tizen>
313         public static implicit operator Vector3(Size size)
314         {
315             return new Vector3(size.Width, size.Height, size.Depth);
316         }
317
318         /// <summary>
319         /// The type cast operator, Vector3 to Size type.
320         /// </summary>
321         /// <param name="vec">The object of Vector3 type.</param>
322         /// <since_tizen> 5 </since_tizen>
323         public static implicit operator Size(Vector3 vec)
324         {
325             return new Size(vec.Width, vec.Height, vec.Depth);
326         }
327
328         /// <summary>
329         /// Implicit type cast operator, Size2D to Size
330         /// </summary>
331         /// <param name="size2d">The object of Size2D type.</param>
332         /// <since_tizen> none </since_tizen>
333         /// This will be public opened in tizen_next by ACR.
334         [EditorBrowsable(EditorBrowsableState.Never)]
335         public static implicit operator Size(Size2D size2d)
336         {
337             return new Size(size2d.Width, size2d.Height, 0);
338         }
339         
340
341         internal static Size GetSizeFromPtr(global::System.IntPtr cPtr)
342         {
343             Size ret = new Size(cPtr, false);
344             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345             return ret;
346         }
347
348         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Size obj)
349         {
350             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
351         }
352
353         internal Size(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
354         {
355         }
356
357         /// This will not be public opened.
358         [EditorBrowsable(EditorBrowsableState.Never)]
359         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
360         {
361             Interop.Vector3.delete_Vector3(swigCPtr);
362         }
363
364         private Size Add(Size rhs)
365         {
366             Size ret = new Size(Interop.Vector3.Vector3_Add(swigCPtr, Size.getCPtr(rhs)), true);
367             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
368             return ret;
369         }
370
371         private Size Subtract(Size rhs)
372         {
373             Size ret = new Size(Interop.Vector3.Vector3_Subtract__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
374             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
375             return ret;
376         }
377
378         private Size Multiply(Size rhs)
379         {
380             Size ret = new Size(Interop.Vector3.Vector3_Multiply__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
381             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
382             return ret;
383         }
384
385         private Size Multiply(float rhs)
386         {
387             Size ret = new Size(Interop.Vector3.Vector3_Multiply__SWIG_1(swigCPtr, rhs), true);
388             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
389             return ret;
390         }
391
392         private Size Divide(Size rhs)
393         {
394             Size ret = new Size(Interop.Vector3.Vector3_Divide__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
395             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
396             return ret;
397         }
398
399         private Size Divide(float rhs)
400         {
401             Size ret = new Size(Interop.Vector3.Vector3_Divide__SWIG_1(swigCPtr, rhs), true);
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         private Size Subtract()
407         {
408             Size ret = new Size(Interop.Vector3.Vector3_Subtract__SWIG_1(swigCPtr), true);
409             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410             return ret;
411         }
412
413         private float ValueOfIndex(uint index)
414         {
415             float ret = Interop.Vector3.Vector3_ValueOfIndex__SWIG_0(swigCPtr, index);
416             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
417             return ret;
418         }
419
420         internal delegate void SizeChangedCallback(float width, float height, float depth);
421
422         internal Size(SizeChangedCallback cb, float w, float h, float d) : this(Interop.Vector3.new_Vector3__SWIG_1(w, h, d), true)
423         {
424             callback = cb;
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426         }
427
428         private SizeChangedCallback callback = null;
429     }
430 }
431