1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-28 13:07:18 +08:00

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 #nullable enable
using System;
using System.Diagnostics; using System.Diagnostics;
using osu.Framework.Graphics.Performance; using osu.Framework.Graphics.Performance;
using osu.Framework.Graphics.Pooling; using osu.Framework.Graphics.Pooling;
@ -26,17 +27,18 @@ namespace osu.Game.Rulesets.Objects.Pooling
/// </summary> /// </summary>
protected bool HasEntryApplied { get; private set; } 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 public override double LifetimeStart
{ {
get => base.LifetimeStart; get => base.LifetimeStart;
set 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) if (Entry == null) return;
Entry.LifetimeStart = value;
Entry.LifetimeStart = value;
base.LifetimeStart = Entry.LifetimeStart;
} }
} }
@ -45,10 +47,13 @@ namespace osu.Game.Rulesets.Objects.Pooling
get => base.LifetimeEnd; get => base.LifetimeEnd;
set 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) if (Entry == null) return;
Entry.LifetimeEnd = value;
Entry.LifetimeEnd = value;
base.LifetimeEnd = Entry.LifetimeEnd;
} }
} }
@ -79,9 +84,8 @@ namespace osu.Game.Rulesets.Objects.Pooling
free(); free();
Entry = entry; Entry = entry;
entry.LifetimeChanged += entryLifetimeChanged;
base.LifetimeStart = entry.LifetimeStart; setLifetimeFromEntry();
base.LifetimeEnd = entry.LifetimeEnd;
OnApply(entry); OnApply(entry);
@ -117,11 +121,23 @@ namespace osu.Game.Rulesets.Objects.Pooling
OnFree(Entry); OnFree(Entry);
Entry.LifetimeChanged -= entryLifetimeChanged;
Entry = null; Entry = null;
base.LifetimeStart = double.MinValue; setLifetimeFromEntry();
base.LifetimeEnd = double.MaxValue;
HasEntryApplied = false; 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;
}
} }
} }