// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Effects;
using CUE.NET.Groups;
using CUE.NET.Helper;
using CUE.NET.Native;
namespace CUE.NET.Devices.Keyboard
{
///
/// Represents the SDK for a corsair keyboard.
///
public class CorsairKeyboard : AbstractCueDevice, IEnumerable
{
#region Properties & Fields
#region Indexer
///
/// Gets the with the specified ID.
///
/// The ID of the key to get.
/// The key with the specified ID or null if no key is found.
public CorsairKey this[CorsairKeyboardKeyId keyId]
{
get
{
CorsairKey key;
return _keys.TryGetValue(keyId, out key) ? key : null;
}
}
///
/// Gets the representing the given character by calling the SDK-method 'CorsairGetLedIdForKeyName'.
/// Note that this currently only works for letters.
///
/// The character of the key.
/// The key representing the given character or null if no key is found.
public CorsairKey this[char key]
{
get
{
CorsairKeyboardKeyId keyId = _CUESDK.CorsairGetLedIdForKeyName(key);
CorsairKey cKey;
return _keys.TryGetValue(keyId, out cKey) ? cKey : null;
}
}
///
/// Gets the at the given physical location.
///
/// The point to get the key from.
/// The key at the given point or null if no key is found.
public new CorsairKey this[PointF location] => _keys.Values.FirstOrDefault(x => x.Led.LedRectangle.Contains(location));
///
/// Gets a list of inside the given rectangle.
///
/// The rectangle to check.
/// The minimal percentage overlay a key must have with the to be taken into the list.
///
public new IEnumerable this[RectangleF referenceRect, float minOverlayPercentage = 0.5f] => _keys.Values
.Where(x => RectangleHelper.CalculateIntersectPercentage(x.Led.LedRectangle, referenceRect) >= minOverlayPercentage);
#endregion
private Dictionary _keys = new Dictionary();
///
/// Gets a read-only collection containing the keys of the keyboard.
///
public IEnumerable Keys => new ReadOnlyCollection(_keys.Values.ToList());
///
/// Gets specific information provided by CUE for the keyboard.
///
public CorsairKeyboardDeviceInfo KeyboardDeviceInfo { get; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The specific information provided by CUE for the keyboard
internal CorsairKeyboard(CorsairKeyboardDeviceInfo info)
: base(info)
{
this.KeyboardDeviceInfo = info;
}
#endregion
#region Methods
protected override void InitializeLeds()
{
_CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositions(), typeof(_CorsairLedPositions));
int structSize = Marshal.SizeOf(typeof(_CorsairLedPosition));
IntPtr ptr = nativeLedPositions.pLedPosition;
for (int i = 0; i < nativeLedPositions.numberOfLed; i++)
{
_CorsairLedPosition ledPosition = (_CorsairLedPosition)Marshal.PtrToStructure(ptr, typeof(_CorsairLedPosition));
CorsairLed led = InitializeLed(ledPosition.ledId, new RectangleF((float)ledPosition.left, (float)ledPosition.top, (float)ledPosition.width, (float)ledPosition.height));
_keys.Add((CorsairKeyboardKeyId)ledPosition.ledId, new CorsairKey((CorsairKeyboardKeyId)ledPosition.ledId, led));
ptr = new IntPtr(ptr.ToInt64() + structSize);
}
}
#region IEnumerable
///
/// Returns an enumerator that iterates over all keys of the keyboard.
///
/// An enumerator for all keys of the keyboard.
public new IEnumerator GetEnumerator()
{
return _keys.Values.GetEnumerator();
}
///
/// Returns an enumerator that iterates over all keys of the keyboard.
///
/// An enumerator for all keys of the keyboard.
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#endregion
}
}