1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 18:52:55 +08:00

Remove PoolHitObjects, use return value of CreateDrawableRepresentation() instead

This commit is contained in:
smoogipoo 2020-11-12 14:04:16 +09:00
parent f652eb9982
commit 5dbbe11fc6
3 changed files with 44 additions and 29 deletions

View File

@ -140,8 +140,6 @@ namespace osu.Game.Tests.Visual.Gameplay
RegisterPool<TestHitObject, DrawableTestHitObject>(PoolSize); RegisterPool<TestHitObject, DrawableTestHitObject>(PoolSize);
} }
protected override bool PoolHitObjects => true;
protected override HitObjectLifetimeEntry CreateLifetimeEntry(TestHitObject hitObject) => new TestHitObjectLifetimeEntry(hitObject); protected override HitObjectLifetimeEntry CreateLifetimeEntry(TestHitObject hitObject) => new TestHitObjectLifetimeEntry(hitObject);
protected override PassThroughInputManager CreateInputManager() => new PassThroughInputManager(); protected override PassThroughInputManager CreateInputManager() => new PassThroughInputManager();

View File

@ -242,29 +242,38 @@ namespace osu.Game.Rulesets.UI
/// <param name="hitObject">The <see cref="HitObject"/> to add.</param> /// <param name="hitObject">The <see cref="HitObject"/> to add.</param>
public void AddHitObject(TObject hitObject) public void AddHitObject(TObject hitObject)
{ {
if (PoolHitObjects) var drawableRepresentation = CreateDrawableRepresentation(hitObject);
Playfield.Add(GetLifetimeEntry(hitObject));
// If a drawable representation exists, use it, otherwise assume the hitobject is being pooled.
if (drawableRepresentation != null)
Playfield.Add(drawableRepresentation);
else else
Playfield.Add(CreateDrawableRepresentation(hitObject)); Playfield.Add(GetLifetimeEntry(hitObject));
} }
/// <summary> /// <summary>
/// Removes a <see cref="HitObject"/> from this <see cref="HitObject"/>. /// Removes a <see cref="HitObject"/> from this <see cref="DrawableRuleset"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This does not remove the <see cref="HitObject"/> from the beatmap. /// This does not remove the <see cref="HitObject"/> from the beatmap.
/// </remarks> /// </remarks>
/// <param name="hitObject">The <see cref="HitObject"/> to remove.</param> /// <param name="hitObject">The <see cref="HitObject"/> to remove.</param>
public void RemoveHitObject(TObject hitObject) public bool RemoveHitObject(TObject hitObject)
{ {
if (PoolHitObjects) var entry = GetLifetimeEntry(hitObject);
Playfield.Remove(GetLifetimeEntry(hitObject));
else // May have been newly-created by the above call - remove it anyway.
{ RemoveLifetimeEntry(hitObject);
var drawableObject = Playfield.AllHitObjects.SingleOrDefault(d => d.HitObject == hitObject);
if (drawableObject != null) if (Playfield.Remove(entry))
Playfield.Remove(drawableObject); return true;
}
// If the entry was not removed from the playfield, assume the hitobject is not being pooled and attempt a direct removal.
var drawableObject = Playfield.AllHitObjects.SingleOrDefault(d => d.HitObject == hitObject);
if (drawableObject != null)
return Playfield.Remove(drawableObject);
return false;
} }
protected sealed override HitObjectLifetimeEntry CreateLifetimeEntry(HitObject hitObject) protected sealed override HitObjectLifetimeEntry CreateLifetimeEntry(HitObject hitObject)
@ -539,14 +548,6 @@ namespace osu.Game.Rulesets.UI
private readonly Dictionary<Type, IDrawablePool> pools = new Dictionary<Type, IDrawablePool>(); private readonly Dictionary<Type, IDrawablePool> pools = new Dictionary<Type, IDrawablePool>();
private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntries = new Dictionary<HitObject, HitObjectLifetimeEntry>(); private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntries = new Dictionary<HitObject, HitObjectLifetimeEntry>();
/// <summary>
/// Whether this <see cref="DrawableRuleset"/> should retrieve pooled <see cref="DrawableHitObject"/>s.
/// </summary>
/// <remarks>
/// Pools must be registered with this <see cref="DrawableRuleset"/> via <see cref="RegisterPool{TObject,TDrawable}"/> in order for <see cref="DrawableHitObject"/>s to be retrieved.
/// </remarks>
protected virtual bool PoolHitObjects => false;
/// <summary> /// <summary>
/// Registers a <see cref="DrawableHitObject"/> pool with this <see cref="DrawableRuleset"/> which is to be used whenever /// Registers a <see cref="DrawableHitObject"/> pool with this <see cref="DrawableRuleset"/> which is to be used whenever
/// <see cref="DrawableHitObject"/> representations are requested for the given <typeparamref name="TObject"/> type (via <see cref="GetPooledDrawableRepresentation"/>). /// <see cref="DrawableHitObject"/> representations are requested for the given <typeparamref name="TObject"/> type (via <see cref="GetPooledDrawableRepresentation"/>).
@ -627,6 +628,12 @@ namespace osu.Game.Rulesets.UI
return lifetimeEntries[hitObject] = CreateLifetimeEntry(hitObject); return lifetimeEntries[hitObject] = CreateLifetimeEntry(hitObject);
} }
/// <summary>
/// Removes the <see cref="HitObjectLifetimeEntry"/> for a <see cref="HitObject"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to remove the <see cref="HitObjectLifetimeEntry"/> for.</param>
internal void RemoveLifetimeEntry([NotNull] HitObject hitObject) => lifetimeEntries.Remove(hitObject);
} }
public class BeatmapInvalidForRulesetException : ArgumentException public class BeatmapInvalidForRulesetException : ArgumentException

View File

@ -143,7 +143,7 @@ namespace osu.Game.Rulesets.UI
private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntryMap = new Dictionary<HitObject, HitObjectLifetimeEntry>(); private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntryMap = new Dictionary<HitObject, HitObjectLifetimeEntry>();
/// <summary> /// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="Playfield"/>. /// Adds a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> to this <see cref="Playfield"/>.
/// </summary> /// </summary>
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param> /// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
public void Add(HitObjectLifetimeEntry entry) public void Add(HitObjectLifetimeEntry entry)
@ -154,14 +154,24 @@ namespace osu.Game.Rulesets.UI
} }
/// <summary> /// <summary>
/// Removes a <see cref="HitObject"/> to this <see cref="Playfield"/>. /// Removes a <see cref="HitObjectLifetimeEntry"/> for a pooled <see cref="HitObject"/> from this <see cref="Playfield"/>.
/// </summary> /// </summary>
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param> /// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
public void Remove(HitObjectLifetimeEntry entry) /// <returns>Whether the <see cref="HitObject"/> was successfully removed.</returns>
public bool Remove(HitObjectLifetimeEntry entry)
{ {
if (HitObjectContainer.Remove(entry)) if (lifetimeEntryMap.Remove(entry.HitObject))
OnHitObjectRemoved(entry.HitObject); {
lifetimeEntryMap.Remove(entry.HitObject); HitObjectContainer.Remove(entry);
return true;
}
bool removedFromNested = false;
if (nestedPlayfields.IsValueCreated)
removedFromNested = nestedPlayfields.Value.Any(p => p.Remove(entry));
return removedFromNested;
} }
/// <summary> /// <summary>