Update drawable lifetime on entry lifetime change

This commit is contained in:
ekrctb 2021-05-31 14:37:28 +09:00
parent 8c6277f0d8
commit e25cca6fbe

View File

@ -3,6 +3,7 @@
#nullable enable
using System;
using System.Diagnostics;
using osu.Framework.Graphics.Performance;
using osu.Framework.Graphics.Pooling;
@ -26,17 +27,18 @@ namespace osu.Game.Rulesets.Objects.Pooling
/// </summary>
protected bool HasEntryApplied { get; private set; }
// Drawable's lifetime gets out of sync with entry's lifetime if entry's lifetime is modified.
// We cannot delegate getter to `Entry.LifetimeStart` because it is incompatible with `LifetimeManagementContainer` due to how lifetime change is detected.
public override double LifetimeStart
{
get => base.LifetimeStart;
set
{
base.LifetimeStart = value;
if (Entry == null && LifetimeStart != value)
throw new InvalidOperationException($"Cannot modify lifetime of {nameof(PoolableDrawableWithLifetime<TEntry>)} when entry is not set");
if (Entry != null)
Entry.LifetimeStart = value;
if (Entry == null) return;
Entry.LifetimeStart = value;
base.LifetimeStart = Entry.LifetimeStart;
}
}
@ -45,10 +47,13 @@ namespace osu.Game.Rulesets.Objects.Pooling
get => base.LifetimeEnd;
set
{
base.LifetimeEnd = value;
if (Entry == null && LifetimeEnd != value)
throw new InvalidOperationException($"Cannot modify lifetime of {nameof(PoolableDrawableWithLifetime<TEntry>)} when entry is not set");
if (Entry != null)
Entry.LifetimeEnd = value;
if (Entry == null) return;
Entry.LifetimeEnd = value;
base.LifetimeEnd = Entry.LifetimeEnd;
}
}
@ -79,9 +84,8 @@ namespace osu.Game.Rulesets.Objects.Pooling
free();
Entry = entry;
base.LifetimeStart = entry.LifetimeStart;
base.LifetimeEnd = entry.LifetimeEnd;
entry.LifetimeChanged += entryLifetimeChanged;
setLifetimeFromEntry();
OnApply(entry);
@ -117,11 +121,23 @@ namespace osu.Game.Rulesets.Objects.Pooling
OnFree(Entry);
Entry.LifetimeChanged -= entryLifetimeChanged;
Entry = null;
base.LifetimeStart = double.MinValue;
base.LifetimeEnd = double.MaxValue;
setLifetimeFromEntry();
HasEntryApplied = false;
}
private void entryLifetimeChanged(LifetimeEntry entry)
{
Debug.Assert(entry == Entry);
setLifetimeFromEntry();
}
private void setLifetimeFromEntry()
{
base.LifetimeStart = Entry?.LifetimeStart ?? double.MinValue;
base.LifetimeEnd = Entry?.LifetimeEnd ?? double.MaxValue;
}
}
}