using System; using System.Collections.Generic; using System.IO; using System.Linq; using RGB.NET.Core; using RGB.NET.Core.Layout; 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; } #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(); /// /// Applies the given layout. /// /// The file containing the layout. /// The name of the layout used to get the images of the leds. /// The path images for this device are collected in. protected void ApplyLayoutFromFile(string layoutPath, string imageLayout, string imageBasePath) { DeviceLayout layout = DeviceLayout.Load(layoutPath); if (layout != null) { LedImageLayout ledImageLayout = layout.LedImageLayouts.FirstOrDefault(x => string.Equals(x.Layout, imageLayout, StringComparison.OrdinalIgnoreCase)); Size = new Size(layout.Width, layout.Height); if (layout.Leds != null) foreach (LedLayout layoutLed in layout.Leds) { if (Enum.TryParse(layoutLed.Id, true, out int ledIndex)) { if (LedMapping.TryGetValue(new RazerLedId(this, ledIndex), out Led led)) { led.LedRectangle.Location = new Point(layoutLed.X, layoutLed.Y); led.LedRectangle.Size = new Size(layoutLed.Width, layoutLed.Height); led.Shape = layoutLed.Shape; led.ShapeData = layoutLed.ShapeData; LedImage image = ledImageLayout?.LedImages.FirstOrDefault(x => x.Id == layoutLed.Id); led.Image = (!string.IsNullOrEmpty(image?.Image)) ? new Uri(Path.Combine(imageBasePath, image.Image), UriKind.Absolute) : new Uri(Path.Combine(imageBasePath, "Missing.png"), UriKind.Absolute); } } } } } /// 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 } }