Added basic CoolerMaster support

This commit is contained in:
DarthAffe committed 2017-04-02 11:41:32 +02:00
1 parent 8446b9a47b
commit 8cc4688932
23 files changed
+1220 -2

No files matched your search

@@ -0,0 +1,125 @@
using System.Diagnostics;
using RGB.NET.Core;
namespace RGB.NET.Devices.CoolerMaster
{
/// <summary>
/// Represents a Id of a <see cref="Led"/> on a <see cref="CoolerMasterRGBDevice"/>.
/// </summary>
[DebuggerDisplay("{" + nameof(LedId) + "}")]
public class CoolerMasterLedId : ILedId
{
#region Properties & Fields
internal readonly CoolerMasterLedIds LedId;
internal readonly int Row;
internal readonly int Column;
/// <inheritdoc />
public IRGBDevice Device { get; }
/// <inheritdoc />
public bool IsValid => LedId != CoolerMasterLedIds.Invalid;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CoolerMasterLedId"/> class.
/// </summary>
/// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
/// <param name="ledId">The <see cref="CoolerMasterLedId"/> of the represented <see cref="Led"/>.</param>
public CoolerMasterLedId(IRGBDevice device, CoolerMasterLedIds ledId)
{
this.Device = device;
this.LedId = ledId;
}
/// <summary>
/// Initializes a new instance of the <see cref="CoolerMasterLedId"/> class.
/// </summary>
/// <param name="device">The <see cref="IRGBDevice"/> the <see cref="ILedId"/> belongs to.</param>
/// <param name="ledId">The <see cref="CoolerMasterLedId"/> of the represented <see cref="Led"/>.</param>
/// <param name="row">The row in the mapping table of the device.</param>
/// <param name="column">The column in the mapping table of the device.</param>
public CoolerMasterLedId(IRGBDevice device, CoolerMasterLedIds ledId, int row, int column)
{
this.Device = device;
this.LedId = ledId;
this.Row = row;
this.Column = column;
}
#endregion
#region Methods
/// <summary>
/// Converts the Id of this <see cref="CoolerMasterLedId"/> to a human-readable string.
/// </summary>
/// <returns>A string that contains the Id of this <see cref="CoolerMasterLedId"/>. For example "Enter".</returns>
public override string ToString()
{
return LedId.ToString();
}
/// <summary>
/// Tests whether the specified object is a <see cref="CoolerMasterLedId" /> and is equivalent to this <see cref="CoolerMasterLedId" />.
/// </summary>
/// <param name="obj">The object to test.</param>
/// <returns><c>true</c> if <paramref name="obj" /> is a <see cref="CoolerMasterLedId" /> equivalent to this <see cref="CoolerMasterLedId" />; otherwise, <c>false</c>.</returns>
public override bool Equals(object obj)
{
CoolerMasterLedId compareLedId = obj as CoolerMasterLedId;
if (ReferenceEquals(compareLedId, null))
return false;
if (ReferenceEquals(this, compareLedId))
return true;
if (GetType() != compareLedId.GetType())
return false;
return compareLedId.LedId == LedId;
}
/// <summary>
/// Returns a hash code for this <see cref="CoolerMasterLedId" />.
/// </summary>
/// <returns>An integer value that specifies the hash code for this <see cref="CoolerMasterLedId" />.</returns>
public override int GetHashCode()
{
return LedId.GetHashCode();
}
#endregion
#region Operators
/// <summary>
/// Returns a value that indicates whether two specified <see cref="CoolerMasterLedId" /> are equal.
/// </summary>
/// <param name="ledId1">The first <see cref="CoolerMasterLedId" /> to compare.</param>
/// <param name="ledId2">The second <see cref="CoolerMasterLedId" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(CoolerMasterLedId ledId1, CoolerMasterLedId ledId2)
{
return ReferenceEquals(ledId1, null) ? ReferenceEquals(ledId2, null) : ledId1.Equals(ledId2);
}
/// <summary>
/// Returns a value that indicates whether two specified <see cref="CoolerMasterLedId" /> are equal.
/// </summary>
/// <param name="ledId1">The first <see cref="CoolerMasterLedId" /> to compare.</param>
/// <param name="ledId2">The second <see cref="CoolerMasterLedId" /> to compare.</param>
/// <returns><c>true</c> if <paramref name="ledId1" /> and <paramref name="ledId2" /> are not equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(CoolerMasterLedId ledId1, CoolerMasterLedId ledId2)
{
return !(ledId1 == ledId2);
}
#endregion
}
}
@@ -0,0 +1,131 @@
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.CoolerMaster.Native;
namespace RGB.NET.Devices.CoolerMaster
{
/// <summary>
/// Represents a generic CoolerMaster-device. (keyboard, mouse, headset, mousmat).
/// </summary>
public abstract class CoolerMasterRGBDevice : AbstractRGBDevice
{
#region Properties & Fields
/// <summary>
/// Gets information about the <see cref="CoolerMasterRGBDevice"/>.
/// </summary>
public override IRGBDeviceInfo DeviceInfo { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CoolerMasterRGBDevice"/> class.
/// </summary>
/// <param name="info">The generic information provided by CoolerMaster for the device.</param>
protected CoolerMasterRGBDevice(IRGBDeviceInfo info)
{
this.DeviceInfo = info;
}
#endregion
#region Methods
/// <summary>
/// Initializes the device.
/// </summary>
internal void Initialize()
{
InitializeLayout();
if (InternalSize == null)
{
Rectangle ledRectangle = new Rectangle(this.Select(x => x.LedRectangle));
InternalSize = ledRectangle.Size + new Size(ledRectangle.Location.X, ledRectangle.Location.Y);
}
}
/// <summary>
/// Initializes the <see cref="Led"/> and <see cref="Size"/> of the device.
/// </summary>
protected abstract void InitializeLayout();
/// <summary>
/// Applies the given layout.
/// </summary>
/// <param name="layoutPath">The file containing the layout.</param>
/// <param name="imageLayout">The name of the layout used to get the images of the leds.</param>
/// <param name="imageBasePath">The path images for this device are collected in.</param>
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));
InternalSize = new Size(layout.Width, layout.Height);
if (layout.Leds != null)
foreach (LedLayout layoutLed in layout.Leds)
{
CoolerMasterLedIds ledId;
if (Enum.TryParse(layoutLed.Id, true, out ledId))
{
Led led;
if (LedMapping.TryGetValue(new CoolerMasterLedId(this, ledId), out led))
{
led.LedRectangle.Location.X = layoutLed.X;
led.LedRectangle.Location.Y = layoutLed.Y;
led.LedRectangle.Size.Width = layoutLed.Width;
led.LedRectangle.Size.Height = 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);
}
}
}
}
}
/// <inheritdoc />
protected override void UpdateLeds(IEnumerable<Led> ledsToUpdate)
{
List<Led> leds = ledsToUpdate.Where(x => x.Color.A > 0).ToList();
if (leds.Count > 0)
{
_CoolerMasterSDK.SetControlDevice(((CoolerMasterRGBDeviceInfo)DeviceInfo).DeviceIndex);
foreach (Led led in leds)
{
CoolerMasterLedId ledId = (CoolerMasterLedId)led.Id;
_CoolerMasterSDK.SetLedColor(ledId.Row, ledId.Column, led.Color.R, led.Color.G, led.Color.B);
}
_CoolerMasterSDK.RefreshLed(false);
}
}
/// <inheritdoc />
public override void Dispose()
{
_CoolerMasterSDK.SetControlDevice(((CoolerMasterRGBDeviceInfo)DeviceInfo).DeviceIndex);
_CoolerMasterSDK.EnableLedControl(false);
base.Dispose();
}
#endregion
}
}
@@ -0,0 +1,53 @@
using System;
using RGB.NET.Core;
using RGB.NET.Devices.CoolerMaster.Helper;
namespace RGB.NET.Devices.CoolerMaster
{
/// <summary>
/// Represents a generic information for a Corsair-<see cref="IRGBDevice"/>.
/// </summary>
public class CoolerMasterRGBDeviceInfo : IRGBDeviceInfo
{
#region Properties & Fields
/// <inheritdoc />
public RGBDeviceType DeviceType { get; }
/// <inheritdoc />
public string Manufacturer => "Cooler Master";
/// <inheritdoc />
public string Model { get; }
/// <inheritdoc />
public Uri Image { get; protected set; }
/// <inheritdoc />
public RGBDeviceLighting Lighting => RGBDeviceLighting.Key;
/// <summary>
/// Gets the <see cref="CoolerMasterDevicesIndexes"/> of the <see cref="CoolerMasterRGBDevice"/>.
/// </summary>
public CoolerMasterDevicesIndexes DeviceIndex { get; }
#endregion
#region Constructors
/// <summary>
/// Internal constructor of managed <see cref="CoolerMasterRGBDeviceInfo"/>.
/// </summary>
/// <param name="deviceType">The type of the <see cref="IRGBDevice"/>.</param>
/// <param name="deviceIndex">The <see cref="CoolerMasterDevicesIndexes"/> of the <see cref="IRGBDevice"/>.</param>
internal CoolerMasterRGBDeviceInfo(RGBDeviceType deviceType, CoolerMasterDevicesIndexes deviceIndex)
{
this.DeviceType = deviceType;
this.DeviceIndex = deviceIndex;
Model = deviceIndex.GetDescription();
}
#endregion
}
}