mirror of
https://github.com/DarthAffe/CUE.NET.git
synced 2025-12-13 09:08:34 +00:00
Refactored effects into AbstractCueDevice
This commit is contained in:
parent
630090ac6f
commit
e49945004f
@ -6,6 +6,7 @@ using System.Runtime.InteropServices;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using CUE.NET.Devices.Generic.Enums;
|
using CUE.NET.Devices.Generic.Enums;
|
||||||
|
using CUE.NET.Effects;
|
||||||
using CUE.NET.Native;
|
using CUE.NET.Native;
|
||||||
|
|
||||||
namespace CUE.NET.Devices.Generic
|
namespace CUE.NET.Devices.Generic
|
||||||
@ -51,6 +52,11 @@ namespace CUE.NET.Devices.Generic
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected abstract bool HasEffect { get; }
|
protected abstract bool HasEffect { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
///
|
||||||
|
/// </summary>
|
||||||
|
protected LinkedList<EffectTimeContainer> Effects { get; } = new LinkedList<EffectTimeContainer>();
|
||||||
|
|
||||||
private CancellationTokenSource _updateTokenSource;
|
private CancellationTokenSource _updateTokenSource;
|
||||||
private CancellationToken _updateToken;
|
private CancellationToken _updateToken;
|
||||||
private Task _updateTask;
|
private Task _updateTask;
|
||||||
@ -146,11 +152,13 @@ namespace CUE.NET.Devices.Generic
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Perform an update for all dirty keys, or all keys if flushLeds is set to true.
|
/// Performs an update for all dirty keys, or all keys if flushLeds is set to true.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="flushLeds">Specifies whether all keys (including clean ones) should be updated.</param>
|
/// <param name="flushLeds">Specifies whether all keys (including clean ones) should be updated.</param>
|
||||||
public virtual void Update(bool flushLeds = false)
|
public virtual void Update(bool flushLeds = false)
|
||||||
{
|
{
|
||||||
|
UpdateEffects();
|
||||||
|
|
||||||
IList<KeyValuePair<int, CorsairLed>> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList();
|
IList<KeyValuePair<int, CorsairLed>> ledsToUpdate = (flushLeds ? Leds : Leds.Where(x => x.Value.IsDirty)).ToList();
|
||||||
|
|
||||||
foreach (CorsairLed led in Leds.Values)
|
foreach (CorsairLed led in Leds.Values)
|
||||||
@ -159,6 +167,48 @@ namespace CUE.NET.Devices.Generic
|
|||||||
UpdateLeds(ledsToUpdate);
|
UpdateLeds(ledsToUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) { ManageException(ex); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (IEffect effect in effectsToRemove)
|
||||||
|
DetachEffect(effect);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the given effect to the device LEDs.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="effect">The effect to apply.</param>
|
||||||
|
protected abstract void ApplyEffect(IEffect effect);
|
||||||
|
|
||||||
private static void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate)
|
private static void UpdateLeds(ICollection<KeyValuePair<int, CorsairLed>> ledsToUpdate)
|
||||||
{
|
{
|
||||||
ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList();
|
ledsToUpdate = ledsToUpdate.Where(x => x.Value.Color != Color.Transparent).ToList();
|
||||||
@ -186,6 +236,53 @@ namespace CUE.NET.Devices.Generic
|
|||||||
Marshal.FreeHGlobal(ptr);
|
Marshal.FreeHGlobal(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <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>
|
/// <summary>
|
||||||
/// Handles the needed event-calls for an exception.
|
/// Handles the needed event-calls for an exception.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -4,9 +4,11 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using CUE.NET.Devices.Generic;
|
using CUE.NET.Devices.Generic;
|
||||||
using CUE.NET.Devices.Headset.Enums;
|
using CUE.NET.Devices.Headset.Enums;
|
||||||
|
using CUE.NET.Effects;
|
||||||
|
|
||||||
namespace CUE.NET.Devices.Headset
|
namespace CUE.NET.Devices.Headset
|
||||||
{
|
{
|
||||||
@ -69,6 +71,13 @@ namespace CUE.NET.Devices.Headset
|
|||||||
|
|
||||||
#region Methods
|
#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()
|
private void InitializeLeds()
|
||||||
{
|
{
|
||||||
GetLed((int)CorsairHeadsetLedId.LeftLogo);
|
GetLed((int)CorsairHeadsetLedId.LeftLogo);
|
||||||
|
|||||||
@ -76,7 +76,6 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
private readonly LinkedList<IKeyGroup> _keyGroups = new LinkedList<IKeyGroup>();
|
private readonly LinkedList<IKeyGroup> _keyGroups = new LinkedList<IKeyGroup>();
|
||||||
private readonly LinkedList<EffectTimeContainer> _effects = new LinkedList<EffectTimeContainer>();
|
|
||||||
|
|
||||||
private Dictionary<CorsairKeyboardKeyId, CorsairKey> _keys = new Dictionary<CorsairKeyboardKeyId, CorsairKey>();
|
private Dictionary<CorsairKeyboardKeyId, CorsairKey> _keys = new Dictionary<CorsairKeyboardKeyId, CorsairKey>();
|
||||||
|
|
||||||
@ -113,8 +112,8 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
lock (_effects)
|
lock (Effects)
|
||||||
return _effects.Any();
|
return Effects.Any();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,7 +147,6 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
public override void Update(bool flushLeds = false)
|
public override void Update(bool flushLeds = false)
|
||||||
{
|
{
|
||||||
UpdateKeyGroups();
|
UpdateKeyGroups();
|
||||||
UpdateEffects();
|
|
||||||
|
|
||||||
// Perform 'real' update
|
// Perform 'real' update
|
||||||
base.Update(flushLeds);
|
base.Update(flushLeds);
|
||||||
@ -166,42 +164,13 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateEffects()
|
protected override void ApplyEffect(IEffect effect)
|
||||||
{
|
{
|
||||||
List<IEffect> effectsToRemove = new List<IEffect>();
|
if (effect == null) return;
|
||||||
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);
|
|
||||||
|
|
||||||
//TODO DarthAffe 18.10.2015: This is really dirty and might have a really negative performance impact - find a better solution.
|
//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.Effect?.LedList?.Select(x => this.FirstOrDefault(y => y.Led == x));
|
IEnumerable<CorsairKey> keys = effect.LedList?.Select(x => this.FirstOrDefault(y => y.Led == x));
|
||||||
ApplyBrush((keys ?? this).ToList(), effect.Effect.EffectBrush);
|
ApplyBrush((keys ?? this).ToList(), effect.EffectBrush);
|
||||||
|
|
||||||
if (effect.Effect.IsDone)
|
|
||||||
effectsToRemove.Add(effect.Effect);
|
|
||||||
}
|
|
||||||
// ReSharper disable once CatchAllClause
|
|
||||||
catch (Exception ex) { ManageException(ex); }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (IEffect effect in effectsToRemove)
|
|
||||||
DetachEffect(effect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
|
// ReSharper disable once MemberCanBeMadeStatic.Local - idc
|
||||||
@ -259,53 +228,6 @@ namespace CUE.NET.Devices.Keyboard
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <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;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeKeys()
|
private void InitializeKeys()
|
||||||
{
|
{
|
||||||
_CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositions(), typeof(_CorsairLedPositions));
|
_CorsairLedPositions nativeLedPositions = (_CorsairLedPositions)Marshal.PtrToStructure(_CUESDK.CorsairGetLedPositions(), typeof(_CorsairLedPositions));
|
||||||
|
|||||||
@ -2,12 +2,15 @@
|
|||||||
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
// ReSharper disable UnusedAutoPropertyAccessor.Global
|
||||||
// ReSharper disable UnusedMember.Global
|
// ReSharper disable UnusedMember.Global
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using CUE.NET.Devices.Generic;
|
using CUE.NET.Devices.Generic;
|
||||||
using CUE.NET.Devices.Mouse.Enums;
|
using CUE.NET.Devices.Mouse.Enums;
|
||||||
|
using CUE.NET.Effects;
|
||||||
using CUE.NET.Exceptions;
|
using CUE.NET.Exceptions;
|
||||||
|
|
||||||
namespace CUE.NET.Devices.Mouse
|
namespace CUE.NET.Devices.Mouse
|
||||||
@ -72,6 +75,13 @@ namespace CUE.NET.Devices.Mouse
|
|||||||
|
|
||||||
#region Methods
|
#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()
|
private void InitializeLeds()
|
||||||
{
|
{
|
||||||
switch (MouseDeviceInfo.PhysicalLayout)
|
switch (MouseDeviceInfo.PhysicalLayout)
|
||||||
|
|||||||
@ -6,24 +6,24 @@ namespace CUE.NET.Effects
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents a wrapped effect with additional time information.
|
/// Represents a wrapped effect with additional time information.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class EffectTimeContainer
|
public class EffectTimeContainer
|
||||||
{
|
{
|
||||||
#region Properties & Fields
|
#region Properties & Fields
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the wrapped effect.
|
/// Gets or sets the wrapped effect.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal IEffect Effect { get; set; }
|
public IEffect Effect { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the tick-count from the last time the effect was updated.
|
/// Gets or sets the tick-count from the last time the effect was updated.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal long TicksAtLastUpdate { get; set; }
|
public long TicksAtLastUpdate { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the z-index of the effect.
|
/// Gets the z-index of the effect.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal int ZIndex => Effect?.ZIndex ?? 0;
|
public int ZIndex => Effect?.ZIndex ?? 0;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -34,7 +34,7 @@ namespace CUE.NET.Effects
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="effect">The wrapped effect.</param>
|
/// <param name="effect">The wrapped effect.</param>
|
||||||
/// <param name="ticksAtLastUpdate">The tick-count from the last time the effect was updated.</param>
|
/// <param name="ticksAtLastUpdate">The tick-count from the last time the effect was updated.</param>
|
||||||
internal EffectTimeContainer(IEffect effect, long ticksAtLastUpdate)
|
public EffectTimeContainer(IEffect effect, long ticksAtLastUpdate)
|
||||||
{
|
{
|
||||||
this.Effect = effect;
|
this.Effect = effect;
|
||||||
this.TicksAtLastUpdate = ticksAtLastUpdate;
|
this.TicksAtLastUpdate = ticksAtLastUpdate;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user