First attempt to rewrite Effects

This commit is contained in:
DarthAffe committed 2016-08-21 13:11:07 +02:00
1 parent 5a296dd048
commit ba80ef9848
32 files changed
+353 -325

No files matched your search

+7 -106
View File
@@ -10,7 +10,6 @@ using System.Threading;
using System.Threading.Tasks;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Generic.EventArgs;
using CUE.NET.Effects;
using CUE.NET.Native;
namespace CUE.NET.Devices.Generic
@@ -27,7 +26,7 @@ namespace CUE.NET.Devices.Generic
/// </summary>
public IDeviceInfo DeviceInfo { get; }
private UpdateMode _updateMode = UpdateMode.AutoOnEffect;
private UpdateMode _updateMode = UpdateMode.Manual;
/// <summary>
/// Gets or sets the update-mode for the device.
/// </summary>
@@ -51,16 +50,6 @@ namespace CUE.NET.Devices.Generic
/// </summary>
protected Dictionary<int, CorsairLed> Leds { get; } = new Dictionary<int, CorsairLed>();
/// <summary>
/// Indicates if the device has an active effect to deal with.
/// </summary>
protected abstract bool HasEffect { get; }
/// <summary>
///
/// </summary>
protected LinkedList<EffectTimeContainer> Effects { get; } = new LinkedList<EffectTimeContainer>();
private CancellationTokenSource _updateTokenSource;
private CancellationToken _updateToken;
private Task _updateTask;
@@ -139,9 +128,6 @@ namespace CUE.NET.Devices.Generic
case UpdateMode.Manual:
shouldRun = false;
break;
case UpdateMode.AutoOnEffect:
shouldRun = HasEffect;
break;
case UpdateMode.Continuous:
shouldRun = true;
break;
@@ -184,8 +170,8 @@ namespace CUE.NET.Devices.Generic
{
OnUpdating();
DeviceUpdateEffects();
DeviceUpdate();
UpdateEffects();
ICollection<LedUpateRequest> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).Select(x => new LedUpateRequest(x.Key, x.Value.RequestedColor)).ToList();
@@ -198,53 +184,15 @@ namespace CUE.NET.Devices.Generic
}
/// <summary>
/// Empty method which can be overwritten to perform device specific updates.
/// Performs device specific updates.
/// </summary>
protected virtual void DeviceUpdate()
{ }
private void UpdateEffects()
{
List<IEffect> effectsToRemove = new List<IEffect>();
lock (Effects)
{
long currentTicks = DateTime.Now.Ticks;
foreach (EffectTimeContainer effect in Effects.OrderBy(x => x.ZIndex))
{
try
{
float deltaTime;
if (effect.TicksAtLastUpdate < 0)
{
effect.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
}
else
deltaTime = (currentTicks - effect.TicksAtLastUpdate) / 10000000f;
effect.TicksAtLastUpdate = currentTicks;
effect.Effect.Update(deltaTime);
ApplyEffect(effect.Effect);
if (effect.Effect.IsDone)
effectsToRemove.Add(effect.Effect);
}
// ReSharper disable once CatchAllClause
catch (Exception ex) { OnException(ex); }
}
}
foreach (IEffect effect in effectsToRemove)
DetachEffect(effect);
}
protected abstract void DeviceUpdate();
/// <summary>
/// Applies the given effect to the device LEDs.
/// Performs device specific updates effect-updates.
/// </summary>
/// <param name="effect">The effect to apply.</param>
protected abstract void ApplyEffect(IEffect effect);
protected abstract void DeviceUpdateEffects();
private void UpdateLeds(ICollection<LedUpateRequest> updateRequests)
{
updateRequests = updateRequests.Where(x => x.Color != Color.Transparent).ToList();
@@ -276,53 +224,6 @@ namespace CUE.NET.Devices.Generic
OnLedsUpdated(updateRequests);
}
/// <summary>
/// Attaches the given effect.
/// </summary>
/// <param name="effect">The effect to attach.</param>
/// <returns><c>true</c> if the effect could be attached; otherwise, <c>false</c>.</returns>
public bool AttachEffect(IEffect effect)
{
bool retVal = false;
lock (Effects)
{
if (effect != null && Effects.All(x => x.Effect != effect))
{
effect.OnAttach();
Effects.AddLast(new EffectTimeContainer(effect, -1));
retVal = true;
}
}
CheckUpdateLoop();
return retVal;
}
/// <summary>
/// Detaches the given effect.
/// </summary>
/// <param name="effect">The effect to detached.</param>
/// <returns><c>true</c> if the effect could be detached; otherwise, <c>false</c>.</returns>
public bool DetachEffect(IEffect effect)
{
bool retVal = false;
lock (Effects)
{
if (effect != null)
{
EffectTimeContainer val = Effects.FirstOrDefault(x => x.Effect == effect);
if (val != null)
{
effect.OnDetach();
Effects.Remove(val);
retVal = true;
}
}
}
CheckUpdateLoop();
return retVal;
}
/// <summary>
/// Resets all loaded LEDs back to default.
/// </summary>
-6
View File
@@ -12,12 +12,6 @@ namespace CUE.NET.Devices.Generic.Enums
/// </summary>
Manual,
/// <summary>
/// The device will perform automatic updates at the rate set in <see cref="CUE.NET.Devices.ICueDevice.UpdateFrequency" />
/// as long as an <see cref="IEffect" /> is attached.
/// </summary>
AutoOnEffect,
/// <summary>
/// The device will perform automatic updates at the rate set in <see cref="CUE.NET.Devices.ICueDevice.UpdateFrequency" />.
/// </summary>
+10 -14
View File
@@ -5,11 +5,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Headset.Enums;
using CUE.NET.Effects;
namespace CUE.NET.Devices.Headset
{
@@ -43,11 +41,6 @@ namespace CUE.NET.Devices.Headset
/// </summary>
public CorsairHeadsetDeviceInfo HeadsetDeviceInfo { get; }
/// <summary>
/// Gets a value indicating if the headset has an active effect to deal with or not.
/// </summary>
protected override bool HasEffect => false;
/// <summary>
/// Gets a read-only collection containing all LEDs of the headset.
/// </summary>
@@ -72,19 +65,22 @@ namespace CUE.NET.Devices.Headset
#region Methods
protected override void ApplyEffect(IEffect effect)
{
//TODO DarthAffe 18.10.2015: How should brushes be applied to headsets?
foreach (CorsairLed led in effect.LedList)
led.Color = effect.EffectBrush.GetColorAtPoint(new RectangleF(0, 0, 2, 2), new PointF(1, 1));
}
private void InitializeLeds()
{
GetLed((int)CorsairHeadsetLedId.LeftLogo);
GetLed((int)CorsairHeadsetLedId.RightLogo);
}
protected override void DeviceUpdate()
{
//TODO DarthAffe 21.08.2016: Create something fancy for headsets
}
protected override void DeviceUpdateEffects()
{
//TODO DarthAffe 21.08.2016: Create something fancy for headsets
}
#region IEnumerable
/// <summary>
+26 -18
View File
@@ -106,18 +106,6 @@ namespace CUE.NET.Devices.Keyboard
/// </summary>
public int ZIndex { get; set; } = 0;
/// <summary>
/// Gets a value indicating if the keyboard has an active effect to deal with or not.
/// </summary>
protected override bool HasEffect
{
get
{
lock (Effects)
return Effects.Any();
}
}
#endregion
#region Constructors
@@ -156,13 +144,9 @@ namespace CUE.NET.Devices.Keyboard
}
}
protected override void ApplyEffect(IEffect effect)
protected override void DeviceUpdateEffects()
{
if (effect == null) return;
//TODO DarthAffe 18.10.2015: This is really dirty and might have a really negative performance impact - find a better solution.
IEnumerable<CorsairKey> keys = effect.LedList?.Select(x => this.FirstOrDefault(y => y.Led == x));
ApplyBrush((keys ?? this).ToList(), effect.EffectBrush);
UpdateEffects();
}
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
@@ -200,6 +184,30 @@ namespace CUE.NET.Devices.Keyboard
#endregion
public void UpdateEffects()
{
lock (_keyGroups)
{
foreach (IKeyGroup keyGroup in _keyGroups)
{
keyGroup.UpdateEffects();
keyGroup.Brush.UpdateEffects();
}
}
Brush.UpdateEffects();
}
public void AddEffect(IEffect<IKeyGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.");
}
public void RemoveEffect(IEffect<IKeyGroup> effect)
{
throw new NotSupportedException("Effects can't be applied directly to the keyboard. Add it to the Brush or create a keygroup instead.");
}
/// <summary>
/// Attaches the given keygroup.
/// </summary>
@@ -13,11 +13,11 @@ namespace CUE.NET.Devices.Keyboard.Extensions
public static class KeyGroupExtension
{
/// <summary>
/// Converts the given <see cref="BaseKeyGroup" /> to a <see cref="ListKeyGroup" />.
/// Converts the given <see cref="AbstractKeyGroup" /> to a <see cref="ListKeyGroup" />.
/// </summary>
/// <param name="keyGroup">The <see cref="BaseKeyGroup" /> to convert.</param>
/// <param name="keyGroup">The <see cref="AbstractKeyGroup" /> to convert.</param>
/// <returns>The converted <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup ToSimpleKeyGroup(this BaseKeyGroup keyGroup)
public static ListKeyGroup ToSimpleKeyGroup(this AbstractKeyGroup keyGroup)
{
ListKeyGroup simpleKeyGroup = keyGroup as ListKeyGroup;
if (simpleKeyGroup == null)
@@ -34,7 +34,7 @@ namespace CUE.NET.Devices.Keyboard.Extensions
/// <param name="keyGroup">The base keygroup.</param>
/// <param name="keyIds">The ids of the keys to exclude.</param>
/// <returns>The new <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
public static ListKeyGroup Exclude(this AbstractKeyGroup keyGroup, params CorsairKeyboardKeyId[] keyIds)
{
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKeyboardKeyId keyId in keyIds)
@@ -48,7 +48,7 @@ namespace CUE.NET.Devices.Keyboard.Extensions
/// <param name="keyGroup">The base keygroup.</param>
/// <param name="keyIds">The keys to exclude.</param>
/// <returns>The new <see cref="ListKeyGroup" />.</returns>
public static ListKeyGroup Exclude(this BaseKeyGroup keyGroup, params CorsairKey[] keyIds)
public static ListKeyGroup Exclude(this AbstractKeyGroup keyGroup, params CorsairKey[] keyIds)
{
ListKeyGroup simpleKeyGroup = keyGroup.ToSimpleKeyGroup();
foreach (CorsairKey key in keyIds)
@@ -62,7 +62,7 @@ namespace CUE.NET.Devices.Keyboard.Extensions
/// </summary>
/// <param name="keyGroup">The keygroup to attach.</param>
/// <returns><c>true</c> if the keygroup could be attached; otherwise, <c>false</c>.</returns>
public static bool Attach(this BaseKeyGroup keyGroup)
public static bool Attach(this AbstractKeyGroup keyGroup)
{
return keyGroup.Keyboard?.AttachKeyGroup(keyGroup) ?? false;
}
@@ -72,7 +72,7 @@ namespace CUE.NET.Devices.Keyboard.Extensions
/// </summary>
/// <param name="keyGroup">The keygroup to attach.</param>
/// <returns><c>true</c> if the keygroup could be detached; otherwise, <c>false</c>.</returns>
public static bool Detach(this BaseKeyGroup keyGroup)
public static bool Detach(this AbstractKeyGroup keyGroup)
{
return keyGroup.Keyboard?.DetachKeyGroup(keyGroup) ?? false;
}
@@ -4,13 +4,14 @@ using System.Linq;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Keyboard.Extensions;
using CUE.NET.Effects;
namespace CUE.NET.Devices.Keyboard.Keys
{
/// <summary>
/// Represents a basic keygroup.
/// </summary>
public abstract class BaseKeyGroup : IKeyGroup
public abstract class AbstractKeyGroup : AbstractEffectTarget<IKeyGroup>, IKeyGroup
{
#region Properties & Fields
@@ -39,11 +40,11 @@ namespace CUE.NET.Devices.Keyboard.Keys
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="BaseKeyGroup"/> class.
/// Initializes a new instance of the <see cref="AbstractKeyGroup"/> class.
/// </summary>
/// <param name="keyboard">The keyboard this keygroup belongs to.</param>
/// <param name="autoAttach">Specifies whether this group should be automatically attached or not.</param>
protected BaseKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
protected AbstractKeyGroup(CorsairKeyboard keyboard, bool autoAttach = true)
{
this.Keyboard = keyboard;
+2 -1
View File
@@ -4,10 +4,11 @@
using System.Collections.Generic;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
using CUE.NET.Effects;
namespace CUE.NET.Devices.Keyboard.Keys
{
public interface IKeyGroup
public interface IKeyGroup : IEffectTarget<IKeyGroup>
{
/// <summary>
/// Gets a read-only collection containing the keys from this group.
+3 -1
View File
@@ -9,10 +9,12 @@ namespace CUE.NET.Devices.Keyboard.Keys
/// <summary>
/// Represents a keygroup containing arbitrary keys.
/// </summary>
public class ListKeyGroup : BaseKeyGroup
public class ListKeyGroup : AbstractKeyGroup
{
#region Properties & Fields
protected override IKeyGroup EffectTarget => this;
/// <summary>
/// Gets the list containing the keys of this keygroup.
/// </summary>
+3 -1
View File
@@ -13,10 +13,12 @@ namespace CUE.NET.Devices.Keyboard.Keys
/// <summary>
/// Represents a keygroup containing keys which physically lay inside a rectangle.
/// </summary>
public class RectangleKeyGroup : BaseKeyGroup
public class RectangleKeyGroup : AbstractKeyGroup
{
#region Properties & Fields
protected override IKeyGroup EffectTarget => this;
private IList<CorsairKey> _keyCache;
private RectangleF _rectangle;
+10 -14
View File
@@ -5,11 +5,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Linq;
using CUE.NET.Devices.Generic;
using CUE.NET.Devices.Mouse.Enums;
using CUE.NET.Effects;
using CUE.NET.Exceptions;
namespace CUE.NET.Devices.Mouse
@@ -44,11 +42,6 @@ namespace CUE.NET.Devices.Mouse
/// </summary>
public CorsairMouseDeviceInfo MouseDeviceInfo { get; }
/// <summary>
/// Gets a value indicating if the mouse has an active effect to deal with or not.
/// </summary>
protected override bool HasEffect => false;
/// <summary>
/// Gets a read-only collection containing all LEDs of the mouse.
/// </summary>
@@ -74,13 +67,6 @@ namespace CUE.NET.Devices.Mouse
#region Methods
protected override void ApplyEffect(IEffect effect)
{
//TODO DarthAffe 18.10.2015: How should brushes be applied to mice?
foreach (CorsairLed led in effect.LedList)
led.Color = effect.EffectBrush.GetColorAtPoint(new RectangleF(0, 0, 2, 2), new PointF(1, 1));
}
private void InitializeLeds()
{
switch (MouseDeviceInfo.PhysicalLayout)
@@ -108,6 +94,16 @@ namespace CUE.NET.Devices.Mouse
}
}
protected override void DeviceUpdate()
{
//TODO DarthAffe 21.08.2016: Create something fancy for mice
}
protected override void DeviceUpdateEffects()
{
//TODO DarthAffe 21.08.2016: Create something fancy for mice
}
#region IEnumerable
/// <summary>