|
| 1 | +using LCDPossible.Core.Services; |
| 2 | + |
| 3 | +namespace LCDPossible.Core.Effects; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Default implementation of the effect registry with built-in effects. |
| 7 | +/// </summary> |
| 8 | +public sealed class EffectRegistry : IEffectRegistry, IDisposable |
| 9 | +{ |
| 10 | + private readonly Dictionary<string, Func<IVisualEffect>> _effectFactories = new(StringComparer.OrdinalIgnoreCase); |
| 11 | + private readonly List<IVisualEffect> _activeEffects = []; |
| 12 | + private bool _disposed; |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Creates a new effect registry with built-in effects registered. |
| 16 | + /// </summary> |
| 17 | + public EffectRegistry() |
| 18 | + { |
| 19 | + // Register built-in effects |
| 20 | + RegisterEffect("vignette", () => new VignetteEffect()); |
| 21 | + RegisterEffect("scanlines", () => new ScanlinesEffect()); |
| 22 | + RegisterEffect("noise", () => new NoiseEffect()); |
| 23 | + RegisterEffect("glow", () => new GlowEffect()); |
| 24 | + RegisterEffect("color-tint", () => new ColorTintEffect()); |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Registers an effect factory. |
| 29 | + /// </summary> |
| 30 | + /// <param name="effectId">Unique effect identifier.</param> |
| 31 | + /// <param name="factory">Factory function to create effect instances.</param> |
| 32 | + public void RegisterEffect(string effectId, Func<IVisualEffect> factory) |
| 33 | + { |
| 34 | + _effectFactories[effectId] = factory; |
| 35 | + } |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public IReadOnlyList<EffectTypeInfo> GetEffectTypes() |
| 39 | + { |
| 40 | + var types = new List<EffectTypeInfo>(); |
| 41 | + |
| 42 | + foreach (var (id, factory) in _effectFactories) |
| 43 | + { |
| 44 | + using var effect = factory(); |
| 45 | + types.Add(new EffectTypeInfo(id, effect.DisplayName)); |
| 46 | + } |
| 47 | + |
| 48 | + return types; |
| 49 | + } |
| 50 | + |
| 51 | + /// <inheritdoc /> |
| 52 | + public IVisualEffect? CreateEffect(string effectId) |
| 53 | + { |
| 54 | + return _effectFactories.TryGetValue(effectId, out var factory) ? factory() : null; |
| 55 | + } |
| 56 | + |
| 57 | + /// <inheritdoc /> |
| 58 | + public IReadOnlyList<IVisualEffect> ActiveEffects => _activeEffects.AsReadOnly(); |
| 59 | + |
| 60 | + /// <inheritdoc /> |
| 61 | + public void AddEffect(string effectId) |
| 62 | + { |
| 63 | + var effect = CreateEffect(effectId); |
| 64 | + if (effect != null) |
| 65 | + { |
| 66 | + _activeEffects.Add(effect); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// <inheritdoc /> |
| 71 | + public void RemoveEffect(string effectId) |
| 72 | + { |
| 73 | + var effect = _activeEffects.FirstOrDefault(e => e.EffectId.Equals(effectId, StringComparison.OrdinalIgnoreCase)); |
| 74 | + if (effect != null) |
| 75 | + { |
| 76 | + _activeEffects.Remove(effect); |
| 77 | + effect.Dispose(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// <inheritdoc /> |
| 82 | + public void ClearEffects() |
| 83 | + { |
| 84 | + foreach (var effect in _activeEffects) |
| 85 | + { |
| 86 | + effect.Dispose(); |
| 87 | + } |
| 88 | + _activeEffects.Clear(); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Applies all active effects to an image in order. |
| 93 | + /// </summary> |
| 94 | + /// <param name="image">The image to apply effects to.</param> |
| 95 | + public void ApplyAll(SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32> image) |
| 96 | + { |
| 97 | + foreach (var effect in _activeEffects) |
| 98 | + { |
| 99 | + if (effect.IsEnabled) |
| 100 | + { |
| 101 | + effect.Apply(image); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// <inheritdoc /> |
| 107 | + public void Dispose() |
| 108 | + { |
| 109 | + if (_disposed) return; |
| 110 | + _disposed = true; |
| 111 | + |
| 112 | + ClearEffects(); |
| 113 | + GC.SuppressFinalize(this); |
| 114 | + } |
| 115 | +} |
0 commit comments