osu/osu.Game/Screens/Edit/Compose/HitObjectUsageEventBuffer.cs

84 lines
3.1 KiB
C#
Raw Normal View History

2021-05-13 11:56:38 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
2021-05-13 12:16:19 +00:00
using JetBrains.Annotations;
2021-05-13 11:56:38 +00:00
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Objects.Drawables;
using osu.Game.Rulesets.UI;
namespace osu.Game.Screens.Edit.Compose
{
/// <summary>
2021-05-18 10:13:13 +00:00
/// Buffers events from the many <see cref="HitObjectContainer"/>s in a nested <see cref="Playfield"/> hierarchy
/// to ensure correct ordering of events.
2021-05-13 11:56:38 +00:00
/// </summary>
2021-05-18 09:59:45 +00:00
internal class HitObjectUsageEventBuffer : IDisposable
2021-05-13 11:56:38 +00:00
{
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes used by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If the ruleset uses pooled objects, this represents the time when the <see cref="HitObject"/>s become alive.
/// </remarks>
2021-05-13 12:16:19 +00:00
public event Action<HitObject> HitObjectUsageBegan;
2021-05-13 11:56:38 +00:00
/// <summary>
/// Invoked when a <see cref="HitObject"/> becomes unused by a <see cref="DrawableHitObject"/>.
/// </summary>
/// <remarks>
/// If the ruleset uses pooled objects, this represents the time when the <see cref="HitObject"/>s become dead.
/// </remarks>
public event Action<HitObject> HitObjectUsageFinished;
/// <summary>
/// Invoked when a <see cref="HitObject"/> has been transferred to another <see cref="DrawableHitObject"/>.
/// </summary>
public event Action<HitObject, DrawableHitObject> HitObjectUsageTransferred;
private readonly Playfield playfield;
/// <summary>
2021-05-18 09:57:02 +00:00
/// Creates a new <see cref="HitObjectUsageEventBuffer"/>.
2021-05-13 11:56:38 +00:00
/// </summary>
/// <param name="playfield">The most top-level <see cref="Playfield"/>.</param>
2021-05-18 09:57:02 +00:00
public HitObjectUsageEventBuffer([NotNull] Playfield playfield)
2021-05-13 11:56:38 +00:00
{
this.playfield = playfield;
2021-05-13 12:16:19 +00:00
playfield.HitObjectUsageBegan += onHitObjectUsageBegan;
playfield.HitObjectUsageFinished += onHitObjectUsageFinished;
2021-05-13 11:56:38 +00:00
}
2021-05-18 10:10:45 +00:00
private readonly List<HitObject> usageFinishedHitObjects = new List<HitObject>();
2021-05-13 11:56:38 +00:00
2021-05-18 10:10:45 +00:00
private void onHitObjectUsageBegan(HitObject hitObject)
{
2021-05-18 10:10:45 +00:00
if (usageFinishedHitObjects.Remove(hitObject))
HitObjectUsageTransferred?.Invoke(hitObject, playfield.AllHitObjects.Single(d => d.HitObject == hitObject));
else
HitObjectUsageBegan?.Invoke(hitObject);
}
2021-05-13 11:56:38 +00:00
2021-05-18 10:10:45 +00:00
private void onHitObjectUsageFinished(HitObject hitObject) => usageFinishedHitObjects.Add(hitObject);
2021-05-18 09:59:45 +00:00
public void Update()
2021-05-13 11:56:38 +00:00
{
2021-05-18 10:10:45 +00:00
foreach (var hitObject in usageFinishedHitObjects)
HitObjectUsageFinished?.Invoke(hitObject);
usageFinishedHitObjects.Clear();
2021-05-13 11:56:38 +00:00
}
2021-05-13 12:16:19 +00:00
2021-05-18 09:59:45 +00:00
public void Dispose()
2021-05-13 12:16:19 +00:00
{
2021-05-18 09:59:45 +00:00
if (playfield != null)
{
playfield.HitObjectUsageBegan -= onHitObjectUsageBegan;
playfield.HitObjectUsageFinished -= onHitObjectUsageFinished;
}
2021-05-13 12:16:19 +00:00
}
2021-05-13 11:56:38 +00:00
}
}