using System.Collections.Generic; using AuraServiceLib; using RGB.NET.Core; namespace RGB.NET.Devices.Asus { /// /// /// Represents a Asus keyboard. /// public class AsusKeyboardRGBDevice : AsusRGBDevice, IKeyboard { #region Properties & Fields private Dictionary _ledAsusLed = new(); private Dictionary _ledAsusLights = new(); IKeyboardDeviceInfo IKeyboard.DeviceInfo => DeviceInfo; #endregion #region Constructors /// /// /// Initializes a new instance of the class. /// /// The specific information provided by Asus for the keyboard. internal AsusKeyboardRGBDevice(AsusKeyboardRGBDeviceInfo info, IDeviceUpdateTrigger updateTrigger) : base(info, updateTrigger) { InitializeLayout(); } #endregion #region Methods private void InitializeLayout() { if (DeviceInfo.Device.Type != (uint)AsusDeviceType.NB_KB_4ZONE_RGB) { int pos = 0; int unknownLed = (int)LedId.Unknown1; ClearTags(); foreach (IAuraRgbKey key in ((IAuraSyncKeyboard)DeviceInfo.Device).Keys) { if (AsusKeyboardLedMapping.MAPPING.TryGetValue((AsusLedId)key.Code, out LedId ledId)) AddAsusLed((AsusLedId)key.Code, ledId, new Point(pos++ * 19, 0), new Size(19, 19)); else { AddAsusLed((AsusLedId)key.Code, (LedId)unknownLed, new Point(pos++ * 19, 0), new Size(19, 19)); unknownLed++; } TagAsusLed(key); } for (int index = 0; index < ((IAuraSyncKeyboard)DeviceInfo.Device).Lights.Count; index++) { IAuraRgbLight light = ((IAuraSyncKeyboard)DeviceInfo.Device).Lights[index]; if (IsAsusLedTagged(light)) continue; AddAsusLed(index, (LedId)unknownLed, new Point(pos++ * 19, 0), new Size(19, 19)); unknownLed++; } ClearTags(); } else { int ledCount = DeviceInfo.Device.Lights.Count; for (int i = 0; i < ledCount; i++) AddLed(LedId.Keyboard_Custom1 + i, new Point(i * 19, 0), new Size(19, 19)); } } private void AddAsusLed(AsusLedId asusLedId, LedId ledId, Point position, Size size) { if (this._ledAsusLed.TryGetValue(ledId, out AsusLedId firstAsusLed)) throw new RGBDeviceException($"Got LED '{ledId}' twice, first ASUS LED '{firstAsusLed}' second ASUS LED '{asusLedId}' on device '{DeviceInfo.DeviceName}'"); this._ledAsusLed.Add(ledId, asusLedId); AddLed(ledId, position, size); } private void AddAsusLed(int index, LedId ledId, Point position, Size size) { this._ledAsusLights.Add(ledId, index); AddLed(ledId, position, size); } /// protected override object? GetLedCustomData(LedId ledId) { if (this._ledAsusLed.TryGetValue(ledId, out AsusLedId asusLedId)) return (true, (int)asusLedId); if (this._ledAsusLights.TryGetValue(ledId, out int lightIndex)) return (false, lightIndex); return null; } private void ClearTags() { foreach (IAuraRgbLight light in ((IAuraSyncKeyboard)DeviceInfo.Device).Lights) light.Color = 0x000000; } private void TagAsusLed(IAuraRgbKey key) { key.Color = 0x000001; } private bool IsAsusLedTagged(IAuraRgbLight light) { return light.Color == 0x000001; } #endregion } }