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

Added rectangle KeyGroup

This commit is contained in:
unknown 2015-09-19 20:22:40 +02:00
parent 64e0902046
commit 3e238adcc4
5 changed files with 109 additions and 3 deletions

View File

@ -69,6 +69,7 @@
<Compile Include="Devices\Keyboard\Enums\CorsairPhysicalKeyboardLayout.cs" />
<Compile Include="Devices\Keyboard\Keys\BaseKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\IKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\RectangleKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\SimpleKeyGroup.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairMouseButtonId.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairPhysicalMouseLayout.cs" />
@ -84,6 +85,7 @@
<Compile Include="Devices\Keyboard\CorsairKeyboard.cs" />
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
<Compile Include="Devices\Mouse\CorsairMouse.cs" />
<Compile Include="Helper\RectangleHelper.cs" />
<Compile Include="Native\_CorsairDeviceInfo.cs" />
<Compile Include="Native\_CorsairLedColor.cs" />
<Compile Include="Native\_CorsairLedPosition.cs" />

View File

@ -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<CorsairKey>
{
#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<CorsairKey> GetEnumerator()
{
return _keys.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}

View File

@ -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
}
}

View File

@ -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);
}

36
Helper/RectangleHelper.cs Normal file
View File

@ -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);
}
}
}