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

Add pooling support to DrawableRuleset + Playfield

This commit is contained in:
smoogipoo 2020-11-10 23:32:30 +09:00
parent 31e4d71852
commit 66213f2ed0
4 changed files with 173 additions and 34 deletions

View File

@ -41,9 +41,8 @@ namespace osu.Game.Rulesets.UI
public abstract class DrawableRuleset<TObject> : DrawableRuleset, IProvideCursor, ICanAttachKeyCounter
where TObject : HitObject
{
public override event Action<JudgementResult> OnNewResult;
public override event Action<JudgementResult> OnRevertResult;
public override event Action<JudgementResult> NewResult;
public override event Action<JudgementResult> RevertResult;
/// <summary>
/// The selected variant.
@ -125,7 +124,11 @@ namespace osu.Game.Rulesets.UI
RelativeSizeAxes = Axes.Both;
KeyBindingInputManager = CreateInputManager();
playfield = new Lazy<Playfield>(CreatePlayfield);
playfield = new Lazy<Playfield>(() => CreatePlayfield().With(p =>
{
p.NewResult += (_, r) => NewResult?.Invoke(r);
p.RevertResult += (_, r) => RevertResult?.Invoke(r);
}));
IsPaused.ValueChanged += paused =>
{
@ -183,7 +186,7 @@ namespace osu.Game.Rulesets.UI
RegenerateAutoplay();
loadObjects(cancellationToken);
loadObjects(cancellationToken ?? default);
}
public void RegenerateAutoplay()
@ -196,15 +199,15 @@ namespace osu.Game.Rulesets.UI
/// <summary>
/// Creates and adds drawable representations of hit objects to the play field.
/// </summary>
private void loadObjects(CancellationToken? cancellationToken)
private void loadObjects(CancellationToken cancellationToken)
{
foreach (TObject h in Beatmap.HitObjects)
{
cancellationToken?.ThrowIfCancellationRequested();
addHitObject(h);
cancellationToken.ThrowIfCancellationRequested();
AddHitObject(h);
}
cancellationToken?.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
Playfield.PostProcess();
@ -230,21 +233,24 @@ namespace osu.Game.Rulesets.UI
ResumeOverlay?.Hide();
}
/// <summary>
/// Creates and adds the visual representation of a <typeparamref name="TObject"/> to this <see cref="DrawableRuleset{TObject}"/>.
/// </summary>
/// <param name="hitObject">The <typeparamref name="TObject"/> to add the visual representation for.</param>
private void addHitObject(TObject hitObject)
public void AddHitObject(TObject hitObject)
{
var drawableObject = CreateDrawableRepresentation(hitObject);
if (PoolHitObjects)
Playfield.Add(GetLifetimeEntry(hitObject));
else
Playfield.Add(CreateDrawableRepresentation(hitObject));
}
if (drawableObject == null)
return;
drawableObject.OnNewResult += (_, r) => OnNewResult?.Invoke(r);
drawableObject.OnRevertResult += (_, r) => OnRevertResult?.Invoke(r);
Playfield.Add(drawableObject);
public void 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);
}
}
protected sealed override HitObjectLifetimeEntry CreateLifetimeEntry(HitObject hitObject)
@ -382,12 +388,12 @@ namespace osu.Game.Rulesets.UI
/// <summary>
/// Invoked when a <see cref="JudgementResult"/> has been applied by a <see cref="DrawableHitObject"/>.
/// </summary>
public abstract event Action<JudgementResult> OnNewResult;
public abstract event Action<JudgementResult> NewResult;
/// <summary>
/// Invoked when a <see cref="JudgementResult"/> is being reverted by a <see cref="DrawableHitObject"/>.
/// </summary>
public abstract event Action<JudgementResult> OnRevertResult;
public abstract event Action<JudgementResult> RevertResult;
/// <summary>
/// Whether a replay is currently loaded.
@ -524,10 +530,6 @@ namespace osu.Game.Rulesets.UI
/// </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.
/// <para>
/// If <c>true</c>, hitobjects will be added to the <see cref="Playfield"/> via <see cref="osu.Game.Rulesets.UI.Playfield.Add(HitObject)"/>.
/// If <c>false</c>, <see cref="osu.Game.Rulesets.UI.Playfield.Add(DrawableHitObject)"/> will be used instead.
/// </para>
/// </remarks>
protected virtual bool PoolHitObjects => false;

View File

@ -31,7 +31,14 @@ namespace osu.Game.Rulesets.UI
/// </remarks>
public IEnumerable<DrawableHitObject> AliveObjects => AliveInternalChildren.OfType<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is judged.
/// </summary>
public event Action<DrawableHitObject, JudgementResult> NewResult;
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> judgement is reverted.
/// </summary>
public event Action<DrawableHitObject, JudgementResult> RevertResult;
/// <summary>
@ -79,7 +86,7 @@ namespace osu.Game.Rulesets.UI
public void Add(HitObjectLifetimeEntry entry) => lifetimeManager.AddEntry(entry);
public void Remove(HitObjectLifetimeEntry entry) => lifetimeManager.RemoveEntry(entry);
public bool Remove(HitObjectLifetimeEntry entry) => lifetimeManager.RemoveEntry(entry);
private void entryBecameAlive(LifetimeEntry entry) => addDrawable((HitObjectLifetimeEntry)entry);

View File

@ -10,13 +10,41 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.Containers;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Mods;
using osu.Game.Rulesets.Objects;
using osuTK;
namespace osu.Game.Rulesets.UI
{
public abstract class Playfield : CompositeDrawable
{
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> is judged.
/// </summary>
public event Action<DrawableHitObject, JudgementResult> NewResult;
/// <summary>
/// Invoked when a <see cref="DrawableHitObject"/> judgement is reverted.
/// </summary>
public event Action<DrawableHitObject, JudgementResult> RevertResult;
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes used by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If this <see cref="HitObjectContainer"/> uses pooled objects, this represents the time when the <see cref="HitObject"/>s become alive.
/// </remarks>
public event Action<HitObject> HitObjectUsageBegan;
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes unused by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If this <see cref="HitObjectContainer"/> uses pooled objects, this represents the time when the <see cref="HitObject"/>s become dead.
/// </remarks>
public event Action<HitObject> HitObjectUsageFinished;
/// <summary>
/// The <see cref="DrawableHitObject"/> contained in this Playfield.
/// </summary>
@ -72,7 +100,13 @@ namespace osu.Game.Rulesets.UI
{
RelativeSizeAxes = Axes.Both;
hitObjectContainerLazy = new Lazy<HitObjectContainer>(CreateHitObjectContainer);
hitObjectContainerLazy = new Lazy<HitObjectContainer>(() => CreateHitObjectContainer().With(h =>
{
h.NewResult += (d, r) => NewResult?.Invoke(d, r);
h.RevertResult += (d, r) => RevertResult?.Invoke(d, r);
h.HitObjectUsageBegan += o => HitObjectUsageBegan?.Invoke(o);
h.HitObjectUsageFinished += o => HitObjectUsageFinished?.Invoke(o);
}));
}
[Resolved(CanBeNull = true)]
@ -101,13 +135,103 @@ namespace osu.Game.Rulesets.UI
/// Adds a DrawableHitObject to this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to add.</param>
public virtual void Add(DrawableHitObject h) => HitObjectContainer.Add(h);
public virtual void Add(DrawableHitObject h)
{
HitObjectContainer.Add(h);
h.OnNewResult += (d, r) => NewResult?.Invoke(d, r);
h.OnRevertResult += (d, r) => RevertResult?.Invoke(d, r);
OnHitObjectAdded(h.HitObject);
}
/// <summary>
/// Remove a DrawableHitObject from this Playfield.
/// </summary>
/// <param name="h">The DrawableHitObject to remove.</param>
public virtual bool Remove(DrawableHitObject h) => HitObjectContainer.Remove(h);
public virtual bool Remove(DrawableHitObject h)
{
if (!HitObjectContainer.Remove(h))
return false;
OnHitObjectRemoved(h.HitObject);
return false;
}
private readonly Dictionary<HitObject, HitObjectLifetimeEntry> lifetimeEntryMap = new Dictionary<HitObject, HitObjectLifetimeEntry>();
/// <summary>
/// Adds a <see cref="HitObject"/> to this <see cref="Playfield"/>.
/// </summary>
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
public void Add(HitObjectLifetimeEntry entry)
{
HitObjectContainer.Add(entry);
lifetimeEntryMap[entry.HitObject] = entry;
OnHitObjectAdded(entry.HitObject);
}
/// <summary>
/// Removes a <see cref="HitObject"/> to this <see cref="Playfield"/>.
/// </summary>
/// <param name="entry">The <see cref="HitObjectLifetimeEntry"/> controlling the lifetime of the <see cref="HitObject"/>.</param>
public void Remove(HitObjectLifetimeEntry entry)
{
if (HitObjectContainer.Remove(entry))
OnHitObjectRemoved(entry.HitObject);
lifetimeEntryMap.Remove(entry.HitObject);
}
/// <summary>
/// Invoked when a <see cref="HitObject"/> is added to this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The added <see cref="HitObject"/>.</param>
protected virtual void OnHitObjectAdded(HitObject hitObject)
{
}
/// <summary>
/// Invoked when a <see cref="HitObject"/> is removed from this <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The removed <see cref="HitObject"/>.</param>
protected virtual void OnHitObjectRemoved(HitObject hitObject)
{
}
/// <summary>
/// Sets whether to keep a given <see cref="HitObject"/> always alive within this or any nested <see cref="Playfield"/>.
/// </summary>
/// <param name="hitObject">The <see cref="HitObject"/> to set.</param>
/// <param name="keepAlive">Whether to keep <paramref name="hitObject"/> always alive.</param>
public void SetKeepAlive(HitObject hitObject, bool keepAlive)
{
if (lifetimeEntryMap.TryGetValue(hitObject, out var entry))
{
entry.KeepAlive = keepAlive;
return;
}
if (!nestedPlayfields.IsValueCreated)
return;
foreach (var p in nestedPlayfields.Value)
p.SetKeepAlive(hitObject, keepAlive);
}
/// <summary>
/// Keeps all <see cref="HitObject"/>s alive within this and all nested <see cref="Playfield"/>s.
/// </summary>
public void KeepAllAlive()
{
foreach (var (_, entry) in lifetimeEntryMap)
entry.KeepAlive = true;
if (!nestedPlayfields.IsValueCreated)
return;
foreach (var p in nestedPlayfields.Value)
p.KeepAllAlive();
}
/// <summary>
/// The cursor currently being used by this <see cref="Playfield"/>. May be null if no cursor is provided.
@ -131,6 +255,12 @@ namespace osu.Game.Rulesets.UI
protected void AddNested(Playfield otherPlayfield)
{
otherPlayfield.DisplayJudgements.BindTo(DisplayJudgements);
otherPlayfield.NewResult += (d, r) => NewResult?.Invoke(d, r);
otherPlayfield.RevertResult += (d, r) => RevertResult?.Invoke(d, r);
otherPlayfield.HitObjectUsageBegan += h => HitObjectUsageBegan?.Invoke(h);
otherPlayfield.HitObjectUsageFinished += h => HitObjectUsageFinished?.Invoke(h);
nestedPlayfields.Value.Add(otherPlayfield);
}

View File

@ -261,14 +261,14 @@ namespace osu.Game.Screens.Play
// bind clock into components that require it
DrawableRuleset.IsPaused.BindTo(GameplayClockContainer.IsPaused);
DrawableRuleset.OnNewResult += r =>
DrawableRuleset.NewResult += r =>
{
HealthProcessor.ApplyResult(r);
ScoreProcessor.ApplyResult(r);
gameplayBeatmap.ApplyResult(r);
};
DrawableRuleset.OnRevertResult += r =>
DrawableRuleset.RevertResult += r =>
{
HealthProcessor.RevertResult(r);
ScoreProcessor.RevertResult(r);