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