mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2026-11-11 13:06:24 +00:00
Added some extensions for KeyGroups
This commit is contained in:
1 parent
4e1a1d11bd
commit
ced6c299b0
6 files changed
+86
-30
No files matched your search
@@ -51,7 +51,7 @@ namespace CUE.NET.Devices.Keyboard
|
||||
}
|
||||
|
||||
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(_keys.Values.ToList());
|
||||
|
||||
|
||||
public Color Color { get; set; } = Color.Transparent;
|
||||
|
||||
private readonly IList<IKeyGroup> _keyGroups = new List<IKeyGroup>();
|
||||
@@ -92,19 +92,20 @@ namespace CUE.NET.Devices.Keyboard
|
||||
base.UpdateLeds(forceUpdate);
|
||||
}
|
||||
|
||||
public void AttachKeyGroup(IKeyGroup keyGroup)
|
||||
public bool AttachKeyGroup(IKeyGroup keyGroup)
|
||||
{
|
||||
if (keyGroup == null) return;
|
||||
if (keyGroup == null || _keyGroups.Contains(keyGroup)) return false;
|
||||
|
||||
if (!_keyGroups.Contains(keyGroup))
|
||||
_keyGroups.Add(keyGroup);
|
||||
_keyGroups.Add(keyGroup);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void DetachKeyGroup(IKeyGroup keyGroup)
|
||||
public bool DetachKeyGroup(IKeyGroup keyGroup)
|
||||
{
|
||||
if (keyGroup == null) return;
|
||||
if (keyGroup == null || !_keyGroups.Contains(keyGroup)) return false;
|
||||
|
||||
_keyGroups.Remove(keyGroup);
|
||||
return true;
|
||||
}
|
||||
|
||||
private void InitializeKeys()
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.Linq;
|
||||
using CUE.NET.Devices.Keyboard.Enums;
|
||||
using CUE.NET.Devices.Keyboard.Keys;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Extensions
|
||||
{
|
||||
public static class KeyGroupExtension
|
||||
{
|
||||
public static SimpleKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup)
|
||||
{
|
||||
SimpleKeyGroup simpleKeyGroup = keyGroup as SimpleKeyGroup;
|
||||
if (simpleKeyGroup == null)
|
||||
{
|
||||
bool wasAttached = keyGroup.Detach();
|
||||
simpleKeyGroup = new SimpleKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Color = keyGroup.Color };
|
||||
}
|
||||
return simpleKeyGroup;
|
||||
}
|
||||
|
||||
public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
|
||||
{
|
||||
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
|
||||
foreach (CorsairKeyboardKeyId keyId in keyIds)
|
||||
simpleKeyGroup.RemoveKey(keyId);
|
||||
return simpleKeyGroup;
|
||||
}
|
||||
|
||||
public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds)
|
||||
{
|
||||
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
|
||||
foreach (CorsairKey key in keyIds)
|
||||
simpleKeyGroup.RemoveKey(key);
|
||||
return simpleKeyGroup;
|
||||
}
|
||||
|
||||
public static bool Attach(this BaseKeyGroup keyGroup)
|
||||
{
|
||||
return keyGroup.Keyboard?.AttachKeyGroup(keyGroup) ?? false;
|
||||
}
|
||||
|
||||
public static bool Detach(this BaseKeyGroup keyGroup)
|
||||
{
|
||||
return keyGroup.Keyboard?.DetachKeyGroup(keyGroup) ?? false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Drawing;
|
||||
using CUE.NET.Devices.Keyboard.Extensions;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard.Keys
|
||||
{
|
||||
@@ -8,7 +9,7 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
||||
{
|
||||
#region Properties & Fields
|
||||
|
||||
protected CorsairKeyboard Keyboard { get; }
|
||||
internal CorsairKeyboard Keyboard { get; }
|
||||
|
||||
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GroupKeys);
|
||||
protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();
|
||||
@@ -24,18 +25,7 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
||||
this.Keyboard = keyboard;
|
||||
|
||||
if (autoAttach)
|
||||
keyboard.AttachKeyGroup(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
public void MergeKeys(IKeyGroup groupToMerge)
|
||||
{
|
||||
foreach (CorsairKey key in groupToMerge.Keys)
|
||||
if (!GroupKeys.Contains(key))
|
||||
GroupKeys.Add(key);
|
||||
this.Attach();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -6,18 +6,26 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard)
|
||||
: base(keyboard)
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
|
||||
: base(keyboard, autoAttach)
|
||||
{ }
|
||||
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
|
||||
: base(keyboard)
|
||||
: this(keyboard, true, keys)
|
||||
{ }
|
||||
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKey[] keys)
|
||||
: base(keyboard, autoAttach)
|
||||
{
|
||||
AddKey(keys);
|
||||
}
|
||||
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
|
||||
: base(keyboard)
|
||||
: this(keyboard, true, keys)
|
||||
{ }
|
||||
|
||||
public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKeyboardKeyId[] keys)
|
||||
: base(keyboard, autoAttach)
|
||||
{
|
||||
AddKey(keys);
|
||||
}
|
||||
@@ -67,6 +75,13 @@ namespace CUE.NET.Devices.Keyboard.Keys
|
||||
return ContainsKey(Keyboard[keyId]);
|
||||
}
|
||||
|
||||
public void MergeKeys(IKeyGroup groupToMerge)
|
||||
{
|
||||
foreach (CorsairKey key in groupToMerge.Keys)
|
||||
if (!GroupKeys.Contains(key))
|
||||
GroupKeys.Add(key);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user