// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
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 = value;
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
{
_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();
}
///
/// Gets a list containing all of this .
///
/// The list containing all of this .
public override IEnumerable GetLeds()
{
return _ledCache ?? (_ledCache = RGBSurface.Instance.Leds.Where(x => x.LedRectangle.CalculateIntersectPercentage(Rectangle) >= MinOverlayPercentage).ToList());
}
private void InvalidateCache()
{
_ledCache = null;
}
#endregion
}
}