1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Layer effects - Fixed placeholder system

This commit is contained in:
Robert 2022-11-12 22:32:04 +01:00
parent 9681231215
commit 24cb397556
6 changed files with 49 additions and 32 deletions

View File

@ -273,7 +273,7 @@ public abstract class RenderProfileElement : ProfileElement
// If no descriptor was found and there was no existing placeholder, create a placeholder // If no descriptor was found and there was no existing placeholder, create a placeholder
else else
{ {
descriptor = PlaceholderLayerEffectDescriptor.Create(layerEffectEntity.ProviderId); descriptor = PlaceholderLayerEffectDescriptor.Create();
layerEffect = descriptor.CreateInstance(this, layerEffectEntity); layerEffect = descriptor.CreateInstance(this, layerEffectEntity);
} }
@ -286,7 +286,7 @@ public abstract class RenderProfileElement : ProfileElement
if (index == -1) if (index == -1)
return; return;
LayerEffectDescriptor descriptor = PlaceholderLayerEffectDescriptor.Create(layerEffect.ProviderId); LayerEffectDescriptor descriptor = PlaceholderLayerEffectDescriptor.Create();
BaseLayerEffect placeholder = descriptor.CreateInstance(this, layerEffect.LayerEffectEntity); BaseLayerEffect placeholder = descriptor.CreateInstance(this, layerEffect.LayerEffectEntity);
_layerEffects[index] = placeholder; _layerEffects[index] = placeholder;
layerEffect.Dispose(); layerEffect.Dispose();
@ -299,7 +299,7 @@ public abstract class RenderProfileElement : ProfileElement
if (index == -1) if (index == -1)
return; return;
LayerEffectDescriptor? descriptor = LayerEffectStore.Get(placeholder.OriginalEntity.ProviderId, placeholder.PlaceholderFor)?.LayerEffectDescriptor; LayerEffectDescriptor? descriptor = LayerEffectStore.Get(placeholder.OriginalEntity.ProviderId, placeholder.OriginalEntity.EffectType)?.LayerEffectDescriptor;
if (descriptor == null) if (descriptor == null)
throw new ArtemisCoreException("Can't replace a placeholder effect because the real effect isn't available."); throw new ArtemisCoreException("Can't replace a placeholder effect because the real effect isn't available.");
@ -324,7 +324,7 @@ public abstract class RenderProfileElement : ProfileElement
private void LayerEffectStoreOnLayerEffectRemoved(object? sender, LayerEffectStoreEvent e) private void LayerEffectStoreOnLayerEffectRemoved(object? sender, LayerEffectStoreEvent e)
{ {
// Find effects that just got disabled and replace them with placeholders // Find effects that just got disabled and replace them with placeholders
List<BaseLayerEffect> affectedLayerEffects = _layerEffects.Where(ef => ef.ProviderId == e.Registration.PluginFeature.Id).ToList(); List<BaseLayerEffect> affectedLayerEffects = _layerEffects.Where(e.Registration.Matches).ToList();
if (!affectedLayerEffects.Any()) if (!affectedLayerEffects.Any())
return; return;
@ -338,7 +338,7 @@ public abstract class RenderProfileElement : ProfileElement
{ {
// Find placeholders that just got enabled and replace them with real effects // Find placeholders that just got enabled and replace them with real effects
List<PlaceholderLayerEffect> affectedPlaceholders = LayerEffects List<PlaceholderLayerEffect> affectedPlaceholders = LayerEffects
.Where(l => l is PlaceholderLayerEffect ph && ph.OriginalEntity.ProviderId == e.Registration.PluginFeature.Id) .Where(l => l is PlaceholderLayerEffect ph && e.Registration.Matches(ph))
.Cast<PlaceholderLayerEffect>() .Cast<PlaceholderLayerEffect>()
.ToList(); .ToList();

View File

@ -164,12 +164,7 @@ public abstract class BaseLayerEffect : BreakableModel, IDisposable, IStorageMod
// Not only is this needed to initialize properties on the layer effects, it also prevents implementing anything // Not only is this needed to initialize properties on the layer effects, it also prevents implementing anything
// but LayerEffect<T> outside the core // but LayerEffect<T> outside the core
internal abstract void Initialize(); internal abstract void Initialize();
internal virtual string GetEffectTypeName()
{
return GetType().Name;
}
internal void InternalUpdate(Timeline timeline) internal void InternalUpdate(Timeline timeline)
{ {
BaseProperties?.Update(timeline); BaseProperties?.Update(timeline);
@ -220,20 +215,23 @@ public abstract class BaseLayerEffect : BreakableModel, IDisposable, IStorageMod
/// <inheritdoc /> /// <inheritdoc />
public void Load() public void Load()
{ {
HasBeenRenamed = LayerEffectEntity.HasBeenRenamed;
Name = HasBeenRenamed ? LayerEffectEntity.Name : Descriptor.DisplayName; Name = HasBeenRenamed ? LayerEffectEntity.Name : Descriptor.DisplayName;
HasBeenRenamed = LayerEffectEntity.HasBeenRenamed;
Order = LayerEffectEntity.Order; Order = LayerEffectEntity.Order;
} }
/// <inheritdoc /> /// <inheritdoc />
public void Save() public void Save()
{ {
LayerEffectEntity.ProviderId = Descriptor.Provider.Id;
LayerEffectEntity.EffectType = GetType().FullName;
LayerEffectEntity.Name = Name; LayerEffectEntity.Name = Name;
LayerEffectEntity.HasBeenRenamed = HasBeenRenamed; LayerEffectEntity.HasBeenRenamed = HasBeenRenamed;
LayerEffectEntity.Order = Order; LayerEffectEntity.Order = Order;
if (Descriptor.IsPlaceholder)
return;
LayerEffectEntity.ProviderId = Descriptor.Provider.Id;
LayerEffectEntity.EffectType = GetType().FullName;
BaseProperties?.ApplyToEntity(); BaseProperties?.ApplyToEntity();
LayerEffectEntity.PropertyGroup = BaseProperties?.PropertyGroupEntity; LayerEffectEntity.PropertyGroup = BaseProperties?.PropertyGroupEntity;
} }

View File

@ -19,9 +19,9 @@ public class LayerEffectDescriptor
Provider = provider ?? throw new ArgumentNullException(nameof(provider)); Provider = provider ?? throw new ArgumentNullException(nameof(provider));
} }
internal LayerEffectDescriptor(string placeholderFor, LayerEffectProvider provider) private LayerEffectDescriptor(LayerEffectProvider provider)
{ {
PlaceholderFor = placeholderFor ?? throw new ArgumentNullException(nameof(placeholderFor)); IsPlaceholder = true;
Provider = provider ?? throw new ArgumentNullException(nameof(provider)); Provider = provider ?? throw new ArgumentNullException(nameof(provider));
DisplayName = "Missing effect"; DisplayName = "Missing effect";
Description = "This effect could not be loaded"; Description = "This effect could not be loaded";
@ -55,16 +55,21 @@ public class LayerEffectDescriptor
public LayerEffectProvider Provider { get; } public LayerEffectProvider Provider { get; }
/// <summary> /// <summary>
/// Gets the GUID this descriptor is acting as a placeholder for. If null, this descriptor is not a placeholder /// Gets a boolean indicating whether this descriptor is a placeholder descriptor.
/// </summary> /// </summary>
public string? PlaceholderFor { get; } public bool IsPlaceholder { get; }
internal static LayerEffectDescriptor CreatePlaceholder(LayerEffectProvider provider)
{
return new LayerEffectDescriptor(provider);
}
/// <summary> /// <summary>
/// Creates an instance of the described effect and applies it to the render element /// Creates an instance of the described effect and applies it to the render element
/// </summary> /// </summary>
public BaseLayerEffect CreateInstance(RenderProfileElement renderElement, LayerEffectEntity? entity) public BaseLayerEffect CreateInstance(RenderProfileElement renderElement, LayerEffectEntity? entity)
{ {
if (PlaceholderFor != null) if (IsPlaceholder)
{ {
if (entity == null) if (entity == null)
throw new ArtemisCoreException("Cannot create a placeholder for a layer effect that wasn't loaded from an entity"); throw new ArtemisCoreException("Cannot create a placeholder for a layer effect that wasn't loaded from an entity");
@ -97,10 +102,10 @@ public class LayerEffectDescriptor
private BaseLayerEffect CreatePlaceHolderInstance(RenderProfileElement renderElement, LayerEffectEntity entity) private BaseLayerEffect CreatePlaceHolderInstance(RenderProfileElement renderElement, LayerEffectEntity entity)
{ {
if (PlaceholderFor == null) if (!IsPlaceholder)
throw new ArtemisCoreException("Cannot create a placeholder instance using a layer effect descriptor that is not a placeholder for anything"); throw new ArtemisCoreException("Cannot create a placeholder instance using a layer effect descriptor that is not a placeholder for anything");
PlaceholderLayerEffect effect = new(entity, PlaceholderFor) PlaceholderLayerEffect effect = new(entity)
{ {
ProfileElement = renderElement, ProfileElement = renderElement,
Descriptor = this Descriptor = this

View File

@ -8,10 +8,9 @@ namespace Artemis.Core.LayerEffects.Placeholder;
/// </summary> /// </summary>
internal class PlaceholderLayerEffect : LayerEffect<PlaceholderProperties> internal class PlaceholderLayerEffect : LayerEffect<PlaceholderProperties>
{ {
internal PlaceholderLayerEffect(LayerEffectEntity originalEntity, string placeholderFor) internal PlaceholderLayerEffect(LayerEffectEntity originalEntity)
{ {
OriginalEntity = originalEntity; OriginalEntity = originalEntity;
PlaceholderFor = placeholderFor;
LayerEffectEntity = originalEntity; LayerEffectEntity = originalEntity;
Order = OriginalEntity.Order; Order = OriginalEntity.Order;
@ -19,8 +18,6 @@ internal class PlaceholderLayerEffect : LayerEffect<PlaceholderProperties>
HasBeenRenamed = OriginalEntity.HasBeenRenamed; HasBeenRenamed = OriginalEntity.HasBeenRenamed;
} }
public string PlaceholderFor { get; }
internal LayerEffectEntity OriginalEntity { get; } internal LayerEffectEntity OriginalEntity { get; }
/// <inheritdoc /> /// <inheritdoc />
@ -47,11 +44,6 @@ internal class PlaceholderLayerEffect : LayerEffect<PlaceholderProperties>
public override void PostProcess(SKCanvas canvas, SKRect bounds, SKPaint paint) public override void PostProcess(SKCanvas canvas, SKRect bounds, SKPaint paint)
{ {
} }
internal override string GetEffectTypeName()
{
return OriginalEntity.EffectType;
}
} }
/// <summary> /// <summary>

View File

@ -2,9 +2,9 @@
internal static class PlaceholderLayerEffectDescriptor internal static class PlaceholderLayerEffectDescriptor
{ {
public static LayerEffectDescriptor Create(string missingProviderId) public static LayerEffectDescriptor Create()
{ {
LayerEffectDescriptor descriptor = new(missingProviderId, Constants.EffectPlaceholderPlugin); LayerEffectDescriptor descriptor = LayerEffectDescriptor.CreatePlaceholder(Constants.EffectPlaceholderPlugin);
return descriptor; return descriptor;
} }
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using Artemis.Core.LayerEffects; using Artemis.Core.LayerEffects;
using Artemis.Core.LayerEffects.Placeholder;
namespace Artemis.Core; namespace Artemis.Core;
@ -37,4 +38,25 @@ public class LayerEffectRegistration
if (IsInStore) if (IsInStore)
LayerEffectStore.Remove(this); LayerEffectStore.Remove(this);
} }
/// <summary>
/// Determines whether the provided placeholder matches this event.
/// </summary>
/// <param name="placeholder">The placeholder to check</param>
/// <returns><see langword="true" /> if the placeholder is for the provided layer effect registration, otherwise <see langword="false" />.</returns>
internal bool Matches(PlaceholderLayerEffect placeholder)
{
return placeholder.OriginalEntity.ProviderId == PluginFeature.Id &&
placeholder.OriginalEntity.EffectType == LayerEffectDescriptor.LayerEffectType?.FullName;
}
/// <summary>
/// Determines whether the provided layer effect matches this event.
/// </summary>
/// <param name="layerEffect">The layer effect to check</param>
/// <returns><see langword="true" /> if the placeholder is for the provided layer effect registration, otherwise <see langword="false" />.</returns>
internal bool Matches(BaseLayerEffect layerEffect)
{
return layerEffect.Descriptor == LayerEffectDescriptor;
}
} }