using System;
using System.Runtime.CompilerServices;
namespace OpenMacroBoard.SDK
{
///
/// Stores a set of four integers that represent the location and size of a rectangle.
///
public readonly struct OmbRectangle : IEquatable
{
///
/// Represents a that has X, Y, Width, and Height values set to zero.
///
public static readonly OmbRectangle Empty = default;
///
/// Initializes a new instance of the struct.
///
/// The horizontal position of the rectangle.
/// The vertical position of the rectangle.
/// The width of the rectangle.
/// The height of the rectangle.
public OmbRectangle(int x, int y, int width, int height)
{
X = x;
Y = y;
Width = width;
Height = height;
}
///
/// Initializes a new instance of the struct.
///
///
/// The which specifies the rectangles point in a two-dimensional plane.
///
///
/// The which specifies the rectangles height and width.
///
public OmbRectangle(OmbPoint point, OmbSize size)
{
X = point.X;
Y = point.Y;
Width = size.Width;
Height = size.Height;
}
///
/// Gets or sets the x-coordinate of this .
///
public int X { get; }
///
/// Gets or sets the y-coordinate of this .
///
public int Y { get; }
///
/// Gets or sets the width of this .
///
public int Width { get; }
///
/// Gets or sets the height of this .
///
public int Height { get; }
///
/// Gets or sets the coordinates of the upper-left corner of the rectangular region represented by this .
///
public OmbPoint Location
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(X, Y);
}
///
/// Gets or sets the size of this .
///
public OmbSize Size
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(Width, Height);
}
///
/// Gets a value indicating whether this is empty.
///
public bool IsEmpty => Equals(Empty);
///
/// Gets the y-coordinate of the top edge of this .
///
public int Top => Y;
///
/// Gets the x-coordinate of the right edge of this .
///
public int Right
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => unchecked(X + Width);
}
///
/// Gets the y-coordinate of the bottom edge of this .
///
public int Bottom
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => unchecked(Y + Height);
}
///
/// Gets the x-coordinate of the left edge of this .
///
public int Left => X;
///
/// Compares two objects for equality.
///
/// The on the left side of the operand.
/// The on the right side of the operand.
///
/// True if the current left is equal to the parameter; otherwise, false.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(OmbRectangle left, OmbRectangle right)
{
return left.Equals(right);
}
///
/// Compares two objects for inequality.
///
/// The on the left side of the operand.
/// The on the right side of the operand.
///
/// True if the current left is unequal to the parameter; otherwise, false.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(OmbRectangle left, OmbRectangle right)
{
return !left.Equals(right);
}
///
/// Creates a rectangle from left, top, right, bottom.
///
public static OmbRectangle FromLTRB(int left, int top, int right, int bottom)
{
return new OmbRectangle(left, top, unchecked(right - left), unchecked(bottom - top));
}
///
/// Deconstructs this rectangle into four integers.
///
/// The out value for X.
/// The out value for Y.
/// The out value for the width.
/// The out value for the height.
public void Deconstruct(out int x, out int y, out int width, out int height)
{
x = X;
y = Y;
width = Width;
height = Height;
}
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(OmbRectangle other)
{
return
X.Equals(other.X) &&
Y.Equals(other.Y) &&
Width.Equals(other.Width) &&
Height.Equals(other.Height);
}
///
public override bool Equals(object obj)
{
return obj is OmbRectangle other && Equals(other);
}
///
public override int GetHashCode()
{
return HashCode.Combine(X, Y, Width, Height);
}
///
public override string ToString()
{
return $"Rectangle [ X={X}, Y={Y}, Width={Width}, Height={Height} ]";
}
}
}