1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 01:03:02 +08:00
osu-lazer/osu.Game/Screens/Edit/Compose/HitObjectUsageEventBuffer.cs

84 lines
3.1 KiB
C#
Raw Normal View History

2021-05-13 19:56:38 +08: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 20:16:19 +08:00
using JetBrains.Annotations;
2021-05-13 19:56:38 +08: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 18:13:13 +08: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 19:56:38 +08:00
/// </summary>
2021-05-18 17:59:45 +08:00
internal class HitObjectUsageEventBuffer : IDisposable
2021-05-13 19:56:38 +08: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 20:16:19 +08:00
public event Action<HitObject> HitObjectUsageBegan;
2021-05-13 19:56:38 +08: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 17:57:02 +08:00
/// Creates a new <see cref="HitObjectUsageEventBuffer"/>.
2021-05-13 19:56:38 +08:00
/// </summary>
/// <param name="playfield">The most top-level <see cref="Playfield"/>.</param>
2021-05-18 17:57:02 +08:00
public HitObjectUsageEventBuffer([NotNull] Playfield playfield)
2021-05-13 19:56:38 +08:00
{
this.playfield = playfield;
2021-05-13 20:16:19 +08:00
playfield.HitObjectUsageBegan += onHitObjectUsageBegan;
playfield.HitObjectUsageFinished += onHitObjectUsageFinished;
2021-05-13 19:56:38 +08:00
}
2021-05-18 18:10:45 +08:00
private readonly List<HitObject> usageFinishedHitObjects = new List<HitObject>();
2021-05-13 19:56:38 +08:00
2021-05-18 18:10:45 +08:00
private void onHitObjectUsageBegan(HitObject hitObject)
{
2021-05-18 18:10:45 +08:00
if (usageFinishedHitObjects.Remove(hitObject))
HitObjectUsageTransferred?.Invoke(hitObject, playfield.AllHitObjects.Single(d => d.HitObject == hitObject));
else
HitObjectUsageBegan?.Invoke(hitObject);
}
2021-05-13 19:56:38 +08:00
2021-05-18 18:10:45 +08:00
private void onHitObjectUsageFinished(HitObject hitObject) => usageFinishedHitObjects.Add(hitObject);
2021-05-18 17:59:45 +08:00
public void Update()
2021-05-13 19:56:38 +08:00
{
2021-05-18 18:10:45 +08:00
foreach (var hitObject in usageFinishedHitObjects)
HitObjectUsageFinished?.Invoke(hitObject);
usageFinishedHitObjects.Clear();
2021-05-13 19:56:38 +08:00
}
2021-05-13 20:16:19 +08:00
2021-05-18 17:59:45 +08:00
public void Dispose()
2021-05-13 20:16:19 +08:00
{
2021-05-18 17:59:45 +08:00
if (playfield != null)
{
playfield.HitObjectUsageBegan -= onHitObjectUsageBegan;
playfield.HitObjectUsageFinished -= onHitObjectUsageFinished;
}
2021-05-13 20:16:19 +08:00
}
2021-05-13 19:56:38 +08:00
}
}