2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
24 /// A two-dimensional size.
26 /// <since_tizen> 3 </since_tizen>
27 [Tizen.NUI.Binding.TypeConverter(typeof(Size2DTypeConverter))]
28 public class Size2D : Disposable, ICloneable
31 private Size2DChangedCallback callback = null;
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 />
44 /// <since_tizen> 3 </since_tizen>
45 public Size2D() : this(Interop.Vector2.NewVector2(), true)
47 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
53 /// <param name="width">The width component.</param>
54 /// <param name="height">The height component.</param>
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 />
63 /// <since_tizen> 3 </since_tizen>
64 public Size2D(int width, int height) : this(Interop.Vector2.NewVector2((float)width, (float)height), true)
66 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
69 internal delegate void Size2DChangedCallback(int width, int height);
72 /// Hidden API (Inhouse API).
76 /// Following the guide of https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose.
77 /// This will replace "protected virtual void Dispose(DisposeTypes type)" which is exactly same in functionality.
79 /// <param name="disposing">true in order to free managed objects</param>
80 // Protected implementation of Dispose pattern.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 protected override void Dispose(bool disposing)
91 //perform dipose here without being added to DisposeQueue.
92 if (SwigCMemOwn && SwigCPtr.Handle != IntPtr.Zero)
96 base.Dispose(DisposeTypes.Explicit);
100 base.Dispose(DisposeTypes.Implicit);
104 base.Dispose(disposing);
108 /// The property for the width component of a size.
111 /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
114 /// // DO NOT use like the followings!
115 /// Size2D size2d = new Size2D();
116 /// size2d.Width = 1;
117 /// // Please USE like this
118 /// int width = 1, height = 2;
119 /// Size2D size2d = new Size2D(width, height);
121 /// <since_tizen> 3 </since_tizen>
124 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
127 Interop.Vector2.WidthSet(SwigCPtr, (float)value);
128 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
130 callback?.Invoke(value, Height);
134 float ret = Interop.Vector2.WidthGet(SwigCPtr);
135 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
141 /// The property for the height component of a size.
144 /// The setter is deprecated in API8 and will be removed in API10. Please use new Size2D(...) constructor.
147 /// // DO NOT use like the followings!
148 /// Size2D size2d = new Size2D();
149 /// size2d.Height = 2;
150 /// // Please USE like this
151 /// int width = 1, height = 2;
152 /// Size2D size2d = new Size2D(width, height);
154 /// <since_tizen> 3 </since_tizen>
157 [Obsolete("Please do not use this setter, Deprecated in API8, will be removed in API10. please use new Size2D(...) constructor")]
160 Interop.Vector2.HeightSet(SwigCPtr, (float)value);
161 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163 callback?.Invoke(Width, value);
167 float ret = Interop.Vector2.HeightGet(SwigCPtr);
168 if (NDalicPINVOKE.SWIGPendingException.Pending) throw new InvalidOperationException("FATAL: get Exception", NDalicPINVOKE.SWIGPendingException.Retrieve());
174 /// The addition operator for A+B.
176 /// <param name="arg1">Size A.</param>
177 /// <param name="arg2">Size to assign B.</param>
178 /// <returns>A size containing the result of the addition.</returns>
179 /// <since_tizen> 3 </since_tizen>
180 public static Size2D operator +(Size2D arg1, Size2D arg2)
182 return arg1?.Add(arg2);
186 /// The subtraction operator for A-B.
188 /// <param name="arg1">Size A.</param>
189 /// <param name="arg2">Size to subtract B.</param>
190 /// <returns>A size containing the result of the subtraction.</returns>
191 /// <since_tizen> 3 </since_tizen>
192 public static Size2D operator -(Size2D arg1, Size2D arg2)
194 return arg1?.Subtract(arg2);
198 /// The unary negation operator.
200 /// <param name="arg1">Size for unary negation.</param>
201 /// <returns>A size containing the negation.</returns>
202 /// <since_tizen> 3 </since_tizen>
203 public static Size2D operator -(Size2D arg1)
205 return arg1?.Subtract();
209 /// The multiplication operator.
211 /// <param name="arg1">Size for multiplication.</param>
212 /// <param name="arg2">Size to multiply.</param>
213 /// <returns>A size containing the result of the multiplication.</returns>
214 /// <since_tizen> 3 </since_tizen>
215 public static Size2D operator *(Size2D arg1, Size2D arg2)
217 return arg1?.Multiply(arg2);
221 /// The multiplication operator.
223 /// <param name="arg1">Size for multiplication</param>
224 /// <param name="arg2">The integer value to scale the size.</param>
225 /// <returns>A size containing the result of the scaling.</returns>
227 /// <since_tizen> 3 </since_tizen>
228 public static Size2D operator *(Size2D arg1, int arg2)
230 return arg1?.Multiply(arg2);
234 /// The division operator.
236 /// <param name="arg1">Size for division.</param>
237 /// <param name="arg2">Size to divide.</param>
238 /// <returns>A size containing the result of the division.</returns>
239 /// <since_tizen> 3 </since_tizen>
240 public static Size2D operator /(Size2D arg1, Size2D arg2)
242 return arg1?.Divide(arg2);
246 /// The division operator.
248 /// <param name="arg1">Size for division.</param>
249 /// <param name="arg2">The integer value to scale the size by.</param>
250 /// <returns>A size containing the result of the scaling.</returns>
251 /// <since_tizen> 3 </since_tizen>
252 public static Size2D operator /(Size2D arg1, int arg2)
254 return arg1?.Divide(arg2);
258 /// The type cast operator, Size2D to Vector2.
260 /// <param name="size">An object of the Size2D type.</param>
261 /// <returns>return a Vector2 instance</returns>
262 /// <since_tizen> 3 </since_tizen>
263 public static implicit operator Vector2(Size2D size)
269 return new Vector2(size.Width, size.Height);
273 /// The type cast operator, Vector2 to Size2D type.
275 /// <param name="vector2">An object of the Vector2 type.</param>
276 /// <returns>return a Size2D instance</returns>
277 /// <since_tizen> 3 </since_tizen>
278 public static implicit operator Size2D(Vector2 vector2)
284 return new Size2D((int)vector2.X, (int)vector2.Y);
288 /// Implicit type cast operator, Size to Size2D
290 /// <param name="size">The object of Size type.</param>
291 /// <since_tizen> none </since_tizen>
292 /// This will be public opened in tizen_next by ACR.
293 [EditorBrowsable(EditorBrowsableState.Never)]
294 public static implicit operator Size2D(Size size)
300 return new Size2D((int)size.Width, (int)size.Height);
305 /// The array subscript operator.
307 /// <param name="index">The subscript index.</param>
308 /// <returns>The float at the given index.</returns>
309 /// <since_tizen> 3 </since_tizen>
310 public float this[uint index]
314 return ValueOfIndex(index);
319 /// Determines whether the specified object is equal to the current object.
321 /// <param name="obj">The object to compare with the current object.</param>
322 /// <returns>true if the specified object is equal to the current object; otherwise, false.</returns>
323 public override bool Equals(System.Object obj)
325 Size2D size2D = obj as Size2D;
327 if (Width == size2D?.Width && Height == size2D?.Height)
335 /// Gets the hash code of this Size2D.
337 /// <returns>The Hash Code.</returns>
338 /// <since_tizen> 6 </since_tizen>
339 public override int GetHashCode()
341 return SwigCPtr.Handle.GetHashCode();
345 /// Checks equality.<br />
346 /// Utilizes appropriate machine epsilon values.<br />
348 /// <param name="rhs">The size to test against.</param>
349 /// <returns>True if the sizes are equal.</returns>
350 /// <since_tizen> 3 </since_tizen>
351 public bool EqualTo(Size2D rhs)
353 bool ret = Interop.Vector2.EqualTo(SwigCPtr, Size2D.getCPtr(rhs));
354 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
359 /// Checks inequality.<br />
360 /// Utilizes appropriate machine epsilon values.<br />
362 /// <param name="rhs">The size to test against.</param>
363 /// <returns>True if the sizes are not equal.</returns>
364 /// <since_tizen> 3 </since_tizen>
365 public bool NotEqualTo(Size2D rhs)
367 bool ret = Interop.Vector2.NotEqualTo(SwigCPtr, Size2D.getCPtr(rhs));
368 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373 [EditorBrowsable(EditorBrowsableState.Never)]
374 public object Clone() => new Size2D(Width, Height);
377 /// Gets the size from the pointer.
379 /// <param name="cPtr">The pointer of the size.</param>
380 /// <returns>Size</returns>
381 internal static Size2D GetSize2DFromPtr(global::System.IntPtr cPtr)
383 Size2D ret = new Size2D(cPtr, false);
384 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
388 internal Size2D(Size2DChangedCallback cb, int x, int y) : this(Interop.Vector2.NewVector2((float)x, (float)y), true)
391 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
394 internal Size2D(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
398 /// This will not be public opened.
399 [EditorBrowsable(EditorBrowsableState.Never)]
400 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
402 Interop.Vector2.DeleteVector2(swigCPtr);
405 private Size2D Add(Size2D rhs)
407 Size2D ret = new Size2D(Interop.Vector2.Add(SwigCPtr, Size2D.getCPtr(rhs)), true);
408 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
412 private Size2D Subtract(Size2D rhs)
414 Size2D ret = new Size2D(Interop.Vector2.Subtract(SwigCPtr, Size2D.getCPtr(rhs)), true);
415 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
419 private Size2D Multiply(Size2D rhs)
421 Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, Size2D.getCPtr(rhs)), true);
422 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426 private Size2D Multiply(int rhs)
428 Size2D ret = new Size2D(Interop.Vector2.Multiply(SwigCPtr, (float)rhs), true);
429 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
433 private Size2D Divide(Size2D rhs)
435 Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, Size2D.getCPtr(rhs)), true);
436 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
440 private Size2D Divide(int rhs)
442 Size2D ret = new Size2D(Interop.Vector2.Divide(SwigCPtr, (float)rhs), true);
443 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
447 private Size2D Subtract()
449 Size2D ret = new Size2D(Interop.Vector2.Subtract(SwigCPtr), true);
450 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
454 private int ValueOfIndex(uint index)
456 int ret = (int)Interop.Vector2.ValueOfIndex(SwigCPtr, index);
457 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();