using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
namespace RGB.NET.Devices.Corsair
{
///
///
///
/// Represents a generic CUE-device. (keyboard, mouse, headset, mousepad).
///
public abstract class CorsairRGBDevice : AbstractRGBDevice, ICorsairRGBDevice
where TDeviceInfo : CorsairRGBDeviceInfo
{
#region Properties & Fields
///
///
/// Gets information about the .
///
public override TDeviceInfo DeviceInfo { get; }
///
/// Gets a dictionary containing all of the .
///
// ReSharper disable once MemberCanBePrivate.Global
protected Dictionary InternalLedMapping { get; } = new();
///
/// Gets or sets the update queue performing updates for this device.
///
// ReSharper disable once MemberCanBePrivate.Global
protected CorsairDeviceUpdateQueue? DeviceUpdateQueue { get; set; }
#endregion
#region Indexer
///
/// Gets the with the specified .
///
/// The of the to get.
/// The with the specified or null if no is found.
// ReSharper disable once MemberCanBePrivate.Global
public Led? this[CorsairLedId ledId] => InternalLedMapping.TryGetValue(ledId, out Led? led) ? led : null;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The generic information provided by CUE for the device.
protected CorsairRGBDevice(TDeviceInfo info)
{
this.DeviceInfo = info;
}
#endregion
#region Methods
///
/// Initializes the device.
///
public void Initialize(CorsairDeviceUpdateQueue deviceUpdateQueue)
{
DeviceUpdateQueue = deviceUpdateQueue;
InitializeLayout();
foreach (Led led in LedMapping.Values)
{
if (led.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))
InternalLedMapping.Add(ledId, led);
}
}
///
/// Initializes the and of the device.
///
protected abstract void InitializeLayout();
///
protected override void UpdateLeds(IEnumerable ledsToUpdate)
=> DeviceUpdateQueue?.SetData(ledsToUpdate.Where(x => (x.Color.A > 0) && (x.CustomData is CorsairLedId ledId && (ledId != CorsairLedId.Invalid))));
///
public override void Dispose()
{
try { DeviceUpdateQueue?.Dispose(); }
catch { /* at least we tried */ }
base.Dispose();
}
#endregion
}
}