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 SizeF : IEquatable, IEquatableByRef { /// /// Returns a with and equal to 0.0f. /// public static readonly SizeF Empty = new SizeF(); /// /// The horizontal component of this . /// public float Width; /// /// The vertical component of this . /// public float Height; /// /// Gets a value that indicates whether this is empty. /// // ReSharper disable CompareOfFloatsByEqualityOperator public bool IsEmpty => (Width == 0) && (Height == 0); // ReSharper restore CompareOfFloatsByEqualityOperator /// /// Initializes a new instance of the structure from the specified dimensions. /// /// The width. /// The height. public SizeF(float width, float 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 ==(SizeF first, SizeF 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(SizeF 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 SizeF size) { // ReSharper disable CompareOfFloatsByEqualityOperator return (Width == size.Width) && (Height == size.Height); // ReSharper restore CompareOfFloatsByEqualityOperator } /// /// 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 SizeF) return Equals((SizeF) 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 !=(SizeF first, SizeF 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 SizeF operator +(SizeF first, SizeF 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 SizeF Add(SizeF first, SizeF second) { SizeF 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 SizeF operator -(SizeF first, SizeF second) { return Subtract(first, second); } public static Vector2 operator -(Vector2 first, SizeF second) { return Subtract(first, second); } public static SizeF operator /(SizeF size, float value) { return new SizeF(size.Width / value, size.Height / value); } public static SizeF operator *(SizeF size, float value) { return new SizeF(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 SizeF Subtract(SizeF first, SizeF second) { SizeF 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 { return (Width.GetHashCode()*397) ^ Height.GetHashCode(); } } /// /// Performs an implicit conversion from a to a . /// /// The point. /// /// The resulting . /// public static implicit operator SizeF(Vector2 point) { return new SizeF(point.X, point.Y); } /// /// Performs an implicit conversion from a to a . /// /// The point. /// /// The resulting . /// public static implicit operator SizeF(Point point) { return new SizeF(point.X, point.Y); } /// /// Performs an implicit conversion from a to a . /// /// The size. /// /// The resulting . /// public static implicit operator Vector2(SizeF size) { return new Vector2(size.Width, size.Height); } ///// ///// Performs an implicit conversion from a to a . ///// ///// The size. ///// ///// The resulting . ///// //public static implicit operator Size2(Size size) //{ // return new Size2(size.Width, size.Height); //} /// /// Performs an explicit conversion from a to a . /// /// The size. /// /// The resulting . /// public static explicit operator Point(SizeF size) { return new Point((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(); } }