-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathCorsairRGBDevice.cs
107 lines (84 loc) · 3.74 KB
/
CorsairRGBDevice.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Collections.Generic;
using System.Linq;
using RGB.NET.Core;
using RGB.NET.Devices.Corsair.Native;
namespace RGB.NET.Devices.Corsair;
/// <inheritdoc cref="AbstractRGBDevice{TDeviceInfo}" />
/// <summary>
/// Represents a generic CUE-device. (keyboard, mouse, headset, mousepad).
/// </summary>
public abstract class CorsairRGBDevice<TDeviceInfo> : AbstractRGBDevice<TDeviceInfo>, ICorsairRGBDevice
where TDeviceInfo : CorsairRGBDeviceInfo
{
#region Properties & Fields
/// <summary>
/// <inheritdoc cref="ICorsairRGBDevice"/>
/// </summary>
public string DeviceId => DeviceInfo.DeviceId;
/// <summary>
/// Gets the mapping of <see cref="LedId"/> to <see cref="CorsairLedId"/> used to update the LEDs of this device.
/// </summary>
protected LedMapping<CorsairLedId> Mapping { get; private set; } = LedMapping<CorsairLedId>.Empty;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="CorsairRGBDevice{TDeviceInfo}"/> class.
/// </summary>
/// <param name="info">The generic information provided by CUE for the device.</param>
/// <param name="mapping">The mapping <see cref="LedId"/> to <see cref="CorsairLedId"/> used to update the LEDs of this device.</param>
/// <param name="updateQueue">The queue used to update this device.</param>
protected CorsairRGBDevice(TDeviceInfo info, IUpdateQueue updateQueue)
: base(info, updateQueue)
{ }
#endregion
#region Methods
protected bool Equals(CorsairRGBDevice<TDeviceInfo> other)
{
return DeviceId == other.DeviceId;
}
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((CorsairRGBDevice<TDeviceInfo>)obj);
}
public override int GetHashCode()
{
return DeviceId.GetHashCode();
}
void ICorsairRGBDevice.Initialize() => InitializeLayout();
/// <summary>
/// Initializes the LEDs of the device based on the data provided by the SDK.
/// </summary>
protected virtual void InitializeLayout()
{
CorsairError error = _CUESDK.CorsairGetLedPositions(DeviceInfo.DeviceId, out _CorsairLedPosition[] ledPositions);
if (error != CorsairError.Success)
throw new RGBDeviceException($"Failed to load device '{DeviceInfo.DeviceId}'. (ErrorCode: {error})");
List<_CorsairLedPosition> deviceLeds = ledPositions.Skip(DeviceInfo.LedOffset).Take(DeviceInfo.LedCount).ToList();
Mapping = CreateMapping(deviceLeds.Select(x => new CorsairLedId(x.id)));
foreach (_CorsairLedPosition ledPosition in deviceLeds)
{
LedId ledId = Mapping.TryGetValue(new CorsairLedId(ledPosition.id), out LedId id) ? id : LedId.Invalid;
Rectangle rectangle = ledPosition.ToRectangle();
AddLed(ledId, rectangle.Location, rectangle.Size);
}
if (DeviceInfo.LedOffset > 0)
FixOffsetDeviceLayout();
}
/// <summary>
/// Fixes the locations for devices split by offset by aligning them to the top left.
/// </summary>
protected virtual void FixOffsetDeviceLayout()
{
float minX = this.Min(x => x.Location.X);
float minY = this.Min(x => x.Location.Y);
foreach (Led led in this)
led.Location = led.Location.Translate(-minX, -minY);
}
protected abstract LedMapping<CorsairLedId> CreateMapping(IEnumerable<CorsairLedId> ids);
/// <inheritdoc />
protected override object GetLedCustomData(LedId ledId) => Mapping.TryGetValue(ledId, out CorsairLedId corsairLedId) ? corsairLedId : new CorsairLedId(0);
#endregion
}