5df3635290c19c671d556b2daab899a53c590b3a
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Size2D.cs
1 /*
2  * Copyright (c) 2018 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 Tizen.NUI.Binding;
19
20 namespace Tizen.NUI
21 {
22     /// <summary>
23     /// A two-dimensional size.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     [Tizen.NUI.Binding.TypeConverter(typeof(Size2DTypeConverter))]
27     public class Size2D : global::System.IDisposable
28     {
29         /// <summary>
30         /// swigCMemOwn
31         /// </summary>
32         /// <since_tizen> 3 </since_tizen>
33         protected bool swigCMemOwn;
34
35         /// <summary>
36         /// A Flat to check if it is already disposed.
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         protected bool disposed = false;
40
41         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
42
43         //A Flag to check who called Dispose(). (By User or DisposeQueue)
44         private bool isDisposeQueued = false;
45
46         private Size2DChangedCallback callback = null;
47
48         /// <summary>
49         /// The constructor.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         public Size2D() : this(NDalicPINVOKE.new_Vector2__SWIG_0(), true)
53         {
54             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
55         }
56
57         /// <summary>
58         /// The constructor.
59         /// </summary>
60         /// <param name="x">The x (or width) component.</param>
61         /// <param name="y">The y (or height) component.</param>
62         /// <since_tizen> 3 </since_tizen>
63         public Size2D(int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
64         {
65             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
66         }
67
68         /// <summary>
69         /// Dispose.
70         /// </summary>
71         /// <since_tizen> 3 </since_tizen>
72         ~Size2D()
73         {
74             if (!isDisposeQueued)
75             {
76                 isDisposeQueued = true;
77                 DisposeQueue.Instance.Add(this);
78             }
79         }
80
81         internal delegate void Size2DChangedCallback(int width, int height);
82
83         /// <summary>
84         /// The property for the width component of a size.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         public int Width
88         {
89             set
90             {
91                 NDalicPINVOKE.Vector2_Width_set(swigCPtr, (float)value);
92                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
93
94                 callback?.Invoke(Width, Height);
95             }
96             get
97             {
98                 float ret = NDalicPINVOKE.Vector2_Width_get(swigCPtr);
99                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
100                 return (int)ret;
101             }
102         }
103
104         /// <summary>
105         /// The property for the height component of a size.
106         /// </summary>
107         /// <since_tizen> 3 </since_tizen>
108         public int Height
109         {
110             set
111             {
112                 NDalicPINVOKE.Vector2_Height_set(swigCPtr, (float)value);
113                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
114
115                 callback?.Invoke(Width, Height);
116             }
117             get
118             {
119                 float ret = NDalicPINVOKE.Vector2_Height_get(swigCPtr);
120                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
121                 return (int)ret;
122             }
123         }
124
125         /// <summary>
126         /// The addition operator for A+B.
127         /// </summary>
128         /// <param name="arg1">Size A.</param>
129         /// <param name="arg2">Size to assign B.</param>
130         /// <returns>A size containing the result of the addition.</returns>
131         /// <since_tizen> 3 </since_tizen>
132         public static Size2D operator +(Size2D arg1, Size2D arg2)
133         {
134             return arg1.Add(arg2);
135         }
136
137         /// <summary>
138         /// The subtraction operator for A-B.
139         /// </summary>
140         /// <param name="arg1">Size A.</param>
141         /// <param name="arg2">Size to subtract B.</param>
142         /// <returns>A size containing the result of the subtraction.</returns>
143         /// <since_tizen> 3 </since_tizen>
144         public static Size2D operator -(Size2D arg1, Size2D arg2)
145         {
146             return arg1.Subtract(arg2);
147         }
148
149         /// <summary>
150         /// The unary negation operator.
151         /// </summary>
152         /// <param name="arg1">Size for unary negation.</param>
153         /// <returns>A size containing the negation.</returns>
154         /// <since_tizen> 3 </since_tizen>
155         public static Size2D operator -(Size2D arg1)
156         {
157             return arg1.Subtract();
158         }
159
160         /// <summary>
161         /// The multiplication operator.
162         /// </summary>
163         /// <param name="arg1">Size for multiplication.</param>
164         /// <param name="arg2">Size to multiply.</param>
165         /// <returns>A size containing the result of the multiplication.</returns>
166         /// <since_tizen> 3 </since_tizen>
167         public static Size2D operator *(Size2D arg1, Size2D arg2)
168         {
169             return arg1.Multiply(arg2);
170         }
171
172         /// <summary>
173         /// The multiplication operator.
174         /// </summary>
175         /// <param name="arg1">Size for multiplication</param>
176         /// <param name="arg2">The integer value to scale the size.</param>
177         /// <returns>A size containing the result of the scaling.</returns>
178
179         /// <since_tizen> 3 </since_tizen>
180         public static Size2D operator *(Size2D arg1, int arg2)
181         {
182             return arg1.Multiply(arg2);
183         }
184
185         /// <summary>
186         /// The division operator.
187         /// </summary>
188         /// <param name="arg1">Size for division.</param>
189         /// <param name="arg2">Size to divide.</param>
190         /// <returns>A size containing the result of the division.</returns>
191         /// <since_tizen> 3 </since_tizen>
192         public static Size2D operator /(Size2D arg1, Size2D arg2)
193         {
194             return arg1.Divide(arg2);
195         }
196
197         /// <summary>
198         /// The division operator.
199         /// </summary>
200         /// <param name="arg1">Size for division.</param>
201         /// <param name="arg2">The integer value to scale the size by.</param>
202         /// <returns>A size containing the result of the scaling.</returns>
203         /// <since_tizen> 3 </since_tizen>
204         public static Size2D operator /(Size2D arg1, int arg2)
205         {
206             return arg1.Divide(arg2);
207         }
208
209         /// <summary>
210         /// The type cast operator, Size2D to Vector2.
211         /// </summary>
212         /// <param name="size">An object of the Size2D type.</param>
213         /// <returns>return a Vector2 instance</returns>
214         /// <since_tizen> 3 </since_tizen>
215         public static implicit operator Vector2(Size2D size)
216         {
217             return new Vector2((float)size.Width, (float)size.Height);
218         }
219
220         /// <summary>
221         /// The type cast operator, Vector2 to Size2D type.
222         /// </summary>
223         /// <param name="vector2">An object of the Vector2 type.</param>
224         /// <returns>return a Size2D instance</returns>
225         /// <since_tizen> 3 </since_tizen>
226         public static implicit operator Size2D(Vector2 vector2)
227         {
228             return new Size2D((int)vector2.X, (int)vector2.Y);
229         }
230
231         /// <summary>
232         /// The array subscript operator.
233         /// </summary>
234         /// <param name="index">The subscript index.</param>
235         /// <returns>The float at the given index.</returns>
236         /// <since_tizen> 3 </since_tizen>
237         public float this[uint index]
238         {
239             get
240             {
241                 return ValueOfIndex(index);
242             }
243         }
244
245         /// <summary>
246         /// Dispose.
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         public void Dispose()
250         {
251             //Throw excpetion if Dispose() is called in separate thread.
252             if (!Window.IsInstalled())
253             {
254                 throw new System.InvalidOperationException("This API called from separate thread. This API must be called from MainThread.");
255             }
256
257             if (isDisposeQueued)
258             {
259                 Dispose(DisposeTypes.Implicit);
260             }
261             else
262             {
263                 Dispose(DisposeTypes.Explicit);
264                 System.GC.SuppressFinalize(this);
265             }
266         }
267
268         /// <summary>
269         /// Determines whether the specified object is equal to the current object.
270         /// </summary>
271         /// <param name="obj">The object to compare with the current object.</param>
272         /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
273         public override bool Equals(System.Object obj)
274         {
275             Size2D size2D = obj as Size2D;
276             bool equal = false;
277             if (Width == size2D?.Width && Height == size2D?.Height)
278             {
279                 equal = true;
280             }
281             return equal;
282         }
283
284         /// <summary>
285         /// Gets the the hash code of this Size2D.
286         /// </summary>
287         /// <returns>The Hash Code.</returns>
288         /// <since_tizen> 5 </since_tizen>
289         public override int GetHashCode()
290         {
291             return Width.GetHashCode();
292         }
293
294         /// <summary>
295         /// Checks equality.<br />
296         /// Utilizes appropriate machine epsilon values.<br />
297         /// </summary>
298         /// <param name="rhs">The size to test against.</param>
299         /// <returns>True if the sizes are equal.</returns>
300         /// <since_tizen> 3 </since_tizen>
301         public bool EqualTo(Size2D rhs)
302         {
303             bool ret = NDalicPINVOKE.Vector2_EqualTo(swigCPtr, Size2D.getCPtr(rhs));
304             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
305             return ret;
306         }
307
308         /// <summary>
309         /// Checks inequality.<br />
310         /// Utilizes appropriate machine epsilon values.<br />
311         /// </summary>
312         /// <param name="rhs">The size to test against.</param>
313         /// <returns>True if the sizes are not equal.</returns>
314         /// <since_tizen> 3 </since_tizen>
315         public bool NotEqualTo(Size2D rhs)
316         {
317             bool ret = NDalicPINVOKE.Vector2_NotEqualTo(swigCPtr, Size2D.getCPtr(rhs));
318             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
319             return ret;
320         }
321
322         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Size2D obj)
323         {
324             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
325         }
326
327         /// <summary>
328         /// Gets the size from the pointer.
329         /// </summary>
330         /// <param name="cPtr">The pointer of the size.</param>
331         /// <returns>Size</returns>
332         internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
333         {
334             Size2D ret = new Size2D(cPtr, false);
335             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
336             return ret;
337         }
338
339         internal Size2D(Size2DChangedCallback cb, int x, int y) : this(NDalicPINVOKE.new_Vector2__SWIG_1((float)x, (float)y), true)
340         {
341             callback = cb;
342             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
343         }
344
345         internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn)
346         {
347             swigCMemOwn = cMemoryOwn;
348             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
349         }
350
351         /// <summary>
352         /// Dispose.
353         /// </summary>
354         /// <since_tizen> 3 </since_tizen>
355         protected virtual void Dispose(DisposeTypes type)
356         {
357             if (disposed)
358             {
359                 return;
360             }
361
362             if (type == DisposeTypes.Explicit)
363             {
364                 //Called by User
365                 //Release your own managed resources here.
366                 //You should release all of your own disposable objects here.
367             }
368
369             //Release your own unmanaged resources here.
370             //You should not access any managed member here except static instance.
371             //because the execution order of Finalizes is non-deterministic.
372
373             if (swigCPtr.Handle != global::System.IntPtr.Zero)
374             {
375                 if (swigCMemOwn)
376                 {
377                     swigCMemOwn = false;
378                     NDalicPINVOKE.delete_Vector2(swigCPtr);
379                 }
380                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
381             }
382             disposed = true;
383         }
384
385         private Size2D Add(Size2D rhs)
386         {
387             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Add(swigCPtr, Size2D.getCPtr(rhs)), true);
388             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
389             return ret;
390         }
391
392         private Size2D Subtract(Size2D rhs)
393         {
394             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Subtract__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
395             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
396             return ret;
397         }
398
399         private Size2D Multiply(Size2D rhs)
400         {
401             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Multiply__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
402             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403             return ret;
404         }
405
406         private Size2D Multiply(int rhs)
407         {
408             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Multiply__SWIG_1(swigCPtr, (float)rhs), true);
409             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
410             return ret;
411         }
412
413         private Size2D Divide(Size2D rhs)
414         {
415             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Divide__SWIG_0(swigCPtr, Size2D.getCPtr(rhs)), true);
416             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
417             return ret;
418         }
419
420         private Size2D Divide(int rhs)
421         {
422             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Divide__SWIG_1(swigCPtr, (float)rhs), true);
423             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
424             return ret;
425         }
426
427         private Size2D Subtract()
428         {
429             Size2D ret = new Size2D(NDalicPINVOKE.Vector2_Subtract__SWIG_1(swigCPtr), true);
430             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
431             return ret;
432         }
433
434         private int ValueOfIndex(uint index)
435         {
436             int ret = (int)NDalicPINVOKE.Vector2_ValueOfIndex__SWIG_0(swigCPtr, index);
437             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
438             return ret;
439         }
440     }
441 }