Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Merge basic brush concept (ongoing work) and color-blending #7

Merged
merged 8 commits into from
Sep 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CUE.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@
<Compile Include="Devices\Generic\Enums\CorsairAccessMode.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceCaps.cs" />
<Compile Include="Devices\Generic\Enums\CorsairDeviceType.cs" />
<Compile Include="Devices\Keyboard\ColorBrushes\IBrush.cs" />
<Compile Include="Devices\Keyboard\ColorBrushes\SolidColorBrush.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairLogicalKeyboardLayout.cs" />
<Compile Include="Devices\Headset\Enums\CorsairHeadsetLedId.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
<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\Keyboard\Keys\ListKeyGroup.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairMouseButtonId.cs" />
<Compile Include="Devices\Mouse\Enums\CorsairPhysicalMouseLayout.cs" />
<Compile Include="Exceptions\CUEException.cs" />
Expand All @@ -86,6 +88,7 @@
<Compile Include="Devices\Mouse\CorsairMouseDeviceInfo.cs" />
<Compile Include="Devices\Mouse\CorsairMouse.cs" />
<Compile Include="Devices\Keyboard\Extensions\KeyGroupExtension.cs" />
<Compile Include="Helper\ColorHelper.cs" />
<Compile Include="Helper\RectangleHelper.cs" />
<Compile Include="Native\_CorsairDeviceInfo.cs" />
<Compile Include="Native\_CorsairLedColor.cs" />
Expand Down
3 changes: 2 additions & 1 deletion Devices/Generic/CorsairLed.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using CUE.NET.Helper;

namespace CUE.NET.Devices.Generic
{
Expand All @@ -19,7 +20,7 @@ public Color Color
{
if (!IsLocked)
{
RequestedColor = value;
RequestedColor = RequestedColor.Blend(value);
IsUpdated = true;
}
}
Expand Down
9 changes: 9 additions & 0 deletions Devices/Keyboard/ColorBrushes/IBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace CUE.NET.Devices.Keyboard.ColorBrushes
{
public interface IBrush
{
Color GetColorAtPoint(RectangleF rectangle, PointF point);
}
}
31 changes: 31 additions & 0 deletions Devices/Keyboard/ColorBrushes/SolidColorBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Drawing;

namespace CUE.NET.Devices.Keyboard.ColorBrushes
{
public class SolidColorBrush : IBrush
{
#region Properties & Fields

public Color Color { get; set; }

#endregion

#region Constructors

public SolidColorBrush(Color color)
{
this.Color = color;
}

#endregion

#region Methods

public Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
return Color; // A solid color brush returns the same color no matter the point
}

#endregion
}
}
42 changes: 14 additions & 28 deletions Devices/Keyboard/CorsairKeyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.ColorBrushes;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Helper;
Expand Down Expand Up @@ -52,7 +53,7 @@ public CorsairKey this[PointF location]

public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(_keys.Values.ToList());

public Color Color { get; set; } = Color.Transparent;
public IBrush Brush { get; set; }

private readonly IList<IKeyGroup> _keyGroups = new List<IKeyGroup>();

Expand All @@ -66,7 +67,7 @@ internal CorsairKeyboard(CorsairKeyboardDeviceInfo info)
this.KeyboardDeviceInfo = info;

InitializeKeys();
CalculateKeyboardRectangle();
KeyboardRectangle = RectangleHelper.CreateRectangleFromRectangles(this.Select(x => x.KeyRectangle));
}

#endregion
Expand All @@ -75,23 +76,26 @@ internal CorsairKeyboard(CorsairKeyboardDeviceInfo info)

public override void UpdateLeds(bool forceUpdate = false)
{
// Apply all KeyGroups first
// Update only 'clean' leds, manual set should always override groups
IEnumerable<CorsairKey> cleanKeys = this.Where(x => !x.Led.IsUpdated).ToList();
// Apply all KeyGroups

if (Color != Color.Transparent)
foreach (CorsairKey key in cleanKeys)
key.Led.Color = Color;
if (Brush != null)
ApplyBrush(this.ToList(), Brush);

//TODO DarthAffe 20.09.2015: Add some sort of priority
foreach (IKeyGroup keyGroup in _keyGroups)
foreach (CorsairKey key in keyGroup.Keys.Where(key => cleanKeys.Contains(key)))
key.Led.Color = keyGroup.Color;
ApplyBrush(keyGroup.Keys.ToList(), keyGroup.Brush);

// Perform 'real' update
base.UpdateLeds(forceUpdate);
}

private void ApplyBrush(ICollection<CorsairKey> keys, IBrush brush)
{
RectangleF brushRectangle = RectangleHelper.CreateRectangleFromRectangles(keys.Select(x => x.KeyRectangle));
foreach (CorsairKey key in keys)
key.Led.Color = brush.GetColorAtPoint(brushRectangle, key.KeyRectangle.GetCenter());
}

public bool AttachKeyGroup(IKeyGroup keyGroup)
{
if (keyGroup == null || _keyGroups.Contains(keyGroup)) return false;
Expand Down Expand Up @@ -124,24 +128,6 @@ private void InitializeKeys()
}
}

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()
Expand Down
14 changes: 7 additions & 7 deletions Devices/Keyboard/Extensions/KeyGroupExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ namespace CUE.NET.Devices.Keyboard.Extensions
{
public static class KeyGroupExtension
{
public static SimpleKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup)
public static ListKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup)
{
SimpleKeyGroup simpleKeyGroup = keyGroup as SimpleKeyGroup;
ListKeyGroup simpleKeyGroup = keyGroup as ListKeyGroup;
if (simpleKeyGroup == null)
{
bool wasAttached = keyGroup.Detach();
simpleKeyGroup = new SimpleKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Color = keyGroup.Color };
simpleKeyGroup = new ListKeyGroup(keyGroup.Keyboard, wasAttached, keyGroup.Keys.ToArray()) { Brush = keyGroup.Brush };
}
return simpleKeyGroup;
}

public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
{
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKeyboardKeyId keyId in keyIds)
simpleKeyGroup.RemoveKey(keyId);
return simpleKeyGroup;
}

public static SimpleKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds)
public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds)
{
SimpleKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKey key in keyIds)
simpleKeyGroup.RemoveKey(key);
return simpleKeyGroup;
Expand Down
11 changes: 6 additions & 5 deletions Devices/Keyboard/Keys/BaseKeyGroup.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using CUE.NET.Devices.Keyboard.ColorBrushes;
using CUE.NET.Devices.Keyboard.Extensions;

namespace CUE.NET.Devices.Keyboard.Keys
{
public class BaseKeyGroup : IKeyGroup
public abstract class BaseKeyGroup : IKeyGroup
{
#region Properties & Fields

internal CorsairKeyboard Keyboard { get; }

public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GroupKeys);
protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();
public IEnumerable<CorsairKey> Keys => new ReadOnlyCollection<CorsairKey>(GetGroupKeys());

public Color Color { get; set; } = Color.Transparent;
public IBrush Brush { get; set; }

#endregion

Expand All @@ -28,6 +27,8 @@ protected BaseKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
this.Attach();
}

protected abstract IList<CorsairKey> GetGroupKeys();

#endregion
}
}
4 changes: 2 additions & 2 deletions Devices/Keyboard/Keys/IKeyGroup.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Collections.Generic;
using System.Drawing;
using CUE.NET.Devices.Keyboard.ColorBrushes;

namespace CUE.NET.Devices.Keyboard.Keys
{
public interface IKeyGroup
{
IEnumerable<CorsairKey> Keys { get; }

Color Color { get; set; }
IBrush Brush { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
using CUE.NET.Devices.Keyboard.Enums;
using System.Collections.Generic;
using CUE.NET.Devices.Keyboard.Enums;

namespace CUE.NET.Devices.Keyboard.Keys
{
public class SimpleKeyGroup : BaseKeyGroup
public class ListKeyGroup : BaseKeyGroup
{
#region Properties & Fields

protected IList<CorsairKey> GroupKeys { get; } = new List<CorsairKey>();

#endregion

#region Constructors

public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
: base(keyboard, autoAttach)
{ }

public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
public ListKeyGroup(CorsairKeyboard keyboard, params CorsairKey[] keys)
: this(keyboard, true, keys)
{ }

public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKey[] keys)
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKey[] keys)
: base(keyboard, autoAttach)
{
AddKey(keys);
}

public SimpleKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
public ListKeyGroup(CorsairKeyboard keyboard, params CorsairKeyboardKeyId[] keys)
: this(keyboard, true, keys)
{ }

public SimpleKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKeyboardKeyId[] keys)
public ListKeyGroup(CorsairKeyboard keyboard, bool autoAttach, params CorsairKeyboardKeyId[] keys)
: base(keyboard, autoAttach)
{
AddKey(keys);
Expand Down Expand Up @@ -82,6 +89,12 @@ public void MergeKeys(IKeyGroup groupToMerge)
GroupKeys.Add(key);
}


protected override IList<CorsairKey> GetGroupKeys()
{
return GroupKeys;
}

#endregion
}
}
19 changes: 11 additions & 8 deletions Devices/Keyboard/Keys/RectangleKeyGroup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Helper;
Expand All @@ -9,8 +10,8 @@ public class RectangleKeyGroup : BaseKeyGroup
{
#region Properties & Fields

public RectangleF RequestedRectangle { get; }
public float MinOverlayPercentage { get; }
public RectangleF Rectangle { get; set; }
public float MinOverlayPercentage { get; set; }

#endregion

Expand All @@ -28,20 +29,22 @@ public RectangleKeyGroup(CorsairKeyboard keyboard, PointF fromPoint, PointF toPo
: this(keyboard, RectangleHelper.CreateRectangleFromPoints(fromPoint, toPoint), minOverlayPercentage, autoAttach)
{ }

public RectangleKeyGroup(CorsairKeyboard keyboard, RectangleF requestedRectangle, float minOverlayPercentage = 0.5f, bool autoAttach = true)
public RectangleKeyGroup(CorsairKeyboard keyboard, RectangleF rectangle, float minOverlayPercentage = 0.5f, bool autoAttach = true)
: base(keyboard, autoAttach)
{
this.RequestedRectangle = requestedRectangle;
this.Rectangle = rectangle;
this.MinOverlayPercentage = minOverlayPercentage;

foreach (CorsairKey key in Keyboard.Where(x => RectangleHelper.CalculateIntersectPercentage(x.KeyRectangle, requestedRectangle) >= minOverlayPercentage))
GroupKeys.Add(key);
}

#endregion

#region Methods

protected override IList<CorsairKey> GetGroupKeys()
{
return Keyboard.Where(x => RectangleHelper.CalculateIntersectPercentage(x.KeyRectangle, Rectangle) >= MinOverlayPercentage).ToList();
}

#endregion
}
}
Loading