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