// Copyright (c) 2015 - 2023 Doozy Entertainment. All Rights Reserved. // This code can only be used under the standard Unity Asset Store End User License Agreement // A Copy of the EULA APPENDIX 1 is available at http://unity3d.com/company/legal/as_terms using System; // ReSharper disable ConvertToAutoProperty namespace Doozy.Runtime.UIElements { /// Representation of a rectangle edge left, top, right and bottom values public struct EdgeValues : IEquatable { /// Left edge value public float Left; /// Top edge value public float Top; /// Right edge value public float Right; /// Bottom edge value public float Bottom; /// Creates a new edge values set with the given left, top, right, bottom values /// Left edge value /// Top edge value /// Right edge value /// Bottom edge value public EdgeValues(float left, float top, float right, float bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } /// Set left, top, right and bottom values of an existing edge values set /// New 'left' value /// New 'top' value /// New 'right' value /// New 'bottom' value public EdgeValues Set(float newLeft, float newTop, float newRight, float newBottom) { Left = newLeft; Top = newTop; Right = newRight; Bottom = newBottom; return this; } /// Set the 'left' value of an existing edge values set /// New left value public EdgeValues SetLeft(float newLeft) { Left = newLeft; return this; } /// Set the 'top' value of an existing edge values set /// New top value public EdgeValues SetTop(float newTop) { Top = newTop; return this; } /// Set the 'right' value of an existing edge values set /// New right value public EdgeValues SetRight(float newRight) { Right = newRight; return this; } /// Set the 'bottom' value of an existing edge values set /// New 'bottom' value public EdgeValues SetBottom(float newBottom) { Bottom = newBottom; return this; } private static readonly EdgeValues ZeroValues = new EdgeValues(0f, 0f, 0f, 0f); private static readonly EdgeValues OneValues = new EdgeValues(1f, 1f, 1f, 1f); private static readonly EdgeValues OneLeftRightValues = new EdgeValues(1f, 0f, 1f, 0f); private static readonly EdgeValues OneTopBottomValues = new EdgeValues(0f, 1f, 0f, 1f); private static readonly EdgeValues TwoValues = new EdgeValues(2f, 2f, 2f, 2f); private static readonly EdgeValues TwoLeftRightValues = new EdgeValues(2f, 0f, 2f, 0f); private static readonly EdgeValues TwoTopBottomValues = new EdgeValues(0f, 2f, 0f, 2f); private static readonly EdgeValues ThreeValues = new EdgeValues(3f, 3f, 3f, 3f); private static readonly EdgeValues ThreeLeftRightValues = new EdgeValues(3f, 0f, 3f, 0f); private static readonly EdgeValues ThreeTopBottomValues = new EdgeValues(0f, 3f, 0f, 3f); private static readonly EdgeValues FourValues = new EdgeValues(4f, 4f, 4f, 4f); private static readonly EdgeValues FourLeftRightValues = new EdgeValues(4f, 0f, 4f, 0f); private static readonly EdgeValues FourTopBottomValues = new EdgeValues(0f, 4f, 0f, 4f); /// Shorthand for writing EdgeValues(0f, 0f, 0f, 0f) public static EdgeValues zero => ZeroValues; /// Shorthand for writing EdgeValues(1f, 1f, 1f, 1f) public static EdgeValues one => OneValues; /// Shorthand for writing EdgeValues(1f, 0f, 1f, 0f) public static EdgeValues oneLeftRight => OneLeftRightValues; /// Shorthand for writing EdgeValues(0f, 1f, 0f, 1f) public static EdgeValues oneTopBottom => OneTopBottomValues; /// Shorthand for writing EdgeValues(2f, 2f, 2f, 2f) public static EdgeValues two => TwoValues; /// Shorthand for writing EdgeValues(2f, 0f, 2f, 0f) public static EdgeValues twoLeftRight => TwoLeftRightValues; /// Shorthand for writing EdgeValues(0f, 2f, 0f, 2f) public static EdgeValues twoTopBottom => TwoTopBottomValues; /// Shorthand for writing EdgeValues(3f, 3f, 3f, 3f) public static EdgeValues three => ThreeValues; /// Shorthand for writing EdgeValues(3f, 0f, 3f, 0f) public static EdgeValues threeLeftRight => ThreeLeftRightValues; /// Shorthand for writing EdgeValues(0f, 3f, 0f, 3f) public static EdgeValues threeTopBottom => ThreeTopBottomValues; /// Shorthand for writing EdgeValues(4f, 4f, 4f, 4f) public static EdgeValues four => FourValues; /// Shorthand for writing EdgeValues(4f, 0f, 4f, 0f) public static EdgeValues fourLeftRight => FourLeftRightValues; /// Shorthand for writing EdgeValues(0f, 4f, 0f, 4f) public static EdgeValues fourTopBottom => FourTopBottomValues; public bool Equals(EdgeValues other) => Left == (double) other.Left && Top == (double) other.Top && Right == (double) other.Right && Bottom == (double) other.Bottom; public override bool Equals(object obj) => obj is EdgeValues other && Equals(other); public override int GetHashCode() { unchecked { int hashCode = Left.GetHashCode(); hashCode = (hashCode * 397) ^ Top.GetHashCode(); hashCode = (hashCode * 397) ^ Right.GetHashCode(); hashCode = (hashCode * 397) ^ Bottom.GetHashCode(); return hashCode; } } public static EdgeValues operator +(EdgeValues a, EdgeValues b) => new EdgeValues(a.Left + b.Left, a.Top + b.Top, a.Right + b.Right, a.Bottom + b.Bottom); public static EdgeValues operator -(EdgeValues a, EdgeValues b) => new EdgeValues(a.Left - b.Left, a.Top - b.Top, a.Right - b.Right, a.Bottom - b.Bottom); public static EdgeValues operator -(EdgeValues a) => new EdgeValues(-a.Left, -a.Top, -a.Right, -a.Bottom); public static EdgeValues operator *(EdgeValues a, float value) => new EdgeValues(a.Left * value, a.Top * value, a.Right * value, a.Bottom * value); public static EdgeValues operator *(float value, EdgeValues a) => new EdgeValues(a.Left * value, a.Top * value, a.Right * value, a.Bottom * value); public static EdgeValues operator *(EdgeValues a, int value) => new EdgeValues(a.Left * value, a.Top * value, a.Right * value, a.Bottom * value); public static EdgeValues operator *(int value, EdgeValues a) => new EdgeValues(a.Left * value, a.Top * value, a.Right * value, a.Bottom * value); public static EdgeValues operator /(EdgeValues a, float value) => new EdgeValues(a.Left / value, a.Top / value, a.Right / value, a.Bottom / value); public static EdgeValues operator /(EdgeValues a, int value) => new EdgeValues(a.Left / value, a.Top / value, a.Right / value, a.Bottom / value); public static bool operator ==(EdgeValues a, EdgeValues b) { float left = a.Left - b.Left; float top = a.Top - b.Top; float right = a.Right - b.Right; float bottom = a.Bottom - b.Bottom; return (double) left * (double) left + (double) top * (double) top + (double) right * (double) right + (double) bottom * (double) bottom < 9.99999943962493E-11; } public static bool operator !=(EdgeValues a, EdgeValues b) => !(a == b); /// Returns a nicely formatted string for this values set public override string ToString() => $"({Left}, {Top}, {Right}, {Bottom})"; /// Returns a nicely formatted string for this values set /// Show values names public string ToString(bool verbose) => verbose ? $"(left: {Left}, top: {Top}, right: {Right}, bottom: {Bottom})" : $"({Left}, {Top}, {Right}, {Bottom})"; } }