mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2026-11-11 04:56:44 +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:
1 parent
bfe51add66
commit
5a018d0743
24 files changed
+847
-894
No files matched your search
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using CUE.NET.Brushes;
|
||||
using CUE.NET.Devices.Generic;
|
||||
using CUE.NET.Devices.Keyboard.Extensions;
|
||||
using CUE.NET.Effects;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Keys
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a basic keygroup.
|
||||
/// </summary>
|
||||
public abstract class AbstractKeyGroup : AbstractEffectTarget<IKeyGroup>, IKeyGroup
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
/// <summary>
|
||||
/// Gets the keyboard this keygroup belongs to.
|
||||
/// </summary>
|
||||
internal CorsairKeyboard Keyboard { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a read-only collection containing the keys from this group.
|
||||
/// </summary>
|
||||
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GetGroupKeys());
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the brush which should be drawn over this group.
|
||||
/// </summary>
|
||||
public IBrush Brush { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the z-index of this keygroup to allow ordering them before drawing. (lowest first) (default: 0)
|
||||
/// </summary>
|
||||
public int ZIndex { get; set; } = 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AbstractKeyGroup"/> class.
|
||||
/// </summary>
|
||||
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
|
||||
/// <param name="autoAttach">Specifies whether this group should be automatically attached or not.</param>
|
||||
protected AbstractKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
|
||||
{
|
||||
this.Keyboard = keyboard;
|
||||
|
||||
if (autoAttach)
|
||||
this.Attach();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public IEnumerable<CorsairLed> GetLeds()
|
||||
{
|
||||
return GetGroupKeys().Select(x => x.Led);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list containing the keys from this group.
|
||||
/// </summary>
|
||||
/// <returns>The list containing the keys.</returns>
|
||||
protected abstract IList<CorsairKey> GetGroupKeys();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// ReSharper disable UnusedMemberInSuper.Global
|
||||
// ReSharper disable UnusedMember.Global
|
||||
|
||||
using System.Collections.Generic;
|
||||
using CUE.NET.Brushes;
|
||||
using CUE.NET.Devices.Generic;
|
||||
using CUE.NET.Effects;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Keys
|
||||
{
|
||||
public interface IKeyGroup : IEffectTarget<IKeyGroup>
|
||||
{
|
||||
/// <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>
|
||||
IBrush Brush { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the z-index of this keygroup to allow ordering them before drawing. (lowest first) (default: 0)
|
||||
/// </summary>
|
||||
int ZIndex { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a list containing all LEDs of this group.
|
||||
/// </summary>
|
||||
/// <returns>The list containing all LEDs of this group.</returns>
|
||||
IEnumerable<CorsairLed> GetLeds();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user