From 3e238adcc4a134f38f28441b6ea978bd14136bdc Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 19 Sep 2015 20:22:40 +0200 Subject: [PATCH] Added rectangle KeyGroup --- CUE.NET.csproj | 2 + Devices/Keyboard/CorsairKeyboard.cs | 19 ++++++++- Devices/Keyboard/Keys/RectangleKeyGroup.cs | 47 ++++++++++++++++++++++ Examples/SimpleDevTest/Program.cs | 8 +++- Helper/RectangleHelper.cs | 36 +++++++++++++++++ 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 Devices/Keyboard/Keys/RectangleKeyGroup.cs create mode 100644 Helper/RectangleHelper.cs diff --git a/CUE.NET.csproj b/CUE.NET.csproj index 28b2645..5047d45 100644 --- a/CUE.NET.csproj +++ b/CUE.NET.csproj @@ -69,6 +69,7 @@ + @@ -84,6 +85,7 @@ + diff --git a/Devices/Keyboard/CorsairKeyboard.cs b/Devices/Keyboard/CorsairKeyboard.cs index 816042b..2e681d0 100644 --- a/Devices/Keyboard/CorsairKeyboard.cs +++ b/Devices/Keyboard/CorsairKeyboard.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Runtime.InteropServices; @@ -9,7 +10,7 @@ using CUE.NET.Native; namespace CUE.NET.Devices.Keyboard { - public class CorsairKeyboard : AbstractCueDevice + public class CorsairKeyboard : AbstractCueDevice, IEnumerable { #region Properties & Fields @@ -52,11 +53,25 @@ namespace CUE.NET.Devices.Keyboard _CorsairLedPosition ledPosition = Marshal.PtrToStructure<_CorsairLedPosition>(ptr); _keys.Add(ledPosition.ledId, new CorsairKey(ledPosition.ledId, GetLed((int)ledPosition.ledId), //TODO DarthAffe 19.09.2015: Is something like RectangleD needed? I don't think so ... - new RectangleF((float)ledPosition.top, (float)ledPosition.left, (float)ledPosition.width, (float)ledPosition.height))); + new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height))); ptr = new IntPtr(ptr.ToInt64() + structSize); } } + #region IEnumerable + + public IEnumerator GetEnumerator() + { + return _keys.Values.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + #endregion } } diff --git a/Devices/Keyboard/Keys/RectangleKeyGroup.cs b/Devices/Keyboard/Keys/RectangleKeyGroup.cs new file mode 100644 index 0000000..41e4dc9 --- /dev/null +++ b/Devices/Keyboard/Keys/RectangleKeyGroup.cs @@ -0,0 +1,47 @@ +using System.Drawing; +using System.Linq; +using CUE.NET.Devices.Keyboard.Enums; +using CUE.NET.Helper; + +namespace CUE.NET.Devices.Keyboard.Keys +{ + public class RectangleKeyGroup : BaseKeyGroup + { + #region Properties & Fields + + public RectangleF RequestedRectangle { get; } + public float MinOverlayPercentage { get; } + + #endregion + + #region Constructors + + public RectangleKeyGroup(CorsairKeyboard keyboard, CorsairKeyboardKeyId fromKey, CorsairKeyboardKeyId toKey, float minOverlayPercentage = 0.5f) + : this(keyboard, keyboard[fromKey], keyboard[toKey], minOverlayPercentage) + { } + + public RectangleKeyGroup(CorsairKeyboard keyboard, CorsairKey fromKey, CorsairKey toKey, float minOverlayPercentage = 0.5f) + : this(keyboard, RectangleHelper.CreateRectangleFromRectangles(fromKey.KeyRectangle, toKey.KeyRectangle), minOverlayPercentage) + { } + + public RectangleKeyGroup(CorsairKeyboard keyboard, PointF fromPoint, PointF toPoint, float minOverlayPercentage = 0.5f) + : this(keyboard, RectangleHelper.CreateRectangleFromPoints(fromPoint, toPoint), minOverlayPercentage) + { } + + public RectangleKeyGroup(CorsairKeyboard keyboard, RectangleF requestedRectangle, float minOverlayPercentage = 0.5f) + : base(keyboard) + { + this.RequestedRectangle = requestedRectangle; + this.MinOverlayPercentage = minOverlayPercentage; + + foreach (CorsairKey key in Keyboard.Where(x => RectangleHelper.CalculateIntersectPercentage(x.KeyRectangle, requestedRectangle) >= minOverlayPercentage)) + Keys.Add(key); + } + + #endregion + + #region Methods + + #endregion + } +} diff --git a/Examples/SimpleDevTest/Program.cs b/Examples/SimpleDevTest/Program.cs index c810842..43690d4 100644 --- a/Examples/SimpleDevTest/Program.cs +++ b/Examples/SimpleDevTest/Program.cs @@ -21,6 +21,9 @@ namespace SimpleDevTest if (keyboard == null) throw new WrapperException("No keyboard found"); + RectangleKeyGroup centerGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3); + centerGroup.SetColor(Color.Purple); + keyboard[CorsairKeyboardKeyId.R].Led.Color = Color.Red; keyboard[CorsairKeyboardKeyId.G].Led.Color = Color.Green; keyboard[CorsairKeyboardKeyId.B].Led.Color = Color.Blue; @@ -28,7 +31,10 @@ namespace SimpleDevTest SimpleKeyGroup whiteGroup = new SimpleKeyGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E); whiteGroup.SetColor(Color.White); - keyboard.UpdateLeds(); + RectangleKeyGroup numberGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.D1, CorsairKeyboardKeyId.D0); + numberGroup.SetColor(Color.Yellow); + + keyboard.UpdateLeds(true); Console.WriteLine(CueSDK.LastError); } diff --git a/Helper/RectangleHelper.cs b/Helper/RectangleHelper.cs new file mode 100644 index 0000000..f7c0027 --- /dev/null +++ b/Helper/RectangleHelper.cs @@ -0,0 +1,36 @@ +using System; +using System.Drawing; + +namespace CUE.NET.Helper +{ + public static class RectangleHelper + { + public static RectangleF CreateRectangleFromPoints(PointF point1, PointF point2) + { + float posX = Math.Min(point1.X, point2.X); + float posY = Math.Min(point1.Y, point2.Y); + float width = Math.Max(point1.X, point2.X) - posX; + float height = Math.Max(point1.Y, point2.Y) - posY; + + return new RectangleF(posX, posY, width, height); + } + + public static RectangleF CreateRectangleFromRectangles(RectangleF point1, RectangleF point2) + { + float posX = Math.Min(point1.X, point2.X); + float posY = Math.Min(point1.Y, point2.Y); + float width = Math.Max(point1.X + point1.Width, point2.X + point2.Width) - posX; + float height = Math.Max(point1.Y + point1.Height, point2.Y + point2.Height) - posY; + + return new RectangleF(posX, posY, width, height); + } + + public static float CalculateIntersectPercentage(RectangleF rect, RectangleF referenceRect) + { + if (rect.IsEmpty || referenceRect.IsEmpty) return 0; + + referenceRect.Intersect(rect); // replace referenceRect with intersect + return referenceRect.IsEmpty ? 0 : (referenceRect.Width * referenceRect.Height) / (rect.Width * rect.Height); + } + } +}