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