2 * Copyright(c) 2019 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 /// The Rectangle class.
26 /// <since_tizen> 3 </since_tizen>
27 [Binding.TypeConverter(typeof(RectangleTypeConverter))]
28 public class Rectangle : Disposable, ICloneable
33 /// <since_tizen> 3 </since_tizen>
34 public Rectangle() : this(Interop.Rectangle.NewRectangle(), true)
36 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
42 /// <param name="x">The x coordinate (or left).</param>
43 /// <param name="y">The y coordinate (or right).</param>
44 /// <param name="width">The width (or bottom).</param>
45 /// <param name="height">The height (or top).</param>
46 /// <since_tizen> 3 </since_tizen>
47 public Rectangle(int x, int y, int width, int height) : this(Interop.Rectangle.NewRectangle(x, y, width, height), true)
49 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
52 internal Rectangle(Rectangle other) : this(other.x, other.y, other.width, other.height)
56 internal Rectangle(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
60 internal Rectangle(RectangleChangedCallback cb, int x, int y, int width, int height) : this(Interop.Rectangle.NewRectangle(x, y, width, height), true)
63 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
66 internal Rectangle(RectangleChangedCallback cb) : this()
71 internal Rectangle(RectangleChangedCallback cb, Rectangle other) : this(cb, other.x, other.y, other.width, other.height)
76 /// The type cast operator, int to Rectangle.
78 /// <param name="value">A value of int type.</param>
79 /// <returns>return a Extents instance</returns>
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public static implicit operator Rectangle(int value)
83 return new Rectangle(value, value, value, value);
86 internal delegate void RectangleChangedCallback(int x, int y, int width, int height);
87 private RectangleChangedCallback callback = null;
90 /// The x position of the rectangle.
93 /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
96 /// // DO NOT use like the followings!
97 /// Rectangle rectangle = new Rectangle();
100 /// int x = 1, y = 2, width = 3, height = 4;
101 /// Rectangle rectangle = new Rectangle(x, y, width, height);
103 /// <since_tizen> 3 </since_tizen>
106 [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
111 callback?.Invoke(X, Y, Width, Height);
120 /// The Y position of the rectangle.
123 /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
126 /// // DO NOT use like the followings!
127 /// Rectangle rectangle = new Rectangle();
130 /// int x = 1, y = 2, width = 3, height = 4;
131 /// Rectangle rectangle = new Rectangle(x, y, width, height);
133 /// <since_tizen> 3 </since_tizen>
136 [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
141 callback?.Invoke(X, Y, Width, Height);
150 /// The width of the rectangle.
153 /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
156 /// // DO NOT use like the followings!
157 /// Rectangle rectangle = new Rectangle();
158 /// rectangle.Width = 3;
160 /// int x = 1, y = 2, width = 3, height = 4;
161 /// Rectangle rectangle = new Rectangle(x, y, width, height);
163 /// <since_tizen> 3 </since_tizen>
166 [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
171 callback?.Invoke(X, Y, Width, Height);
180 /// The height of the rectangle.
183 /// The setter is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor.
186 /// // DO NOT use like the followings!
187 /// Rectangle rectangle = new Rectangle();
188 /// rectangle.Height = 4;
190 /// int x = 1, y = 2, width = 3, height = 4;
191 /// Rectangle rectangle = new Rectangle(x, y, width, height);
193 /// <since_tizen> 3 </since_tizen>
196 [Obsolete("Do not use this setter, that is deprecated in API8 and will be removed in API10. Use new Rectangle(...) constructor")]
201 callback?.Invoke(X, Y, Width, Height);
213 Interop.Rectangle.XSet(SwigCPtr, value);
214 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218 int ret = Interop.Rectangle.XGet(SwigCPtr);
219 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
228 Interop.Rectangle.LeftSet(SwigCPtr, value);
229 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
233 int ret = Interop.Rectangle.LeftGet(SwigCPtr);
234 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
243 Interop.Rectangle.YSet(SwigCPtr, value);
244 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
248 int ret = Interop.Rectangle.YGet(SwigCPtr);
249 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258 Interop.Rectangle.RightSet(SwigCPtr, value);
259 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
263 int ret = Interop.Rectangle.RightGet(SwigCPtr);
264 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
273 Interop.Rectangle.WidthSet(SwigCPtr, value);
274 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
278 int ret = Interop.Rectangle.WidthGet(SwigCPtr);
279 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
288 Interop.Rectangle.BottomSet(SwigCPtr, value);
289 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
293 int ret = Interop.Rectangle.BottomGet(SwigCPtr);
294 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
303 Interop.Rectangle.HeightSet(SwigCPtr, value);
304 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
308 int ret = Interop.Rectangle.HeightGet(SwigCPtr);
309 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
318 Interop.Rectangle.TopSet(SwigCPtr, value);
319 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
323 int ret = Interop.Rectangle.TopGet(SwigCPtr);
324 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
330 /// THe Equality operator.
332 /// <param name="a">The first operand.</param>
333 /// <param name="b">The second operand.</param>
334 /// <returns>True if the boxes are exactly the same.</returns>
335 /// <since_tizen> 3 </since_tizen>
336 public static bool operator ==(Rectangle a, Rectangle b)
338 // If both are null, or both are same instance, return true.
339 if (System.Object.ReferenceEquals(a, b))
344 // If one is null, but not both, return false.
345 if (((object)a == null) || ((object)b == null))
350 // Return true if the fields match:
351 return a.X == b.X && a.Y == b.Y && a.Width == b.Width && a.Height == b.Height;
355 /// Inequality operator.
357 /// <param name="a">The first rectangle.</param>
358 /// <param name="b">The second rectangle.</param>
359 /// <returns>True if the rectangles are not identical.</returns>
360 /// <since_tizen> 3 </since_tizen>
361 public static bool operator !=(Rectangle a, Rectangle b)
367 /// Equality operator.
369 /// <param name="o">The object to compare with the current object.</param>
370 /// <returns>True if boxes are exactly same.</returns>
371 /// <since_tizen> 4 </since_tizen>
372 public override bool Equals(object o)
378 if (!(o is Rectangle))
382 Rectangle r = (Rectangle)o;
384 // Return true if the fields match:
385 return X == r.X && Y == r.Y && Width == r.Width && Height == r.Height;
389 /// Serves as the default hash function.
391 /// <returns>A hash code for the current object.</returns>
392 /// <since_tizen> 4 </since_tizen>
393 public override int GetHashCode()
395 return base.GetHashCode();
399 /// Assignment from individual values.
401 /// <param name="newX">The x coordinate.</param>
402 /// <param name="newY">The y coordinate.</param>
403 /// <param name="newWidth">The width.</param>
404 /// <param name="newHeight">The height.</param>
405 /// <since_tizen> 3 </since_tizen>
406 public void Set(int newX, int newY, int newWidth, int newHeight)
408 Interop.Rectangle.Set(SwigCPtr, newX, newY, newWidth, newHeight);
409 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
413 /// Determines whether or not this rectangle is empty.
415 /// <returns>True if width or height are zero.</returns>
416 /// <since_tizen> 3 </since_tizen>
417 public bool IsEmpty()
419 bool ret = Interop.Rectangle.IsEmpty(SwigCPtr);
420 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
425 /// Gets the left of the rectangle.
427 /// <returns>The left edge of the rectangle.</returns>
428 /// <since_tizen> 3 </since_tizen>
431 int ret = Interop.Rectangle.Left(SwigCPtr);
432 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
437 /// Gets the right of the rectangle.
439 /// <returns>The right edge of the rectangle.</returns>
440 /// <since_tizen> 3 </since_tizen>
443 int ret = Interop.Rectangle.Right(SwigCPtr);
444 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
449 /// Gets the top of the rectangle.
451 /// <returns>The top of the rectangle.</returns>
452 /// <since_tizen> 3 </since_tizen>
455 int ret = Interop.Rectangle.Top(SwigCPtr);
456 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
461 /// Gets the bottom of the rectangle.
463 /// <returns>The bottom of the rectangle.</returns>
464 /// <since_tizen> 3 </since_tizen>
467 int ret = Interop.Rectangle.Bottom(SwigCPtr);
468 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
473 /// Gets the area of the rectangle.
475 /// <returns>The area of the rectangle.</returns>
476 /// <since_tizen> 3 </since_tizen>
479 int ret = Interop.Rectangle.Area(SwigCPtr);
480 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
485 /// Determines whether or not this rectangle and the specified rectangle intersect.
487 /// <param name="other">The other rectangle to test against this rectangle.</param>
488 /// <returns>True if the rectangles intersect.</returns>
489 /// <since_tizen> 3 </since_tizen>
490 public bool Intersects(Rectangle other)
492 bool ret = Interop.Rectangle.Intersects(SwigCPtr, Rectangle.getCPtr(other));
493 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
498 /// Determines whether or not this rectangle contains the specified rectangle.
500 /// <param name="other">The other rectangle to test against this rectangle.</param>
501 /// <returns>True if the specified rectangle is contained.</returns>
502 /// <since_tizen> 3 </since_tizen>
503 public bool Contains(Rectangle other)
505 bool ret = Interop.Rectangle.Contains(SwigCPtr, Rectangle.getCPtr(other));
506 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
511 [EditorBrowsable(EditorBrowsableState.Never)]
512 public object Clone() => new Rectangle(this);
514 /// This will not be public opened.
515 [EditorBrowsable(EditorBrowsableState.Never)]
516 protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
518 Interop.Rectangle.DeleteRectangle(swigCPtr);
522 /// Determines whether the reference is null or the Rectangle has all 0 properties.
524 internal static bool IsNullOrZero(Rectangle rectangle) => (rectangle == null || (rectangle.top == 0 && rectangle.right == 0 && rectangle.bottom == 0 && rectangle.left == 0));