using System; using System.Runtime.Serialization; using Microsoft.Xna.Framework; namespace MLEM.Misc { /// /// Represents a generic padding. /// A padding is an object of data that stores an offset from each side of a rectangle or square. /// [DataContract] public struct Padding : IEquatable { /// /// The empty padding, with all borders set to 0 /// public static Padding Empty => default; /// /// The amount of padding on the left side /// [DataMember] public float Left; /// /// The amount of padding on the right side /// [DataMember] public float Right; /// /// The amount of padding on the top side /// [DataMember] public float Top; /// /// The amount of padding on the bottom side /// [DataMember] public float Bottom; /// /// The total width of this padding, a sum of the left and right padding. /// public float Width => this.Left + this.Right; /// /// The total height of this padding, a sum of the top and bottom padding. /// public float Height => this.Top + this.Bottom; /// /// Create a new padding with the specified borders. /// /// The amount of padding on the left side /// The amount of padding on the right side /// The amount of padding on the top side /// The amount of padding on the bottom side public Padding(float left, float right, float top, float bottom) { this.Left = left; this.Right = right; this.Top = top; this.Bottom = bottom; } /// /// Creates a new padding with the specified value, which will be applied to each edge. /// /// The padding to apply to each edge public Padding(float value) : this(value, value) {} /// /// Creates a new padding with the specified x and y values, applying them to both edges. /// /// The x padding, which will turn into the left and right padding /// The y padding, which till turn into the top and bottom padding public Padding(float x, float y) : this(x, x, y, y) {} /// /// Creates a new padding from an existing padding, modifying it by growing or shrinking it. /// /// The padding whose initial values to use /// The amount to grow each border by. Negative values will shrink the padding. public Padding(Padding padding, float growth) : this(padding.Left + growth, padding.Right + growth, padding.Top + growth, padding.Bottom + growth) {} /// /// Implicitly creates a padding from the given two-dimensional vector. /// The left and right padding will both be the vector's x value, and the top and bottom padding will both be the vector's y value. /// /// The vector to convert /// A padding based on the vector's values public static implicit operator Padding(Vector2 vec) { return new Padding(vec.X, vec.X, vec.Y, vec.Y); } /// /// Scales a padding by a scalar. /// public static Padding operator *(Padding p, float scale) { return new Padding(p.Left * scale, p.Right * scale, p.Top * scale, p.Bottom * scale); } /// /// Adds two paddings together in a memberwise fashion. /// public static Padding operator +(Padding left, Padding right) { return new Padding(left.Left + right.Left, left.Right + right.Right, left.Top + right.Top, left.Bottom + right.Bottom); } /// /// Subtracts two paddings in a memberwise fashion. /// public static Padding operator -(Padding left, Padding right) { return new Padding(left.Left - right.Left, left.Right - right.Right, left.Top - right.Top, left.Bottom - right.Bottom); } /// public static bool operator ==(Padding left, Padding right) { return left.Equals(right); } /// public static bool operator !=(Padding left, Padding right) { return !(left == right); } /// public bool Equals(Padding other) { return this.Left.Equals(other.Left) && this.Right.Equals(other.Right) && this.Top.Equals(other.Top) && this.Bottom.Equals(other.Bottom); } /// Indicates whether this instance and a specified object are equal. /// The object to compare with the current instance. /// if and this instance are the same type and represent the same value; otherwise, . public override bool Equals(object obj) { return obj is Padding other && this.Equals(other); } /// Returns the hash code for this instance. /// A 32-bit signed integer that is the hash code for this instance. public override int GetHashCode() { var hashCode = this.Left.GetHashCode(); hashCode = hashCode * 397 ^ this.Right.GetHashCode(); hashCode = hashCode * 397 ^ this.Top.GetHashCode(); hashCode = hashCode * 397 ^ this.Bottom.GetHashCode(); return hashCode; } } }