2019-12-14 14:00:12 +01:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
|
|
|
namespace MLEM.Misc {
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Represents a generic padding.
|
|
|
|
/// A padding is an object of data that stores an offset from each side of a rectangle or square.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public struct Padding {
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The amount of padding on the left side
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Left;
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The amount of padding on the right side
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Right;
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The amount of padding on the top side
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Top;
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The amount of padding on the bottom side
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Bottom;
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The total width of this padding, a sum of the left and right padding.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Width => this.Left + this.Right;
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The total height of this padding, a sum of the top and bottom padding.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public float Height => this.Top + this.Bottom;
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Create a new padding with the specified borders.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The amount of padding on the left side</param>
|
|
|
|
/// <param name="right">The amount of padding on the right side</param>
|
|
|
|
/// <param name="top">The amount of padding on the top side</param>
|
|
|
|
/// <param name="bottom">The amount of padding on the bottom side</param>
|
2019-12-14 14:00:12 +01:00
|
|
|
public Padding(float left, float right, float top, float bottom) {
|
|
|
|
this.Left = left;
|
|
|
|
this.Right = right;
|
|
|
|
this.Top = top;
|
|
|
|
this.Bottom = bottom;
|
|
|
|
}
|
|
|
|
|
2020-10-25 01:33:05 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new padding with the specified x and y values, applying them to both edges.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="x">The x padding, which will turn into the left and right padding</param>
|
|
|
|
/// <param name="y">The y padding, which till turn into the top and bottom padding</param>
|
|
|
|
public Padding(float x, float y) :
|
|
|
|
this(x, x, y, y) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Creates a new padding from an existing padding, modifying it by growing or shrinking it.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="padding">The padding whose initial values to use</param>
|
|
|
|
/// <param name="growth">The amount to grow each border by. Negative values will shrink the padding.</param>
|
|
|
|
public Padding(Padding padding, float growth) :
|
|
|
|
this(padding.Left + growth, padding.Right + growth, padding.Top + growth, padding.Bottom + growth) {
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// 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.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="vec">The vector to convert</param>
|
|
|
|
/// <returns>A padding based on the vector's values</returns>
|
2019-12-14 14:00:12 +01:00
|
|
|
public static implicit operator Padding(Vector2 vec) {
|
|
|
|
return new Padding(vec.X, vec.X, vec.Y, vec.Y);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Scales a padding by a scalar.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
public static Padding operator *(Padding p, float scale) {
|
|
|
|
return new Padding(p.Left * scale, p.Right * scale, p.Top * scale, p.Bottom * scale);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Adds two paddings together in a memberwise fashion.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Subtracts two paddings in a memberwise fashion.
|
|
|
|
/// </summary>
|
2019-12-14 14:00:12 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <inheritdoc cref="Equals(Padding)"/>
|
2019-12-14 14:00:12 +01:00
|
|
|
public static bool operator ==(Padding left, Padding right) {
|
|
|
|
return left.Equals(right);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <inheritdoc cref="Equals(Padding)"/>
|
2019-12-14 14:00:12 +01:00
|
|
|
public static bool operator !=(Padding left, Padding right) {
|
|
|
|
return !(left == right);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <inheritdoc cref="Equals(object)"/>
|
2019-12-14 14:00:12 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <inheritdoc />
|
2019-12-14 14:00:12 +01:00
|
|
|
public override bool Equals(object obj) {
|
|
|
|
return obj is Padding other && this.Equals(other);
|
|
|
|
}
|
|
|
|
|
2020-05-21 17:21:34 +02:00
|
|
|
/// <inheritdoc />
|
2019-12-14 14:00:12 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|