Replaced effects with decorators.

Decorators should be way more flexible while beeing easy to use.
This commit is contained in:
DarthAffe committed 2017-09-05 13:44:23 +02:00
1 parent 1f3213fa73
commit 2086b3729d
38 files changed
+414 -552

No files matched your search

+156
View File
@@ -0,0 +1,156 @@
// ReSharper disable MemberCanBePrivate.Global
// ReSharper disable AutoPropertyCanBeMadeGetOnly.Global
// ReSharper disable UnusedMember.Global
using System;
using RGB.NET.Core;
namespace RGB.NET.Decorators.Brush
{
/// <summary>
/// Represents a decorator which allows to flash a brush by modifying his opacity.
/// </summary>
public class FlashDecorator : AbstractUpdateAwareDecorator, IBrushDecorator
{
#region Properties & Fields
/// <summary>
/// Gets or sets the attack-time (in seconds) of the decorator. (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 decorator. (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 decorator. (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 decorator. (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 decorator should repeat (if repetition is enabled). (default: 1)
/// </summary>
public double Interval { get; set; } = 1;
/// <summary>
/// Gets or sets the amount of repetitions the decorator 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;
private double _currentValue;
#endregion
#region Methods
/// <inheritdoc />
public void ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, ref Color color) => color.APercent = _currentValue;
/// <inheritdoc />
protected 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)
_currentValue = Math.Min(1, (Attack - _currentPhaseValue) / Attack) * AttackValue;
else
{
_currentPhaseValue = Decay;
_currentPhase = ADSRPhase.Decay;
}
if (_currentPhase == ADSRPhase.Decay)
if (_currentPhaseValue > 0)
_currentValue = SustainValue + (Math.Min(1, _currentPhaseValue / Decay) * (AttackValue - SustainValue));
else
{
_currentPhaseValue = Sustain;
_currentPhase = ADSRPhase.Sustain;
}
if (_currentPhase == ADSRPhase.Sustain)
if (_currentPhaseValue > 0)
_currentValue = SustainValue;
else
{
_currentPhaseValue = Release;
_currentPhase = ADSRPhase.Release;
}
if (_currentPhase == ADSRPhase.Release)
if (_currentPhaseValue > 0)
_currentValue = Math.Min(1, _currentPhaseValue / Release) * SustainValue;
else
{
_currentPhaseValue = Interval;
_currentPhase = ADSRPhase.Pause;
}
if (_currentPhase == ADSRPhase.Pause)
if (_currentPhaseValue > 0)
_currentValue = 0;
else
{
if ((++_repetitionCount >= Repetitions) && (Repetitions > 0))
Detach<IBrush, FlashDecorator>();
_currentPhaseValue = Attack;
_currentPhase = ADSRPhase.Attack;
}
// ReSharper restore InvertIf
}
/// <inheritdoc />
public override void OnAttached(IDecoratable decoratable)
{
base.OnAttached(decoratable);
_currentPhase = ADSRPhase.Attack;
_currentPhaseValue = Attack;
_repetitionCount = 0;
_currentValue = 0;
}
#endregion
// ReSharper disable once InconsistentNaming
private enum ADSRPhase
{
Attack,
Decay,
Sustain,
Release,
Pause
}
}
}
@@ -0,0 +1,70 @@
using RGB.NET.Brushes;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace RGB.NET.Decorators.Gradient
{
/// <summary>
/// Represents a decorator which allows to move an <see cref="IGradient"/> by modifying his offset.
/// </summary>
public class MoveGradientDecorator : AbstractUpdateAwareDecorator, IGradientDecorator
{
#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>
/// Initializes a new instance of the <see cref="MoveGradientDecorator"/> class.
/// </summary>
/// <param name="speed">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.</param>
/// <param name="direction">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).</param>
public MoveGradientDecorator(double speed = 180.0, bool direction = true)
{
this.Speed = speed;
this.Direction = direction;
}
#endregion
#region Methods
/// <inheritdoc />
protected override void Update(double deltaTime)
{
double movement = Speed * deltaTime;
if (!Direction)
movement = -movement;
foreach (IDecoratable decoratedObject in DecoratedObjects)
if (decoratedObject is IGradient gradient)
gradient.Move(movement);
}
#endregion
}
}
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RGB.NET.Decorators")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RGB.NET.Decorators")]
[assembly: AssemblyCopyright("Copyright © 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("7012c431-244a-453f-b7fd-59e030cdbc44")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{7012C431-244A-453F-B7FD-59E030CDBC44}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RGB.NET.Decorators</RootNamespace>
<AssemblyName>RGB.NET.Decorators</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>..\bin\RGB.NET.Decorators.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>..\bin\RGB.NET.Decorators.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.ValueTuple, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.3.1\lib\netstandard1.0\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Brush\FlashDecorator.cs" />
<Compile Include="Gradient\MoveGradientDecorator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RGB.NET.Brushes\RGB.NET.Brushes.csproj">
<Project>{347c5f0f-f490-4dec-9c1c-6e84750d838d}</Project>
<Name>RGB.NET.Brushes</Name>
</ProjectReference>
<ProjectReference Include="..\RGB.NET.Core\RGB.NET.Core.csproj">
<Project>{5a4f9a75-75fe-47cd-90e5-914d5b20d232}</Project>
<Name>RGB.NET.Core</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
+6
View File
@@ -0,0 +1,6 @@
<?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" />
<package id="System.ValueTuple" version="4.3.1" targetFramework="net45" />
</packages>