using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using RGB.NET.Core;
namespace RGB.NET.Devices.Asus
{
///
///
///
/// Represents a generic Asus-device. (keyboard, mouse, headset, mousepad).
///
public abstract class AsusRGBDevice : AbstractRGBDevice, IAsusRGBDevice
where TDeviceInfo : AsusRGBDeviceInfo
{
#region Properties & Fields
///
/// Gets or sets the internal color-data cache.
///
protected byte[] ColorData { get; private set; }
///
///
/// Gets information about the .
///
public override TDeviceInfo DeviceInfo { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The generic information provided by Asus for the device.
protected AsusRGBDevice(TDeviceInfo info)
{
this.DeviceInfo = info;
}
#endregion
#region Methods
///
/// Initializes the device.
///
public void Initialize()
{
InitializeLayout();
if (Size == Size.Invalid)
{
Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle));
Size = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y);
}
ColorData = new byte[LedMapping.Count * 3];
}
///
/// Initializes the and of the device.
///
protected abstract void InitializeLayout();
///
protected override void UpdateLeds(IEnumerable ledsToUpdate)
{
List leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList();
if (leds.Count > 0)
{
foreach (Led led in leds)
{
int index = ((int)led.CustomData) * 3;
ColorData[index] = led.Color.R;
ColorData[index + 1] = led.Color.B;
ColorData[index + 2] = led.Color.G;
}
ApplyColorData();
}
}
///
/// Sends the color-data-cache to the device.
///
protected abstract void ApplyColorData();
///
///
public override void Dispose()
{
if ((DeviceInfo is AsusRGBDeviceInfo deviceInfo) && (deviceInfo.Handle != IntPtr.Zero))
Marshal.FreeHGlobal(deviceInfo.Handle);
ColorData = null;
base.Dispose();
}
#endregion
}
}