1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-28 20:47:22 +08:00

Allow setting Entry of PoolableDrawableWithLifetime

It is more convenient than using the constructor because the only limited kind of expression is allowed in a base constructor call.
Also, the object initializer syntax can be used.
This commit is contained in:
ekrctb 2021-06-16 15:58:17 +09:00
parent 81c43bcf19
commit 117e94bc94
2 changed files with 31 additions and 11 deletions

View File

@ -156,10 +156,11 @@ namespace osu.Game.Rulesets.Objects.Drawables
/// If <c>null</c>, a hitobject is expected to be later applied via <see cref="PoolableDrawableWithLifetime{TEntry}.Apply"/> (or automatically via pooling). /// If <c>null</c>, a hitobject is expected to be later applied via <see cref="PoolableDrawableWithLifetime{TEntry}.Apply"/> (or automatically via pooling).
/// </param> /// </param>
protected DrawableHitObject([CanBeNull] HitObject initialHitObject = null) protected DrawableHitObject([CanBeNull] HitObject initialHitObject = null)
: base(initialHitObject != null ? new SyntheticHitObjectEntry(initialHitObject) : null)
{ {
if (Entry != null) if (initialHitObject == null) return;
ensureEntryHasResult();
Entry = new SyntheticHitObjectEntry(initialHitObject);
ensureEntryHasResult();
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Performance; using osu.Framework.Graphics.Performance;
using osu.Framework.Graphics.Pooling; using osu.Framework.Graphics.Pooling;
@ -16,14 +17,32 @@ namespace osu.Game.Rulesets.Objects.Pooling
/// <typeparam name="TEntry">The <see cref="LifetimeEntry"/> type storing state and controlling this drawable.</typeparam> /// <typeparam name="TEntry">The <see cref="LifetimeEntry"/> type storing state and controlling this drawable.</typeparam>
public abstract class PoolableDrawableWithLifetime<TEntry> : PoolableDrawable where TEntry : LifetimeEntry public abstract class PoolableDrawableWithLifetime<TEntry> : PoolableDrawable where TEntry : LifetimeEntry
{ {
private TEntry? entry;
/// <summary> /// <summary>
/// The entry holding essential state of this <see cref="PoolableDrawableWithLifetime{TEntry}"/>. /// The entry holding essential state of this <see cref="PoolableDrawableWithLifetime{TEntry}"/>.
/// </summary> /// </summary>
public TEntry? Entry { get; private set; } /// <remarks>
/// If a non-null value is set before loading is started, the entry is applied when the loading is completed.
/// </remarks>
public TEntry? Entry
{
get => entry;
set
{
if (LoadState < LoadState.Ready)
entry = value;
else if (value != null)
Apply(value);
else if (HasEntryApplied)
free();
}
}
/// <summary> /// <summary>
/// Whether <see cref="Entry"/> is applied to this <see cref="PoolableDrawableWithLifetime{TEntry}"/>. /// Whether <see cref="Entry"/> is applied to this <see cref="PoolableDrawableWithLifetime{TEntry}"/>.
/// When an initial entry is specified in the constructor, <see cref="Entry"/> is set but not applied until loading is completed. /// When an <see cref="Entry"/> is set during initialization, it is not applied until loading is completed.
/// </summary> /// </summary>
protected bool HasEntryApplied { get; private set; } protected bool HasEntryApplied { get; private set; }
@ -65,7 +84,7 @@ namespace osu.Game.Rulesets.Objects.Pooling
{ {
base.LoadAsyncComplete(); base.LoadAsyncComplete();
// Apply the initial entry given in the constructor. // Apply the initial entry.
if (Entry != null && !HasEntryApplied) if (Entry != null && !HasEntryApplied)
Apply(Entry); Apply(Entry);
} }
@ -79,7 +98,7 @@ namespace osu.Game.Rulesets.Objects.Pooling
if (HasEntryApplied) if (HasEntryApplied)
free(); free();
Entry = entry; this.entry = entry;
entry.LifetimeChanged += setLifetimeFromEntry; entry.LifetimeChanged += setLifetimeFromEntry;
setLifetimeFromEntry(entry); setLifetimeFromEntry(entry);
@ -113,12 +132,12 @@ namespace osu.Game.Rulesets.Objects.Pooling
private void free() private void free()
{ {
Debug.Assert(Entry != null && HasEntryApplied); Debug.Assert(entry != null && HasEntryApplied);
OnFree(Entry); OnFree(entry);
Entry.LifetimeChanged -= setLifetimeFromEntry; entry.LifetimeChanged -= setLifetimeFromEntry;
Entry = null; entry = null;
base.LifetimeStart = double.MinValue; base.LifetimeStart = double.MinValue;
base.LifetimeEnd = double.MaxValue; base.LifetimeEnd = double.MaxValue;