| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- using System;
- using System.Linq;
- namespace MonoGame.Extended
- {
- public struct Thickness : IEquatable<Thickness>
- {
- public Thickness(int all)
- : this(all, all, all, all)
- {
- }
- public Thickness(int leftRight, int topBottom)
- : this(leftRight, topBottom, leftRight, topBottom)
- {
- }
- public Thickness(int left, int top, int right, int bottom)
- : this()
- {
- Left = left;
- Top = top;
- Right = right;
- Bottom = bottom;
- }
- public int Left { get; set; }
- public int Top { get; set; }
- public int Right { get; set; }
- public int Bottom { get; set; }
- public int Width => Left + Right;
- public int Height => Top + Bottom;
- public Size Size => new Size(Width, Height);
- public static implicit operator Thickness(int value)
- {
- return new Thickness(value);
- }
- public override bool Equals(object obj)
- {
- if (obj is Thickness)
- {
- var other = (Thickness)obj;
- return Equals(other);
- }
- return base.Equals(obj);
- }
- public override int GetHashCode()
- {
- unchecked
- {
- var hashCode = Left;
- hashCode = (hashCode * 397) ^ Top;
- hashCode = (hashCode * 397) ^ Right;
- hashCode = (hashCode * 397) ^ Bottom;
- return hashCode;
- }
- }
- public bool Equals(Thickness other)
- {
- return Left == other.Left && Right == other.Right && Top == other.Top && Bottom == other.Bottom;
- }
- public static Thickness FromValues(int[] values)
- {
- switch (values.Length)
- {
- case 1:
- return new Thickness(values[0]);
- case 2:
- return new Thickness(values[0], values[1]);
- case 4:
- return new Thickness(values[0], values[1], values[2], values[3]);
- default:
- throw new FormatException("Invalid thickness");
- }
- }
- public static Thickness Parse(string value)
- {
- var values = value
- .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
- .Select(int.Parse)
- .ToArray();
- return FromValues(values);
- }
- public void Deconstruct(out int top, out int right, out int bottom, out int left) =>
- (top, right, bottom, left) = (Top, Right, Bottom, Left);
- public override string ToString()
- {
- if (Left == Right && Top == Bottom)
- return Left == Top ? $"{Left}" : $"{Left} {Top}";
- return $"{Left}, {Right}, {Top}, {Bottom}";
- }
- }
- }
|