1
0
mirror of https://github.com/DarthAffe/CUE.NET.git synced 2025-12-12 08:48:30 +00:00

First attempt to rewrite Effects

This commit is contained in:
Darth Affe 2016-08-21 13:11:07 +02:00
parent 5a296dd048
commit ba80ef9848
32 changed files with 353 additions and 325 deletions

View File

@ -2,6 +2,7 @@
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
using CUE.NET.Helper;
namespace CUE.NET.Brushes
@ -9,7 +10,7 @@ namespace CUE.NET.Brushes
/// <summary>
/// Represents a basic brush.
/// </summary>
public abstract class AbstractBrush : IBrush
public abstract class AbstractBrush : AbstractEffectTarget<IBrush>, IBrush
{
#region Properties & Fields

View File

@ -2,13 +2,14 @@
using System.Drawing;
using CUE.NET.Devices.Keyboard.Enums;
using CUE.NET.Effects;
namespace CUE.NET.Brushes
{
/// <summary>
/// Represents a basic brush.
/// </summary>
public interface IBrush
public interface IBrush : IEffectTarget<IBrush>
{
/// <summary>
/// Gets or sets the calculation mode used for the rectangle/points used for color-selection in brushes.

View File

@ -39,6 +39,8 @@ namespace CUE.NET.Brushes
#region Properties & Fields
protected override IBrush EffectTarget => this;
/// <summary>
/// Gets or sets the image drawn by the brush. If null it will default to full transparent.
/// </summary>

View File

@ -18,6 +18,8 @@ namespace CUE.NET.Brushes
{
#region Properties & Fields
protected override IBrush EffectTarget => this;
/// <summary>
/// Gets or sets the start point (as percentage in the range [0..1]) of the gradient drawn by the brush. (default: 0f, 0.5f)
/// </summary>

View File

@ -11,7 +11,9 @@ namespace CUE.NET.Brushes
public class ProfileBrush : AbstractBrush
{
#region Properties & Fields
protected override IBrush EffectTarget => this;
private Dictionary<CorsairKeyboardKeyId, Color> _keyLights;
#endregion

View File

@ -16,6 +16,8 @@ namespace CUE.NET.Brushes
{
#region Properties & Fields
protected override IBrush EffectTarget => this;
/// <summary>
/// Gets or sets the center point (as percentage in the range [0..1]) around which the brush should be drawn.
/// </summary>

View File

@ -15,6 +15,8 @@ namespace CUE.NET.Brushes
{
#region Properties & Fields
protected override IBrush EffectTarget => this;
private Random _random = new Random();
#endregion

View File

@ -12,6 +12,8 @@ namespace CUE.NET.Brushes
{
#region Properties & Fields
protected override IBrush EffectTarget => this;
/// <summary>
/// Gets or sets the color drawn by the brush.
/// </summary>

View File

@ -58,6 +58,10 @@
<Compile Include="Devices\Generic\EventArgs\UpdatingEventArgs.cs" />
<Compile Include="Devices\Generic\LedUpateRequest.cs" />
<Compile Include="Devices\Keyboard\Enums\BrushCalculationMode.cs" />
<Compile Include="Effects\AbstractKeyGroupEffect.cs" />
<Compile Include="Effects\AbstractBrushEffect.cs" />
<Compile Include="Effects\AbstractEffectTarget.cs" />
<Compile Include="Effects\IEffectTarget.cs" />
<Compile Include="Gradients\AbstractGradient.cs" />
<Compile Include="Gradients\GradientStop.cs" />
<Compile Include="Gradients\IGradient.cs" />
@ -67,7 +71,6 @@
<Compile Include="Brushes\RadialGradientBrush.cs" />
<Compile Include="Brushes\RandomColorBrush.cs" />
<Compile Include="Brushes\SolidColorBrush.cs" />
<Compile Include="Effects\AbstractEffect.cs" />
<Compile Include="Effects\FlashEffect.cs" />
<Compile Include="Effects\IEffect.cs" />
<Compile Include="Effects\EffectTimeContainer.cs" />
@ -76,7 +79,7 @@
<Compile Include="Devices\Keyboard\Enums\CorsairKeyboardKeyId.cs" />
<Compile Include="Devices\Keyboard\Enums\CorsairPhysicalKeyboardLayout.cs" />
<Compile Include="Devices\Generic\Enums\UpdateMode.cs" />
<Compile Include="Devices\Keyboard\Keys\BaseKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\AbstractKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\IKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\RectangleKeyGroup.cs" />
<Compile Include="Devices\Keyboard\Keys\ListKeyGroup.cs" />

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>

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>

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>

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>

View File

@ -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;
}

View File

@ -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;

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.

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>

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;

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>

View File

@ -1,38 +1,26 @@
// ReSharper disable MemberCanBePrivate.Global
using System.Collections.Generic;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Effects
{
/// <summary>
/// Represents a basic effect.
/// Represents a basic effect targeting an <see cref="IBrush"/>.
/// </summary>
public abstract class AbstractEffect : IEffect
public abstract class AbstractBrushEffect : IEffect<IBrush>
{
#region Properties & Fields
/// <summary>
/// Gets or sets the list of LEDSs to which the effect applies.
/// </summary>
public IEnumerable<CorsairLed> LedList { get; set; }
/// <summary>
/// Gets the brush which is drawn by the effect.
/// </summary>
public abstract IBrush EffectBrush { get; }
/// <summary>
/// Gets or sets the z-index of the brush to allow ordering them before drawing. (lowest first) (default: 0)
/// </summary>
public int ZIndex { get; set; } = 0;
/// <summary>
/// Gets or sets if this effect has finished all of his work.
/// </summary>
public bool IsDone { get; protected set; }
/// <summary>
/// Gets the <see cref="IBrush"/> this effect is targeting.
/// </summary>
protected IBrush Brush { get; private set; }
#endregion
#region Methods
@ -46,14 +34,20 @@ namespace CUE.NET.Effects
/// <summary>
/// Hook which is called when the effect is attached to a device.
/// </summary>
public virtual void OnAttach()
{ }
/// <param name="target">The <see cref="IBrush"/> this effect is attached to.</param>
public virtual void OnAttach(IBrush target)
{
Brush = target;
}
/// <summary>
/// Hook which is called when the effect is detached from a device.
/// </summary>
public virtual void OnDetach()
{ }
/// <param name="target">The <see cref="IBrush"/> this effect is detached from.</param>
public virtual void OnDetach(IBrush target)
{
Brush = null;
}
#endregion
}

View File

@ -0,0 +1,71 @@
// ReSharper disable MemberCanBePrivate.Global
using System;
using System.Collections.Generic;
using System.Linq;
namespace CUE.NET.Effects
{
public abstract class AbstractEffectTarget<T> : IEffectTarget<T>
where T : IEffectTarget<T>
{
#region Properties & Fields
private IList<EffectTimeContainer> _effectTimes = new List<EffectTimeContainer>();
protected IList<IEffect<T>> Effects => _effectTimes.Select(x => x.Effect).Cast<IEffect<T>>().ToList();
protected abstract T EffectTarget { get; }
#endregion
#region Methods
public void UpdateEffects()
{
lock (Effects)
{
for (int i = _effectTimes.Count - 1; i >= 0; i--)
{
EffectTimeContainer effectTime = _effectTimes[i];
long currentTicks = DateTime.Now.Ticks;
float deltaTime;
if (effectTime.TicksAtLastUpdate < 0)
{
effectTime.TicksAtLastUpdate = currentTicks;
deltaTime = 0f;
}
else
deltaTime = (currentTicks - effectTime.TicksAtLastUpdate) / 10000000f;
effectTime.TicksAtLastUpdate = currentTicks;
effectTime.Effect.Update(deltaTime);
if (effectTime.Effect.IsDone)
_effectTimes.RemoveAt(i);
}
}
}
public void AddEffect(IEffect<T> effect)
{
if (_effectTimes.All(x => x.Effect != effect))
{
effect.OnAttach(EffectTarget);
_effectTimes.Add(new EffectTimeContainer(effect, -1));
}
}
public void RemoveEffect(IEffect<T> effect)
{
EffectTimeContainer effectTimeToRemove = _effectTimes.FirstOrDefault(x => x.Effect == effect);
if (effectTimeToRemove != null)
{
effect.OnDetach(EffectTarget);
_effectTimes.Remove(effectTimeToRemove);
}
}
#endregion
}
}

View File

@ -0,0 +1,54 @@
// ReSharper disable MemberCanBePrivate.Global
using CUE.NET.Devices.Keyboard.Keys;
namespace CUE.NET.Effects
{
/// <summary>
/// Represents a basic effect targeting an <see cref="IKeyGroup"/>.
/// </summary>
public abstract class AbstractKeyGroupEffect : IEffect<IKeyGroup>
{
#region Properties & Fields
/// <summary>
/// Gets or sets if this effect has finished all of his work.
/// </summary>
public bool IsDone { get; protected set; }
/// <summary>
/// Gets the <see cref="IKeyGroup"/> this effect is targeting.
/// </summary>
protected IKeyGroup KeyGroup { get; private set; }
#endregion
#region Methods
/// <summary>
/// Updates the effect.
/// </summary>
/// <param name="deltaTime">The elapsed time (in seconds) since the last update.</param>
public abstract void Update(float deltaTime);
/// <summary>
/// Hook which is called when the effect is attached to a device.
/// </summary>
/// <param name="target">The <see cref="IKeyGroup"/> this effect is attached to.</param>
public virtual void OnAttach(IKeyGroup target)
{
KeyGroup = target;
}
/// <summary>
/// Hook which is called when the effect is detached from a device.
/// </summary>
/// <param name="target">The <see cref="IKeyGroup"/> this effect is detached from.</param>
public virtual void OnDetach(IKeyGroup target)
{
KeyGroup = null;
}
#endregion
}
}

View File

@ -13,18 +13,13 @@ namespace CUE.NET.Effects
/// <summary>
/// Gets or sets the wrapped effect.
/// </summary>
public IEffect Effect { get; set; }
public IEffect Effect { get; }
/// <summary>
/// Gets or sets the tick-count from the last time the effect was updated.
/// </summary>
public long TicksAtLastUpdate { get; set; }
/// <summary>
/// Gets the z-index of the effect.
/// </summary>
public int ZIndex => Effect?.ZIndex ?? 0;
#endregion
#region Constructors

View File

@ -3,7 +3,6 @@
// ReSharper disable UnusedMember.Global
using System;
using System.Drawing;
using CUE.NET.Brushes;
namespace CUE.NET.Effects
@ -11,15 +10,10 @@ namespace CUE.NET.Effects
/// <summary>
/// Represents an effect which allows to flash an brush by modifying his opacity.
/// </summary>
public class FlashEffect : AbstractEffect
public class FlashEffect : AbstractBrushEffect
{
#region Properties & Fields
/// <summary>
/// Gets the brush which is drawn by the effect.
/// </summary>
public override IBrush EffectBrush { get; }
/// <summary>
/// Gets or sets the attack-time (in seconds) of the effect. (default: 0.2f)<br />
/// This is close to a synthesizer envelope. (See <see cref="http://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope" /> as reference)
@ -71,27 +65,6 @@ namespace CUE.NET.Effects
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="FlashEffect"/> class.
/// </summary>
/// <param name="flashColor">The color from which a <see cref="SolidColorBrush" /> should be created and used by this effect.</param>
public FlashEffect(Color flashColor)
: this(new SolidColorBrush(flashColor))
{ }
/// <summary>
/// Initializes a new instance of the <see cref="FlashEffect"/> class.
/// </summary>
/// <param name="effectBrush">The brush which should be used by this effect,</param>
public FlashEffect(IBrush effectBrush)
{
this.EffectBrush = effectBrush;
}
#endregion
#region Methods
/// <summary>
@ -106,7 +79,7 @@ namespace CUE.NET.Effects
if (_currentPhase == ADSRPhase.Attack)
if (_currentPhaseValue > 0f)
EffectBrush.Opacity = Math.Min(1f, (Attack - _currentPhaseValue) / Attack) * AttackValue;
Brush.Opacity = Math.Min(1f, (Attack - _currentPhaseValue) / Attack) * AttackValue;
else
{
_currentPhaseValue = Decay;
@ -115,7 +88,7 @@ namespace CUE.NET.Effects
if (_currentPhase == ADSRPhase.Decay)
if (_currentPhaseValue > 0f)
EffectBrush.Opacity = SustainValue + (Math.Min(1f, _currentPhaseValue / Decay) * (AttackValue - SustainValue));
Brush.Opacity = SustainValue + (Math.Min(1f, _currentPhaseValue / Decay) * (AttackValue - SustainValue));
else
{
_currentPhaseValue = Sustain;
@ -124,7 +97,7 @@ namespace CUE.NET.Effects
if (_currentPhase == ADSRPhase.Sustain)
if (_currentPhaseValue > 0f)
EffectBrush.Opacity = SustainValue;
Brush.Opacity = SustainValue;
else
{
_currentPhaseValue = Release;
@ -133,7 +106,7 @@ namespace CUE.NET.Effects
if (_currentPhase == ADSRPhase.Release)
if (_currentPhaseValue > 0f)
EffectBrush.Opacity = Math.Min(1f, _currentPhaseValue / Release) * SustainValue;
Brush.Opacity = Math.Min(1f, _currentPhaseValue / Release) * SustainValue;
else
{
_currentPhaseValue = Interval;
@ -142,7 +115,7 @@ namespace CUE.NET.Effects
if (_currentPhase == ADSRPhase.Pause)
if (_currentPhaseValue > 0f)
EffectBrush.Opacity = 0f;
Brush.Opacity = 0f;
else
{
if (++_repetitionCount >= Repetitions && Repetitions > 0)
@ -155,14 +128,14 @@ namespace CUE.NET.Effects
/// <summary>
/// Resets the effect.
/// </summary>
public override void OnAttach()
public override void OnAttach(IBrush brush)
{
base.OnAttach();
base.OnAttach(brush);
_currentPhase = ADSRPhase.Attack;
_currentPhaseValue = Attack;
_repetitionCount = 0;
EffectBrush.Opacity = 0f;
brush.Opacity = 0f;
}
#endregion

View File

@ -1,10 +1,6 @@
// ReSharper disable UnusedMember.Global
// ReSharper disable UnusedMemberInSuper.Global
using System.Collections.Generic;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic;
namespace CUE.NET.Effects
{
/// <summary>
@ -15,22 +11,7 @@ namespace CUE.NET.Effects
#region Properties & Fields
/// <summary>
/// Gets or sets the list of LEDs to which the effect applies.
/// </summary>
IEnumerable<CorsairLed> LedList { get; set; }
/// <summary>
/// Gets the brush which is drawn by the effect.
/// </summary>
IBrush EffectBrush { get; }
/// <summary>
/// Gets or sets the z-index of the effect to allow ordering them before drawing. (lowest first) (default: 0)
/// </summary>
int ZIndex { get; set; }
/// <summary>
/// Gets or sets if this effect has finished all of his work.
/// Gets if this effect has finished all of his work.
/// </summary>
bool IsDone { get; }
@ -44,15 +25,29 @@ namespace CUE.NET.Effects
/// <param name="deltaTime">The elapsed time (in seconds) since the last update.</param>
void Update(float deltaTime);
#endregion
}
/// <summary>
/// Represents a basic effect.
/// </summary>
/// <typeparam name="T">The type of <see cref="IEffectTarget{T}"/> this effect can be attached to.</typeparam>
public interface IEffect<in T> : IEffect
where T : IEffectTarget<T>
{
#region Methods
/// <summary>
/// Hook which is called when the effect is attached to a device.
/// </summary>
void OnAttach();
/// <param name="target">The <see cref="IEffectTarget{T}"/> this effect is attached to.</param>
void OnAttach(T target);
/// <summary>
/// Hook which is called when the effect is detached from a device.
/// </summary>
void OnDetach();
/// <param name="target">The <see cref="IEffectTarget{T}"/> this effect is detached from.</param>
void OnDetach(T target);
#endregion
}

31
Effects/IEffectTarget.cs Normal file
View File

@ -0,0 +1,31 @@
namespace CUE.NET.Effects
{
/// <summary>
/// Represents a basic effect-target.
/// </summary>
/// <typeparam name="T">The type this target represents.</typeparam>
public interface IEffectTarget<out T>
where T : IEffectTarget<T>
{
#region Methods
/// <summary>
/// Updates all effects added to this target.
/// </summary>
void UpdateEffects();
/// <summary>
/// Adds an affect.
/// </summary>
/// <param name="effect">The effect to add.</param>
void AddEffect(IEffect<T> effect);
/// <summary>
/// Removes an effect
/// </summary>
/// <param name="effect">The effect to remove.</param>
void RemoveEffect(IEffect<T> effect);
#endregion
}
}

37
Effects/RippleEffect.cs Normal file
View File

@ -0,0 +1,37 @@
using System;
using System.Drawing;
using CUE.NET.Brushes;
namespace CUE.NET.Effects
{
public class RippleEffect : AbstractEffect
{
#region Properties & Fields
private RippleBrush _brush = new RippleBrush();
public override IBrush EffectBrush => _brush;
#endregion
#region Constructors
#endregion
#region Methods
public override void Update(float deltaTime)
{
throw new NotImplementedException();
}
#endregion
private class RippleBrush : AbstractBrush
{
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
return FinalizeColor(Color.Black);
}
}
}
}

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.Drawing;
using CUE.NET;
using CUE.NET.Brushes;
using CUE.NET.Devices.Generic.Enums;
using CUE.NET.Devices.Keyboard;
using CUE.NET.Devices.Keyboard.Keys;
using CUE.NET.Exceptions;
using CUE.NET.Gradients;
using Example_AudioAnalyzer_full.TakeAsIs;
@ -60,18 +62,22 @@ namespace Example_AudioAnalyzer_full
public void Run()
{
// Add a lack background. We want this to be semi-transparent to add some sort of fade-effect - this will smooth everything out a bit
_keyboard.UpdateMode = UpdateMode.Continuous;
// Add a black background. We want this to be semi-transparent to add some sort of fade-effect - this will smooth everything out a bit
// Note that this isn't a 'real effect' since it's update-rate dependent. A real effect would do always the same thing not mather how fast the keyboard updates.
_keyboard.Brush = new SolidColorBrush(Color.FromArgb(96, 0, 0, 0));
// Add our song-beat-effect. Remember to uncomment the update in the spectrum effect if you want to remove this.
_keyboard.AttachEffect(new SongBeatEffect(_soundDataProcessor, Color.FromArgb(127, 164, 164, 164)));
ListKeyGroup songBeatGroup = new ListKeyGroup(_keyboard, _keyboard);
songBeatGroup.Brush = new SolidColorBrush(Color.FromArgb(127, 164, 164, 164));
songBeatGroup.Brush.AddEffect(new SongBeatEffect(_soundDataProcessor));
// Add our spectrum-effect using the soundDataProcessor and a rainbow from purple to red as gradient
_keyboard.AttachEffect(new AudioSpectrumEffect(_soundDataProcessor, new RainbowGradient(300, -14)));
ListKeyGroup spectrumGroup = new ListKeyGroup(_keyboard, _keyboard);
spectrumGroup.Brush = new AudioSpectrumBrush(_soundDataProcessor, new RainbowGradient(300, -14));
// Hook onto the keyboard update and process data
_keyboard.Updating += (sender, args) => _soundDataProcessor.Process();
// If you don't like rainbows replace the gradient with anything you like. For example:
//_keyboard.AttachEffect(new AudioSpectrumEffect(_soundDataProcessor, new LinearGradient(new GradientStop(0f, Color.Blue), new GradientStop(1f, Color.Red))));

View File

@ -11,16 +11,18 @@ namespace Example_AudioAnalyzer_full
{
#region Properties & Fields
public float[] BarData { get; set; }
private SoundDataProcessor _soundDataProcessor;
#endregion
#region Constructors
// default values for start/end are fine
public AudioSpectrumBrush(IGradient gradient)
public AudioSpectrumBrush(SoundDataProcessor soundDataProcessor, IGradient gradient)
: base(gradient)
{ }
{
this._soundDataProcessor = soundDataProcessor;
}
#endregion
@ -28,11 +30,11 @@ namespace Example_AudioAnalyzer_full
public override Color GetColorAtPoint(RectangleF rectangle, PointF point)
{
if (BarData == null) return Color.Transparent;
if (_soundDataProcessor?.BarValues == null) return Color.Transparent;
// This logic is also stolen from AterialDawn
int barSampleIndex = (int)Math.Floor(BarData.Length * (point.X / (rectangle.X + rectangle.Width))); // Calculate bar sampling index
float curBarHeight = 1f - Utility.Clamp(BarData[barSampleIndex], 0f, 1f); // Invert this value since the keyboard is laid out with topleft being point 0,0
int barSampleIndex = (int)Math.Floor(_soundDataProcessor.BarValues.Length * (point.X / (rectangle.X + rectangle.Width))); // Calculate bar sampling index
float curBarHeight = 1f - Utility.Clamp(_soundDataProcessor.BarValues[barSampleIndex], 0f, 1f); // Invert this value since the keyboard is laid out with topleft being point 0,0
float verticalPos = (point.Y / rectangle.Height);
// If the barHeight is lower than the vertical pos currently calculated return the brush value. Otherwise do nothing by returning transparent.

View File

@ -1,41 +0,0 @@
using CUE.NET.Brushes;
using CUE.NET.Effects;
using CUE.NET.Gradients;
using Example_AudioAnalyzer_full.TakeAsIs;
namespace Example_AudioAnalyzer_full
{
public class AudioSpectrumEffect : AbstractEffect
{
#region Properties & Fields
private SoundDataProcessor _dataProcessor;
private AudioSpectrumBrush _audioSpectrumBrush;
public override IBrush EffectBrush => _audioSpectrumBrush;
#endregion
#region Constructors
public AudioSpectrumEffect(SoundDataProcessor dataProcessor, IGradient gradient)
{
this._dataProcessor = dataProcessor;
_audioSpectrumBrush = new AudioSpectrumBrush(gradient);
// Give this effect a high Z-Index to keep it in the foreground
ZIndex = 10;
}
#endregion
#region Methods
public override void Update(float deltaTime)
{
_audioSpectrumBrush.BarData = _dataProcessor.BarValues;
}
#endregion
}
}

View File

@ -62,7 +62,6 @@
<ItemGroup>
<Compile Include="AudioAnalyzerExample.cs" />
<Compile Include="AudioSpectrumBrush.cs" />
<Compile Include="AudioSpectrumEffect.cs" />
<Compile Include="SongBeatEffect.cs" />
<Compile Include="TakeAsIs\BassHelper.cs" />
<Compile Include="TakeAsIs\SoundDataProcessor.cs" />

View File

@ -1,11 +1,9 @@
using System.Drawing;
using CUE.NET.Brushes;
using CUE.NET.Effects;
using CUE.NET.Effects;
using Example_AudioAnalyzer_full.TakeAsIs;
namespace Example_AudioAnalyzer_full
{
public class SongBeatEffect : AbstractEffect
public class SongBeatEffect : AbstractBrushEffect
{
#region Constants
@ -22,17 +20,13 @@ namespace Example_AudioAnalyzer_full
private float _currentEffect = -1f;
private SolidColorBrush _brush;
public override IBrush EffectBrush => _brush;
#endregion
#region Constructors
public SongBeatEffect(SoundDataProcessor dataProcessor, Color color)
public SongBeatEffect(SoundDataProcessor dataProcessor)
{
this._dataProcessor = dataProcessor;
_brush = new SolidColorBrush(color);
}
#endregion
@ -48,7 +42,7 @@ namespace Example_AudioAnalyzer_full
_currentEffect -= deltaTime;
// and set the current brush-opacity
_brush.Opacity = _currentEffect > 0f ? (_currentEffect / FLASH_DURATION) : 0f;
Brush.Opacity = _currentEffect > 0f ? (_currentEffect / FLASH_DURATION) : 0f;
}
#endregion