// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System;
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
where TDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
///
public abstract TDeviceInfo DeviceInfo { get; }
///
IRGBDeviceInfo IRGBDevice.DeviceInfo => DeviceInfo;
///
public Size Size => new Size(InternalSize?.Width ?? 0, InternalSize?.Height ?? 0);
private Size _internalSize;
///
/// Gets the of the whole .
///
protected Size InternalSize
{
get => _internalSize;
set
{
// ReSharper disable once ExplicitCallerInfoArgument
if (SetProperty(ref _internalSize, value))
OnPropertyChanged(nameof(Size));
}
}
private Point _location = new Point();
///
public Point Location
{
get => _location;
set => SetProperty(ref _location, value ?? new Point());
}
///
/// Gets a dictionary containing all of the .
///
protected Dictionary LedMapping { get; } = new Dictionary();
///
/// Gets a dictionary containing all associated with this .
///
protected Dictionary SpecialDeviceParts { get; } = new Dictionary();
#region Indexer
///
Led IRGBDevice.this[ILedId ledId] => LedMapping.TryGetValue(ledId, out Led 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)).ToList();
foreach (Led ledToUpdate in ledsToUpdate)
ledToUpdate.Update();
UpdateLeds(ledsToUpdate);
}
///
public virtual void Dispose()
{
SpecialDeviceParts.Clear();
LedMapping.Clear();
}
///
/// Performs device specific updates.
///
protected virtual void DeviceUpdate()
{ }
///
/// Sends all the updated to the device.
///
protected abstract void UpdateLeds(IEnumerable ledsToUpdate);
///
/// 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;
}
///
public void AddSpecialDevicePart(T specialDevicePart)
where T : class, IRGBDeviceSpecialPart
=> SpecialDeviceParts[typeof(T)] = specialDevicePart;
///
public T GetSpecialDevicePart()
where T : class, IRGBDeviceSpecialPart
=> SpecialDeviceParts.TryGetValue(typeof(T), out IRGBDeviceSpecialPart devicePart) ? (T)devicePart : default(T);
#region Enumerator
///
///
/// Returns an enumerator that iterates over all of the .
///
/// An enumerator for all of the .
public IEnumerator GetEnumerator() => LedMapping.Values.GetEnumerator();
///
///
/// Returns an enumerator that iterates over all of the .
///
/// An enumerator for all of the .
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
#endregion
}
}