// ReSharper disable MemberCanBePrivate.Global // ReSharper disable AutoPropertyCanBeMadeGetOnly.Global // ReSharper disable UnusedMember.Global using System; using System.Collections.Generic; using System.Linq; using RGB.NET.Core; namespace RGB.NET.Groups { /// /// Represents a containing which physically lay inside a . /// public class RectangleLedGroup : AbstractLedGroup { #region Properties & Fields private IList _ledCache; private Rectangle _rectangle; /// /// Gets or sets the the should be taken from. /// public Rectangle Rectangle { get => _rectangle; set { Rectangle oldValue = _rectangle; if (SetProperty(ref _rectangle, value)) { if (oldValue != null) oldValue.Changed -= RectangleChanged; if (_rectangle != null) _rectangle.Changed += RectangleChanged; InvalidateCache(); } } } private double _minOverlayPercentage; /// /// Gets or sets the minimal percentage overlay a must have with the to be taken into the . /// public double MinOverlayPercentage { get => _minOverlayPercentage; set { if (SetProperty(ref _minOverlayPercentage, value)) InvalidateCache(); } } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// They ID of the first to calculate the of this from. /// They ID of the second to calculate the of this from. /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) public RectangleLedGroup(ILedId fromLed, ILedId toLed, double minOverlayPercentage = 0.5, bool autoAttach = true) : this(fromLed.Device[fromLed], toLed.Device[toLed], minOverlayPercentage, autoAttach) { } /// /// Initializes a new instance of the class. /// /// They first to calculate the of this from. /// They second to calculate the of this from. /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) public RectangleLedGroup(Led fromLed, Led toLed, double minOverlayPercentage = 0.5, bool autoAttach = true) : this(new Rectangle(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage, autoAttach) { } /// /// Initializes a new instance of the class. /// /// They first point to calculate the of this from. /// They second point to calculate the of this from. /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) public RectangleLedGroup(Point fromPoint, Point toPoint, double minOverlayPercentage = 0.5, bool autoAttach = true) : this(new Rectangle(fromPoint, toPoint), minOverlayPercentage, autoAttach) { } /// /// Initializes a new instance of the class. /// /// The of this . /// (optional) The minimal percentage overlay a must have with the to be taken into the . (default: 0.5) /// (optional) Specifies whether this should be automatically attached or not. (default: true) public RectangleLedGroup(Rectangle rectangle, double minOverlayPercentage = 0.5, bool autoAttach = true) : base(autoAttach) { this.Rectangle = rectangle; this.MinOverlayPercentage = minOverlayPercentage; } #endregion #region Methods /// public override void OnAttach() => RGBSurface.Instance.SurfaceLayoutChanged += RGBSurfaceOnSurfaceLayoutChanged; /// public override void OnDetach() => RGBSurface.Instance.SurfaceLayoutChanged -= RGBSurfaceOnSurfaceLayoutChanged; private void RGBSurfaceOnSurfaceLayoutChanged(SurfaceLayoutChangedEventArgs args) => InvalidateCache(); private void RectangleChanged(object sender, EventArgs eventArgs) => InvalidateCache(); /// /// Gets a list containing all of this . /// /// The list containing all of this . public override IEnumerable GetLeds() => _ledCache ?? (_ledCache = RGBSurface.Instance.Leds.Where(x => x.LedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList()); private void InvalidateCache() => _ledCache = null; #endregion } }