mirror of
https://github.com/osukey/osukey.git
synced 2025-08-03 06:36:31 +09:00
Add pooling support to DrawableRuleset + Playfield
This commit is contained in:
@ -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);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user