1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-12 17:48:31 +00:00

Added basic effects

This commit is contained in:
Darth Affe 2017-01-26 20:48:47 +01:00
parent 7d3ed9dcbd
commit 56e7524aad
8 changed files with 386 additions and 0 deletions

View File

@ -0,0 +1,54 @@
// ReSharper disable MemberCanBePrivate.Global
namespace RGB.NET.Core
{
/// <summary>
/// Represents a basic effect targeting an <see cref="IBrush"/>.
/// </summary>
public abstract class AbstractBrushEffect<T> : IEffect<IBrush>
where T : IBrush
{
#region Properties & Fields
/// <inheritdoc />
public bool IsDone { get; protected set; }
/// <summary>
/// Gets the <see cref="IBrush"/> this effect is targeting.
/// </summary>
protected T Brush { get; set; }
#endregion
#region Methods
/// <inheritdoc />
public abstract void Update(double deltaTime);
/// <inheritdoc />
public virtual bool CanBeAppliedTo(IBrush target)
{
return target is T;
}
/// <inheritdoc />
public virtual void OnAttach(IBrush target)
{
Brush = (T)target;
}
/// <inheritdoc />
public virtual void OnDetach(IBrush target)
{
Brush = default(T);
}
#endregion
}
/// <summary>
/// Represents a basic effect targeting an <see cref="IBrush"/>.
/// </summary>
public abstract class AbstractBrushEffect : AbstractBrushEffect<IBrush>
{ }
}

View File

@ -0,0 +1,56 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable UnusedAutoPropertyAccessor.Global
// ReSharper disable UnusedMember.Global
namespace RGB.NET.Core
{
/// <summary>
/// Represents a basic effect targeting an <see cref="ILedGroup"/>.
/// </summary>
public abstract class AbstractLedGroupEffect<T> : IEffect<ILedGroup>
where T : ILedGroup
{
#region Properties & Fields
/// <inheritdoc />
public bool IsDone { get; protected set; }
/// <summary>
/// Gets the <see cref="ILedGroup"/> this effect is targeting.
/// </summary>
protected T LedGroup { get; set; }
#endregion
#region Methods
/// <inheritdoc />
public abstract void Update(double deltaTime);
/// <inheritdoc />
public virtual bool CanBeAppliedTo(ILedGroup target)
{
return target is T;
}
/// <inheritdoc />
public virtual void OnAttach(ILedGroup target)
{
LedGroup = (T)target;
}
/// <inheritdoc />
public virtual void OnDetach(ILedGroup target)
{
LedGroup = default(T);
}
#endregion
}
/// <summary>
/// Represents a basic effect targeting an <see cref="ILedGroup"/>.
/// </summary>
public abstract class AbstractLedGroupEffect : AbstractLedGroupEffect<ILedGroup>
{ }
}

View File

@ -51,7 +51,9 @@
<Compile Include="Devices\RGBDeviceType.cs" />
<Compile Include="Devices\IRGBDeviceProvider.cs" />
<Compile Include="Devices\IRGBDeviceInfo.cs" />
<Compile Include="Effects\AbstractBrushEffect.cs" />
<Compile Include="Effects\AbstractEffectTarget.cs" />
<Compile Include="Effects\AbstractLedGroupEffect.cs" />
<Compile Include="Effects\EffectTimeContainer.cs" />
<Compile Include="Effects\IEffect.cs" />
<Compile Include="Effects\IEffectTarget.cs" />

View File

@ -0,0 +1,153 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using RGB.NET.Core;
namespace RGB.NET.Effects
{
/// <summary>
/// Represents an effect which allows to flash an brush by modifying his opacity.
/// </summary>
public class FlashEffect : AbstractBrushEffect
{
#region Properties & Fields
/// <summary>
/// Gets or sets the attack-time (in seconds) of the effect. (default: 0.2)<br />
/// This is close to a synthesizer envelope. (See <see href="http://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope" /> as reference)
/// </summary>
public double Attack { get; set; } = 0.2;
/// <summary>
/// Gets or sets the decay-time (in seconds) of the effect. (default: 0)<br />
/// This is close to a synthesizer envelope. (See <see href="http://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope" /> as reference)
/// </summary>
public double Decay { get; set; } = 0;
/// <summary>
/// Gets or sets the sustain-time (in seconds) of the effect. (default: 0.3)<br />
/// This is close to a synthesizer envelope. (See <see href="http://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope" /> as reference)<br />
/// Note that this value for naming reasons represents the time NOT the level.
/// </summary>
public double Sustain { get; set; } = 0.3;
/// <summary>
/// Gets or sets the release-time (in seconds) of the effect. (default: 0.2)<br />
/// This is close to a synthesizer envelope. (See <see href="http://en.wikipedia.org/wiki/Synthesizer#ADSR_envelope" /> as reference)
/// </summary>
public double Release { get; set; } = 0.2;
/// <summary>
/// Gets or sets the level to which the oppacity (percentage) should raise in the attack-cycle. (default: 1);
/// </summary>
public double AttackValue { get; set; } = 1;
/// <summary>
/// Gets or sets the level at which the oppacity (percentage) should stay in the sustain-cycle. (default: 1);
/// </summary>
public double SustainValue { get; set; } = 1;
/// <summary>
/// Gets or sets the interval (in seconds) in which the effect should repeat (if repetition is enabled). (default: 1)
/// </summary>
public double Interval { get; set; } = 1;
/// <summary>
/// Gets or sets the amount of repetitions the effect should do until it's finished. Zero means infinite. (default: 0)
/// </summary>
public int Repetitions { get; set; } = 0;
private ADSRPhase _currentPhase;
private double _currentPhaseValue;
private int _repetitionCount;
#endregion
#region Methods
/// <inheritdoc />
public override void Update(double deltaTime)
{
_currentPhaseValue -= deltaTime;
// Using ifs instead of a switch allows to skip phases with time 0.
// ReSharper disable InvertIf
if (_currentPhase == ADSRPhase.Attack)
if (_currentPhaseValue > 0)
Brush.Opacity = Math.Min(1, (Attack - _currentPhaseValue) / Attack) * AttackValue;
else
{
_currentPhaseValue = Decay;
_currentPhase = ADSRPhase.Decay;
}
if (_currentPhase == ADSRPhase.Decay)
if (_currentPhaseValue > 0)
Brush.Opacity = SustainValue + (Math.Min(1, _currentPhaseValue / Decay) * (AttackValue - SustainValue));
else
{
_currentPhaseValue = Sustain;
_currentPhase = ADSRPhase.Sustain;
}
if (_currentPhase == ADSRPhase.Sustain)
if (_currentPhaseValue > 0)
Brush.Opacity = SustainValue;
else
{
_currentPhaseValue = Release;
_currentPhase = ADSRPhase.Release;
}
if (_currentPhase == ADSRPhase.Release)
if (_currentPhaseValue > 0)
Brush.Opacity = Math.Min(1, _currentPhaseValue / Release) * SustainValue;
else
{
_currentPhaseValue = Interval;
_currentPhase = ADSRPhase.Pause;
}
if (_currentPhase == ADSRPhase.Pause)
if (_currentPhaseValue > 0)
Brush.Opacity = 0;
else
{
if ((++_repetitionCount >= Repetitions) && (Repetitions > 0))
IsDone = true;
_currentPhaseValue = Attack;
_currentPhase = ADSRPhase.Attack;
}
// ReSharper restore InvertIf
}
/// <summary>
/// Resets the effect.
/// </summary>
public override void OnAttach(IBrush brush)
{
base.OnAttach(brush);
_currentPhase = ADSRPhase.Attack;
_currentPhaseValue = Attack;
_repetitionCount = 0;
brush.Opacity = 0;
}
#endregion
// ReSharper disable once InconsistentNaming
private enum ADSRPhase
{
Attack,
Decay,
Sustain,
Release,
Pause
}
}
}

View File

@ -0,0 +1,101 @@
using RGB.NET.Brushes;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace RGB.NET.Effects
{
/// <summary>
/// Represents an effect which allows to move an <see cref="IGradient"/> by modifying his offset.
/// </summary>
public class MoveGradientEffect : AbstractBrushEffect<IGradientBrush>
{
#region Properties & Fields
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable MemberCanBePrivate.Global
/// <summary>
/// Gets or sets the direction the <see cref="IGradient"/> is moved.
/// True leads to an offset-increment (normaly moving to the right), false to an offset-decrement (normaly moving to the left).
/// </summary>
public bool Direction { get; set; }
/// <summary>
/// Gets or sets the speed of the movement in units per second.
/// The meaning of units differs for the different <see cref="IGradient"/> , but 360 units will always be one complete cycle:
/// <see cref="LinearGradient"/>: 360 unit = 1 offset.
/// <see cref="RainbowGradient"/>: 1 unit = 1 degree.
/// </summary>
public double Speed { get; set; }
// ReSharper restore MemberCanBePrivate.Global
// ReSharper restore AutoPropertyCanBeMadeGetOnly.Global
#endregion
#region Constructors
/// <summary>
///
/// </summary>
/// <param name="speed"></param>
/// <param name="direction"></param>
public MoveGradientEffect(double speed = 180.0, bool direction = true)
{
this.Speed = speed;
this.Direction = direction;
}
#endregion
#region Methods
/// <inheritdoc />
public override void Update(double deltaTime)
{
double movement = Speed * deltaTime;
if (!Direction)
movement = -movement;
// ReSharper disable once CanBeReplacedWithTryCastAndCheckForNull
if (Brush.Gradient is LinearGradient)
{
LinearGradient linearGradient = (LinearGradient)Brush.Gradient;
movement /= 360.0;
foreach (GradientStop gradientStop in linearGradient.GradientStops)
{
gradientStop.Offset = gradientStop.Offset + movement;
if (gradientStop.Offset > 1)
gradientStop.Offset -= 1;
else if (gradientStop.Offset < 0)
gradientStop.Offset += 1;
}
}
else if (Brush.Gradient is RainbowGradient)
{
RainbowGradient rainbowGradient = (RainbowGradient)Brush.Gradient;
// RainbowGradient is calculated inverse but the movement should be the same for all.
movement *= -1;
rainbowGradient.StartHue += movement;
rainbowGradient.EndHue += movement;
if ((rainbowGradient.StartHue > 360) && (rainbowGradient.EndHue > 360))
{
rainbowGradient.StartHue -= 360;
rainbowGradient.EndHue -= 360;
}
else if ((rainbowGradient.StartHue < -360) && (rainbowGradient.EndHue < -360))
{
rainbowGradient.StartHue += 360;
rainbowGradient.EndHue += 360;
}
}
}
#endregion
}
}

View File

@ -31,6 +31,14 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="RGB.NET.Brushes, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RGB.NET.Brushes.1.0.0\lib\net45\RGB.NET.Brushes.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="RGB.NET.Core, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\RGB.NET.Core.1.0.0\lib\net45\RGB.NET.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
@ -41,8 +49,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Effects\FlashEffect.cs" />
<Compile Include="Effects\MoveGradientEffect.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

View File

@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=effects/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="RGB.NET.Brushes" version="1.0.0" targetFramework="net45" />
<package id="RGB.NET.Core" version="1.0.0" targetFramework="net45" />
</packages>