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

@@ -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
}
+71
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
}
}
+54
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
}
}
+1 -6
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
+9 -36
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
+17 -22
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
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
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);
}
}
}
}