2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
using System;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-11-10 21:49:02 +08:00
|
|
|
|
using System.Diagnostics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using System.Linq;
|
2020-11-10 21:49:02 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2020-10-29 14:20:10 +08:00
|
|
|
|
using osu.Framework.Graphics.Performance;
|
2020-11-10 21:49:02 +08:00
|
|
|
|
using osu.Game.Rulesets.Judgements;
|
|
|
|
|
using osu.Game.Rulesets.Objects;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Game.Rulesets.Objects.Drawables;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.UI
|
|
|
|
|
{
|
2018-12-13 13:55:28 +08:00
|
|
|
|
public class HitObjectContainer : LifetimeManagementContainer
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// All currently in-use <see cref="DrawableHitObject"/>s.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<DrawableHitObject> Objects => InternalChildren.OfType<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// All currently in-use <see cref="DrawableHitObject"/>s that are alive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// If this <see cref="HitObjectContainer"/> uses pooled objects, this is equivalent to <see cref="Objects"/>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public IEnumerable<DrawableHitObject> AliveObjects => AliveInternalChildren.OfType<DrawableHitObject>().OrderBy(h => h.HitObject.StartTime);
|
|
|
|
|
|
2020-11-10 22:32:30 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="DrawableHitObject"/> is judged.
|
|
|
|
|
/// </summary>
|
2020-11-10 21:49:02 +08:00
|
|
|
|
public event Action<DrawableHitObject, JudgementResult> NewResult;
|
2020-11-10 22:32:30 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Invoked when a <see cref="DrawableHitObject"/> judgement is reverted.
|
|
|
|
|
/// </summary>
|
2020-11-10 21:49:02 +08:00
|
|
|
|
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 amount of time prior to the current time within which <see cref="HitObject"/>s should be considered alive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double PastLifetimeExtension { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The amount of time after the current time within which <see cref="HitObject"/>s should be considered alive.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public double FutureLifetimeExtension { get; set; }
|
|
|
|
|
|
|
|
|
|
private readonly Dictionary<DrawableHitObject, IBindable> startTimeMap = new Dictionary<DrawableHitObject, IBindable>();
|
|
|
|
|
private readonly Dictionary<HitObjectLifetimeEntry, DrawableHitObject> drawableMap = new Dictionary<HitObjectLifetimeEntry, DrawableHitObject>();
|
|
|
|
|
private readonly LifetimeEntryManager lifetimeManager = new LifetimeEntryManager();
|
|
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
|
|
|
|
private DrawableRuleset drawableRuleset { get; set; }
|
2019-12-18 01:56:29 +08:00
|
|
|
|
|
2018-09-21 13:35:50 +08:00
|
|
|
|
public HitObjectContainer()
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2020-11-10 21:49:02 +08:00
|
|
|
|
|
|
|
|
|
lifetimeManager.EntryBecameAlive += entryBecameAlive;
|
|
|
|
|
lifetimeManager.EntryBecameDead += entryBecameDead;
|
2018-09-21 13:35:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
#region Pooling support
|
|
|
|
|
|
|
|
|
|
public void Add(HitObjectLifetimeEntry entry) => lifetimeManager.AddEntry(entry);
|
|
|
|
|
|
2020-11-10 22:32:30 +08:00
|
|
|
|
public bool Remove(HitObjectLifetimeEntry entry) => lifetimeManager.RemoveEntry(entry);
|
2020-11-10 21:49:02 +08:00
|
|
|
|
|
|
|
|
|
private void entryBecameAlive(LifetimeEntry entry) => addDrawable((HitObjectLifetimeEntry)entry);
|
|
|
|
|
|
|
|
|
|
private void entryBecameDead(LifetimeEntry entry) => removeDrawable((HitObjectLifetimeEntry)entry);
|
|
|
|
|
|
|
|
|
|
private void addDrawable(HitObjectLifetimeEntry entry)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(!drawableMap.ContainsKey(entry));
|
|
|
|
|
|
|
|
|
|
var drawable = drawableRuleset.GetDrawableRepresentation(entry.HitObject);
|
|
|
|
|
drawable.OnNewResult += onNewResult;
|
|
|
|
|
drawable.OnRevertResult += onRevertResult;
|
|
|
|
|
|
|
|
|
|
bindStartTime(drawable);
|
|
|
|
|
AddInternal(drawableMap[entry] = drawable, false);
|
|
|
|
|
|
|
|
|
|
HitObjectUsageBegan?.Invoke(entry.HitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void removeDrawable(HitObjectLifetimeEntry entry)
|
2019-12-18 01:56:29 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
Debug.Assert(drawableMap.ContainsKey(entry));
|
2020-11-06 23:57:33 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
var drawable = drawableMap[entry];
|
|
|
|
|
drawable.OnNewResult -= onNewResult;
|
|
|
|
|
drawable.OnRevertResult -= onRevertResult;
|
|
|
|
|
drawable.OnKilled();
|
|
|
|
|
|
|
|
|
|
drawableMap.Remove(entry);
|
|
|
|
|
|
|
|
|
|
unbindStartTime(drawable);
|
|
|
|
|
RemoveInternal(drawable);
|
|
|
|
|
|
|
|
|
|
HitObjectUsageFinished?.Invoke(entry.HitObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Non-pooling support
|
|
|
|
|
|
|
|
|
|
public virtual void Add(DrawableHitObject hitObject)
|
|
|
|
|
{
|
|
|
|
|
bindStartTime(hitObject);
|
2019-12-18 01:56:29 +08:00
|
|
|
|
AddInternal(hitObject);
|
2020-11-10 21:49:02 +08:00
|
|
|
|
|
|
|
|
|
hitObject.OnNewResult += onNewResult;
|
|
|
|
|
hitObject.OnRevertResult += onRevertResult;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual bool Remove(DrawableHitObject hitObject)
|
|
|
|
|
{
|
2019-12-18 11:03:15 +08:00
|
|
|
|
if (!RemoveInternal(hitObject))
|
|
|
|
|
return false;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
hitObject.OnNewResult -= onNewResult;
|
|
|
|
|
hitObject.OnRevertResult -= onRevertResult;
|
|
|
|
|
|
|
|
|
|
unbindStartTime(hitObject);
|
2019-12-18 01:56:29 +08:00
|
|
|
|
|
2019-12-18 11:03:15 +08:00
|
|
|
|
return true;
|
2019-12-18 01:56:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
public int IndexOf(DrawableHitObject hitObject) => IndexOfInternal(hitObject);
|
|
|
|
|
|
|
|
|
|
protected override void OnChildLifetimeBoundaryCrossed(LifetimeBoundaryCrossedEvent e)
|
2020-04-23 10:16:59 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
if (!(e.Child is DrawableHitObject hitObject))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if ((e.Kind == LifetimeBoundaryKind.End && e.Direction == LifetimeBoundaryCrossingDirection.Forward)
|
|
|
|
|
|| (e.Kind == LifetimeBoundaryKind.Start && e.Direction == LifetimeBoundaryCrossingDirection.Backward))
|
|
|
|
|
{
|
|
|
|
|
hitObject.OnKilled();
|
|
|
|
|
}
|
2020-09-21 17:27:15 +08:00
|
|
|
|
}
|
2020-04-23 10:16:59 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2020-11-06 23:57:33 +08:00
|
|
|
|
public virtual void Clear(bool disposeChildren = true)
|
2020-09-21 17:27:15 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
lifetimeManager.ClearEntries();
|
|
|
|
|
|
2020-11-06 23:57:33 +08:00
|
|
|
|
ClearInternal(disposeChildren);
|
2020-11-10 21:49:02 +08:00
|
|
|
|
unbindAllStartTimes();
|
2020-04-23 10:16:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
protected override bool CheckChildrenLife() => base.CheckChildrenLife() | lifetimeManager.Update(Time.Current - PastLifetimeExtension, Time.Current + FutureLifetimeExtension);
|
|
|
|
|
|
|
|
|
|
private void onNewResult(DrawableHitObject d, JudgementResult r) => NewResult?.Invoke(d, r);
|
|
|
|
|
private void onRevertResult(DrawableHitObject d, JudgementResult r) => RevertResult?.Invoke(d, r);
|
|
|
|
|
|
|
|
|
|
#region Comparator + StartTime tracking
|
|
|
|
|
|
|
|
|
|
private void bindStartTime(DrawableHitObject hitObject)
|
2019-12-18 01:56:29 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
var bindable = hitObject.StartTimeBindable.GetBoundCopy();
|
|
|
|
|
bindable.BindValueChanged(_ => SortInternal());
|
2020-11-06 21:15:00 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
startTimeMap[hitObject] = bindable;
|
|
|
|
|
}
|
2020-11-06 21:15:00 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
private void unbindStartTime(DrawableHitObject hitObject)
|
2020-11-06 21:15:00 +08:00
|
|
|
|
{
|
2020-11-10 21:49:02 +08:00
|
|
|
|
startTimeMap[hitObject].UnbindAll();
|
|
|
|
|
startTimeMap.Remove(hitObject);
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
private void unbindAllStartTimes()
|
|
|
|
|
{
|
|
|
|
|
foreach (var kvp in startTimeMap)
|
|
|
|
|
kvp.Value.UnbindAll();
|
|
|
|
|
startTimeMap.Clear();
|
2020-11-06 23:57:33 +08:00
|
|
|
|
}
|
2020-11-06 21:15:00 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override int Compare(Drawable x, Drawable y)
|
|
|
|
|
{
|
|
|
|
|
if (!(x is DrawableHitObject xObj) || !(y is DrawableHitObject yObj))
|
|
|
|
|
return base.Compare(x, y);
|
|
|
|
|
|
|
|
|
|
// Put earlier hitobjects towards the end of the list, so they handle input first
|
2020-11-10 21:49:02 +08:00
|
|
|
|
int i = yObj.HitObject.StartTime.CompareTo(xObj.HitObject.StartTime);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
return i == 0 ? CompareReverseChildID(x, y) : i;
|
|
|
|
|
}
|
2019-01-29 14:25:27 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
#endregion
|
2020-11-06 23:57:33 +08:00
|
|
|
|
|
2020-11-10 21:49:02 +08:00
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
unbindAllStartTimes();
|
2019-01-29 14:25:27 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|