From 5dbbe11fc668b3a54ae2be4a190479ec5aa42126 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Thu, 12 Nov 2020 14:04:16 +0900 Subject: [PATCH] Remove PoolHitObjects, use return value of CreateDrawableRepresentation() instead --- .../Gameplay/TestScenePoolingRuleset.cs | 2 - osu.Game/Rulesets/UI/DrawableRuleset.cs | 49 +++++++++++-------- osu.Game/Rulesets/UI/Playfield.cs | 22 ++++++--- 3 files changed, 44 insertions(+), 29 deletions(-) diff --git a/osu.Game.Tests/Visual/Gameplay/TestScenePoolingRuleset.cs b/osu.Game.Tests/Visual/Gameplay/TestScenePoolingRuleset.cs index bb09aec416..c3ae753eae 100644 --- a/osu.Game.Tests/Visual/Gameplay/TestScenePoolingRuleset.cs +++ b/osu.Game.Tests/Visual/Gameplay/TestScenePoolingRuleset.cs @@ -140,8 +140,6 @@ namespace osu.Game.Tests.Visual.Gameplay RegisterPool(PoolSize); } - protected override bool PoolHitObjects => true; - protected override HitObjectLifetimeEntry CreateLifetimeEntry(TestHitObject hitObject) => new TestHitObjectLifetimeEntry(hitObject); protected override PassThroughInputManager CreateInputManager() => new PassThroughInputManager(); diff --git a/osu.Game/Rulesets/UI/DrawableRuleset.cs b/osu.Game/Rulesets/UI/DrawableRuleset.cs index 357f2d27d9..ce4cef4977 100644 --- a/osu.Game/Rulesets/UI/DrawableRuleset.cs +++ b/osu.Game/Rulesets/UI/DrawableRuleset.cs @@ -242,29 +242,38 @@ namespace osu.Game.Rulesets.UI /// The to add. public void AddHitObject(TObject hitObject) { - if (PoolHitObjects) - Playfield.Add(GetLifetimeEntry(hitObject)); + var drawableRepresentation = CreateDrawableRepresentation(hitObject); + + // If a drawable representation exists, use it, otherwise assume the hitobject is being pooled. + if (drawableRepresentation != null) + Playfield.Add(drawableRepresentation); else - Playfield.Add(CreateDrawableRepresentation(hitObject)); + Playfield.Add(GetLifetimeEntry(hitObject)); } /// - /// Removes a from this . + /// Removes a from this . /// /// /// This does not remove the from the beatmap. /// /// The to remove. - public void RemoveHitObject(TObject hitObject) + public bool RemoveHitObject(TObject hitObject) { - if (PoolHitObjects) - Playfield.Remove(GetLifetimeEntry(hitObject)); - else - { - var drawableObject = Playfield.AllHitObjects.SingleOrDefault(d => d.HitObject == hitObject); - if (drawableObject != null) - Playfield.Remove(drawableObject); - } + var entry = GetLifetimeEntry(hitObject); + + // May have been newly-created by the above call - remove it anyway. + RemoveLifetimeEntry(hitObject); + + if (Playfield.Remove(entry)) + 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) @@ -539,14 +548,6 @@ namespace osu.Game.Rulesets.UI private readonly Dictionary pools = new Dictionary(); private readonly Dictionary lifetimeEntries = new Dictionary(); - /// - /// Whether this should retrieve pooled s. - /// - /// - /// Pools must be registered with this via in order for s to be retrieved. - /// - protected virtual bool PoolHitObjects => false; - /// /// Registers a pool with this which is to be used whenever /// representations are requested for the given type (via ). @@ -627,6 +628,12 @@ namespace osu.Game.Rulesets.UI return lifetimeEntries[hitObject] = CreateLifetimeEntry(hitObject); } + + /// + /// Removes the for a . + /// + /// The to remove the for. + internal void RemoveLifetimeEntry([NotNull] HitObject hitObject) => lifetimeEntries.Remove(hitObject); } public class BeatmapInvalidForRulesetException : ArgumentException diff --git a/osu.Game/Rulesets/UI/Playfield.cs b/osu.Game/Rulesets/UI/Playfield.cs index cdaf9364af..7c47f046dc 100644 --- a/osu.Game/Rulesets/UI/Playfield.cs +++ b/osu.Game/Rulesets/UI/Playfield.cs @@ -143,7 +143,7 @@ namespace osu.Game.Rulesets.UI private readonly Dictionary lifetimeEntryMap = new Dictionary(); /// - /// Adds a to this . + /// Adds a for a pooled to this . /// /// The controlling the lifetime of the . public void Add(HitObjectLifetimeEntry entry) @@ -154,14 +154,24 @@ namespace osu.Game.Rulesets.UI } /// - /// Removes a to this . + /// Removes a for a pooled from this . /// /// The controlling the lifetime of the . - public void Remove(HitObjectLifetimeEntry entry) + /// Whether the was successfully removed. + public bool Remove(HitObjectLifetimeEntry entry) { - if (HitObjectContainer.Remove(entry)) - OnHitObjectRemoved(entry.HitObject); - lifetimeEntryMap.Remove(entry.HitObject); + if (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; } ///