mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2025-12-13 00:58:31 +00:00
Added reactangle of keyboard, improved example
This commit is contained in:
parent
3e238adcc4
commit
f3d498afcf
@ -2,10 +2,12 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using CUE.NET.Devices.Generic;
|
||||
using CUE.NET.Devices.Keyboard.Enums;
|
||||
using CUE.NET.Devices.Keyboard.Keys;
|
||||
using CUE.NET.Helper;
|
||||
using CUE.NET.Native;
|
||||
|
||||
namespace CUE.NET.Devices.Keyboard
|
||||
@ -16,6 +18,8 @@ namespace CUE.NET.Devices.Keyboard
|
||||
|
||||
public CorsairKeyboardDeviceInfo KeyboardDeviceInfo { get; }
|
||||
|
||||
public RectangleF KeyboardRectangle { get; private set; }
|
||||
|
||||
private Dictionary<CorsairKeyboardKeyId, CorsairKey> _keys = new Dictionary<CorsairKeyboardKeyId, CorsairKey>();
|
||||
public CorsairKey this[CorsairKeyboardKeyId keyId]
|
||||
{
|
||||
@ -27,6 +31,18 @@ namespace CUE.NET.Devices.Keyboard
|
||||
private set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public CorsairKey this[PointF location]
|
||||
{
|
||||
get { return _keys.Values.FirstOrDefault(x => x.KeyRectangle.Contains(location)); }
|
||||
private set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
public IEnumerable<CorsairKey> this[RectangleF referenceRect, float minOverlayPercentage = 0.5f]
|
||||
{
|
||||
get { return _keys.Values.Where(x => RectangleHelper.CalculateIntersectPercentage(x.KeyRectangle, referenceRect) >= minOverlayPercentage); }
|
||||
private set { throw new NotSupportedException(); }
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
@ -37,6 +53,7 @@ namespace CUE.NET.Devices.Keyboard
|
||||
this.KeyboardDeviceInfo = info;
|
||||
|
||||
InitializeKeys();
|
||||
CalculateKeyboardRectangle();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -58,6 +75,24 @@ namespace CUE.NET.Devices.Keyboard
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculateKeyboardRectangle()
|
||||
{
|
||||
float posX = float.MaxValue;
|
||||
float posY = float.MaxValue;
|
||||
float posX2 = float.MinValue;
|
||||
float posY2 = float.MinValue;
|
||||
|
||||
foreach (CorsairKey key in this)
|
||||
{
|
||||
posX = Math.Min(posX, key.KeyRectangle.X);
|
||||
posY = Math.Min(posY, key.KeyRectangle.Y);
|
||||
posX2 = Math.Max(posX2, key.KeyRectangle.X + key.KeyRectangle.Width);
|
||||
posY2 = Math.Max(posY2, key.KeyRectangle.Y + key.KeyRectangle.Height);
|
||||
}
|
||||
|
||||
KeyboardRectangle = RectangleHelper.CreateRectangleFromPoints(new PointF(posX, posY), new PointF(posX2, posY2));
|
||||
}
|
||||
|
||||
#region IEnumerable
|
||||
|
||||
public IEnumerator<CorsairKey> GetEnumerator()
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CUE.NET;
|
||||
using CUE.NET.Devices.Generic.Enums;
|
||||
using CUE.NET.Devices.Keyboard;
|
||||
@ -9,34 +12,104 @@ using CUE.NET.Exceptions;
|
||||
|
||||
namespace SimpleDevTest
|
||||
{
|
||||
class Program
|
||||
internal class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Press any key to exit ...");
|
||||
Task.Factory.StartNew(
|
||||
() =>
|
||||
{
|
||||
Console.ReadKey();
|
||||
Environment.Exit(0);
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
// Initialize CUE-SDK
|
||||
CueSDK.Initialize();
|
||||
|
||||
// Get connected keyboard or throw exception if there is no light controllable keyboard connected
|
||||
CorsairKeyboard keyboard = CueSDK.KeyboardSDK;
|
||||
if (keyboard == null)
|
||||
throw new WrapperException("No keyboard found");
|
||||
|
||||
//Ink all numbers on the keypad purple
|
||||
RectangleKeyGroup centerGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.Keypad7, CorsairKeyboardKeyId.Keypad3);
|
||||
centerGroup.SetColor(Color.Purple);
|
||||
|
||||
// Ink the Keys 'r', 'g', 'b' in their respective color
|
||||
keyboard[CorsairKeyboardKeyId.R].Led.Color = Color.Red;
|
||||
keyboard[CorsairKeyboardKeyId.G].Led.Color = Color.Green;
|
||||
keyboard[CorsairKeyboardKeyId.B].Led.Color = Color.Blue;
|
||||
|
||||
// Ink the letters of 'white' white
|
||||
SimpleKeyGroup whiteGroup = new SimpleKeyGroup(keyboard, CorsairKeyboardKeyId.W, CorsairKeyboardKeyId.H, CorsairKeyboardKeyId.I, CorsairKeyboardKeyId.T, CorsairKeyboardKeyId.E);
|
||||
whiteGroup.SetColor(Color.White);
|
||||
|
||||
// Ink the keys '1' to '0' yellow
|
||||
RectangleKeyGroup numberGroup = new RectangleKeyGroup(keyboard, CorsairKeyboardKeyId.D1, CorsairKeyboardKeyId.D0);
|
||||
numberGroup.SetColor(Color.Yellow);
|
||||
|
||||
// Update the keyboard to show the configured colors, the parameter 'true' overrides the whole keyboard (default: black),
|
||||
// 'false' (or nothing) overrides only changed keys (your CUE settings defines the rest) - this default behaviour might change soon
|
||||
keyboard.UpdateLeds(true);
|
||||
|
||||
Console.WriteLine(CueSDK.LastError);
|
||||
// Wait 5 sec
|
||||
for (int i = 5; i > 0; i--)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Now let us move a orange point random over the keyboard
|
||||
// Something like this could become some sort of effect
|
||||
|
||||
// Initialize needed stuff
|
||||
const float SPEED = 8f; // mm/tick
|
||||
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.UpdateLeds();
|
||||
Thread.Sleep(160);
|
||||
keyboardGroup.SetColor(Color.Black);
|
||||
keyboard.UpdateLeds();
|
||||
Thread.Sleep(200);
|
||||
}
|
||||
|
||||
// Spawn our point (rectangle since circles are too hard to calculate :p) in the top-left corner (right over G1 or on ESC depending on your keyboard)
|
||||
RectangleF point = new RectangleF(keyboard.KeyboardRectangle.X, keyboard.KeyboardRectangle.Y, 40, 40);
|
||||
// Target of our movement
|
||||
PointF target = new PointF(point.X, point.Y);
|
||||
while (true)
|
||||
{
|
||||
// Choose new target if we arrived
|
||||
if (point.Contains(target))
|
||||
target = new PointF((float)(keyboard.KeyboardRectangle.X + (random.NextDouble() * keyboard.KeyboardRectangle.Width)),
|
||||
(float)(keyboard.KeyboardRectangle.Y + (random.NextDouble() * keyboard.KeyboardRectangle.Height)));
|
||||
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);
|
||||
|
||||
IEnumerable<CorsairKey> keys = keyboard[point];
|
||||
if (keys != null)
|
||||
foreach (CorsairKey key in keys)
|
||||
key.Led.Color = Color.Orange;
|
||||
|
||||
keyboard.UpdateLeds();
|
||||
|
||||
// 20 updates per sec should be enought for what this
|
||||
Thread.Sleep(50);
|
||||
}
|
||||
}
|
||||
catch (CUEException ex)
|
||||
{
|
||||
@ -50,9 +123,19 @@ namespace SimpleDevTest
|
||||
{
|
||||
Console.WriteLine("Exception! Message:" + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("Press any key to exit ...");
|
||||
Console.ReadKey();
|
||||
private static PointF Interpolate(PointF p1, PointF p2, float length)
|
||||
{
|
||||
float distance = (float)(Math.Sqrt(Math.Pow(p2.X - p1.X, 2) + Math.Pow(p2.Y - p1.Y, 2)));
|
||||
if (distance > length)
|
||||
{
|
||||
float t = length / distance;
|
||||
float xt = (1 - t) * p1.X + (t * p2.X);
|
||||
float yt = (1 - t) * p1.Y + (t * p2.Y);
|
||||
return new PointF(xt, yt);
|
||||
}
|
||||
return p2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user