using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Devices.CoolerMaster.Native;
namespace RGB.NET.Devices.CoolerMaster
{
///
///
/// Represents a CoolerMaster keyboard.
///
public class CoolerMasterKeyboardRGBDevice : CoolerMasterRGBDevice
{
#region Constructors
///
///
/// Initializes a new instance of the class.
///
/// The specific information provided by CoolerMaster for the keyboard
internal CoolerMasterKeyboardRGBDevice(CoolerMasterKeyboardRGBDeviceInfo info)
: base(info)
{ }
#endregion
#region Methods
///
protected override void UpdateLeds(IEnumerable ledsToUpdate)
{
List leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList();
if (leds.Count > 0)
{
// 6 by 22 seems hard-coded but it's what the CM SDK expects regardless of keyboard size
_CoolerMasterSDK.COLOR_MATRIX matrix = new _CoolerMasterSDK.COLOR_MATRIX { KeyColor = new _CoolerMasterSDK.KEY_COLOR[6, 22] };
foreach (Led led in leds)
{
(int row, int column) = ((int, int))led.CustomData;
matrix.KeyColor[row, column] = new _CoolerMasterSDK.KEY_COLOR(led.Color.R, led.Color.G, led.Color.B);
}
_CoolerMasterSDK.SetControlDevice(DeviceInfo.DeviceIndex);
_CoolerMasterSDK.SetAllLedColor(matrix);
_CoolerMasterSDK.RefreshLed(false);
}
}
///
protected override void InitializeLayout()
{
Dictionary mapping = CoolerMasterKeyboardLedMappings.Mapping[DeviceInfo.DeviceIndex][DeviceInfo.PhysicalLayout];
foreach (KeyValuePair led in mapping)
InitializeLed(led.Key, new Rectangle(led.Value.column * 19, led.Value.row * 19, 19, 19));
string model = DeviceInfo.Model.Replace(" ", string.Empty).ToUpper();
ApplyLayoutFromFile(PathHelper.GetAbsolutePath(
$@"Layouts\CoolerMaster\Keyboards\{model}\{DeviceInfo.PhysicalLayout.ToString().ToUpper()}.xml"),
DeviceInfo.LogicalLayout.ToString(), PathHelper.GetAbsolutePath(@"Images\CoolerMaster\Keyboards"));
}
///
protected override object CreateLedCustomData(LedId ledId) => CoolerMasterKeyboardLedMappings.Mapping[DeviceInfo.DeviceIndex][DeviceInfo.PhysicalLayout][ledId];
#endregion
}
}