1
0
mirror of https://github.com/DarthAffe/RGB.NET.git synced 2025-12-13 01:58:30 +00:00

Improved usabillity of decorators a bit

This commit is contained in:
Darth Affe 2020-07-08 21:24:56 +02:00
parent f229bb8938
commit 52fd670ba7
4 changed files with 27 additions and 21 deletions

View File

@ -12,18 +12,15 @@ namespace RGB.NET.Core
#region Properties & Fields
private readonly List<T> _decorators = new List<T>();
/// <summary>
/// Gets a readonly-list of all <see cref="IDecorator"/> attached to this <see cref="IDecoratable{T}"/>.
/// </summary>
protected IReadOnlyCollection<T> Decorators { get; }
#endregion
#region Constructors
protected AbstractDecoratable()
/// <inheritdoc />
public IReadOnlyCollection<T> Decorators
{
Decorators = new ReadOnlyCollection<T>(_decorators);
get
{
lock (_decorators)
return new ReadOnlyCollection<T>(_decorators);
}
}
#endregion

View File

@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
namespace RGB.NET.Core
{
@ -42,16 +44,17 @@ namespace RGB.NET.Core
/// <summary>
/// Detaches the decorator from all <see cref="IDecoratable"/> it is currently attached to.
/// </summary>
/// <typeparam name="TDecoratable">The type of the <see cref="IDecoratable"/> this decorator is attached to.</typeparam>
/// <typeparam name="TDecorator">The type of this <see cref="IDecorator"/>.</typeparam>
protected virtual void Detach<TDecoratable, TDecorator>()
where TDecoratable : IDecoratable<TDecorator>
where TDecorator : AbstractDecorator
protected virtual void Detach()
{
List<IDecoratable> decoratables = new List<IDecoratable>(DecoratedObjects);
foreach (IDecoratable decoratable in decoratables)
if (decoratable is TDecoratable typedDecoratable)
typedDecoratable.RemoveDecorator((TDecorator)this);
{
IEnumerable<Type> types = decoratable.GetType().GetInterfaces().Where(t => t.IsGenericType
&& (t.Name == typeof(IDecoratable<>).Name)
&& t.GenericTypeArguments[0].IsInstanceOfType(this));
foreach (Type decoratableType in types)
decoratableType.GetMethod(nameof(IDecoratable<IDecorator>.RemoveDecorator))?.Invoke(decoratable, new object[] { this });
}
}
#endregion

View File

@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Collections.Generic;
using System.ComponentModel;
namespace RGB.NET.Core
{
@ -13,9 +14,14 @@ namespace RGB.NET.Core
/// Represents a basic decoratable for a specific type of <see cref="T:RGB.NET.Core.IDecorator" />
/// </summary>
/// <typeparam name="T"></typeparam>
public interface IDecoratable<in T> : IDecoratable
public interface IDecoratable<T> : IDecoratable
where T : IDecorator
{
/// <summary>
/// Gets a readonly-list of all <see cref="IDecorator"/> attached to this <see cref="IDecoratable{T}"/>.
/// </summary>
IReadOnlyCollection<T> Decorators { get; }
/// <summary>
/// Adds an <see cref="IDecorator"/> to the <see cref="IDecoratable"/>.
/// </summary>

View File

@ -129,7 +129,7 @@ namespace RGB.NET.Decorators.Brush
else
{
if ((++_repetitionCount >= Repetitions) && (Repetitions > 0))
Detach<IBrush, FlashDecorator>();
Detach();
_currentPhaseValue = Attack;
_currentPhase = ADSRPhase.Attack;
}