using System; using Microsoft.Xna.Framework; namespace MonoGame.Extended { /// /// A two dimensional size defined by two real numbers, a width and a height. /// /// /// /// A size is a subspace of two-dimensional space, the area of which is described in terms of a two-dimensional /// coordinate system, given by a reference point and two coordinate axes. /// /// /// /// public struct Size : IEquatable, IEquatableByRef { /// /// Returns a with and equal to 0.0f. /// public static readonly Size Empty = new Size(); /// /// The horizontal component of this . /// public int Width; /// /// The vertical component of this . /// public int Height; /// /// Gets a value that indicates whether this is empty. /// public bool IsEmpty => Width == 0 && Height == 0; /// /// Initializes a new instance of the structure from the specified dimensions. /// /// The width. /// The height. public Size(int width, int height) { Width = width; Height = height; } /// /// Compares two structures. The result specifies /// whether the values of the and /// fields of the two structures are equal. /// /// The first size. /// The second size. /// /// true if the and /// fields of the two structures are equal; otherwise, false. /// public static bool operator ==(Size first, Size second) { return first.Equals(ref second); } /// /// Indicates whether this is equal to another . /// /// The size. /// /// true if this is equal to the parameter; otherwise, /// false. /// public bool Equals(Size size) { return Equals(ref size); } /// /// Indicates whether this is equal to another . /// /// The size. /// /// true if this is equal to the ; otherwise, /// false. /// public bool Equals(ref Size size) { return Width == size.Width && Height == size.Height; } /// /// Returns a value indicating whether this is equal to a specified object. /// /// The object to make the comparison with. /// /// true if this is equal to ; otherwise, false. /// public override bool Equals(object obj) { if (obj is Size) return Equals((Size) obj); return false; } /// /// Compares two structures. The result specifies /// whether the values of the or /// fields of the two structures are unequal. /// /// The first size. /// The second size. /// /// true if the or /// fields of the two structures are unequal; otherwise, false. /// public static bool operator !=(Size first, Size second) { return !(first == second); } /// /// Calculates the representing the vector addition of two structures as if /// they /// were structures. /// /// The first size. /// The second size. /// /// The representing the vector addition of two structures as if they /// were structures. /// public static Size operator +(Size first, Size second) { return Add(first, second); } /// /// Calculates the representing the vector addition of two structures. /// /// The first size. /// The second size. /// /// The representing the vector addition of two structures. /// public static Size Add(Size first, Size second) { Size size; size.Width = first.Width + second.Width; size.Height = first.Height + second.Height; return size; } /// /// Calculates the representing the vector subtraction of two structures. /// /// The first size. /// The second size. /// /// The representing the vector subtraction of two structures. /// public static Size operator -(Size first, Size second) { return Subtract(first, second); } public static Size operator /(Size size, int value) { return new Size(size.Width / value, size.Height / value); } public static Size operator *(Size size, int value) { return new Size(size.Width * value, size.Height * value); } /// /// Calculates the representing the vector subtraction of two structures. /// /// The first size. /// The second size. /// /// The representing the vector subtraction of two structures. /// public static Size Subtract(Size first, Size second) { Size size; size.Width = first.Width - second.Width; size.Height = first.Height - second.Height; return size; } /// /// Returns a hash code of this suitable for use in hashing algorithms and data /// structures like a hash table. /// /// /// A hash code of this . /// public override int GetHashCode() { unchecked { // ReSharper disable NonReadonlyMemberInGetHashCode return (Width.GetHashCode()*397) ^ Height.GetHashCode(); // ReSharper restore NonReadonlyMemberInGetHashCode } } /// /// Performs an implicit conversion from a to a . /// /// The point. /// /// The resulting . /// public static implicit operator Size(Point point) { return new Size(point.X, point.Y); } /// /// Performs an implicit conversion from a to a . /// /// The size. /// /// The resulting . /// public static implicit operator Point(Size size) { return new Point(size.Width, size.Height); } public static explicit operator Size(SizeF size) { return new Size((int) size.Width, (int) size.Height); } /// /// Returns a that represents this . /// /// /// A that represents this . /// public override string ToString() { return $"Width: {Width}, Height: {Height}"; } internal string DebugDisplayString => ToString(); } }