// ReSharper disable MemberCanBePrivate.Global // ReSharper disable UnusedMember.Global using System.Collections; using System.Collections.Generic; using System.Linq; namespace RGB.NET.Core { /// /// Represents a generic RGB-device /// public abstract class AbstractRGBDevice : AbstractBindable, IRGBDevice { #region Properties & Fields /// public abstract IRGBDeviceInfo DeviceInfo { get; } /// public Size Size => new Size(InternalSize?.Width ?? 0, InternalSize?.Height ?? 0); /// /// Gets the of the whole . /// protected abstract Size InternalSize { get; set; } private Point _location = new Point(); /// public Point Location { get { return _location; } set { SetProperty(ref _location, value ?? new Point()); } } /// /// Gets a dictionary containing all of the . /// protected Dictionary LedMapping { get; } = new Dictionary(); #region Indexer /// Led IRGBDevice.this[ILedId ledId] { get { Led led; return LedMapping.TryGetValue(ledId, out led) ? led : null; } } /// Led IRGBDevice.this[Point location] => LedMapping.Values.FirstOrDefault(x => x.LedRectangle.Contains(location)); /// IEnumerable IRGBDevice.this[Rectangle referenceRect, double minOverlayPercentage] => LedMapping.Values.Where(x => referenceRect.CalculateIntersectPercentage(x.LedRectangle) >= minOverlayPercentage); #endregion #endregion #region Methods /// public virtual void Update(bool flushLeds = false) { // Device-specific updates DeviceUpdate(); // Send LEDs to SDK IEnumerable ledsToUpdate = flushLeds ? LedMapping.Values : LedMapping.Values.Where(x => x.IsDirty); foreach (Led ledToUpdate in ledsToUpdate) ledToUpdate.Update(); } /// /// Performs device specific updates. /// protected virtual void DeviceUpdate() { } /// /// Initializes the with the specified id. /// /// The to initialize. /// The representing the position of the to initialize. /// protected virtual Led InitializeLed(ILedId ledId, Rectangle ledRectangle) { if (LedMapping.ContainsKey(ledId)) return null; Led led = new Led(this, ledId, ledRectangle); LedMapping.Add(ledId, led); return led; } #region Enumerator /// /// Returns an enumerator that iterates over all of the . /// /// An enumerator for all of the . public IEnumerator GetEnumerator() { return LedMapping.Values.GetEnumerator(); } /// /// Returns an enumerator that iterates over all of the . /// /// An enumerator for all of the . IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion #endregion } }