using System;
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Devices.Razer.Native;
namespace RGB.NET.Devices.Razer
{
///
///
///
/// Represents a generic razer-device. (keyboard, mouse, headset, mousepad).
///
public abstract class RazerRGBDevice : AbstractRGBDevice, IRazerRGBDevice
where TDeviceInfo : RazerRGBDeviceInfo
{
#region Properties & Fields
private Guid? _lastEffect;
///
///
/// Gets information about the .
///
public override TDeviceInfo DeviceInfo { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The generic information provided by razer for the device.
protected RazerRGBDevice(TDeviceInfo info)
{
this.DeviceInfo = info;
RequiresFlush = true;
}
#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);
}
}
///
/// 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) return;
IntPtr effectParams = CreateEffectParams(leds);
Guid effectId = Guid.NewGuid();
_RazerSDK.CreateEffect(DeviceInfo.DeviceId, _Defines.EFFECT_ID, effectParams, ref effectId);
_RazerSDK.SetEffect(effectId);
if (_lastEffect.HasValue)
_RazerSDK.DeleteEffect(_lastEffect.Value);
_lastEffect = effectId;
}
///
/// Creates the device-specific effect parameters for the led-update.
///
/// The leds to be updated.
/// An pointing to the effect parameter struct.
protected abstract IntPtr CreateEffectParams(IEnumerable leds);
///
/// Resets the device.
///
public void Reset()
{
if (_lastEffect.HasValue)
{
_RazerSDK.DeleteEffect(_lastEffect.Value);
_lastEffect = null;
}
}
#endregion
}
}