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