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.Diagnostics;
|
|
|
|
using System.Linq;
|
2021-05-13 20:16:19 +08:00
|
|
|
using JetBrains.Annotations;
|
2021-05-13 19:56:38 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
|
|
|
using osu.Game.Rulesets.UI;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Edit.Compose
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A queue which processes events from the many <see cref="HitObjectContainer"/>s in a nested <see cref="Playfield"/> hierarchy.
|
|
|
|
/// </summary>
|
|
|
|
internal class HitObjectContainerEventQueue : Component
|
|
|
|
{
|
|
|
|
/// <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>
|
|
|
|
/// Creates a new <see cref="HitObjectContainerEventQueue"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="playfield">The most top-level <see cref="Playfield"/>.</param>
|
2021-05-13 20:16:19 +08:00
|
|
|
public HitObjectContainerEventQueue([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
|
|
|
}
|
|
|
|
|
|
|
|
private readonly Dictionary<HitObject, int> pendingUsagesBegan = new Dictionary<HitObject, int>();
|
|
|
|
private readonly Dictionary<HitObject, int> pendingUsagesFinished = new Dictionary<HitObject, int>();
|
|
|
|
|
|
|
|
private void onHitObjectUsageBegan(HitObject hitObject) => pendingUsagesBegan[hitObject] = pendingUsagesBegan.GetValueOrDefault(hitObject, 0) + 1;
|
|
|
|
|
|
|
|
private void onHitObjectUsageFinished(HitObject hitObject) => pendingUsagesFinished[hitObject] = pendingUsagesFinished.GetValueOrDefault(hitObject, 0) + 1;
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
foreach (var (hitObject, countBegan) in pendingUsagesBegan)
|
|
|
|
{
|
|
|
|
if (pendingUsagesFinished.TryGetValue(hitObject, out int countFinished))
|
|
|
|
{
|
|
|
|
Debug.Assert(countFinished > 0);
|
|
|
|
|
|
|
|
if (countBegan > countFinished)
|
|
|
|
{
|
|
|
|
// The hitobject is still in use, but transferred to a different HOC.
|
|
|
|
HitObjectUsageTransferred?.Invoke(hitObject, playfield.AllHitObjects.Single(d => d.HitObject == hitObject));
|
|
|
|
pendingUsagesFinished.Remove(hitObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// This is a new usage of the hitobject.
|
2021-05-13 20:16:19 +08:00
|
|
|
HitObjectUsageBegan?.Invoke(hitObject);
|
2021-05-13 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Go through any remaining pending finished usages.
|
|
|
|
foreach (var (hitObject, _) in pendingUsagesFinished)
|
|
|
|
HitObjectUsageFinished?.Invoke(hitObject);
|
|
|
|
|
|
|
|
pendingUsagesBegan.Clear();
|
|
|
|
pendingUsagesFinished.Clear();
|
|
|
|
}
|
2021-05-13 20:16:19 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
playfield.HitObjectUsageBegan -= onHitObjectUsageBegan;
|
|
|
|
playfield.HitObjectUsageFinished -= onHitObjectUsageFinished;
|
|
|
|
}
|
2021-05-13 19:56:38 +08:00
|
|
|
}
|
|
|
|
}
|