// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex { using System; public struct Thickness { /// /// The thickness on the left. /// private double left; /// /// The thickness on the top. /// private double top; /// /// The thickness on the right. /// private double right; /// /// The thickness on the bottom. /// private double bottom; /// /// Initializes a new instance of the structure. /// /// The length that should be applied to all sides. public Thickness(double uniformLength) { Contract.Requires(uniformLength >= 0); this.left = this.top = this.right = this.bottom = uniformLength; } /// /// Initializes a new instance of the structure. /// /// The thickness on the left and right. /// The thickness on the top and bottom. public Thickness(double horizontal, double vertical) { Contract.Requires(horizontal >= 0); Contract.Requires(vertical >= 0); this.left = this.right = horizontal; this.top = this.bottom = vertical; } /// /// Initializes a new instance of the structure. /// /// The thickness on the left. /// The thickness on the top. /// The thickness on the right. /// The thickness on the bottom. public Thickness(double left, double top, double right, double bottom) { Contract.Requires(left >= 0); Contract.Requires(top >= 0); Contract.Requires(right >= 0); Contract.Requires(bottom >= 0); this.left = left; this.top = top; this.right = right; this.bottom = bottom; } /// /// Gets the thickness on the left. /// public double Left { get { return this.left; } } /// /// Gets the thickness on the top. /// public double Top { get { return this.top; } } /// /// Gets the thickness on the right. /// public double Right { get { return this.right; } } /// /// Gets the thickness on the bottom. /// public double Bottom { get { return this.bottom; } } /// /// Gets a value indicating whether all sides are set to 0. /// public bool IsEmpty { get { return this.Left == 0 && this.Top == 0 && this.Right == 0 && this.Bottom == 0; } } /// /// Compares two Thicknesses. /// /// The first thickness. /// The second thickness. /// The equality. public static bool operator ==(Thickness a, Thickness b) { return a.Equals(b); } /// /// Compares two Thicknesses. /// /// The first thickness. /// The second thickness. /// The unequality. public static bool operator !=(Thickness a, Thickness b) { return !a.Equals(b); } /// /// Adds two Thicknesses. /// /// The first thickness. /// The second thickness. /// The equality. public static Thickness operator +(Thickness a, Thickness b) { return new Thickness( a.Left + b.Left, a.Top + b.Top, a.Right + b.Right, a.Bottom + b.Bottom); } public override bool Equals(object obj) { if (obj is Thickness) { Thickness other = (Thickness)obj; return this.Left == other.Left && this.Top == other.Top && this.Right == other.Right && this.Bottom == other.Bottom; } return false; } public override int GetHashCode() { unchecked { int hash = 17; hash = (hash * 23) + this.Left.GetHashCode(); hash = (hash * 23) + this.Top.GetHashCode(); hash = (hash * 23) + this.Right.GetHashCode(); hash = (hash * 23) + this.Bottom.GetHashCode(); return hash; } } public override string ToString() { return string.Format("{0},{1},{2},{3}", this.left, this.top, this.right, this.bottom); } } }