1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 16:58:29 +00:00

Refactored to do everything based on LEDs.

This will adapt the devices and reduces the importance of the "special case keyboard"
This commit is contained in:
Darth Affe 2016-09-10 14:59:25 +02:00
parent bfe51add66
commit 5a018d0743
24 changed files with 847 additions and 894 deletions

View File

@ -2,7 +2,7 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Brushes
{
@ -14,12 +14,12 @@ namespace CUE.NET.Brushes
#region Properties & Fields
/// <summary>
/// Gets the id of the target-key.
/// Gets the ID of the target-LED.
/// </summary>
public CorsairKeyboardKeyId Key { get; }
public CorsairLedId LedId { get; }
/// <summary>
/// Gets the point representing the position to render the target-key.
/// Gets the point representing the position to render the target-LED.
/// </summary>
public PointF Point { get; }
@ -30,12 +30,12 @@ namespace CUE.NET.Brushes
/// <summary>
/// Initializes a new instance of the <see cref="BrushRenderTarget"/> class.
/// </summary>
/// <param name="key">The id of the target-key.</param>
/// <param name="point">The point representing the position to render the target-key.</param>
public BrushRenderTarget(CorsairKeyboardKeyId key, PointF point)
/// <param name="ledId">The ID of the target-LED.</param>
/// <param name="point">The point representing the position to render the target-LED.</param>
public BrushRenderTarget(CorsairLedId ledId, PointF point)
{
this.Point = point;
this.Key = key;
this.LedId = ledId;
}
#endregion

View File

@ -44,7 +44,7 @@ namespace CUE.NET.Brushes
/// <returns>The color at the specified point.</returns>
protected override Color GetColorAtPoint(RectangleF rectangle, BrushRenderTarget renderTarget)
{
CorsairKey key = CueSDK.KeyboardSDK[renderTarget.Key];
CorsairKey key = CueSDK.KeyboardSDK[(CorsairKeyboardKeyId)renderTarget.LedId];
if (key == null) return Color.Transparent;
Color color;

View File

@ -84,10 +84,10 @@
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairPhysicalKeyboardLayout.cs" />
<Compile Include="Devices\Generic\Enums\UpdateMode.cs" />
<Compile Include="Devices\Keyboard\Keys\AbstractKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\IKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\RectangleKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\ListKeyGroup.cs" />
<Compile Include="Groups\AbstractLedGroup.cs" />
<Compile Include="Groups\ILedGroup.cs" />
<Compile Include="Groups\RectangleLedGroup.cs" />
<Compile Include="Groups\ListLedGroup.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairMouseButtonId.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairPhysicalMouseLayout.cs" />
<Compile Include="Exceptions\CUEException.cs" />
@ -102,7 +102,7 @@
<Compile Include="Devices\Keyboard\CorsairKeyboard.cs" />
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
<Compile Include="Devices\Mouse\CorsairMouse.cs" />
<Compile Include="Devices\Keyboard\Extensions\KeyGroupExtension.cs" />
<Compile Include="Groups\Extensions\LedGroupExtension.cs" />
<Compile Include="Helper\ColorHelper.cs" />
<Compile Include="Gradients\LinearGradient.cs" />
<Compile Include="Helper\GradientHelper.cs" />

View File

@ -2,14 +2,21 @@
// ReSharper disable UnusedMethodReturnValue.Global
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic.EventArgs;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
using CUE.NET.Groups;
using CUE.NET.Helper;
using CUE.NET.Native;
namespace CUE.NET.Devices.Generic
@ -21,6 +28,16 @@ namespace CUE.NET.Devices.Generic
{
#region Properties & Fields
private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken;
private Task _updateTask;
private DateTime _lastUpdate = DateTime.Now;
/// <summary>
/// Gets a list of attached ledgroups.
/// </summary>
protected LinkedList<ILedGroup> LedGroups { get; } = new LinkedList<ILedGroup>();
/// <summary>
/// Gets generic information provided by CUE for the device.
/// </summary>
@ -45,15 +62,65 @@ namespace CUE.NET.Devices.Generic
/// </summary>
public float UpdateFrequency { get; set; } = 1f / 30f;
/// <summary>
/// Gets the rectangle containing all LEDs of the device.
/// </summary>
public RectangleF DeviceRectangle { get; }
/// <summary>
/// Gets a dictionary containing all LEDs of the device.
/// </summary>
protected Dictionary<int, CorsairLed> Leds { get; } = new Dictionary<int, CorsairLed>();
protected Dictionary<CorsairLedId, CorsairLed> LedMapping { get; } = new Dictionary<CorsairLedId, CorsairLed>();
private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken;
private Task _updateTask;
private DateTime _lastUpdate = DateTime.Now;
/// <summary>
/// Gets a read-only collection containing the LEDs of the device.
/// </summary>
public IEnumerable<CorsairLed> Leds => new ReadOnlyCollection<CorsairLed>(LedMapping.Values.ToList());
/// <summary>
/// Gets or sets the background brush of the keyboard.
/// </summary>
public IBrush Brush { get; set; }
/// <summary>
/// Gets or sets the z-index of the background brush of the keyboard.<br />
/// This value has absolutely no effect.
/// </summary>
public int ZIndex { get; set; } = 0;
#region Indexers
/// <summary>
/// Gets the <see cref="CorsairLed" /> with the specified ID.
/// </summary>
/// <param name="ledId">The ID of the LED to get.</param>
/// <returns>The LED with the specified ID or null if no LED is found.</returns>
public CorsairLed this[CorsairLedId ledId]
{
get
{
CorsairLed key;
return LedMapping.TryGetValue(ledId, out key) ? key : null;
}
}
/// <summary>
/// Gets the <see cref="CorsairLed" /> at the given physical location.
/// </summary>
/// <param name="location">The point to get the location from.</param>
/// <returns>The LED at the given point or null if no location is found.</returns>
public CorsairLed this[PointF location] => LedMapping.Values.FirstOrDefault(x => x.LedRectangle.Contains(location));
/// <summary>
/// Gets a list of <see cref="CorsairLed" /> inside the given rectangle.
/// </summary>
/// <param name="referenceRect">The rectangle to check.</param>
/// <param name="minOverlayPercentage">The minimal percentage overlay a location must have with the <see cref="Rectangle" /> to be taken into the list.</param>
/// <returns></returns>
public IEnumerable<CorsairLed> this[RectangleF referenceRect, float minOverlayPercentage = 0.5f] => LedMapping.Values
.Where(x => RectangleHelper.CalculateIntersectPercentage(x.LedRectangle, referenceRect) >= minOverlayPercentage);
#endregion
#endregion
@ -96,6 +163,10 @@ namespace CUE.NET.Devices.Generic
{
this.DeviceInfo = info;
// ReSharper disable once VirtualMemberCallInConstructor - I know, but I need this ...
InitializeLeds();
DeviceRectangle = RectangleHelper.CreateRectangleFromRectangles(((IEnumerable<CorsairLed>)this).Select(x => x.LedRectangle));
CheckUpdateLoop();
}
@ -103,18 +174,20 @@ namespace CUE.NET.Devices.Generic
#region Methods
protected abstract void InitializeLeds();
/// <summary>
/// Initializes the LED-Object with the specified id.
/// </summary>
/// <param name="ledId">The LED-Id to initialize.</param>
/// <param name="ledRectangle">The rectangle representing the position of the LED to initialize.</param>
/// <returns></returns>
protected CorsairLed InitializeLed(int ledId, RectangleF ledRectangle)
protected CorsairLed InitializeLed(CorsairLedId ledId, RectangleF ledRectangle)
{
if (!Leds.ContainsKey(ledId))
if (!LedMapping.ContainsKey(ledId))
{
CorsairLed led = new CorsairLed(ledRectangle);
Leds.Add(ledId, led);
CorsairLed led = new CorsairLed(ledId, ledRectangle);
LedMapping.Add(ledId, led);
return led;
}
@ -175,12 +248,22 @@ namespace CUE.NET.Devices.Generic
{
OnUpdating();
// Update effects
foreach (ILedGroup ledGroup in LedGroups)
ledGroup.UpdateEffects();
// Render brushes
Render(this);
foreach (ILedGroup ledGroup in LedGroups.OrderBy(x => x.ZIndex))
Render(ledGroup);
// Device-specific updates
DeviceUpdate();
ICollection<LedUpateRequest> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).Select(x => new LedUpateRequest(x.Key, x.Value.RequestedColor)).ToList();
foreach (CorsairLed led in Leds.Values)
led.Update();
// Send LEDs to SDK
ICollection<LedUpateRequest> ledsToUpdate = (flushLeds ? LedMapping : LedMapping.Where(x => x.Value.IsDirty)).Select(x => new LedUpateRequest(x.Key, x.Value.RequestedColor)).ToList();
foreach (LedUpateRequest updateRequest in ledsToUpdate)
LedMapping[updateRequest.LedId].Update();
UpdateLeds(ledsToUpdate);
@ -190,7 +273,82 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Performs device specific updates.
/// </summary>
protected abstract void DeviceUpdate();
protected virtual void DeviceUpdate()
{ }
/// <summary>
/// Attaches the given ledgroup.
/// </summary>
/// <param name="ledGroup">The ledgroup to attach.</param>
/// <returns><c>true</c> if the ledgroup could be attached; otherwise, <c>false</c>.</returns>
public bool AttachLedGroup(ILedGroup ledGroup)
{
lock (LedGroups)
{
if (ledGroup == null || LedGroups.Contains(ledGroup)) return false;
LedGroups.AddLast(ledGroup);
return true;
}
}
/// <summary>
/// Detaches the given ledgroup.
/// </summary>
/// <param name="ledGroup">The ledgroup to detached.</param>
/// <returns><c>true</c> if the ledgroup could be detached; otherwise, <c>false</c>.</returns>
public bool DetachLedGroup(ILedGroup ledGroup)
{
lock (LedGroups)
{
if (ledGroup == null) return false;
LinkedListNode<ILedGroup> node = LedGroups.Find(ledGroup);
if (node == null) return false;
LedGroups.Remove(node);
return true;
}
}
/// <summary>
/// Renders a ledgroup.
/// </summary>
/// <param name="ledGroup">The led group to render.</param>
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
protected virtual void Render(ILedGroup ledGroup)
{
IList<CorsairLed> leds = ledGroup.GetLeds().ToList();
IBrush brush = ledGroup.Brush;
try
{
switch (brush.BrushCalculationMode)
{
case BrushCalculationMode.Relative:
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(leds.Select(x => x.LedRectangle));
float offsetX = -brushRectangle.X;
float offsetY = -brushRectangle.Y;
brushRectangle.X = 0;
brushRectangle.Y = 0;
brush.PerformRender(brushRectangle, leds.Select(x => new BrushRenderTarget(x.Id, x.LedRectangle.GetCenter(offsetX, offsetY))));
break;
case BrushCalculationMode.Absolute:
brush.PerformRender(DeviceRectangle, leds.Select(x => new BrushRenderTarget(x.Id, x.LedRectangle.GetCenter())));
break;
default:
throw new ArgumentException();
}
brush.UpdateEffects();
brush.PerformFinalize();
foreach (KeyValuePair<BrushRenderTarget, Color> renders in brush.RenderedTargets)
this[renders.Key.LedId].Color = renders.Value;
}
// ReSharper disable once CatchAllClause
catch (Exception ex) { OnException(ex); }
}
private void UpdateLeds(ICollection<LedUpateRequest> updateRequests)
{
@ -207,7 +365,7 @@ namespace CUE.NET.Devices.Generic
{
_CorsairLedColor color = new _CorsairLedColor
{
ledId = ledUpdateRequest.LedId,
ledId = (int)ledUpdateRequest.LedId,
r = ledUpdateRequest.Color.R,
g = ledUpdateRequest.Color.G,
b = ledUpdateRequest.Color.B
@ -228,10 +386,49 @@ namespace CUE.NET.Devices.Generic
/// </summary>
internal void ResetLeds()
{
foreach (CorsairLed led in Leds.Values)
foreach (CorsairLed led in LedMapping.Values)
led.Reset();
}
/// <summary>
/// Gets a list containing all LEDs of this group.
/// </summary>
/// <returns>The list containing all LEDs of this group.</returns>
public IEnumerable<CorsairLed> GetLeds()
{
return Leds;
}
#region Effects
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.
/// </summary>
public void UpdateEffects()
{
throw new NotSupportedException("Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.");
}
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.
/// </summary>
/// <param name="effect">The effect to add.</param>
public void AddEffect(IEffect<ILedGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.");
}
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.
/// </summary>
/// <param name="effect">The effect to remove.</param>
public void RemoveEffect(IEffect<ILedGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the device. Add it to the Brush or create a keygroup instead.");
}
#endregion
#region EventCaller
/// <summary>
@ -313,6 +510,28 @@ namespace CUE.NET.Devices.Generic
}
#endregion
#region IEnumerable
/// <summary>
/// Returns an enumerator that iterates over all LEDs of the device.
/// </summary>
/// <returns>An enumerator for all LEDs of the device.</returns>
public IEnumerator<CorsairLed> GetEnumerator()
{
return LedMapping.Values.GetEnumerator();
}
/// <summary>
/// Returns an enumerator that iterates over all LEDs of the device.
/// </summary>
/// <returns>An enumerator for all LEDs of the device.</returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}

View File

@ -3,6 +3,7 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Helper;
namespace CUE.NET.Devices.Generic
@ -14,6 +15,8 @@ namespace CUE.NET.Devices.Generic
{
#region Properties & Fields
public CorsairLedId Id { get; set; }
/// <summary>
/// Gets a rectangle representing the physical location of the led.
/// </summary>
@ -24,11 +27,6 @@ namespace CUE.NET.Devices.Generic
/// </summary>
public bool IsDirty => RequestedColor != _color;
/// <summary>
/// Indicate whether the Color of the LED was set since the last update.
/// </summary>
public bool IsUpdated { get; private set; }
/// <summary>
/// Gets the Color the LED should be set to on the next update.
/// </summary>
@ -36,7 +34,7 @@ namespace CUE.NET.Devices.Generic
private Color _color = Color.Transparent;
/// <summary>
/// Gets the current color of the LED. Sets the <see cref="RequestedColor" /> for the next update and mark the LED as <see cref="IsUpdated" />.
/// Gets the current color of the LED. Sets the <see cref="RequestedColor" /> for the next update. />.
/// </summary>
public Color Color
{
@ -44,10 +42,7 @@ namespace CUE.NET.Devices.Generic
set
{
if (!IsLocked)
{
RequestedColor = RequestedColor.Blend(value);
IsUpdated = true;
}
}
}
@ -63,9 +58,11 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Initializes a new instance of the <see cref="CorsairLed"/> class.
/// </summary>
/// <param name="ledRectangle">The rectangle representing the physical location of the led.</param>
internal CorsairLed(RectangleF ledRectangle)
/// <param name="id">The ID of the LED</param>
/// <param name="ledRectangle">The rectangle representing the physical location of the LED.</param>
internal CorsairLed(CorsairLedId id, RectangleF ledRectangle)
{
this.Id = id;
this.LedRectangle = ledRectangle;
}
@ -79,7 +76,6 @@ namespace CUE.NET.Devices.Generic
internal void Update()
{
_color = RequestedColor;
IsUpdated = false;
}
/// <summary>
@ -89,7 +85,6 @@ namespace CUE.NET.Devices.Generic
{
_color = Color.Transparent;
RequestedColor = Color.Transparent;
IsUpdated = false;
IsLocked = false;
}

View File

@ -2,6 +2,7 @@
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
using System.Drawing;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Devices.Generic
{
@ -15,7 +16,7 @@ namespace CUE.NET.Devices.Generic
/// <summary>
/// Gets the id of the led to update.
/// </summary>
public int LedId { get; }
public CorsairLedId LedId { get; }
/// <summary>
/// Gets the requested color of the led.
@ -31,7 +32,7 @@ namespace CUE.NET.Devices.Generic
/// </summary>
/// <param name="ledId">The id of the led to update.</param>
/// <param name="color">The requested color of the led.</param>
public LedUpateRequest(int ledId, Color color)
public LedUpateRequest(CorsairLedId ledId, Color color)
{
this.LedId = ledId;
this.Color = color;

View File

@ -2,12 +2,9 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Headset.Enums;
namespace CUE.NET.Devices.Headset
@ -15,38 +12,15 @@ namespace CUE.NET.Devices.Headset
/// <summary>
/// Represents the SDK for a corsair headset.
/// </summary>
public class CorsairHeadset : AbstractCueDevice, IEnumerable<CorsairLed>
public class CorsairHeadset : AbstractCueDevice
{
#region Properties & Fields
#region Indexer
/// <summary>
/// Gets the <see cref="CorsairLed" /> with the specified ID.
/// </summary>
/// <param name="ledId">The ID of the LED to get.</param>
/// <returns>The LED with the specified ID.</returns>
public CorsairLed this[CorsairHeadsetLedId ledId]
{
get
{
CorsairLed led;
return base.Leds.TryGetValue((int)ledId, out led) ? led : null;
}
}
#endregion
/// <summary>
/// Gets specific information provided by CUE for the headset.
/// </summary>
public CorsairHeadsetDeviceInfo HeadsetDeviceInfo { get; }
/// <summary>
/// Gets a read-only collection containing all LEDs of the headset.
/// </summary>
public new IEnumerable<CorsairLed> Leds => new ReadOnlyCollection<CorsairLed>(base.Leds.Values.ToList());
#endregion
#region Constructors
@ -59,42 +33,18 @@ namespace CUE.NET.Devices.Headset
: base(info)
{
this.HeadsetDeviceInfo = info;
InitializeLeds();
}
#endregion
#region Methods
private void InitializeLeds()
protected override void InitializeLeds()
{
InitializeLed((int)CorsairHeadsetLedId.LeftLogo, new RectangleF(0, 0, 1, 1));
InitializeLed((int)CorsairHeadsetLedId.RightLogo, new RectangleF(1, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairHeadsetLedId.LeftLogo, new RectangleF(0, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairHeadsetLedId.RightLogo, new RectangleF(1, 0, 1, 1));
}
protected override void DeviceUpdate()
{
//DarthAffe 21.08.2016: Headsets can't own brushes or groups - nothing to do here for now
}
#region IEnumerable
/// <summary>
/// Returns an enumerator that iterates over all LEDs of the headset.
/// </summary>
/// <returns>An enumerator for all LEDS of the headset.</returns>
public IEnumerator<CorsairLed> GetEnumerator()
{
return Leds.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}

View File

@ -1,8 +1,12 @@
// ReSharper disable UnusedMemberInSuper.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic.EventArgs;
using CUE.NET.Groups;
namespace CUE.NET.Devices
{
@ -44,7 +48,7 @@ namespace CUE.NET.Devices
/// <summary>
/// Represents a generic cue device.
/// </summary>
public interface ICueDevice
public interface ICueDevice : ILedGroup, IEnumerable<CorsairLed>
{
/// <summary>
/// Gets generic information provided by CUE for the device.
@ -61,6 +65,38 @@ namespace CUE.NET.Devices
/// </summary>
float UpdateFrequency { get; set; }
/// <summary>
/// Gets the rectangle containing all LEDs of the device.
/// </summary>
RectangleF DeviceRectangle { get; }
/// <summary>
/// Gets a read-only collection containing the LEDs of the device.
/// </summary>
IEnumerable<CorsairLed> Leds { get; }
/// <summary>
/// Gets the <see cref="CorsairLed" /> with the specified ID.
/// </summary>
/// <param name="ledId">The ID of the LED to get.</param>
/// <returns>The LED with the specified ID or null if no LED is found.</returns>
CorsairLed this[CorsairLedId ledId] { get; }
/// <summary>
/// Gets the <see cref="CorsairLed" /> at the given physical location.
/// </summary>
/// <param name="location">The point to get the location from.</param>
/// <returns>The LED at the given point or null if no location is found.</returns>
CorsairLed this[PointF location] { get; }
/// <summary>
/// Gets a list of <see cref="CorsairLed" /> inside the given rectangle.
/// </summary>
/// <param name="referenceRect">The rectangle to check.</param>
/// <param name="minOverlayPercentage">The minimal percentage overlay a location must have with the <see cref="Rectangle" /> to be taken into the list.</param>
/// <returns></returns>
IEnumerable<CorsairLed> this[RectangleF referenceRect, float minOverlayPercentage = 0.5f] { get; }
// ReSharper disable EventNeverSubscribedTo.Global
/// <summary>
@ -95,5 +131,14 @@ namespace CUE.NET.Devices
/// </summary>
/// <param name="flushLeds">Specifies whether all keys (including clean ones) should be updated.</param>
void Update(bool flushLeds = false);
bool AttachLedGroup(ILedGroup ledGroup);
/// <summary>
/// Detaches the given ledgroup.
/// </summary>
/// <param name="ledGroup">The ledgroup to detached.</param>
/// <returns><c>true</c> if the ledgroup could be detached; otherwise, <c>false</c>.</returns>
bool DetachLedGroup(ILedGroup ledGroup);
}
}

View File

@ -15,6 +15,7 @@ using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Effects;
using CUE.NET.Groups;
using CUE.NET.Helper;
using CUE.NET.Native;
@ -23,7 +24,7 @@ namespace CUE.NET.Devices.Keyboard
/// <summary>
/// Represents the SDK for a corsair keyboard.
/// </summary>
public class CorsairKeyboard : AbstractCueDevice, IEnumerable<CorsairKey>, IKeyGroup
public class CorsairKeyboard : AbstractCueDevice, IEnumerable<CorsairKey>
{
#region Properties & Fields
@ -64,7 +65,7 @@ namespace CUE.NET.Devices.Keyboard
/// </summary>
/// <param name="location">The point to get the key from.</param>
/// <returns>The key at the given point or null if no key is found.</returns>
public CorsairKey this[PointF location] => _keys.Values.FirstOrDefault(x => x.Led.LedRectangle.Contains(location));
public new CorsairKey this[PointF location] => _keys.Values.FirstOrDefault(x => x.Led.LedRectangle.Contains(location));
/// <summary>
/// Gets a list of <see cref="CorsairKey" /> inside the given rectangle.
@ -72,13 +73,11 @@ namespace CUE.NET.Devices.Keyboard
/// <param name="referenceRect">The rectangle to check.</param>
/// <param name="minOverlayPercentage">The minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the list.</param>
/// <returns></returns>
public IEnumerable<CorsairKey> this[RectangleF referenceRect, float minOverlayPercentage = 0.5f] => _keys.Values
public new IEnumerable<CorsairKey> this[RectangleF referenceRect, float minOverlayPercentage = 0.5f] => _keys.Values
.Where(x => RectangleHelper.CalculateIntersectPercentage(x.Led.LedRectangle, referenceRect) >= minOverlayPercentage);
#endregion
private readonly LinkedList<IKeyGroup> _keyGroups = new LinkedList<IKeyGroup>();
private Dictionary<CorsairKeyboardKeyId, CorsairKey> _keys = new Dictionary<CorsairKeyboardKeyId, CorsairKey>();
/// <summary>
@ -91,22 +90,6 @@ namespace CUE.NET.Devices.Keyboard
/// </summary>
public CorsairKeyboardDeviceInfo KeyboardDeviceInfo { get; }
/// <summary>
/// Gets the rectangle containing all keys of the keyboard.
/// </summary>
public RectangleF KeyboardRectangle { get; }
/// <summary>
/// Gets or sets the background brush of the keyboard.
/// </summary>
public IBrush Brush { get; set; }
/// <summary>
/// Gets or sets the z-index of the background brush of the keyboard.<br />
/// This value has absolutely no effect.
/// </summary>
public int ZIndex { get; set; } = 0;
#endregion
#region Constructors
@ -119,117 +102,13 @@ namespace CUE.NET.Devices.Keyboard
: base(info)
{
this.KeyboardDeviceInfo = info;
InitializeKeys();
KeyboardRectangle = RectangleHelper.CreateRectangleFromRectangles(this.Select(x => x.Led.LedRectangle));
}
#endregion
#region Methods
#region Update
/// <summary>
/// Updates all brushes and groups on the keyboard.
/// </summary>
protected override void DeviceUpdate()
{
lock (_keyGroups)
{
foreach (IKeyGroup keyGroup in _keyGroups)
keyGroup.UpdateEffects();
}
if (Brush != null)
ApplyBrush(this.ToList(), Brush);
lock (_keyGroups)
{
foreach (IKeyGroup keyGroup in _keyGroups.OrderBy(x => x.ZIndex))
ApplyBrush(keyGroup.Keys.ToList(), keyGroup.Brush);
}
}
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
private void ApplyBrush(ICollection<CorsairKey> keys, IBrush brush)
{
try
{
switch (brush.BrushCalculationMode)
{
case BrushCalculationMode.Relative:
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.Led.LedRectangle));
float offsetX = -brushRectangle.X;
float offsetY = -brushRectangle.Y;
brushRectangle.X = 0;
brushRectangle.Y = 0;
brush.PerformRender(brushRectangle, keys.Select(x => new BrushRenderTarget(x.KeyId, x.Led.LedRectangle.GetCenter(offsetX, offsetY))));
break;
case BrushCalculationMode.Absolute:
brush.PerformRender(KeyboardRectangle, keys.Select(x => new BrushRenderTarget(x.KeyId, x.Led.LedRectangle.GetCenter())));
break;
default:
throw new ArgumentException();
}
brush.UpdateEffects();
brush.PerformFinalize();
foreach (KeyValuePair<BrushRenderTarget, Color> renders in brush.RenderedTargets)
_keys[renders.Key.Key].Led.Color = renders.Value;
}
// ReSharper disable once CatchAllClause
catch (Exception ex) { OnException(ex); }
}
/// <summary>
/// Gets a list containing all LEDs of this group.
/// </summary>
/// <returns>The list containing all LEDs of this group.</returns>
public IEnumerable<CorsairLed> GetLeds()
{
return this.Select(x => x.Led);
}
#endregion
/// <summary>
/// Attaches the given keygroup.
/// </summary>
/// <param name="keyGroup">The keygroup to attach.</param>
/// <returns><c>true</c> if the keygroup could be attached; otherwise, <c>false</c>.</returns>
public bool AttachKeyGroup(IKeyGroup keyGroup)
{
lock (_keyGroups)
{
if (keyGroup == null || _keyGroups.Contains(keyGroup)) return false;
_keyGroups.AddLast(keyGroup);
return true;
}
}
/// <summary>
/// Detaches the given keygroup.
/// </summary>
/// <param name="keyGroup">The keygroup to detached.</param>
/// <returns><c>true</c> if the keygroup could be detached; otherwise, <c>false</c>.</returns>
public bool DetachKeyGroup(IKeyGroup keyGroup)
{
lock (_keyGroups)
{
if (keyGroup == null) return false;
LinkedListNode<IKeyGroup> node = _keyGroups.Find(keyGroup);
if (node == null) return false;
_keyGroups.Remove(node);
return true;
}
}
private void InitializeKeys()
protected override void InitializeLeds()
{
_CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositions(), typeof(_CorsairLedPositions));
int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition));
@ -237,50 +116,20 @@ namespace CUE.NET.Devices.Keyboard
for (int i = 0; i < nativeLedPositions.numberOfLed; i++)
{
_CorsairLedPosition ledPosition = (_CorsairLedPosition)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
CorsairLed led = InitializeLed((int)ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height));
_keys.Add(ledPosition.ledId, new CorsairKey(ledPosition.ledId, led));
CorsairLed led = InitializeLed(ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height));
_keys.Add((CorsairKeyboardKeyId)ledPosition.ledId, new CorsairKey((CorsairKeyboardKeyId)ledPosition.ledId, led));
ptr = new IntPtr(ptr.ToInt64() + structSize);
}
}
#region Effects
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.
/// </summary>
public void UpdateEffects()
{
throw new NotSupportedException("Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.");
}
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.
/// </summary>
/// <param name="effect">The effect to add.</param>
public void AddEffect(IEffect<IKeyGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.");
}
/// <summary>
/// NOT IMPLEMENTED: Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.
/// </summary>
/// <param name="effect">The effect to remove.</param>
public void RemoveEffect(IEffect<IKeyGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.");
}
#endregion
#region IEnumerable
/// <summary>
/// Returns an enumerator that iterates over all keys of the keyboard.
/// </summary>
/// <returns>An enumerator for all keys of the keyboard.</returns>
public IEnumerator<CorsairKey> GetEnumerator()
public new IEnumerator<CorsairKey> GetEnumerator()
{
return _keys.Values.GetEnumerator();
}

View File

@ -1,80 +0,0 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Linq;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Keys;
namespace CUE.NET.Devices.Keyboard.Extensions
{
/// <summary>
/// Offers some extensions and helper-methods for keygroup related things.
/// </summary>
public static class KeyGroupExtension
{
/// <summary>
/// Converts the given <see cref="AbstractKeyGroup" /> to a <see cref="ListKeyGroup" />.
/// </summary>
/// <param name="keyGroup">The <see cref="AbstractKeyGroup" /> to convert.</param>
/// <returns>The converted <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup ToSimpleKeyGroup(this AbstractKeyGroup keyGroup)
{
ListKeyGroup simpleKeyGroup = keyGroup as ListKeyGroup;
if (simpleKeyGroup == null)
{
bool wasAttached = keyGroup.Detach();
simpleKeyGroup = new ListKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Brush = keyGroup.Brush };
}
return simpleKeyGroup;
}
/// <summary>
/// Returns a new <see cref="ListKeyGroup" /> which contains all keys from the given keygroup excluding the specified ones.
/// </summary>
/// <param name="keyGroup">The base keygroup.</param>
/// <param name="keyIds">The ids of the keys to exclude.</param>
/// <returns>The new <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup Exclude(this AbstractKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
{
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKeyboardKeyId keyId in keyIds)
simpleKeyGroup.RemoveKey(keyId);
return simpleKeyGroup;
}
/// <summary>
/// Returns a new <see cref="ListKeyGroup" /> which contains all keys from the given keygroup excluding the specified ones.
/// </summary>
/// <param name="keyGroup">The base keygroup.</param>
/// <param name="keyIds">The keys to exclude.</param>
/// <returns>The new <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup Exclude(this AbstractKeyGroup keyGroup, params CorsairKey[] keyIds)
{
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKey key in keyIds)
simpleKeyGroup.RemoveKey(key);
return simpleKeyGroup;
}
// ReSharper disable once UnusedMethodReturnValue.Global
/// <summary>
/// Attaches the given keygroup to the keyboard.
/// </summary>
/// <param name="keyGroup">The keygroup to attach.</param>
/// <returns><c>true</c> if the keygroup could be attached; otherwise, <c>false</c>.</returns>
public static bool Attach(this AbstractKeyGroup keyGroup)
{
return keyGroup.Keyboard?.AttachKeyGroup(keyGroup) ?? false;
}
/// <summary>
/// Detaches the given keygroup from the keyboard.
/// </summary>
/// <param name="keyGroup">The keygroup to attach.</param>
/// <returns><c>true</c> if the keygroup could be detached; otherwise, <c>false</c>.</returns>
public static bool Detach(this AbstractKeyGroup keyGroup)
{
return keyGroup.Keyboard?.DetachKeyGroup(keyGroup) ?? false;
}
}
}

View File

@ -1,7 +1,6 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
using System.Drawing;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
@ -40,5 +39,14 @@ namespace CUE.NET.Devices.Keyboard.Keys
}
#endregion
#region Operators
public static implicit operator CorsairLed(CorsairKey key)
{
return key.Led;
}
#endregion
}
}

View File

@ -1,248 +0,0 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using CUE.NET.Devices.Keyboard.Enums;
namespace CUE.NET.Devices.Keyboard.Keys
{
/// <summary>
/// Represents a keygroup containing arbitrary keys.
/// </summary>
public class ListKeyGroup : AbstractKeyGroup
{
#region Properties & Fields
protected override IKeyGroup EffectTarget => this;
/// <summary>
/// Gets the list containing the keys of this keygroup.
/// </summary>
protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this keygroup should be automatically attached or not.</param>
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
: base(keyboard, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="keys">The initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
: this(keyboard, true, keys)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="keys">The initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, IEnumerable<CorsairKey> keys)
: this(keyboard, true, keys)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this keygroup should be automatically attached or not.</param>
/// <param name="keys">The initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, IEnumerable<CorsairKey> keys)
: base(keyboard, autoAttach)
{
AddKeys(keys);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this keygroup should be automatically attached or not.</param>
/// <param name="keys">The initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKey[] keys)
: base(keyboard, autoAttach)
{
AddKeys(keys);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="keys">The IDs of the initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
: this(keyboard, true, keys)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="keys">The IDs of the initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, IEnumerable<CorsairKeyboardKeyId> keys)
: this(keyboard, true, keys)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this keygroup should be automatically attached or not.</param>
/// <param name="keys">The IDs of the initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKeyboardKeyId[] keys)
: base(keyboard, autoAttach)
{
AddKeys(keys);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this keygroup should be automatically attached or not.</param>
/// <param name="keys">The IDs of the initial keys of this keygroup.</param>
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, IEnumerable<CorsairKeyboardKeyId> keys)
: base(keyboard, autoAttach)
{
AddKeys(keys);
}
#endregion
#region Methods
/// <summary>
/// Adds the given key(s) to the keygroup.
/// </summary>
/// <param name="keys">The key(s) to add.</param>
public void AddKey(params CorsairKey[] keys)
{
AddKeys(keys);
}
/// <summary>
/// Adds the given key(s) to the keygroup.
/// </summary>
/// <param name="keyIds">The ID(s) of the key(s) to add.</param>
public void AddKey(params CorsairKeyboardKeyId[] keyIds)
{
AddKeys(keyIds);
}
/// <summary>
/// Adds the given keys to the keygroup.
/// </summary>
/// <param name="keys">The keys to add.</param>
public void AddKeys(IEnumerable<CorsairKey> keys)
{
if (keys != null)
foreach (CorsairKey key in keys)
if (key != null && !ContainsKey(key))
GroupKeys.Add(key);
}
/// <summary>
/// Adds the given keys to the keygroup.
/// </summary>
/// <param name="keyIds">The IDs of the keys to add.</param>
public void AddKeys(IEnumerable<CorsairKeyboardKeyId> keyIds)
{
if (keyIds != null)
foreach (CorsairKeyboardKeyId keyId in keyIds)
AddKey(Keyboard[keyId]);
}
/// <summary>
/// Removes the given key(s) from the keygroup.
/// </summary>
/// <param name="keys">The key(s) to remove.</param>
public void RemoveKey(params CorsairKey[] keys)
{
RemoveKeys(keys);
}
/// <summary>
/// Removes the given key(s) from the keygroup.
/// </summary>
/// <param name="keyIds">The ID(s) of the key(s) to remove.</param>
public void RemoveKey(params CorsairKeyboardKeyId[] keyIds)
{
RemoveKeys(keyIds);
}
/// <summary>
/// Removes the given keys from the keygroup.
/// </summary>
/// <param name="keys">The keys to remove.</param>
public void RemoveKeys(IEnumerable<CorsairKey> keys)
{
if (keys != null)
foreach (CorsairKey key in keys)
if (key != null)
GroupKeys.Remove(key);
}
/// <summary>
/// Removes the given keys from the keygroup.
/// </summary>
/// <param name="keyIds">The IDs of the keys to remove.</param>
public void RemoveKeys(IEnumerable<CorsairKeyboardKeyId> keyIds)
{
if (keyIds != null)
foreach (CorsairKeyboardKeyId keyId in keyIds)
RemoveKey(Keyboard[keyId]);
}
/// <summary>
/// Checks if a given key is contained by this keygroup.
/// </summary>
/// <param name="key">The key which should be checked.</param>
/// <returns><c>true</c> if the key is contained by this keygroup; otherwise, <c>false</c>.</returns>
public bool ContainsKey(CorsairKey key)
{
return key != null && GroupKeys.Contains(key);
}
/// <summary>
/// Checks if a given key is contained by this keygroup.
/// </summary>
/// <param name="keyId">The ID of the key which should be checked.</param>
/// <returns><c>true</c> if the key is contained by this keygroup; otherwise, <c>false</c>.</returns>
public bool ContainsKey(CorsairKeyboardKeyId keyId)
{
return ContainsKey(Keyboard[keyId]);
}
/// <summary>
/// Merges the keys from the given keygroup in this keygroup.
/// </summary>
/// <param name="groupToMerge">The keygroup to merge.</param>
public void MergeKeys(IKeyGroup groupToMerge)
{
foreach (CorsairKey key in groupToMerge.Keys)
if (!GroupKeys.Contains(key))
GroupKeys.Add(key);
}
/// <summary>
/// Gets a list containing the keys from this group.
/// </summary>
/// <returns>The list containing the keys.</returns>
protected override IList<CorsairKey> GetGroupKeys()
{
return GroupKeys;
}
#endregion
}
}

View File

@ -1,112 +0,0 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Helper;
namespace CUE.NET.Devices.Keyboard.Keys
{
/// <summary>
/// Represents a keygroup containing keys which physically lay inside a rectangle.
/// </summary>
public class RectangleKeyGroup : AbstractKeyGroup
{
#region Properties & Fields
protected override IKeyGroup EffectTarget => this;
private IList<CorsairKey> _keyCache;
private RectangleF _rectangle;
/// <summary>
/// Gets or sets the rectangle the keys should be taken from.
/// </summary>
public RectangleF Rectangle
{
get { return _rectangle; }
set
{
_rectangle = value;
_keyCache = null;
}
}
/// <summary>
/// Gets or sets the minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the keygroup.
/// </summary>
public float MinOverlayPercentage { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RectangleKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="fromKey">They ID of the first key to calculate the rectangle of this keygroup from.</param>
/// <param name="toKey">They ID of the second key to calculate the rectangle of this keygroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the keygroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleKeyGroup(CorsairKeyboard keyboard, CorsairKeyboardKeyId fromKey, CorsairKeyboardKeyId toKey, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(keyboard, keyboard[fromKey], keyboard[toKey], minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="fromKey">They first key to calculate the rectangle of this keygroup from.</param>
/// <param name="toKey">They second key to calculate the rectangle of this keygroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the keygroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleKeyGroup(CorsairKeyboard keyboard, CorsairKey fromKey, CorsairKey toKey, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(keyboard, RectangleHelper.CreateRectangleFromRectangles(fromKey.Led.LedRectangle, toKey.Led.LedRectangle), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="fromPoint">They first point to calculate the rectangle of this keygroup from.</param>
/// <param name="toPoint">They second point to calculate the rectangle of this keygroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the keygroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleKeyGroup(CorsairKeyboard keyboard, PointF fromPoint, PointF toPoint, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(keyboard, RectangleHelper.CreateRectangleFromPoints(fromPoint, toPoint), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="rectangle">The rectangle of this keygroup.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a key must have with the <see cref="Rectangle" /> to be taken into the keygroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleKeyGroup(CorsairKeyboard keyboard, RectangleF rectangle, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: base(keyboard, autoAttach)
{
this.Rectangle = rectangle;
this.MinOverlayPercentage = minOverlayPercentage;
}
#endregion
#region Methods
/// <summary>
/// Gets a list containing the keys from this group.
/// </summary>
/// <returns>The list containing the keys.</returns>
protected override IList<CorsairKey> GetGroupKeys()
{
return _keyCache ?? (_keyCache = Keyboard.Where(x => RectangleHelper.CalculateIntersectPercentage(x.Led.LedRectangle, Rectangle) >= MinOverlayPercentage).ToList());
}
#endregion
}
}

View File

@ -2,12 +2,9 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Mouse.Enums;
using CUE.NET.Exceptions;
@ -16,38 +13,15 @@ namespace CUE.NET.Devices.Mouse
/// <summary>
/// Represents the SDK for a corsair mouse.
/// </summary>
public class CorsairMouse : AbstractCueDevice, IEnumerable<CorsairLed>
public class CorsairMouse : AbstractCueDevice
{
#region Properties & Fields
#region Indexer
/// <summary>
/// Gets the <see cref="CorsairLed" /> with the specified ID.
/// </summary>
/// <param name="ledId">The ID of the LED to get.</param>
/// <returns>The LED with the specified ID.</returns>
public CorsairLed this[CorsairMouseLedId ledId]
{
get
{
CorsairLed led;
return base.Leds.TryGetValue((int)ledId, out led) ? led : null;
}
}
#endregion
/// <summary>
/// Gets specific information provided by CUE for the mouse.
/// </summary>
public CorsairMouseDeviceInfo MouseDeviceInfo { get; }
/// <summary>
/// Gets a read-only collection containing all LEDs of the mouse.
/// </summary>
public new IEnumerable<CorsairLed> Leds => new ReadOnlyCollection<CorsairLed>(base.Leds.Values.ToList());
#endregion
#region Constructors
@ -60,64 +34,39 @@ namespace CUE.NET.Devices.Mouse
: base(info)
{
this.MouseDeviceInfo = info;
InitializeLeds();
}
#endregion
#region Methods
private void InitializeLeds()
protected override void InitializeLeds()
{
switch (MouseDeviceInfo.PhysicalLayout)
{
case CorsairPhysicalMouseLayout.Zones1:
InitializeLed((int)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
break;
case CorsairPhysicalMouseLayout.Zones2:
InitializeLed((int)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
break;
case CorsairPhysicalMouseLayout.Zones3:
InitializeLed((int)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B3, new RectangleF(2, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B3, new RectangleF(2, 0, 1, 1));
break;
case CorsairPhysicalMouseLayout.Zones4:
InitializeLed((int)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B3, new RectangleF(2, 0, 1, 1));
InitializeLed((int)CorsairMouseLedId.B4, new RectangleF(3, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B1, new RectangleF(0, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B2, new RectangleF(1, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B3, new RectangleF(2, 0, 1, 1));
InitializeLed((CorsairLedId)CorsairMouseLedId.B4, new RectangleF(3, 0, 1, 1));
break;
default:
throw new WrapperException($"Can't initial mouse with layout '{MouseDeviceInfo.PhysicalLayout}'");
}
}
protected override void DeviceUpdate()
{
//DarthAffe 21.08.2016: Mice can't own brushes or groups - nothing to do here for now
}
#region IEnumerable
/// <summary>
/// Returns an enumerator that iterates over all LEDs of the mouse.
/// </summary>
/// <returns>An enumerator for all LDS of the mouse.</returns>
public IEnumerator<CorsairLed> GetEnumerator()
{
return Leds.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}

View File

@ -11,7 +11,6 @@ using System.Linq;
using System.Runtime.InteropServices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Mousemat.Enums;
using CUE.NET.Exceptions;
using CUE.NET.Native;
@ -20,38 +19,15 @@ namespace CUE.NET.Devices.Mousemat
/// <summary>
/// Represents the SDK for a corsair mousemat.
/// </summary>
public class CorsairMousemat : AbstractCueDevice, IEnumerable<CorsairLed>
public class CorsairMousemat : AbstractCueDevice
{
#region Properties & Fields
#region Indexer
/// <summary>
/// Gets the <see cref="CorsairLed" /> with the specified ID.
/// </summary>
/// <param name="ledId">The ID of the LED to get.</param>
/// <returns>The LED with the specified ID.</returns>
public CorsairLed this[CorsairMousematLedId ledId]
{
get
{
CorsairLed led;
return base.Leds.TryGetValue((int)ledId, out led) ? led : null;
}
}
#endregion
/// <summary>
/// Gets specific information provided by CUE for the mousemat.
/// </summary>
public CorsairMousematDeviceInfo MousematDeviceInfo { get; }
/// <summary>
/// Gets a read-only collection containing all LEDs of the mousemat.
/// </summary>
public new IEnumerable<CorsairLed> Leds => new ReadOnlyCollection<CorsairLed>(base.Leds.Values.ToList());
#endregion
#region Constructors
@ -64,14 +40,13 @@ namespace CUE.NET.Devices.Mousemat
: base(info)
{
this.MousematDeviceInfo = info;
InitializeLeds();
}
#endregion
#region Methods
private void InitializeLeds()
protected override void InitializeLeds()
{
int deviceCount = _CUESDK.CorsairGetDeviceCount();
@ -105,32 +80,9 @@ namespace CUE.NET.Devices.Mousemat
// Sort for easy iteration by clients
foreach (_CorsairLedPosition ledPosition in positions.OrderBy(p => p.ledId))
InitializeLed((int)ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height));
InitializeLed(ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height));
}
protected override void DeviceUpdate()
{
//TODO DarthAffe 10.09.2016: Implement
}
#region IEnumerable
/// <summary>
/// Returns an enumerator that iterates over all LEDs of the mousemat.
/// </summary>
/// <returns>An enumerator for all LEDS of the mousemat.</returns>
public IEnumerator<CorsairLed> GetEnumerator()
{
return Leds.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}

View File

@ -2,13 +2,14 @@
// ReSharper disable UnusedAutoPropertyAccessor.Global
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Groups;
namespace CUE.NET.Effects
{
/// <summary>
/// Represents a basic effect targeting an <see cref="IKeyGroup"/>.
/// Represents a basic effect targeting an <see cref="ILedGroup"/>.
/// </summary>
public abstract class AbstractKeyGroupEffect : IEffect<IKeyGroup>
public abstract class AbstractKeyGroupEffect : IEffect<ILedGroup>
{
#region Properties & Fields
@ -18,9 +19,9 @@ namespace CUE.NET.Effects
public bool IsDone { get; protected set; }
/// <summary>
/// Gets the <see cref="IKeyGroup"/> this effect is targeting.
/// Gets the <see cref="ILedGroup"/> this effect is targeting.
/// </summary>
protected IKeyGroup KeyGroup { get; private set; }
protected ILedGroup LedGroup { get; private set; }
#endregion
@ -35,19 +36,19 @@ namespace CUE.NET.Effects
/// <summary>
/// Hook which is called when the effect is attached to a device.
/// </summary>
/// <param name="target">The <see cref="IKeyGroup"/> this effect is attached to.</param>
public virtual void OnAttach(IKeyGroup target)
/// <param name="target">The <see cref="ILedGroup"/> this effect is attached to.</param>
public virtual void OnAttach(ILedGroup target)
{
KeyGroup = target;
LedGroup = target;
}
/// <summary>
/// Hook which is called when the effect is detached from a device.
/// </summary>
/// <param name="target">The <see cref="IKeyGroup"/> this effect is detached from.</param>
public virtual void OnDetach(IKeyGroup target)
/// <param name="target">The <see cref="ILedGroup"/> this effect is detached from.</param>
public virtual void OnDetach(ILedGroup target)
{
KeyGroup = null;
LedGroup = null;
}
#endregion

View File

@ -8,6 +8,7 @@ using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Exceptions;
using CUE.NET.Gradients;
using CUE.NET.Groups;
using Example_AudioAnalyzer_full.TakeAsIs;
namespace Example_AudioAnalyzer_full
@ -67,12 +68,12 @@ namespace Example_AudioAnalyzer_full
// Note that this isn't a 'real effect' since it's update-rate dependent. A real effect would do always the same thing not mather how fast the keyboard updates.
_keyboard.Brush = new SolidColorBrush(Color.FromArgb(96, 0, 0, 0));
// Add our song-beat-effect. Remember to uncomment the update in the spectrum effect if you want to remove this.
ListKeyGroup songBeatGroup = new ListKeyGroup(_keyboard, _keyboard);
ListLedGroup songBeatGroup = new ListLedGroup(_keyboard, _keyboard);
songBeatGroup.Brush = new SolidColorBrush(Color.FromArgb(127, 164, 164, 164));
songBeatGroup.Brush.AddEffect(new SongBeatEffect(_soundDataProcessor));
// Add our spectrum-effect using the soundDataProcessor and a rainbow from purple to red as gradient
ListKeyGroup spectrumGroup = new ListKeyGroup(_keyboard, _keyboard);
ListLedGroup spectrumGroup = new ListLedGroup(_keyboard, _keyboard);
spectrumGroup.Brush = new AudioSpectrumBrush(_soundDataProcessor, new RainbowGradient(300, -14));
// Hook onto the keyboard update and process data

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
@ -10,13 +9,11 @@ using CUE.NET.Brushes;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Extensions;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Devices.Mousemat;
using CUE.NET.Devices.Mousemat.Enums;
using CUE.NET.Exceptions;
using CUE.NET.Gradients;
using CUE.NET.Profiles;
using CUE.NET.Groups;
namespace SimpleDevTest
{
@ -53,9 +50,9 @@ namespace SimpleDevTest
keyboard.UpdateMode = UpdateMode.Continuous;
keyboard.Brush = new SolidColorBrush(Color.Black);
RectangleF spot = new RectangleF(keyboard.KeyboardRectangle.Width / 2f, keyboard.KeyboardRectangle.Y / 2f, 160, 80);
RectangleF spot = new RectangleF(keyboard.DeviceRectangle.Width / 2f, keyboard.DeviceRectangle.Y / 2f, 160, 80);
PointF target = new PointF(spot.X, spot.Y);
RectangleKeyGroup spotGroup = new RectangleKeyGroup(keyboard, spot) { Brush = new LinearGradientBrush(new RainbowGradient()) };
RectangleLedGroup spotGroup = new RectangleLedGroup(keyboard, spot) { Brush = new LinearGradientBrush(new RainbowGradient()) };
float brushModeTimer = BRUSH_MODE_CHANGE_TIMER;
keyboard.Updating += (sender, eventArgs) =>
@ -69,8 +66,8 @@ namespace SimpleDevTest
}
if (spot.Contains(target))
target = new PointF((float)(keyboard.KeyboardRectangle.X + (random.NextDouble() * keyboard.KeyboardRectangle.Width)),
(float)(keyboard.KeyboardRectangle.Y + (random.NextDouble() * keyboard.KeyboardRectangle.Height)));
target = new PointF((float)(keyboard.DeviceRectangle.X + (random.NextDouble() * keyboard.DeviceRectangle.Width)),
(float)(keyboard.DeviceRectangle.Y + (random.NextDouble() * keyboard.DeviceRectangle.Height)));
else
spot.Location = Interpolate(spot.Location, target, eventArgs.DeltaTime * SPEED);
spotGroup.Rectangle = spot;
@ -80,23 +77,23 @@ namespace SimpleDevTest
mousemat.UpdateMode = UpdateMode.Continuous;
// Left
mousemat[CorsairMousematLedId.Zone1].Color = Color.Red;
mousemat[CorsairMousematLedId.Zone2].Color = Color.Red;
mousemat[CorsairMousematLedId.Zone3].Color = Color.Red;
mousemat[CorsairMousematLedId.Zone4].Color = Color.Red;
mousemat[CorsairMousematLedId.Zone5].Color = Color.Red;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone1].Color = Color.Red;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone2].Color = Color.Red;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone3].Color = Color.Red;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone4].Color = Color.Red;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone5].Color = Color.Red;
// Bottom
mousemat[CorsairMousematLedId.Zone6].Color = Color.LawnGreen;
mousemat[CorsairMousematLedId.Zone7].Color = Color.LawnGreen;
mousemat[CorsairMousematLedId.Zone8].Color = Color.LawnGreen;
mousemat[CorsairMousematLedId.Zone9].Color = Color.LawnGreen;
mousemat[CorsairMousematLedId.Zone10].Color = Color.LawnGreen;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone6].Color = Color.LawnGreen;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone7].Color = Color.LawnGreen;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone8].Color = Color.LawnGreen;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone9].Color = Color.LawnGreen;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone10].Color = Color.LawnGreen;
// Right
mousemat[CorsairMousematLedId.Zone11].Color = Color.Blue;
mousemat[CorsairMousematLedId.Zone12].Color = Color.Blue;
mousemat[CorsairMousematLedId.Zone13].Color = Color.Blue;
mousemat[CorsairMousematLedId.Zone14].Color = Color.Blue;
mousemat[CorsairMousematLedId.Zone15].Color = Color.Blue;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone11].Color = Color.Blue;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone12].Color = Color.Blue;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone13].Color = Color.Blue;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone14].Color = Color.Blue;
mousemat[(CorsairLedId)CorsairMousematLedId.Zone15].Color = Color.Blue;
// Random colors to show update rate
//foreach (var mousematLed in mousemat.Leds)
@ -111,7 +108,7 @@ namespace SimpleDevTest
//};
//keyboard.Brush = new SolidColorBrush(Color.Black);
//IKeyGroup group = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.F1, CorsairKeyboardKeyId.RightShift);
//ILedGroup group = new RectangleLedGroup(keyboard, CorsairKeyboardKeyId.F1, CorsairKeyboardKeyId.RightShift);
//group.Brush = new LinearGradientBrush(new RainbowGradient());
//bool tmp = false;
//while (true)
@ -126,7 +123,7 @@ namespace SimpleDevTest
//keyboard.Brush = new SolidColorBrush(Color.Aqua);
//keyboard.Update();
//IKeyGroup specialKeyGroup = new ListKeyGroup(keyboard, CorsairKeyboardKeyId.Brightness, CorsairKeyboardKeyId.WinLock);
//ILedGroup specialKeyGroup = new ListLedGroup(keyboard, CorsairKeyboardKeyId.Brightness, CorsairKeyboardKeyId.WinLock);
//specialKeyGroup.Brush = new SolidColorBrush(Color.Aqua);
//keyboard.Update();
@ -155,13 +152,13 @@ namespace SimpleDevTest
//Wait(3);
//ListKeyGroup keyGroup = new ListKeyGroup(keyboard, keyboard['R'].KeyId);
//keyGroup.Brush = new SolidColorBrush(Color.White);
//ListLedGroup ledGroup = new ListLedGroup(keyboard, keyboard['R'].KeyId);
//ledGroup.Brush = new SolidColorBrush(Color.White);
//keyboard.Update();
//Wait(2);
//keyGroup.RemoveKey(keyboard['R'].KeyId);
//ledGroup.RemoveKey(keyboard['R'].KeyId);
//keyboard['R'].Led.Color = Color.Black;
//keyGroup.AddKey(keyboard['T'].KeyId);
//ledGroup.AddKey(keyboard['T'].KeyId);
//keyboard.Update();
//Wait(10);
@ -173,7 +170,7 @@ namespace SimpleDevTest
//Console.WriteLine("Basic color-test ...");
//// Ink all numbers on the keypad except the '5' purple, we want that to be gray
//ListKeyGroup purpleGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3)
//ListLedGroup purpleGroup = new RectangleLedGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3)
//{ Brush = new SolidColorBrush(Color.Purple) }
//.Exclude(CorsairKeyboardKeyId.Keypad5);
//keyboard[CorsairKeyboardKeyId.Keypad5].Led.Color = Color.Gray;
@ -190,11 +187,11 @@ namespace SimpleDevTest
////keyboard['B'].Led.IsLocked = true;
//// Ink the letters of 'white' white
//ListKeyGroup whiteGroup = new ListKeyGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E)
//ListLedGroup whiteGroup = new ListLedGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E)
//{ Brush = new SolidColorBrush(Color.White) };
//// Ink the keys '1' to '0' yellow
//RectangleKeyGroup yellowGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.D1, CorsairKeyboardKeyId.D0)
//RectangleLedGroup yellowGroup = new RectangleLedGroup(keyboard, CorsairKeyboardKeyId.D1, CorsairKeyboardKeyId.D0)
//{ Brush = new SolidColorBrush(Color.Yellow) };
//// Update the keyboard to show the configured colors, (your CUE settings defines the rest)
@ -306,7 +303,7 @@ namespace SimpleDevTest
//RectangleF[] points = new RectangleF[NUM_POINTS];
//// KeyGroups which represents our point on the keyboard
//RectangleKeyGroup[] pointGroups = new RectangleKeyGroup[NUM_POINTS];
//RectangleLedGroup[] pointGroups = new RectangleLedGroup[NUM_POINTS];
//// Target of our movement
//PointF[] targets = new PointF[NUM_POINTS];
@ -316,7 +313,7 @@ namespace SimpleDevTest
//{
// // Spawn our point in the top-left corner (right over G1 or on ESC depending on your keyboard)
// points[i] = new RectangleF(keyboard.KeyboardRectangle.X, keyboard.KeyboardRectangle.Y, 60, 60);
// pointGroups[i] = new RectangleKeyGroup(keyboard, points[i], 0.1f) { Brush = new SolidColorBrush(Color.White) };
// pointGroups[i] = new RectangleLedGroup(keyboard, points[i], 0.1f) { Brush = new SolidColorBrush(Color.White) };
// targets[i] = new PointF(points[i].X, points[i].Y);
//}

View File

@ -1,29 +1,28 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using CUE.NET.Brushes;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Extensions;
using CUE.NET.Effects;
using CUE.NET.Groups.Extensions;
namespace CUE.NET.Devices.Keyboard.Keys
namespace CUE.NET.Groups
{
/// <summary>
/// Represents a basic keygroup.
/// </summary>
public abstract class AbstractKeyGroup : AbstractEffectTarget<IKeyGroup>, IKeyGroup
public abstract class AbstractLedGroup : AbstractEffectTarget<ILedGroup>, ILedGroup
{
#region Properties & Fields
/// <summary>
/// Gets the keyboard this keygroup belongs to.
/// Gets the strongly-typed target used for the effect.
/// </summary>
internal CorsairKeyboard Keyboard { get; }
protected override ILedGroup EffectTarget => this;
/// <summary>
/// Gets a read-only collection containing the keys from this group.
/// Gets the device this ledgroup belongs to.
/// </summary>
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GetGroupKeys());
public ICueDevice Device { get; }
/// <summary>
/// Gets or sets the brush which should be drawn over this group.
@ -40,13 +39,13 @@ namespace CUE.NET.Devices.Keyboard.Keys
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="AbstractKeyGroup"/> class.
/// Initializes a new instance of the <see cref="AbstractLedGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this group should be automatically attached or not.</param>
protected AbstractKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
protected AbstractLedGroup(ICueDevice device, bool autoAttach = true)
{
this.Keyboard = keyboard;
this.Device = device;
if (autoAttach)
this.Attach();
@ -56,16 +55,11 @@ namespace CUE.NET.Devices.Keyboard.Keys
#region Methods
public IEnumerable<CorsairLed> GetLeds()
{
return GetGroupKeys().Select(x => x.Led);
}
/// <summary>
/// Gets a list containing the keys from this group.
/// Gets a list containing all LEDs of this group.
/// </summary>
/// <returns>The list containing the keys.</returns>
protected abstract IList<CorsairKey> GetGroupKeys();
/// <returns>The list containing all LEDs of this group.</returns>
public abstract IEnumerable<CorsairLed> GetLeds();
#endregion
}

View File

@ -0,0 +1,79 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Groups.Extensions
{
/// <summary>
/// Offers some extensions and helper-methods for ledgroup related things.
/// </summary>
public static class LedGroupExtension
{
/// <summary>
/// Converts the given <see cref="AbstractLedGroup" /> to a <see cref="ListLedGroup" />.
/// </summary>
/// <param name="ledGroup">The <see cref="AbstractLedGroup" /> to convert.</param>
/// <returns>The converted <see cref="ListLedGroup" />.</returns>
public static ListLedGroup ToSimpleKeyGroup(this AbstractLedGroup ledGroup)
{
ListLedGroup simpleLedGroup = ledGroup as ListLedGroup;
if (simpleLedGroup == null)
{
bool wasAttached = ledGroup.Detach();
simpleLedGroup = new ListLedGroup(ledGroup.Device, wasAttached, ledGroup.GetLeds()) { Brush = ledGroup.Brush };
}
return simpleLedGroup;
}
/// <summary>
/// Returns a new <see cref="ListLedGroup" /> which contains all LEDs from the given ledgroup excluding the specified ones.
/// </summary>
/// <param name="ledGroup">The base ledgroup.</param>
/// <param name="ledIds">The ids of the LEDs to exclude.</param>
/// <returns>The new <see cref="ListLedGroup" />.</returns>
public static ListLedGroup Exclude(this AbstractLedGroup ledGroup, params CorsairLedId[] ledIds)
{
ListLedGroup simpleLedGroup = ledGroup.ToSimpleKeyGroup();
foreach (CorsairLedId ledId in ledIds)
simpleLedGroup.RemoveLed(ledId);
return simpleLedGroup;
}
/// <summary>
/// Returns a new <see cref="ListLedGroup" /> which contains all LEDs from the given ledgroup excluding the specified ones.
/// </summary>
/// <param name="ledGroup">The base ledgroup.</param>
/// <param name="ledIds">The LEDs to exclude.</param>
/// <returns>The new <see cref="ListLedGroup" />.</returns>
public static ListLedGroup Exclude(this AbstractLedGroup ledGroup, params CorsairLed[] ledIds)
{
ListLedGroup simpleLedGroup = ledGroup.ToSimpleKeyGroup();
foreach (CorsairLed led in ledIds)
simpleLedGroup.RemoveLed(led);
return simpleLedGroup;
}
// ReSharper disable once UnusedMethodReturnValue.Global
/// <summary>
/// Attaches the given ledgroup to the device.
/// </summary>
/// <param name="ledGroup">The ledgroup to attach.</param>
/// <returns><c>true</c> if the ledgroup could be attached; otherwise, <c>false</c>.</returns>
public static bool Attach(this AbstractLedGroup ledGroup)
{
return ledGroup.Device?.AttachLedGroup(ledGroup) ?? false;
}
/// <summary>
/// Detaches the given ledgroup from the device.
/// </summary>
/// <param name="ledGroup">The ledgroup to attach.</param>
/// <returns><c>true</c> if the ledgroup could be detached; otherwise, <c>false</c>.</returns>
public static bool Detach(this AbstractLedGroup ledGroup)
{
return ledGroup.Device?.DetachLedGroup(ledGroup) ?? false;
}
}
}

View File

@ -6,15 +6,10 @@ using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Effects;
namespace CUE.NET.Devices.Keyboard.Keys
namespace CUE.NET.Groups
{
public interface IKeyGroup : IEffectTarget<IKeyGroup>
public interface ILedGroup : IEffectTarget<ILedGroup>
{
/// <summary>
/// Gets a read-only collection containing the keys from this group.
/// </summary>
IEnumerable<CorsairKey> Keys { get; }
/// <summary>
/// Gets or sets the brush which should be drawn over this group.
/// </summary>

250
Groups/ListLedGroup.cs Normal file
View File

@ -0,0 +1,250 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Groups
{
/// <summary>
/// Represents a ledgroup containing arbitrary LEDs.
/// </summary>
public class ListLedGroup : AbstractLedGroup
{
#region Properties & Fields
protected override ILedGroup EffectTarget => this;
/// <summary>
/// Gets the list containing the LEDs of this ledgroup.
/// </summary>
protected IList<CorsairLed> GroupLeds { get; } = new List<CorsairLed>();
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this ledgroup should be automatically attached or not.</param>
public ListLedGroup(ICueDevice device, bool autoAttach = true)
: base(device, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="leds">The initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, params CorsairLed[] leds)
: this(device, true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="leds">The initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, IEnumerable<CorsairLed> leds)
: this(device, true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this ledgroup should be automatically attached or not.</param>
/// <param name="leds">The initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, bool autoAttach, IEnumerable<CorsairLed> leds)
: base(device, autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this ledgroup should be automatically attached or not.</param>
/// <param name="leds">The initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, bool autoAttach, params CorsairLed[] leds)
: base(device, autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="leds">The IDs of the initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, params CorsairLedId[] leds)
: this(device, true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="leds">The IDs of the initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, IEnumerable<CorsairLedId> leds)
: this(device, true, leds)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this ledgroup should be automatically attached or not.</param>
/// <param name="leds">The IDs of the initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, bool autoAttach, params CorsairLedId[] leds)
: base(device, autoAttach)
{
AddLeds(leds);
}
/// <summary>
/// Initializes a new instance of the <see cref="ListLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this ledgroup should be automatically attached or not.</param>
/// <param name="leds">The IDs of the initial LEDs of this ledgroup.</param>
public ListLedGroup(ICueDevice device, bool autoAttach, IEnumerable<CorsairLedId> leds)
: base(device, autoAttach)
{
AddLeds(leds);
}
#endregion
#region Methods
/// <summary>
/// Adds the given LED(s) to the ledgroup.
/// </summary>
/// <param name="LEDs">The LED(s) to add.</param>
public void AddLed(params CorsairLed[] LEDs)
{
AddLeds(LEDs);
}
/// <summary>
/// Adds the given LED(s) to the ledgroup.
/// </summary>
/// <param name="ledIds">The ID(s) of the LED(s) to add.</param>
public void AddLed(params CorsairLedId[] ledIds)
{
AddLeds(ledIds);
}
/// <summary>
/// Adds the given LEDs to the ledgroup.
/// </summary>
/// <param name="leds">The LEDs to add.</param>
public void AddLeds(IEnumerable<CorsairLed> leds)
{
if (leds != null)
foreach (CorsairLed led in leds)
if (led != null && !ContainsLed(led))
GroupLeds.Add(led);
}
/// <summary>
/// Adds the given LEDs to the ledgroup.
/// </summary>
/// <param name="ledIds">The IDs of the LEDs to add.</param>
public void AddLeds(IEnumerable<CorsairLedId> ledIds)
{
if (ledIds != null)
foreach (CorsairLedId ledId in ledIds)
AddLed(Device[ledId]);
}
/// <summary>
/// Removes the given LED(s) from the ledgroup.
/// </summary>
/// <param name="leds">The LED(s) to remove.</param>
public void RemoveLed(params CorsairLed[] leds)
{
RemoveLeds(leds);
}
/// <summary>
/// Removes the given LED(s) from the ledgroup.
/// </summary>
/// <param name="ledIds">The ID(s) of the LED(s) to remove.</param>
public void RemoveLed(params CorsairLedId[] ledIds)
{
RemoveLeds(ledIds);
}
/// <summary>
/// Removes the given LEDs from the ledgroup.
/// </summary>
/// <param name="leds">The LEDs to remove.</param>
public void RemoveLeds(IEnumerable<CorsairLed> leds)
{
if (leds != null)
foreach (CorsairLed led in leds)
if (led != null)
GroupLeds.Remove(led);
}
/// <summary>
/// Removes the given LEDs from the ledgroup.
/// </summary>
/// <param name="ledIds">The IDs of the LEDs to remove.</param>
public void RemoveLeds(IEnumerable<CorsairLedId> ledIds)
{
if (ledIds != null)
foreach (CorsairLedId ledId in ledIds)
RemoveLed(Device[ledId]);
}
/// <summary>
/// Checks if a given LED is contained by this ledgroup.
/// </summary>
/// <param name="led">The LED which should be checked.</param>
/// <returns><c>true</c> if the LED is contained by this ledgroup; otherwise, <c>false</c>.</returns>
public bool ContainsLed(CorsairLed led)
{
return led != null && GroupLeds.Contains(led);
}
/// <summary>
/// Checks if a given LED is contained by this ledgroup.
/// </summary>
/// <param name="ledId">The ID of the LED which should be checked.</param>
/// <returns><c>true</c> if the LED is contained by this ledgroup; otherwise, <c>false</c>.</returns>
public bool ContainsLed(CorsairLedId ledId)
{
return ContainsLed(Device[ledId]);
}
/// <summary>
/// Merges the LEDs from the given ledgroup in this ledgroup.
/// </summary>
/// <param name="groupToMerge">The ledgroup to merge.</param>
public void MergeLeds(ILedGroup groupToMerge)
{
foreach (CorsairLed led in groupToMerge.GetLeds())
if (!GroupLeds.Contains(led))
GroupLeds.Add(led);
}
/// <summary>
/// Gets a list containing the LEDs from this group.
/// </summary>
/// <returns>The list containing the LEDs.</returns>
public override IEnumerable<CorsairLed> GetLeds()
{
return GroupLeds;
}
#endregion
}
}

108
Groups/RectangleLedGroup.cs Normal file
View File

@ -0,0 +1,108 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Helper;
namespace CUE.NET.Groups
{
/// <summary>
/// Represents a ledgroup containing LEDs which physically lay inside a rectangle.
/// </summary>
public class RectangleLedGroup : AbstractLedGroup
{
#region Properties & Fields
private IList<CorsairLed> _ledCache;
private RectangleF _rectangle;
/// <summary>
/// Gets or sets the rectangle the LEDs should be taken from.
/// </summary>
public RectangleF Rectangle
{
get { return _rectangle; }
set
{
_rectangle = value;
_ledCache = null;
}
}
/// <summary>
/// Gets or sets the minimal percentage overlay a LED must have with the <see cref="Rectangle" /> to be taken into the ledgroup.
/// </summary>
public float MinOverlayPercentage { get; set; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="fromLed">They ID of the first LED to calculate the rectangle of this ledgroup from.</param>
/// <param name="toLed">They ID of the second LED to calculate the rectangle of this ledgroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a LED must have with the <see cref="Rectangle" /> to be taken into the ledgroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(ICueDevice device, CorsairLedId fromLed, CorsairLedId toLed, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(device, device[fromLed], device[toLed], minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="fromLed">They first LED to calculate the rectangle of this ledgroup from.</param>
/// <param name="toLed">They second LED to calculate the rectangle of this ledgroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a LED must have with the <see cref="Rectangle" /> to be taken into the ledgroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(ICueDevice device, CorsairLed fromLed, CorsairLed toLed, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(device, RectangleHelper.CreateRectangleFromRectangles(fromLed.LedRectangle, toLed.LedRectangle), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="fromPoint">They first point to calculate the rectangle of this ledgroup from.</param>
/// <param name="toPoint">They second point to calculate the rectangle of this ledgroup from.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a LED must have with the <see cref="Rectangle" /> to be taken into the ledgroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(ICueDevice device, PointF fromPoint, PointF toPoint, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: this(device, RectangleHelper.CreateRectangleFromPoints(fromPoint, toPoint), minOverlayPercentage, autoAttach)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="RectangleLedGroup"/> class.
/// </summary>
/// <param name="device">The device this ledgroup belongs to.</param>
/// <param name="rectangle">The rectangle of this ledgroup.</param>
/// <param name="minOverlayPercentage">(optional) The minimal percentage overlay a LED must have with the <see cref="Rectangle" /> to be taken into the ledgroup. (default: 0.5f)</param>
/// <param name="autoAttach">(optional) Specifies whether this group should be automatically attached or not. (default: true)</param>
public RectangleLedGroup(ICueDevice device, RectangleF rectangle, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: base(device, autoAttach)
{
this.Rectangle = rectangle;
this.MinOverlayPercentage = minOverlayPercentage;
}
#endregion
#region Methods
public override IEnumerable<CorsairLed> GetLeds()
{
return _ledCache ?? (_ledCache = Device.Where(x => RectangleHelper.CalculateIntersectPercentage(x.LedRectangle, Rectangle) >= MinOverlayPercentage).ToList());
}
#endregion
}
}

View File

@ -3,7 +3,7 @@
#pragma warning disable 649 // Field 'x' is never assigned
using System.Runtime.InteropServices;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Generic.Enums;
namespace CUE.NET.Native
{
@ -18,7 +18,7 @@ namespace CUE.NET.Native
/// <summary>
/// CUE-SDK: identifier of led
/// </summary>
internal CorsairKeyboardKeyId ledId;
internal CorsairLedId ledId;
/// <summary>
/// CUE-SDK: values in mm