12public sealed class EffectsManager : IDisposable
14 private readonly
object _sync =
new();
15 private readonly List<DisplayEffect> _activeEffects = [];
17 private long? _lastTick;
18 private bool _disposed;
21 _host = host ??
throw new ArgumentNullException(nameof(host));
29 return _activeEffects.ToList().AsReadOnly();
53 ArgumentNullException.ThrowIfNull(effect);
58 removed = _activeEffects.Remove(effect);
63 effect.CancelInternal(restoreState:
true);
64 Invalidate(effect.Target);
74 snapshot = _activeEffects.ToArray();
75 _activeEffects.Clear();
78 foreach (var effect
in snapshot)
80 effect.CancelInternal(restoreState:
true);
81 Invalidate(effect.Target);
85 internal void Update(
long tick)
87 if (!_lastTick.HasValue)
93 float deltaSeconds = HighResTimer.GetDuration(_lastTick.Value, tick);
95 Advance(deltaSeconds);
98 internal void Advance(
float deltaSeconds)
100 DisplayEffect[] snapshot;
103 snapshot = _activeEffects.ToArray();
105 foreach (var effect
in snapshot)
107 if (!OwnsTarget(effect.Target))
109 RemoveAndCancel(effect, restoreState:
false);
113 bool finished = effect.AdvanceInternal(deltaSeconds);
114 Invalidate(effect.Target);
119 _activeEffects.Remove(effect);
124 internal void Invalidate(
object target)
126 if (target is View or SceneLayer)
127 _host.Scene.FullRefreshNeeded =
true;
130 private TEffect RunCore<TEffect>(
object target, TEffect effect)
131 where TEffect : DisplayEffect
133 ObjectDisposedException.ThrowIf(_disposed,
this);
134 ArgumentNullException.ThrowIfNull(target);
135 ArgumentNullException.ThrowIfNull(effect);
137 if (!OwnsTarget(target))
138 throw new ArgumentException(
139 "The effect target is not owned by this render surface host.",
142 if (!effect.SupportsTarget(target))
143 throw new ArgumentException(
144 $
"{effect.GetType().Name} does not support {target.GetType().Name} targets.",
147 DisplayEffect? replaced;
152 throw new InvalidOperationException(
"An effect instance can only be run once.");
154 replaced = _activeEffects.FirstOrDefault(active => ReferenceEquals(active.Target, target)
155 && active.Channel == effect.Channel);
157 if (replaced is not
null)
158 _activeEffects.Remove(replaced);
160 _activeEffects.Add(effect);
165 replaced?.CancelInternal(restoreState:
false);
169 effect.StartInternal(
this, target);
175 _activeEffects.Remove(effect);
183 _activeEffects.Remove(effect);
189 private bool OwnsTarget(
object target) => target
switch
191 View view => _host.ViewManager.Views.Contains(view),
193 && _host.Scene.SceneLayers.Contains(layer),
197 private void RemoveAndCancel(DisplayEffect effect,
bool restoreState)
200 _activeEffects.Remove(effect);
202 effect.CancelInternal(restoreState);