2b93f3488a4058306bd01532521ec0691450a4b5
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Size2D.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 using System;
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
20 using System;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// A two-dimensional size.
26     /// </summary>
27     /// <since_tizen> 3 </since_tizen>
28     [Tizen.NUI.Binding.TypeConverter(typeof(Size2DTypeConverter))]
29     public class Size2D : Disposable, ICloneable
30     {
31
32         private Size2DChangedCallback callback = null;
33
34         /// <summary>
35         /// The constructor.
36         /// </summary>
37         /// <remarks>
38         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
39         /// For example, the followings are possible. <br />
40         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
41         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
42         /// view.MinimumSize = new Size(10, 10, 0); <br />
43         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
44         /// </remarks>
45         /// <since_tizen> 3 </since_tizen>
46         public Size2D() : this(Interop.Vector2.new_Vector2__SWIG_0(), true)
47         {
48             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
49         }
50
51         /// <summary>
52         /// The constructor.
53         /// </summary>
54         /// <param name="width">The width component.</param>
55         /// <param name="height">The height component.</param>
56         /// <remarks>
57         /// Size2D and Size are implicitly converted to each other, so these are compatible and can be replaced without any type casting. <br />
58         /// For example, the followings are possible. <br />
59         /// view.Size2D = new Size(10.0f, 10.0f, 10.0f); // be aware that here the depth value(10.0f) will be lost. <br />
60         /// view.Size = new Size2D(10, 10); // be aware that here the depth value is 0.0f by default. <br />
61         /// view.MinimumSize = new Size(10, 10, 0); <br />
62         /// Size Tmp = view.MaximumSize; //here Tmp.Depth will be 0.0f. <br />
63         /// </remarks>
64         /// <since_tizen> 3 </since_tizen>
65         public Size2D(int width, int height) : this(Interop.Vector2.new_Vector2__SWIG_1((float)width, (float)height), true)
66         {
67             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
68         }
69
70         internal delegate void Size2DChangedCallback(int? width, int? height);
71
72         /// <summary>
73         /// The property for the width component of a size.
74         /// </summary>
75         /// <remarks>
76         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
77         /// </remarks>
78         /// <code>
79         /// // DO NOT use like the followings!
80         /// Size2D size2d = new Size2D();
81         /// size2d.Width = 1; 
82         /// // Please USE like this
83         /// int width = 1, height = 2;
84         /// Size2D size2d = new Size2D(width, height);
85         /// </code>
86         /// <since_tizen> 3 </since_tizen>
87         public int Width
88         {
89             set
90             {
91                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor");
92
93                 Interop.Vector2.Vector2_Width_set(swigCPtr, (float)value);
94                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
95
96                 callback?.Invoke(value, null);
97             }
98             get
99             {
100                 float ret = Interop.Vector2.Vector2_Width_get(swigCPtr);
101                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
102                 return (int)ret;
103             }
104         }
105
106         /// <summary>
107         /// The property for the height component of a size.
108         /// </summary>
109         /// <remarks>
110         /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
111         /// </remarks>
112         /// <code>
113         /// // DO NOT use like the followings!
114         /// Size2D size2d = new Size2D();
115         /// size2d.Height = 2; 
116         /// // Please USE like this
117         /// int width = 1, height = 2;
118         /// Size2D size2d = new Size2D(width, height);
119         /// </code>
120         /// <since_tizen> 3 </since_tizen>
121         public int Height
122         {
123             set
124             {
125                 Tizen.Log.Fatal("NUI", "Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor");
126
127                 Interop.Vector2.Vector2_Height_set(swigCPtr, (float)value);
128                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129
130                 callback?.Invoke(null, value);
131             }
132             get
133             {
134                 float ret = Interop.Vector2.Vector2_Height_get(swigCPtr);
135                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
136                 return (int)ret;
137             }
138         }
139
140         /// <summary>
141         /// The addition operator for A+B.
142         /// </summary>
143         /// <param name="arg1">Size A.</param>
144         /// <param name="arg2">Size to assign B.</param>
145         /// <returns>A size containing the result of the addition.</returns>
146         /// <since_tizen> 3 </since_tizen>
147         public static Size2D operator +(Size2D arg1, Size2D arg2)
148         {
149             return arg1?.Add(arg2);
150         }
151
152         /// <summary>
153         /// The subtraction operator for A-B.
154         /// </summary>
155         /// <param name="arg1">Size A.</param>
156         /// <param name="arg2">Size to subtract B.</param>
157         /// <returns>A size containing the result of the subtraction.</returns>
158         /// <since_tizen> 3 </since_tizen>
159         public static Size2D operator -(Size2D arg1, Size2D arg2)
160         {
161             return arg1?.Subtract(arg2);
162         }
163
164         /// <summary>
165         /// The unary negation operator.
166         /// </summary>
167         /// <param name="arg1">Size for unary negation.</param>
168         /// <returns>A size containing the negation.</returns>
169         /// <since_tizen> 3 </since_tizen>
170         public static Size2D operator -(Size2D arg1)
171         {
172             return arg1?.Subtract();
173         }
174
175         /// <summary>
176         /// The multiplication operator.
177         /// </summary>
178         /// <param name="arg1">Size for multiplication.</param>
179         /// <param name="arg2">Size to multiply.</param>
180         /// <returns>A size containing the result of the multiplication.</returns>
181         /// <since_tizen> 3 </since_tizen>
182         public static Size2D operator *(Size2D arg1, Size2D arg2)
183         {
184             return arg1?.Multiply(arg2);
185         }
186
187         /// <summary>
188         /// The multiplication operator.
189         /// </summary>
190         /// <param name="arg1">Size for multiplication</param>
191         /// <param name="arg2">The integer value to scale the size.</param>
192         /// <returns>A size containing the result of the scaling.</returns>
193
194         /// <since_tizen> 3 </since_tizen>
195         public static Size2D operator *(Size2D arg1, int arg2)
196         {
197             return arg1?.Multiply(arg2);
198         }
199
200         /// <summary>
201         /// The division operator.
202         /// </summary>
203         /// <param name="arg1">Size for division.</param>
204         /// <param name="arg2">Size to divide.</param>
205         /// <returns>A size containing the result of the division.</returns>
206         /// <since_tizen> 3 </since_tizen>
207         public static Size2D operator /(Size2D arg1, Size2D arg2)
208         {
209             return arg1?.Divide(arg2);
210         }
211
212         /// <summary>
213         /// The division operator.
214         /// </summary>
215         /// <param name="arg1">Size for division.</param>
216         /// <param name="arg2">The integer value to scale the size by.</param>
217         /// <returns>A size containing the result of the scaling.</returns>
218         /// <since_tizen> 3 </since_tizen>
219         public static Size2D operator /(Size2D arg1, int arg2)
220         {
221             return arg1?.Divide(arg2);
222         }
223
224         /// <summary>
225         /// The type cast operator, Size2D to Vector2.
226         /// </summary>
227         /// <param name="size">An object of the Size2D type.</param>
228         /// <returns>return a Vector2 instance</returns>
229         /// <since_tizen> 3 </since_tizen>
230         public static implicit operator Vector2(Size2D size)
231         {
232             return new Vector2((float)size?.Width, (float)size.Height);
233         }
234
235         /// <summary>
236         /// The type cast operator, Vector2 to Size2D type.
237         /// </summary>
238         /// <param name="vector2">An object of the Vector2 type.</param>
239         /// <returns>return a Size2D instance</returns>
240         /// <since_tizen> 3 </since_tizen>
241         public static implicit operator Size2D(Vector2 vector2)
242         {
243             return new Size2D((int)vector2?.X, (int)vector2.Y);
244         }
245
246         /// <summary>
247         /// Implicit type cast operator, Size to Size2D
248         /// </summary>
249         /// <param name="size">The object of Size type.</param>
250         /// <since_tizen> none </since_tizen>
251         /// This will be public opened in tizen_next by ACR.
252         [EditorBrowsable(EditorBrowsableState.Never)]
253         public static implicit operator Size2D(Size size)
254         {
255             return new Size2D((int)size?.Width, (int)size.Height);
256         }
257
258
259         /// <summary>
260         /// The array subscript operator.
261         /// </summary>
262         /// <param name="index">The subscript index.</param>
263         /// <returns>The float at the given index.</returns>
264         /// <since_tizen> 3 </since_tizen>
265         public float this[uint index]
266         {
267             get
268             {
269                 return ValueOfIndex(index);
270             }
271         }
272
273         /// <summary>
274         /// Determines whether the specified object is equal to the current object.
275         /// </summary>
276         /// <param name="obj">The object to compare with the current object.</param>
277         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
278         public override bool Equals(System.Object obj)
279         {
280             Size2D size2D = obj as Size2D;
281             bool equal = false;
282             if (Width == size2D?.Width && Height == size2D?.Height)
283             {
284                 equal = true;
285             }
286             return equal;
287         }
288
289         /// <summary>
290         /// Gets the the hash code of this Size2D.
291         /// </summary>
292         /// <returns>The Hash Code.</returns>
293         /// <since_tizen> 6 </since_tizen>
294         public override int GetHashCode()
295         {
296             return swigCPtr.Handle.GetHashCode();
297         }
298
299         /// <summary>
300         /// Checks equality.<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 equal.</returns>
305         /// <since_tizen> 3 </since_tizen>
306         public bool EqualTo(Size2D rhs)
307         {
308             bool ret = Interop.Vector2.Vector2_EqualTo(swigCPtr, Size2D.getCPtr(rhs));
309             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
310             return ret;
311         }
312
313         /// <summary>
314         /// Checks inequality.<br />
315         /// Utilizes appropriate machine epsilon values.<br />
316         /// </summary>
317         /// <param name="rhs">The size to test against.</param>
318         /// <returns>True if the sizes are not equal.</returns>
319         /// <since_tizen> 3 </since_tizen>
320         public bool NotEqualTo(Size2D rhs)
321         {
322             bool ret = Interop.Vector2.Vector2_NotEqualTo(swigCPtr, Size2D.getCPtr(rhs));
323             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
324             return ret;
325         }
326
327         /// <inheritdoc/>
328         [EditorBrowsable(EditorBrowsableState.Never)]
329         public object Clone() => new Size2D(Width, Height);
330
331         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Size2D obj)
332         {
333             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
334         }
335
336         /// <summary>
337         /// Gets the size from the pointer.
338         /// </summary>
339         /// <param name="cPtr">The pointer of the size.</param>
340         /// <returns>Size</returns>
341         internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
342         {
343             Size2D ret = new Size2D(cPtr, false);
344             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345             return ret;
346         }
347
348         internal Size2D(Size2DChangedCallback cb, int x, int y) : this(Interop.Vector2.new_Vector2__SWIG_1((float)x, (float)y), true)
349         {
350             callback = cb;
351             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
352         }
353
354         internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
355         {
356         }
357
358         /// This will not be public opened.
359         [EditorBrowsable(EditorBrowsableState.Never)]
360         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
361         {
362             Interop.Vector2.delete_Vector2(swigCPtr);
363         }
364
365         private Size2D Add(Size2D rhs)
366         {
367             Size2D ret = new Size2D(Interop.Vector2.Vector2_Add(swigCPtr, Size2D.getCPtr(rhs)), true);
368             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
369             return ret;
370         }
371
372         private Size2D Subtract(Size2D rhs)
373         {
374             Size2D ret = new Size2D(Interop.Vector2.Vector2_Subtract__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
375             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
376             return ret;
377         }
378
379         private Size2D Multiply(Size2D rhs)
380         {
381             Size2D ret = new Size2D(Interop.Vector2.Vector2_Multiply__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
382             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
383             return ret;
384         }
385
386         private Size2D Multiply(int rhs)
387         {
388             Size2D ret = new Size2D(Interop.Vector2.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
389             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
390             return ret;
391         }
392
393         private Size2D Divide(Size2D rhs)
394         {
395             Size2D ret = new Size2D(Interop.Vector2.Vector2_Divide__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
396             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
397             return ret;
398         }
399
400         private Size2D Divide(int rhs)
401         {
402             Size2D ret = new Size2D(Interop.Vector2.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
403             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
404             return ret;
405         }
406
407         private Size2D Subtract()
408         {
409             Size2D ret = new Size2D(Interop.Vector2.Vector2_Subtract__SWIG_1(swigCPtr), true);
410             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
411             return ret;
412         }
413
414         private int ValueOfIndex(uint index)
415         {
416             int ret = (int)Interop.Vector2.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
417             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
418             return ret;
419         }
420     }
421 }