d50854f2c0c526cc5db1c3b34345c6503616809a
[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         /// Hidden API (Inhouse API).
73         /// Dispose. 
74         /// </summary>
75         /// <remarks>
76         /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
77         /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
78         /// </remarks>
79         /// <param name="disposing">true in order to free managed objects</param>
80         // Protected implementation of Dispose pattern.
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         protected override void Dispose(bool disposing)
83         {
84             if (disposed)
85             {
86                 return;
87             }
88
89             callback = null;
90
91             //perform dipose here without being added to DisposeQueue.
92             if (SwigCMemOwn && SwigCPtr.Handle != IntPtr.Zero)
93             {
94                 if (disposing)
95                 {
96                     base.Dispose(DisposeTypes.Explicit);
97                 }
98                 else
99                 {
100                     base.Dispose(DisposeTypes.Implicit);
101                 }
102             }
103
104             base.Dispose(disposing);
105         }
106
107         /// <summary>
108         /// The property for the width component of a size.
109         /// </summary>
110         /// <remarks>
111         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
112         /// </remarks>
113         /// <code>
114         /// // DO NOT use like the followings!
115         /// Size2D size2d = new Size2D();
116         /// size2d.Width = 1; 
117         /// // Please USE like this
118         /// int width = 1, height = 2;
119         /// Size2D size2d = new Size2D(width, height);
120         /// </code>
121         /// <since_tizen> 3 </since_tizen>
122         public int Width
123         {
124             [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
125             set
126             {
127                 Interop.Vector2.WidthSet(SwigCPtr, (float)value);
128                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129
130                 callback?.Invoke(value, null);
131             }
132             get
133             {
134                 float ret = Interop.Vector2.WidthGet(SwigCPtr);
135                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
136                 return (int)ret;
137             }
138         }
139
140         /// <summary>
141         /// The property for the height component of a size.
142         /// </summary>
143         /// <remarks>
144         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
145         /// </remarks>
146         /// <code>
147         /// // DO NOT use like the followings!
148         /// Size2D size2d = new Size2D();
149         /// size2d.Height = 2; 
150         /// // Please USE like this
151         /// int width = 1, height = 2;
152         /// Size2D size2d = new Size2D(width, height);
153         /// </code>
154         /// <since_tizen> 3 </since_tizen>
155         public int Height
156         {
157             [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
158             set
159             {
160                 Interop.Vector2.HeightSet(SwigCPtr, (float)value);
161                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
162
163                 callback?.Invoke(null, value);
164             }
165             get
166             {
167                 float ret = Interop.Vector2.HeightGet(SwigCPtr);
168                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
169                 return (int)ret;
170             }
171         }
172
173         /// <summary>
174         /// The addition operator for A+B.
175         /// </summary>
176         /// <param name="arg1">Size A.</param>
177         /// <param name="arg2">Size to assign B.</param>
178         /// <returns>A size containing the result of the addition.</returns>
179         /// <since_tizen> 3 </since_tizen>
180         public static Size2D operator +(Size2D arg1, Size2D arg2)
181         {
182             return arg1?.Add(arg2);
183         }
184
185         /// <summary>
186         /// The subtraction operator for A-B.
187         /// </summary>
188         /// <param name="arg1">Size A.</param>
189         /// <param name="arg2">Size to subtract B.</param>
190         /// <returns>A size containing the result of the subtraction.</returns>
191         /// <since_tizen> 3 </since_tizen>
192         public static Size2D operator -(Size2D arg1, Size2D arg2)
193         {
194             return arg1?.Subtract(arg2);
195         }
196
197         /// <summary>
198         /// The unary negation operator.
199         /// </summary>
200         /// <param name="arg1">Size for unary negation.</param>
201         /// <returns>A size containing the negation.</returns>
202         /// <since_tizen> 3 </since_tizen>
203         public static Size2D operator -(Size2D arg1)
204         {
205             return arg1?.Subtract();
206         }
207
208         /// <summary>
209         /// The multiplication operator.
210         /// </summary>
211         /// <param name="arg1">Size for multiplication.</param>
212         /// <param name="arg2">Size to multiply.</param>
213         /// <returns>A size containing the result of the multiplication.</returns>
214         /// <since_tizen> 3 </since_tizen>
215         public static Size2D operator *(Size2D arg1, Size2D arg2)
216         {
217             return arg1?.Multiply(arg2);
218         }
219
220         /// <summary>
221         /// The multiplication operator.
222         /// </summary>
223         /// <param name="arg1">Size for multiplication</param>
224         /// <param name="arg2">The integer value to scale the size.</param>
225         /// <returns>A size containing the result of the scaling.</returns>
226
227         /// <since_tizen> 3 </since_tizen>
228         public static Size2D operator *(Size2D arg1, int arg2)
229         {
230             return arg1?.Multiply(arg2);
231         }
232
233         /// <summary>
234         /// The division operator.
235         /// </summary>
236         /// <param name="arg1">Size for division.</param>
237         /// <param name="arg2">Size to divide.</param>
238         /// <returns>A size containing the result of the division.</returns>
239         /// <since_tizen> 3 </since_tizen>
240         public static Size2D operator /(Size2D arg1, Size2D arg2)
241         {
242             return arg1?.Divide(arg2);
243         }
244
245         /// <summary>
246         /// The division operator.
247         /// </summary>
248         /// <param name="arg1">Size for division.</param>
249         /// <param name="arg2">The integer value to scale the size by.</param>
250         /// <returns>A size containing the result of the scaling.</returns>
251         /// <since_tizen> 3 </since_tizen>
252         public static Size2D operator /(Size2D arg1, int arg2)
253         {
254             return arg1?.Divide(arg2);
255         }
256
257         /// <summary>
258         /// The type cast operator, Size2D to Vector2.
259         /// </summary>
260         /// <param name="size">An object of the Size2D type.</param>
261         /// <returns>return a Vector2 instance</returns>
262         /// <since_tizen> 3 </since_tizen>
263         public static implicit operator Vector2(Size2D size)
264         {
265             if (size == null)
266             {
267                 return null;
268             }
269             return new Vector2(size.Width, size.Height);
270         }
271
272         /// <summary>
273         /// The type cast operator, Vector2 to Size2D type.
274         /// </summary>
275         /// <param name="vector2">An object of the Vector2 type.</param>
276         /// <returns>return a Size2D instance</returns>
277         /// <since_tizen> 3 </since_tizen>
278         public static implicit operator Size2D(Vector2 vector2)
279         {
280             if (vector2 == null)
281             {
282                 return null;
283             }
284             return new Size2D((int)vector2.X, (int)vector2.Y);
285         }
286
287         /// <summary>
288         /// Implicit type cast operator, Size to Size2D
289         /// </summary>
290         /// <param name="size">The object of Size type.</param>
291         /// <since_tizen> none </since_tizen>
292         /// This will be public opened in tizen_next by ACR.
293         [EditorBrowsable(EditorBrowsableState.Never)]
294         public static implicit operator Size2D(Size size)
295         {
296             if (size == null)
297             {
298                 return null;
299             }
300             return new Size2D((int)size.Width, (int)size.Height);
301         }
302
303
304         /// <summary>
305         /// The array subscript operator.
306         /// </summary>
307         /// <param name="index">The subscript index.</param>
308         /// <returns>The float at the given index.</returns>
309         /// <since_tizen> 3 </since_tizen>
310         public float this[uint index]
311         {
312             get
313             {
314                 return ValueOfIndex(index);
315             }
316         }
317
318         /// <summary>
319         /// Determines whether the specified object is equal to the current object.
320         /// </summary>
321         /// <param name="obj">The object to compare with the current object.</param>
322         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
323         public override bool Equals(System.Object obj)
324         {
325             Size2D size2D = obj as Size2D;
326             bool equal = false;
327             if (Width == size2D?.Width && Height == size2D?.Height)
328             {
329                 equal = true;
330             }
331             return equal;
332         }
333
334         /// <summary>
335         /// Gets the hash code of this Size2D.
336         /// </summary>
337         /// <returns>The Hash Code.</returns>
338         /// <since_tizen> 6 </since_tizen>
339         public override int GetHashCode()
340         {
341             return SwigCPtr.Handle.GetHashCode();
342         }
343
344         /// <summary>
345         /// Checks equality.<br />
346         /// Utilizes appropriate machine epsilon values.<br />
347         /// </summary>
348         /// <param name="rhs">The size to test against.</param>
349         /// <returns>True if the sizes are equal.</returns>
350         /// <since_tizen> 3 </since_tizen>
351         public bool EqualTo(Size2D rhs)
352         {
353             bool ret = Interop.Vector2.EqualTo(SwigCPtr, Size2D.getCPtr(rhs));
354             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355             return ret;
356         }
357
358         /// <summary>
359         /// Checks inequality.<br />
360         /// Utilizes appropriate machine epsilon values.<br />
361         /// </summary>
362         /// <param name="rhs">The size to test against.</param>
363         /// <returns>True if the sizes are not equal.</returns>
364         /// <since_tizen> 3 </since_tizen>
365         public bool NotEqualTo(Size2D rhs)
366         {
367             bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Size2D.getCPtr(rhs));
368             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
369             return ret;
370         }
371
372         /// <inheritdoc/>
373         [EditorBrowsable(EditorBrowsableState.Never)]
374         public object Clone() => new Size2D(Width, Height);
375
376         /// <summary>
377         /// Gets the size from the pointer.
378         /// </summary>
379         /// <param name="cPtr">The pointer of the size.</param>
380         /// <returns>Size</returns>
381         internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
382         {
383             Size2D ret = new Size2D(cPtr, false);
384             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
385             return ret;
386         }
387
388         internal Size2D(Size2DChangedCallback cb, int x, int y) : this(Interop.Vector2.NewVector2((float)x, (float)y), true)
389         {
390             callback = cb;
391             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
392         }
393
394         internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
395         {
396         }
397
398         /// This will not be public opened.
399         [EditorBrowsable(EditorBrowsableState.Never)]
400         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
401         {
402             Interop.Vector2.DeleteVector2(swigCPtr);
403         }
404
405         private Size2D Add(Size2D rhs)
406         {
407             Size2D ret = new Size2D(Interop.Vector2.Add(SwigCPtr, Size2D.getCPtr(rhs)), true);
408             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
409             return ret;
410         }
411
412         private Size2D Subtract(Size2D rhs)
413         {
414             Size2D ret = new Size2D(Interop.Vector2.Subtract(SwigCPtr, Size2D.getCPtr(rhs)), true);
415             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
416             return ret;
417         }
418
419         private Size2D Multiply(Size2D rhs)
420         {
421             Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, Size2D.getCPtr(rhs)), true);
422             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
423             return ret;
424         }
425
426         private Size2D Multiply(int rhs)
427         {
428             Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, (float)rhs), true);
429             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
430             return ret;
431         }
432
433         private Size2D Divide(Size2D rhs)
434         {
435             Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, Size2D.getCPtr(rhs)), true);
436             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
437             return ret;
438         }
439
440         private Size2D Divide(int rhs)
441         {
442             Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, (float)rhs), true);
443             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
444             return ret;
445         }
446
447         private Size2D Subtract()
448         {
449             Size2D ret = new Size2D(Interop.Vector2.Subtract(SwigCPtr), true);
450             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
451             return ret;
452         }
453
454         private int ValueOfIndex(uint index)
455         {
456             int ret = (int)Interop.Vector2.ValueOfIndex(SwigCPtr, index);
457             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
458             return ret;
459         }
460     }
461 }