2021-12-21 11:39:29 +01:00
using System ;
2021-02-28 16:42:51 +01:00
using System.Runtime.Serialization ;
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>
2021-02-28 16:42:51 +01:00
[DataContract]
2021-12-21 11:39:29 +01:00
public struct Padding : IEquatable < Padding > {
2019-12-14 14:00:12 +01:00
2021-11-18 10:58:50 +01:00
/// <summary>
/// The empty padding, with all borders set to 0
/// </summary>
public static Padding Empty = > default ;
2020-05-21 17:21:34 +02:00
/// <summary>
/// The amount of padding on the left side
/// </summary>
2021-02-28 16:42:51 +01:00
[DataMember]
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>
2021-02-28 16:42:51 +01:00
[DataMember]
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>
2021-02-28 16:42:51 +01:00
[DataMember]
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>
2021-02-28 16:42:51 +01:00
[DataMember]
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 ;
}
2021-10-30 15:33:38 +02:00
/// <summary>
/// Creates a new padding with the specified value, which will be applied to each edge.
/// </summary>
/// <param name="value">The padding to apply to each edge</param>
2021-12-28 14:56:11 +01:00
public Padding ( float value ) : this ( value , value ) { }
2021-10-30 15:33:38 +02:00
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>
2021-12-28 14:56:11 +01:00
public Padding ( float x , float y ) : this ( x , x , y , y ) { }
2020-10-25 01:33:05 +02:00
/// <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>
2021-12-28 14:56:11 +01:00
public Padding ( Padding padding , float growth ) : this ( padding . Left + growth , padding . Right + growth , padding . Top + growth , padding . Bottom + growth ) { }
2020-10-25 01:33:05 +02:00
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 ) ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Indicates whether this instance and a specified object are equal.</summary>
/// <param name="obj">The object to compare with the current instance.</param>
/// <returns><see langword="true" /> if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, <see langword="false" />.</returns>
2019-12-14 14:00:12 +01:00
public override bool Equals ( object obj ) {
return obj is Padding other & & this . Equals ( other ) ;
}
2021-11-22 19:25:18 +01:00
/// <summary>Returns the hash code for this instance.</summary>
/// <returns>A 32-bit signed integer that is the hash code for this instance.</returns>
2019-12-14 14:00:12 +01:00
public override int GetHashCode ( ) {
var hashCode = this . Left . GetHashCode ( ) ;
2022-06-15 11:38:11 +02:00
hashCode = hashCode * 397 ^ this . Right . GetHashCode ( ) ;
hashCode = hashCode * 397 ^ this . Top . GetHashCode ( ) ;
hashCode = hashCode * 397 ^ this . Bottom . GetHashCode ( ) ;
2019-12-14 14:00:12 +01:00
return hashCode ;
}
}
2022-06-17 18:23:47 +02:00
}