[NUI] Add implicit conversion for Size, Size2D, Position and Position2D (#1105)
[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         /// <summary>swigCMemOwn.</summary>
32         /// <since_tizen> 5 </since_tizen>
33         protected bool swigCMemOwn;
34
35         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
36
37         /// <summary>
38         /// The constructor.
39         /// </summary>
40         /// <remarks>
41         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
42         /// For example, the followings are possible. <br />
43         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
44         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
45         /// view.MinimumSize = new Size(10, 10, 0); <br />
46         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
47         /// </remarks>
48         /// <since_tizen> 5 </since_tizen>
49         public Size() : this(Interop.Vector3.new_Vector3__SWIG_0(), true)
50         {
51             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
52         }
53
54         /// <summary>
55         /// The constructor.
56         /// </summary>
57         /// <param name="width">The width component.</param>
58         /// <param name="height">The height component.</param>
59         /// <param name="depth">The depth component(optional).</param>
60         /// <remarks>
61         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
62         /// For example, the followings are possible. <br />
63         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
64         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
65         /// view.MinimumSize = new Size(10, 10, 0); <br />
66         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
67         /// </remarks>
68         /// <since_tizen> 5 </since_tizen>
69         public Size(float width, float height, float depth = 0.0f) : this(Interop.Vector3.new_Vector3__SWIG_1(width, height, depth), true)
70         {
71             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72         }
73
74         /// <summary>
75         /// The constructor.
76         /// </summary>
77         /// <param name="size2d">Size2D with width and height.</param>
78         /// <since_tizen> 5 </since_tizen>
79         public Size(Size2D size2d) : this(Interop.Vector3.new_Vector3__SWIG_3(Size2D.getCPtr(size2d)), true)
80         {
81             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
82         }
83
84         /// <summary>
85         /// The Zero constant, (0.0f, 0.0f, 0.0f).
86         /// </summary>
87         /// <since_tizen> 5 </since_tizen>
88         public static Size Zero
89         {
90             get
91             {
92                 global::System.IntPtr cPtr = Interop.Vector3.Vector3_ZERO_get();
93                 Size ret = (cPtr == global::System.IntPtr.Zero) ? null : new Size(cPtr, false);
94                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
95                 return ret;
96             }
97         }
98
99         /// <summary>
100         /// The Width property for the width component of size
101         /// </summary>
102         /// <since_tizen> 5 </since_tizen>
103         public float Width
104         {
105             set
106             {
107                 Interop.Vector3.Vector3_Width_set(swigCPtr, value);
108                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
109
110                 callback?.Invoke(Width, Height, Depth);
111             }
112             get
113             {
114                 float ret = Interop.Vector3.Vector3_Width_get(swigCPtr);
115                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
116                 return ret;
117             }
118         }
119
120         /// <summary>
121         /// The Height property for the height component of size.
122         /// </summary>
123         /// <since_tizen> 5 </since_tizen>
124         public float Height
125         {
126             set
127             {
128                 Interop.Vector3.Vector3_Height_set(swigCPtr, value);
129                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
130
131                 callback?.Invoke(Width, Height, Depth);
132             }
133             get
134             {
135                 float ret = Interop.Vector3.Vector3_Height_get(swigCPtr);
136                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
137                 return ret;
138             }
139         }
140
141         /// <summary>
142         /// The Depth property for the depth component of size.
143         /// </summary>
144         /// <since_tizen> 5 </since_tizen>
145         public float Depth
146         {
147             set
148             {
149                 Interop.Vector3.Vector3_Depth_set(swigCPtr, value);
150                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
151
152                 callback?.Invoke(Width, Height, Depth);
153             }
154             get
155             {
156                 float ret = Interop.Vector3.Vector3_Depth_get(swigCPtr);
157                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
158                 return ret;
159             }
160         }
161
162         /// <summary>
163         /// The addition operator for A+B.
164         /// </summary>
165         /// <param name="arg1">Size to assign A.</param>
166         /// <param name="arg2">Size to assign B.</param>
167         /// <returns>A size containing the result of the addition.</returns>
168         /// <since_tizen> 5 </since_tizen>
169         public static Size operator +(Size arg1, Size arg2)
170         {
171             return arg1.Add(arg2);
172         }
173
174         /// <summary>
175         /// The subtraction operator for A-B.
176         /// </summary>
177         /// <param name="arg1">Size to subtract A.</param>
178         /// <param name="arg2">Size to subtract B.</param>
179         /// <returns>The size containing the result of the subtraction.</returns>
180         /// <since_tizen> 5 </since_tizen>
181         public static Size operator -(Size arg1, Size arg2)
182         {
183             return arg1.Subtract(arg2);
184         }
185
186         /// <summary>
187         /// The unary negation operator.
188         /// </summary>
189         /// <param name="arg1">Size for unary negation.</param>
190         /// <returns>A size containing the negation.</returns>
191         /// <since_tizen> 5 </since_tizen>
192         public static Size operator -(Size arg1)
193         {
194             return arg1.Subtract();
195         }
196
197         /// <summary>
198         /// The multiplication operator.
199         /// </summary>
200         /// <param name="arg1">Size for multiplication.</param>
201         /// <param name="arg2">The size to multiply.</param>
202         /// <returns>A size containing the result of the multiplication.</returns>
203         /// <since_tizen> 5 </since_tizen>
204         public static Size operator *(Size arg1, Size arg2)
205         {
206             return arg1.Multiply(arg2);
207         }
208
209         /// <summary>
210         /// The multiplication operator.
211         /// </summary>
212         /// <param name="arg1">Size for multiplication.</param>
213         /// <param name="arg2">The float value to scale the size.</param>
214         /// <returns>A size containing the result of the scaling.</returns>
215         /// <since_tizen> 5 </since_tizen>
216         public static Size operator *(Size arg1, float arg2)
217         {
218             return arg1.Multiply(arg2);
219         }
220
221         /// <summary>
222         /// The division operator.
223         /// </summary>
224         /// <param name="arg1">Size for division.</param>
225         /// <param name="arg2">The size to divide.</param>
226         /// <returns>A size containing the result of the division.</returns>
227         /// <since_tizen> 5 </since_tizen>
228         public static Size operator /(Size arg1, Size arg2)
229         {
230             return arg1.Divide(arg2);
231         }
232
233         /// <summary>
234         /// The division operator.
235         /// </summary>
236         /// <param name="arg1">Size for division.</param>
237         /// <param name="arg2">The float value to scale the size by.</param>
238         /// <returns>A Size containing the result of the scaling.</returns>
239         /// <since_tizen> 5 </since_tizen>
240         public static Size operator /(Size arg1, float arg2)
241         {
242             return arg1.Divide(arg2);
243         }
244
245         /// <summary>
246         /// The array subscript operator.
247         /// </summary>
248         /// <param name="index">Subscript index.</param>
249         /// <returns>The float at the given index.</returns>
250         /// <since_tizen> 5 </since_tizen>
251         public float this[uint index]
252         {
253             get
254             {
255                 return ValueOfIndex(index);
256             }
257         }
258
259         /// <summary>
260         /// Determines whether the specified object is equal to the current object.
261         /// </summary>
262         /// <param name="obj">The object to compare with the current object.</param>
263         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
264         public override bool Equals(System.Object obj)
265         {
266             Size size = obj as Size;
267             bool equal = false;
268             if (Width == size?.Width && Height == size?.Height && Depth == size?.Depth)
269             {
270                 equal = true;
271             }
272             return equal;
273         }
274
275         /// <summary>
276         /// Gets the the hash code of this Size.
277         /// </summary>
278         /// <returns>The Hash Code.</returns>
279         /// <since_tizen> 6 </since_tizen>
280         public override int GetHashCode()
281         {
282             return swigCPtr.Handle.GetHashCode();
283         }
284
285         /// <summary>
286         /// Checks equality.<br />
287         /// Utilizes appropriate machine epsilon values.<br />
288         /// </summary>
289         /// <param name="rhs">The size to test against.</param>
290         /// <returns>True if the sizes are equal.</returns>
291         /// <since_tizen> 5 </since_tizen>
292         public bool EqualTo(Size rhs)
293         {
294             bool ret = Interop.Vector3.Vector3_EqualTo(swigCPtr, Size.getCPtr(rhs));
295             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
296             return ret;
297         }
298
299         /// <summary>
300         /// Checks inequality.<br />
301         /// Utilizes appropriate machine epsilon values.<br />
302         /// </summary>
303         /// <param name="rhs">The size to test against.</param>
304         /// <returns>True if the sizes are not equal.</returns>
305         /// <since_tizen> 5 </since_tizen>
306         public bool NotEqualTo(Size rhs)
307         {
308             bool ret = Interop.Vector3.Vector3_NotEqualTo(swigCPtr, Size.getCPtr(rhs));
309             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
310             return ret;
311         }
312
313         /// <summary>
314         /// The type cast operator, Size to Vector3.
315         /// </summary>
316         /// <param name="size">The object of size type.</param>
317         /// <since_tizen> 5 </since_tizen>
318         public static implicit operator Vector3(Size size)
319         {
320             return new Vector3(size.Width, size.Height, size.Depth);
321         }
322
323         /// <summary>
324         /// The type cast operator, Vector3 to Size type.
325         /// </summary>
326         /// <param name="vec">The object of Vector3 type.</param>
327         /// <since_tizen> 5 </since_tizen>
328         public static implicit operator Size(Vector3 vec)
329         {
330             return new Size(vec.Width, vec.Height, vec.Depth);
331         }
332
333         /// <summary>
334         /// Implicit type cast operator, Size2D to Size
335         /// </summary>
336         /// <param name="size2d">The object of Size2D type.</param>
337         /// <since_tizen> none </since_tizen>
338         /// This will be public opened in tizen_next by ACR.
339         [EditorBrowsable(EditorBrowsableState.Never)]
340         public static implicit operator Size(Size2D size2d)
341         {
342             return new Size(size2d.Width, size2d.Height, 0);
343         }
344         
345
346         internal static Size GetSizeFromPtr(global::System.IntPtr cPtr)
347         {
348             Size ret = new Size(cPtr, false);
349             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
350             return ret;
351         }
352
353         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Size obj)
354         {
355             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
356         }
357
358         internal Size(global::System.IntPtr cPtr, bool cMemoryOwn)
359         {
360             swigCMemOwn = cMemoryOwn;
361             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
362         }
363
364         /// <summary>
365         /// Dispose.
366         /// </summary>
367         /// <since_tizen> 5 </since_tizen>
368         protected override void Dispose(DisposeTypes type)
369         {
370             if (disposed)
371             {
372                 return;
373             }
374
375             //Release your own unmanaged resources here.
376             //You should not access any managed member here except static instance.
377             //because the execution order of Finalizes is non-deterministic.
378
379             if (swigCPtr.Handle != global::System.IntPtr.Zero)
380             {
381                 if (swigCMemOwn)
382                 {
383                     swigCMemOwn = false;
384                     Interop.Vector3.delete_Vector3(swigCPtr);
385                 }
386                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
387             }
388
389             base.Dispose(type);
390         }
391
392         private Size Add(Size rhs)
393         {
394             Size ret = new Size(Interop.Vector3.Vector3_Add(swigCPtr, Size.getCPtr(rhs)), true);
395             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
396             return ret;
397         }
398
399         private Size Subtract(Size rhs)
400         {
401             Size ret = new Size(Interop.Vector3.Vector3_Subtract__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         private Size Multiply(Size rhs)
407         {
408             Size ret = new Size(Interop.Vector3.Vector3_Multiply__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
409             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410             return ret;
411         }
412
413         private Size Multiply(float rhs)
414         {
415             Size ret = new Size(Interop.Vector3.Vector3_Multiply__SWIG_1(swigCPtr, rhs), true);
416             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
417             return ret;
418         }
419
420         private Size Divide(Size rhs)
421         {
422             Size ret = new Size(Interop.Vector3.Vector3_Divide__SWIG_0(swigCPtr, Size.getCPtr(rhs)), true);
423             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
424             return ret;
425         }
426
427         private Size Divide(float rhs)
428         {
429             Size ret = new Size(Interop.Vector3.Vector3_Divide__SWIG_1(swigCPtr, rhs), true);
430             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
431             return ret;
432         }
433
434         private Size Subtract()
435         {
436             Size ret = new Size(Interop.Vector3.Vector3_Subtract__SWIG_1(swigCPtr), true);
437             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
438             return ret;
439         }
440
441         private float ValueOfIndex(uint index)
442         {
443             float ret = Interop.Vector3.Vector3_ValueOfIndex__SWIG_0(swigCPtr, index);
444             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
445             return ret;
446         }
447
448         internal delegate void SizeChangedCallback(float width, float height, float depth);
449
450         internal Size(SizeChangedCallback cb, float w, float h, float d) : this(Interop.Vector3.new_Vector3__SWIG_1(w, h, d), true)
451         {
452             callback = cb;
453             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454         }
455
456         private SizeChangedCallback callback = null;
457     }
458 }
459