From 5d96a1ba1146982a6f99c50354336d22c54842b7 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 20 Sep 2015 09:11:22 +0200 Subject: [PATCH] Implemented keyboard as key-group --- Devices/Keyboard/CorsairKeyboard.cs | 11 ++++++++++- Devices/Keyboard/Keys/BaseKeyGroup.cs | 10 ++++++---- Devices/Keyboard/Keys/IKeyGroup.cs | 5 +---- Devices/Keyboard/Keys/RectangleKeyGroup.cs | 2 +- Devices/Keyboard/Keys/SimpleKeyGroup.cs | 6 +++--- Examples/SimpleDevTest/Program.cs | 7 +++---- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Devices/Keyboard/CorsairKeyboard.cs b/Devices/Keyboard/CorsairKeyboard.cs index e145db7..6630f1f 100644 --- a/Devices/Keyboard/CorsairKeyboard.cs +++ b/Devices/Keyboard/CorsairKeyboard.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; @@ -12,7 +13,7 @@ using CUE.NET.Native; namespace CUE.NET.Devices.Keyboard { - public class CorsairKeyboard : AbstractCueDevice, IEnumerable + public class CorsairKeyboard : AbstractCueDevice, IEnumerable, IKeyGroup { #region Properties & Fields @@ -43,6 +44,8 @@ namespace CUE.NET.Devices.Keyboard private set { throw new NotSupportedException(); } } + public IEnumerable Keys => new ReadOnlyCollection(_keys.Values.ToList()); + #endregion #region Constructors @@ -60,6 +63,12 @@ namespace CUE.NET.Devices.Keyboard #region Methods + public void SetColor(Color color) + { + foreach (CorsairKey key in this) + key.Led.Color = color; + } + private void InitializeKeys() { _CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositions(), typeof(_CorsairLedPositions)); diff --git a/Devices/Keyboard/Keys/BaseKeyGroup.cs b/Devices/Keyboard/Keys/BaseKeyGroup.cs index 747ccb9..7ba3462 100644 --- a/Devices/Keyboard/Keys/BaseKeyGroup.cs +++ b/Devices/Keyboard/Keys/BaseKeyGroup.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Drawing; namespace CUE.NET.Devices.Keyboard.Keys @@ -9,7 +10,8 @@ namespace CUE.NET.Devices.Keyboard.Keys protected CorsairKeyboard Keyboard { get; } - public IList Keys { get; } = new List(); + public IEnumerable Keys => new ReadOnlyCollection(GroupKeys); + protected IList GroupKeys { get; } = new List(); #endregion @@ -26,15 +28,15 @@ namespace CUE.NET.Devices.Keyboard.Keys public virtual void SetColor(Color color) { - foreach (CorsairKey key in Keys) + foreach (CorsairKey key in GroupKeys) key.Led.Color = color; } public void MergeKeys(IKeyGroup groupToMerge) { foreach (CorsairKey key in groupToMerge.Keys) - if (!Keys.Contains(key)) - Keys.Add(key); + if (!GroupKeys.Contains(key)) + GroupKeys.Add(key); } #endregion diff --git a/Devices/Keyboard/Keys/IKeyGroup.cs b/Devices/Keyboard/Keys/IKeyGroup.cs index e78c9ee..1c5d354 100644 --- a/Devices/Keyboard/Keys/IKeyGroup.cs +++ b/Devices/Keyboard/Keys/IKeyGroup.cs @@ -5,11 +5,8 @@ namespace CUE.NET.Devices.Keyboard.Keys { public interface IKeyGroup { - //TODO DarthAffe 19.09.2015: This might be not needed/bad - IList Keys { get; } + IEnumerable Keys { get; } void SetColor(Color color); - - void MergeKeys(IKeyGroup groupToMerge); } } diff --git a/Devices/Keyboard/Keys/RectangleKeyGroup.cs b/Devices/Keyboard/Keys/RectangleKeyGroup.cs index 41e4dc9..7c2635c 100644 --- a/Devices/Keyboard/Keys/RectangleKeyGroup.cs +++ b/Devices/Keyboard/Keys/RectangleKeyGroup.cs @@ -35,7 +35,7 @@ namespace CUE.NET.Devices.Keyboard.Keys this.MinOverlayPercentage = minOverlayPercentage; foreach (CorsairKey key in Keyboard.Where(x => RectangleHelper.CalculateIntersectPercentage(x.KeyRectangle, requestedRectangle) >= minOverlayPercentage)) - Keys.Add(key); + GroupKeys.Add(key); } #endregion diff --git a/Devices/Keyboard/Keys/SimpleKeyGroup.cs b/Devices/Keyboard/Keys/SimpleKeyGroup.cs index 6706544..eb1ba84 100644 --- a/Devices/Keyboard/Keys/SimpleKeyGroup.cs +++ b/Devices/Keyboard/Keys/SimpleKeyGroup.cs @@ -31,7 +31,7 @@ namespace CUE.NET.Devices.Keyboard.Keys if (keys != null) foreach (CorsairKey key in keys) if (key != null && !ContainsKey(key)) - Keys.Add(key); + GroupKeys.Add(key); } @@ -47,7 +47,7 @@ namespace CUE.NET.Devices.Keyboard.Keys if (keys != null) foreach (CorsairKey key in keys) if (key != null) - Keys.Remove(key); + GroupKeys.Remove(key); } public void RemoveKey(params CorsairKeyboardKeyId[] keyIds) @@ -59,7 +59,7 @@ namespace CUE.NET.Devices.Keyboard.Keys public bool ContainsKey(CorsairKey key) { - return key != null && Keys.Contains(key); + return key != null && GroupKeys.Contains(key); } public bool ContainsKey(CorsairKeyboardKeyId keyId) diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index 41fef15..22afb93 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -72,15 +72,14 @@ namespace SimpleDevTest Random random = new Random(); // Cover whole keyboard as Group to be able to reset (I'll fix this tomorrow) - RectangleKeyGroup keyboardGroup = new RectangleKeyGroup(keyboard, keyboard.KeyboardRectangle, 0f); // Flash whole keyboard three times to ... well ... just to make it happen for (int i = 0; i < 3; i++) { - keyboardGroup.SetColor(Color.Aquamarine); + keyboard.SetColor(Color.Aquamarine); keyboard.UpdateLeds(); Thread.Sleep(160); - keyboardGroup.SetColor(Color.Black); + keyboard.SetColor(Color.Black); keyboard.UpdateLeds(); Thread.Sleep(200); } @@ -98,7 +97,7 @@ namespace SimpleDevTest else point.Location = Interpolate(point.Location, target, SPEED); // It would be better to calculate from the center of our rectangle but the easy way is enough here - keyboardGroup.SetColor(Color.Black); + keyboard.SetColor(Color.Black); IEnumerable keys = keyboard[point, 0.1f]; if (keys != null)