using Newtonsoft.Json; using RhythmBase.Converters; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; namespace RhythmBase.Components { /// /// A size whose horizontal and vertical coordinates are nullable /// [JsonConverter(typeof(RDPointsConverter))] [DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")] public struct RDSize(float? width, float? height) : IRDVortex { /// /// Initializes a new instance of the struct with the specified point. /// /// The point to initialize the size with. public RDSize(RDPoint pt) : this(pt.X, pt.Y) { } /// /// Gets a value indicating whether this size is empty (both width and height are null). /// public readonly bool IsEmpty => Width == null && Height == null; /// /// Gets or sets the width of the size. /// public float? Width { get; set; } = width; /// /// Gets or sets the height of the size. /// public float? Height { get; set; } = height; /// /// Gets the area of the size. /// public readonly float? Area => Width * Height; /// /// Adds two sizes together. /// /// The first size. /// The second size. /// The sum of the two sizes. public static RDSize Add(RDSize sz1, RDSize sz2) => new(sz1.Width + sz2.Width, sz1.Height + sz2.Height); /// /// Subtracts one size from another. /// /// The first size. /// The second size. /// The difference between the two sizes. public static RDSize Subtract(RDSize sz1, RDSize sz2) => new(sz1.Width - sz2.Width, sz1.Height - sz2.Height); /// public override readonly int GetHashCode() => HashCode.Combine(Width, Height); /// public override readonly string ToString() => $"[{Width},{Height}]"; /// public readonly bool Equals(RDSize other) => Width == other.Width && Height == other.Height; /// /// Converts this size to an . /// /// An that represents this size. public readonly RDSizeI ToSize() => new((int?)Width, (int?)Height); /// /// Converts this size to an . /// /// An that represents this size. public readonly RDPoint ToPointF() => new(Width, Height); /// public override readonly bool Equals([NotNullWhen(true)] object? obj) => obj is RDSize e && Equals(e); /// public static RDSize operator +(RDSize sz1, RDSize sz2) => Add(sz1, sz2); /// public static RDSize operator -(RDSize sz1, RDSize sz2) => Subtract(sz1, sz2); /// public static RDSize operator *(float left, RDSize right) => new(left * right.Width, left * right.Height); /// public static RDSize operator *(RDSize left, float? right) => new(left.Width * right, left.Height * right); /// public static RDSize operator /(RDSize left, float? right) => new(left.Width / right, left.Height / right); /// public static bool operator ==(RDSize sz1, RDSize sz2) => sz1.Equals(sz2); /// public static bool operator !=(RDSize sz1, RDSize sz2) => !sz1.Equals(sz2); /// /// Performs an implicit conversion from to . /// /// The size to convert. /// An that represents the converted size. public static implicit operator RDSizeE(RDSize size) => new(size.Width, size.Height); /// /// Performs an explicit conversion from to . /// /// The size to convert. /// An that represents the converted size. public static explicit operator RDPoint(RDSize size) => new(size.Width, size.Height); private readonly string GetDebuggerDisplay() => ToString(); } }